Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_GPS/AP_GPS_DroneCAN.cpp
9726 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
//
17
// UAVCAN GPS driver
18
//
19
#include "AP_GPS_config.h"
20
21
#if AP_GPS_DRONECAN_ENABLED
22
23
#include <AP_HAL/AP_HAL.h>
24
25
#include "AP_GPS_DroneCAN.h"
26
27
#include <AP_CANManager/AP_CANManager.h>
28
#include <AP_DroneCAN/AP_DroneCAN.h>
29
#include <GCS_MAVLink/GCS.h>
30
31
#include <AP_Logger/AP_Logger.h>
32
33
#include <stdio.h>
34
#include <AP_BoardConfig/AP_BoardConfig.h>
35
36
#define GPS_PPS_EMULATION 0
37
38
extern const AP_HAL::HAL& hal;
39
40
#define GPS_UAVCAN_DEBUGGING 0
41
42
#if GPS_UAVCAN_DEBUGGING
43
#if defined(HAL_BUILD_AP_PERIPH)
44
extern "C" {
45
void can_printf(const char *fmt, ...);
46
}
47
# define Debug(fmt, args ...) do {can_printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args);} while(0)
48
#else
49
# define Debug(fmt, args ...) do {hal.console->printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); hal.scheduler->delay(1); } while(0)
50
#endif
51
#else
52
# define Debug(fmt, args ...)
53
#endif
54
55
#define LOG_TAG "GPS"
56
57
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
58
#define NATIVE_TIME_OFFSET (AP_HAL::micros64() - AP_HAL::micros64())
59
#else
60
#define NATIVE_TIME_OFFSET 0
61
#endif
62
AP_GPS_DroneCAN::DetectedModules AP_GPS_DroneCAN::_detected_modules[];
63
HAL_Semaphore AP_GPS_DroneCAN::_sem_registry;
64
65
// Member Methods
66
AP_GPS_DroneCAN::AP_GPS_DroneCAN(AP_GPS &_gps,
67
AP_GPS::Params &_params,
68
AP_GPS::GPS_State &_state,
69
AP_GPS::GPS_Role _role) :
70
AP_GPS_Backend(_gps, _params, _state, nullptr),
71
interim_state(_state),
72
role(_role)
73
{
74
param_int_cb = FUNCTOR_BIND_MEMBER(&AP_GPS_DroneCAN::handle_param_get_set_response_int, bool, AP_DroneCAN*, const uint8_t, const char*, int32_t &);
75
param_float_cb = FUNCTOR_BIND_MEMBER(&AP_GPS_DroneCAN::handle_param_get_set_response_float, bool, AP_DroneCAN*, const uint8_t, const char*, float &);
76
param_save_cb = FUNCTOR_BIND_MEMBER(&AP_GPS_DroneCAN::handle_param_save_response, void, AP_DroneCAN*, const uint8_t, bool);
77
}
78
79
AP_GPS_DroneCAN::~AP_GPS_DroneCAN()
80
{
81
WITH_SEMAPHORE(_sem_registry);
82
83
_detected_modules[_detected_module].driver = nullptr;
84
85
#if GPS_MOVING_BASELINE
86
if (rtcm3_parser != nullptr) {
87
delete rtcm3_parser;
88
}
89
#endif
90
}
91
92
bool AP_GPS_DroneCAN::subscribe_msgs(AP_DroneCAN* ap_dronecan)
93
{
94
const auto driver_index = ap_dronecan->get_driver_index();
95
96
return (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_fix2_msg_trampoline, driver_index) != nullptr)
97
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_aux_msg_trampoline, driver_index) != nullptr)
98
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_heading_msg_trampoline, driver_index) != nullptr)
99
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_status_msg_trampoline, driver_index) != nullptr)
100
#if GPS_MOVING_BASELINE
101
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_moving_baseline_msg_trampoline, driver_index) != nullptr)
102
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_relposheading_msg_trampoline, driver_index) != nullptr)
103
#endif
104
;
105
}
106
107
AP_GPS_Backend* AP_GPS_DroneCAN::probe(AP_GPS &_gps, AP_GPS::GPS_State &_state)
108
{
109
WITH_SEMAPHORE(_sem_registry);
110
int8_t found_match = -1, last_match = -1;
111
AP_GPS_DroneCAN* backend = nullptr;
112
bool bad_override_config = false;
113
for (int8_t i = GPS_MAX_RECEIVERS - 1; i >= 0; i--) {
114
if (_detected_modules[i].driver == nullptr && _detected_modules[i].ap_dronecan != nullptr) {
115
if (_gps.params[_state.instance].override_node_id != 0 &&
116
_gps.params[_state.instance].override_node_id != _detected_modules[i].node_id) {
117
continue; // This device doesn't match the correct node
118
}
119
last_match = found_match;
120
for (uint8_t j = 0; j < GPS_MAX_RECEIVERS; j++) {
121
if (_detected_modules[i].node_id == _gps.params[j].override_node_id &&
122
(j != _state.instance)) {
123
//wrong instance
124
found_match = -1;
125
break;
126
}
127
found_match = i;
128
}
129
130
// Handle Duplicate overrides
131
for (uint8_t j = 0; j < GPS_MAX_RECEIVERS; j++) {
132
if (_gps.params[i].override_node_id != 0 && (i != j) &&
133
_gps.params[i].override_node_id == _gps.params[j].override_node_id) {
134
bad_override_config = true;
135
}
136
}
137
if (bad_override_config) {
138
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Same Node Id %lu set for multiple GPS", (unsigned long int)_gps.params[i].override_node_id.get());
139
last_match = i;
140
}
141
142
if (found_match == -1) {
143
found_match = last_match;
144
continue;
145
}
146
break;
147
}
148
}
149
150
if (found_match == -1) {
151
return NULL;
152
}
153
// initialise the backend based on the UAVCAN Moving baseline selection
154
switch (_gps.get_type(_state.instance)) {
155
case AP_GPS::GPS_TYPE_UAVCAN:
156
backend = NEW_NOTHROW AP_GPS_DroneCAN(_gps, _gps.params[_state.instance], _state, AP_GPS::GPS_ROLE_NORMAL);
157
break;
158
#if GPS_MOVING_BASELINE
159
case AP_GPS::GPS_TYPE_UAVCAN_RTK_BASE:
160
backend = NEW_NOTHROW AP_GPS_DroneCAN(_gps, _gps.params[_state.instance], _state, AP_GPS::GPS_ROLE_MB_BASE);
161
break;
162
case AP_GPS::GPS_TYPE_UAVCAN_RTK_ROVER:
163
backend = NEW_NOTHROW AP_GPS_DroneCAN(_gps, _gps.params[_state.instance], _state, AP_GPS::GPS_ROLE_MB_ROVER);
164
break;
165
#endif
166
default:
167
return NULL;
168
}
169
if (backend == nullptr) {
170
AP::can().log_text(AP_CANManager::LOG_ERROR,
171
LOG_TAG,
172
"Failed to register DroneCAN GPS Node %d on Bus %d\n",
173
_detected_modules[found_match].node_id,
174
_detected_modules[found_match].ap_dronecan->get_driver_index());
175
} else {
176
_detected_modules[found_match].driver = backend;
177
backend->_detected_module = found_match;
178
AP::can().log_text(AP_CANManager::LOG_INFO,
179
LOG_TAG,
180
"Registered DroneCAN GPS Node %d on Bus %d as instance %d\n",
181
_detected_modules[found_match].node_id,
182
_detected_modules[found_match].ap_dronecan->get_driver_index(),
183
_state.instance);
184
snprintf(backend->_name, ARRAY_SIZE(backend->_name), "DroneCAN%u-%u", _detected_modules[found_match].ap_dronecan->get_driver_index()+1, _detected_modules[found_match].node_id);
185
_detected_modules[found_match].instance = _state.instance;
186
for (uint8_t i=0; i < GPS_MAX_RECEIVERS; i++) {
187
if (_detected_modules[found_match].node_id == AP::gps().params[i].node_id) {
188
if (i == _state.instance) {
189
// Nothing to do here
190
break;
191
}
192
// else swap
193
uint8_t tmp = AP::gps().params[_state.instance].node_id.get();
194
AP::gps().params[_state.instance].node_id.set_and_notify(_detected_modules[found_match].node_id);
195
AP::gps().params[i].node_id.set_and_notify(tmp);
196
}
197
}
198
#if GPS_MOVING_BASELINE
199
if (backend->role == AP_GPS::GPS_ROLE_MB_BASE) {
200
backend->rtcm3_parser = NEW_NOTHROW RTCM3_Parser;
201
if (backend->rtcm3_parser == nullptr) {
202
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "DroneCAN%u-%u: failed RTCMv3 parser allocation", _detected_modules[found_match].ap_dronecan->get_driver_index()+1, _detected_modules[found_match].node_id);
203
}
204
}
205
#endif // GPS_MOVING_BASELINE
206
}
207
208
return backend;
209
}
210
211
bool AP_GPS_DroneCAN::inter_instance_pre_arm_checks(char failure_msg[], uint16_t failure_msg_len)
212
{
213
// lint parameters and detected node IDs:
214
for (uint8_t i = 0; i < GPS_MAX_RECEIVERS; i++) {
215
const auto &params_i = AP::gps().params[i];
216
// we are only interested in parameters for DroneCAN GPSs:
217
if (!is_dronecan_gps_type(params_i.type)) {
218
continue;
219
}
220
bool overriden_node_found = false;
221
bool bad_override_config = false;
222
if (params_i.override_node_id == 0) {
223
//anything goes
224
continue;
225
}
226
for (uint8_t j = 0; j < GPS_MAX_RECEIVERS; j++) {
227
const auto &params_j = AP::gps().params[j];
228
// we are only interested in parameters for DroneCAN GPSs:
229
if (!is_dronecan_gps_type(params_j.type)) {
230
continue;
231
}
232
if (params_i.override_node_id == params_j.override_node_id && (i != j)) {
233
bad_override_config = true;
234
break;
235
}
236
if (i == _detected_modules[j].instance && _detected_modules[j].driver) {
237
if (params_i.override_node_id == _detected_modules[j].node_id) {
238
overriden_node_found = true;
239
break;
240
}
241
}
242
}
243
if (bad_override_config) {
244
snprintf(failure_msg, failure_msg_len, "Same Node Id %lu set for multiple GPS", (unsigned long int)params_i.override_node_id.get());
245
return false;
246
}
247
248
if (!overriden_node_found) {
249
snprintf(failure_msg, failure_msg_len, "Selected GPS Node %lu not set as instance %d", (unsigned long int)params_i.override_node_id.get(), i + 1);
250
return false;
251
}
252
}
253
254
return true;
255
}
256
257
AP_GPS_DroneCAN* AP_GPS_DroneCAN::get_dronecan_backend(AP_DroneCAN* ap_dronecan, uint8_t node_id)
258
{
259
if (ap_dronecan == nullptr) {
260
return nullptr;
261
}
262
263
for (uint8_t i = 0; i < GPS_MAX_RECEIVERS; i++) {
264
if (_detected_modules[i].driver != nullptr &&
265
_detected_modules[i].ap_dronecan == ap_dronecan &&
266
_detected_modules[i].node_id == node_id) {
267
return _detected_modules[i].driver;
268
}
269
}
270
271
bool already_detected = false;
272
// Check if there's an empty spot for possible registeration
273
for (uint8_t i = 0; i < GPS_MAX_RECEIVERS; i++) {
274
if (_detected_modules[i].ap_dronecan == ap_dronecan && _detected_modules[i].node_id == node_id) {
275
// Already Detected
276
already_detected = true;
277
break;
278
}
279
}
280
if (!already_detected) {
281
for (uint8_t i = 0; i < GPS_MAX_RECEIVERS; i++) {
282
if (_detected_modules[i].ap_dronecan == nullptr) {
283
_detected_modules[i].ap_dronecan = ap_dronecan;
284
_detected_modules[i].node_id = node_id;
285
// Just set the Node ID in order of appearance
286
// This will be used to set select ids
287
AP::gps().params[i].node_id.set_and_notify(node_id);
288
break;
289
}
290
}
291
}
292
struct DetectedModules tempslot;
293
// Sort based on the node_id, larger values first
294
// we do this, so that we have repeatable GPS
295
// registration
296
for (uint8_t i = 1; i < GPS_MAX_RECEIVERS; i++) {
297
for (uint8_t j = i; j > 0; j--) {
298
if (_detected_modules[j].node_id > _detected_modules[j-1].node_id) {
299
tempslot = _detected_modules[j];
300
_detected_modules[j] = _detected_modules[j-1];
301
_detected_modules[j-1] = tempslot;
302
// also fix the _detected_module in the driver so that RTCM injection
303
// can determine if it has the bus to itself
304
if (_detected_modules[j].driver) {
305
_detected_modules[j].driver->_detected_module = j;
306
}
307
if (_detected_modules[j-1].driver) {
308
_detected_modules[j-1].driver->_detected_module = j-1;
309
}
310
}
311
}
312
}
313
return nullptr;
314
}
315
316
/*
317
handle velocity element of message
318
*/
319
void AP_GPS_DroneCAN::handle_velocity(const float vx, const float vy, const float vz)
320
{
321
if (!isnan(vx)) {
322
const Vector3f vel(vx, vy, vz);
323
interim_state.velocity = vel;
324
velocity_to_speed_course(interim_state);
325
// assume we have vertical velocity if we ever get a non-zero Z velocity
326
if (!isnan(vel.z) && !is_zero(vel.z)) {
327
interim_state.have_vertical_velocity = true;
328
} else {
329
interim_state.have_vertical_velocity = state.have_vertical_velocity;
330
}
331
} else {
332
interim_state.have_vertical_velocity = false;
333
}
334
}
335
336
void AP_GPS_DroneCAN::handle_fix2_msg(const uavcan_equipment_gnss_Fix2& msg, uint64_t timestamp_usec)
337
{
338
bool process = false;
339
seen_fix2 = true;
340
341
WITH_SEMAPHORE(sem);
342
343
if (msg.status == UAVCAN_EQUIPMENT_GNSS_FIX2_STATUS_NO_FIX) {
344
interim_state.status = AP_GPS::GPS_Status::NO_FIX;
345
} else {
346
if (msg.status == UAVCAN_EQUIPMENT_GNSS_FIX2_STATUS_TIME_ONLY) {
347
interim_state.status = AP_GPS::GPS_Status::NO_FIX;
348
} else if (msg.status == UAVCAN_EQUIPMENT_GNSS_FIX2_STATUS_2D_FIX) {
349
interim_state.status = AP_GPS::GPS_Status::GPS_OK_FIX_2D;
350
process = true;
351
} else if (msg.status == UAVCAN_EQUIPMENT_GNSS_FIX2_STATUS_3D_FIX) {
352
interim_state.status = AP_GPS::GPS_Status::GPS_OK_FIX_3D;
353
process = true;
354
}
355
356
if (msg.gnss_time_standard == UAVCAN_EQUIPMENT_GNSS_FIX2_GNSS_TIME_STANDARD_UTC) {
357
uint64_t epoch_ms = msg.gnss_timestamp.usec;
358
if (epoch_ms != 0) {
359
epoch_ms /= 1000;
360
uint64_t gps_ms = epoch_ms - UNIX_OFFSET_MSEC;
361
interim_state.time_week = (uint16_t)(gps_ms / AP_MSEC_PER_WEEK);
362
interim_state.time_week_ms = (uint32_t)(gps_ms - (interim_state.time_week) * AP_MSEC_PER_WEEK);
363
}
364
}
365
366
if (interim_state.status == AP_GPS::GPS_Status::GPS_OK_FIX_3D) {
367
if (msg.mode == UAVCAN_EQUIPMENT_GNSS_FIX2_MODE_DGPS) {
368
interim_state.status = AP_GPS::GPS_Status::GPS_OK_FIX_3D_DGPS;
369
} else if (msg.mode == UAVCAN_EQUIPMENT_GNSS_FIX2_MODE_RTK) {
370
if (msg.sub_mode == UAVCAN_EQUIPMENT_GNSS_FIX2_SUB_MODE_RTK_FLOAT) {
371
interim_state.status = AP_GPS::GPS_Status::GPS_OK_FIX_3D_RTK_FLOAT;
372
} else if (msg.sub_mode == UAVCAN_EQUIPMENT_GNSS_FIX2_SUB_MODE_RTK_FIXED) {
373
interim_state.status = AP_GPS::GPS_Status::GPS_OK_FIX_3D_RTK_FIXED;
374
}
375
}
376
}
377
}
378
379
if (process) {
380
Location loc = { };
381
loc.lat = msg.latitude_deg_1e8 / 10;
382
loc.lng = msg.longitude_deg_1e8 / 10;
383
const int32_t alt_amsl_cm = msg.height_msl_mm / 10;
384
// if ellipsoid height is not supported by the GPS driver (or always
385
// with older releases), AP_Periph reports height_ellipsoid_mm == height_msl_mm .
386
// only trust that we have a valid ellipsoid height if we've ever seen
387
// it different from msl
388
if (msg.height_msl_mm != msg.height_ellipsoid_mm) {
389
seen_valid_height_ellipsoid = true;
390
}
391
interim_state.have_undulation = seen_valid_height_ellipsoid;
392
if (seen_valid_height_ellipsoid) {
393
interim_state.undulation = (msg.height_msl_mm - msg.height_ellipsoid_mm) * 0.001;
394
}
395
interim_state.location = loc;
396
set_alt_amsl_cm(interim_state, alt_amsl_cm);
397
398
handle_velocity(msg.ned_velocity[0], msg.ned_velocity[1], msg.ned_velocity[2]);
399
400
if (msg.covariance.len == 6) {
401
if (!isnan(msg.covariance.data[0])) {
402
interim_state.horizontal_accuracy = sqrtf(msg.covariance.data[0]);
403
interim_state.have_horizontal_accuracy = true;
404
} else {
405
interim_state.have_horizontal_accuracy = false;
406
}
407
if (!isnan(msg.covariance.data[2])) {
408
interim_state.vertical_accuracy = sqrtf(msg.covariance.data[2]);
409
interim_state.have_vertical_accuracy = true;
410
} else {
411
interim_state.have_vertical_accuracy = false;
412
}
413
if (!isnan(msg.covariance.data[3]) &&
414
!isnan(msg.covariance.data[4]) &&
415
!isnan(msg.covariance.data[5])) {
416
interim_state.speed_accuracy = sqrtf((msg.covariance.data[3] + msg.covariance.data[4] + msg.covariance.data[5])/3);
417
interim_state.have_speed_accuracy = true;
418
} else {
419
interim_state.have_speed_accuracy = false;
420
}
421
}
422
} else {
423
interim_state.have_vertical_velocity = false;
424
interim_state.have_vertical_accuracy = false;
425
interim_state.have_horizontal_accuracy = false;
426
interim_state.have_speed_accuracy = false;
427
}
428
429
interim_state.num_sats = msg.sats_used;
430
431
if (!seen_aux) {
432
// if we haven't seen an Aux message then populate vdop and
433
// hdop from pdop. Some GPS modules don't provide the Aux message
434
interim_state.hdop = interim_state.vdop = msg.pdop * 100.0;
435
}
436
437
if ((msg.timestamp.usec > msg.gnss_timestamp.usec) && (msg.gnss_timestamp.usec > 0)) {
438
// we have a valid timestamp based on gnss_timestamp timescale, we can use that to correct our gps message time
439
interim_state.last_corrected_gps_time_us = jitter_correction.correct_offboard_timestamp_usec(msg.timestamp.usec, (timestamp_usec + NATIVE_TIME_OFFSET));
440
interim_state.last_gps_time_ms = interim_state.last_corrected_gps_time_us/1000U;
441
interim_state.last_corrected_gps_time_us -= msg.timestamp.usec - msg.gnss_timestamp.usec;
442
// this is also the time the message was received on the UART on other end.
443
interim_state.corrected_timestamp_updated = true;
444
} else {
445
interim_state.last_gps_time_ms = jitter_correction.correct_offboard_timestamp_usec(msg.timestamp.usec, timestamp_usec + NATIVE_TIME_OFFSET)/1000U;
446
}
447
448
#if GPS_PPS_EMULATION
449
// Emulates a PPS signal, can be used to check how close are we to real GPS time
450
static virtual_timer_t timeout_vt;
451
hal.gpio->pinMode(51, 1);
452
auto handle_timeout = [](void *arg)
453
{
454
(void)arg;
455
//we are called from ISR context
456
chSysLockFromISR();
457
hal.gpio->toggle(51);
458
chSysUnlockFromISR();
459
};
460
461
static uint64_t next_toggle, last_toggle;
462
463
next_toggle = (msg.timestamp.usec) + (1000000ULL - ((msg.timestamp.usec) % 1000000ULL));
464
465
next_toggle += jitter_correction.get_link_offset_usec();
466
if (next_toggle != last_toggle) {
467
chVTSet(&timeout_vt, chTimeUS2I(next_toggle - AP_HAL::micros64()), handle_timeout, nullptr);
468
last_toggle = next_toggle;
469
}
470
#endif
471
472
_new_data = true;
473
if (!seen_message) {
474
if (interim_state.status == AP_GPS::GPS_Status::NO_GPS) {
475
// the first time we see a fix message we change from
476
// NO_GPS to NO_FIX, indicating to user that a DroneCAN GPS
477
// has been seen
478
interim_state.status = AP_GPS::GPS_Status::NO_FIX;
479
}
480
seen_message = true;
481
}
482
}
483
484
void AP_GPS_DroneCAN::handle_aux_msg(const uavcan_equipment_gnss_Auxiliary& msg)
485
{
486
WITH_SEMAPHORE(sem);
487
488
if (!isnan(msg.hdop)) {
489
seen_aux = true;
490
interim_state.hdop = msg.hdop * 100.0;
491
}
492
493
if (!isnan(msg.vdop)) {
494
seen_aux = true;
495
interim_state.vdop = msg.vdop * 100.0;
496
}
497
}
498
499
void AP_GPS_DroneCAN::handle_heading_msg(const ardupilot_gnss_Heading& msg)
500
{
501
#if GPS_MOVING_BASELINE
502
if (seen_relposheading && gps.params[interim_state.instance].mb_params.type.get() != 0) {
503
// we prefer to use the relposheading to get yaw as it allows
504
// the user to more easily control the relative antenna positions
505
return;
506
}
507
#endif
508
509
WITH_SEMAPHORE(sem);
510
511
if (interim_state.gps_yaw_configured == false) {
512
interim_state.gps_yaw_configured = msg.heading_valid;
513
}
514
515
interim_state.have_gps_yaw = msg.heading_valid;
516
interim_state.gps_yaw = degrees(msg.heading_rad);
517
if (interim_state.have_gps_yaw) {
518
interim_state.gps_yaw_time_ms = AP_HAL::millis();
519
}
520
521
interim_state.have_gps_yaw_accuracy = msg.heading_accuracy_valid;
522
interim_state.gps_yaw_accuracy = degrees(msg.heading_accuracy_rad);
523
}
524
525
void AP_GPS_DroneCAN::handle_status_msg(const ardupilot_gnss_Status& msg)
526
{
527
WITH_SEMAPHORE(sem);
528
529
seen_status = true;
530
531
healthy = msg.healthy;
532
status_flags = msg.status;
533
if (error_code != msg.error_codes) {
534
#if HAL_LOGGING_ENABLED
535
AP::logger().Write_MessageF("GPS %d: error changed (0x%08x/0x%08x)",
536
(unsigned int)(state.instance + 1),
537
error_code,
538
msg.error_codes);
539
#endif
540
error_code = msg.error_codes;
541
}
542
}
543
544
#if GPS_MOVING_BASELINE
545
/*
546
handle moving baseline data.
547
*/
548
void AP_GPS_DroneCAN::handle_moving_baseline_msg(const ardupilot_gnss_MovingBaselineData& msg, uint8_t node_id)
549
{
550
WITH_SEMAPHORE(sem);
551
if (role != AP_GPS::GPS_ROLE_MB_BASE) {
552
const uint32_t now_ms = AP_HAL::millis();
553
if (now_ms - last_base_warning_ms > 5000) {
554
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Incorrect Role set for DroneCAN GPS, %d should be Base", node_id);
555
last_base_warning_ms = now_ms;
556
}
557
return;
558
}
559
560
if (rtcm3_parser == nullptr) {
561
return;
562
}
563
for (int i=0; i < msg.data.len; i++) {
564
rtcm3_parser->read(msg.data.data[i]);
565
}
566
}
567
568
/*
569
handle relposheading message
570
*/
571
void AP_GPS_DroneCAN::handle_relposheading_msg(const ardupilot_gnss_RelPosHeading& msg, uint8_t node_id)
572
{
573
WITH_SEMAPHORE(sem);
574
575
interim_state.gps_yaw_configured = true;
576
seen_relposheading = true;
577
// push raw heading data to calculate moving baseline heading states
578
if (calculate_moving_base_yaw(interim_state,
579
msg.reported_heading_deg,
580
msg.relative_distance_m,
581
msg.relative_down_pos_m)) {
582
if (msg.reported_heading_acc_available) {
583
interim_state.gps_yaw_accuracy = msg.reported_heading_acc_deg;
584
}
585
interim_state.have_gps_yaw_accuracy = msg.reported_heading_acc_available;
586
}
587
}
588
589
// support for retrieving RTCMv3 data from a moving baseline base
590
bool AP_GPS_DroneCAN::get_RTCMV3(const uint8_t *&bytes, uint16_t &len)
591
{
592
WITH_SEMAPHORE(sem);
593
if (rtcm3_parser != nullptr) {
594
len = rtcm3_parser->get_len(bytes);
595
return len > 0;
596
}
597
return false;
598
}
599
600
// clear previous RTCM3 packet
601
void AP_GPS_DroneCAN::clear_RTCMV3(void)
602
{
603
WITH_SEMAPHORE(sem);
604
if (rtcm3_parser != nullptr) {
605
rtcm3_parser->clear_packet();
606
}
607
}
608
609
#endif // GPS_MOVING_BASELINE
610
611
void AP_GPS_DroneCAN::handle_fix2_msg_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const uavcan_equipment_gnss_Fix2& msg)
612
{
613
WITH_SEMAPHORE(_sem_registry);
614
615
AP_GPS_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id);
616
if (driver != nullptr) {
617
driver->handle_fix2_msg(msg, transfer.timestamp_usec);
618
}
619
}
620
621
void AP_GPS_DroneCAN::handle_aux_msg_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const uavcan_equipment_gnss_Auxiliary& msg)
622
{
623
WITH_SEMAPHORE(_sem_registry);
624
625
AP_GPS_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id);
626
if (driver != nullptr) {
627
driver->handle_aux_msg(msg);
628
}
629
}
630
631
void AP_GPS_DroneCAN::handle_heading_msg_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const ardupilot_gnss_Heading& msg)
632
{
633
WITH_SEMAPHORE(_sem_registry);
634
635
AP_GPS_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id);
636
if (driver != nullptr) {
637
driver->handle_heading_msg(msg);
638
}
639
}
640
641
void AP_GPS_DroneCAN::handle_status_msg_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const ardupilot_gnss_Status& msg)
642
{
643
WITH_SEMAPHORE(_sem_registry);
644
645
AP_GPS_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id);
646
if (driver != nullptr) {
647
driver->handle_status_msg(msg);
648
}
649
}
650
651
#if GPS_MOVING_BASELINE
652
// Moving Baseline msg trampoline
653
void AP_GPS_DroneCAN::handle_moving_baseline_msg_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const ardupilot_gnss_MovingBaselineData& msg)
654
{
655
WITH_SEMAPHORE(_sem_registry);
656
AP_GPS_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id);
657
if (driver != nullptr) {
658
driver->handle_moving_baseline_msg(msg, transfer.source_node_id);
659
}
660
}
661
662
// RelPosHeading msg trampoline
663
void AP_GPS_DroneCAN::handle_relposheading_msg_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const ardupilot_gnss_RelPosHeading& msg)
664
{
665
WITH_SEMAPHORE(_sem_registry);
666
AP_GPS_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id);
667
if (driver != nullptr) {
668
driver->handle_relposheading_msg(msg, transfer.source_node_id);
669
}
670
}
671
#endif
672
673
bool AP_GPS_DroneCAN::do_config()
674
{
675
AP_DroneCAN *ap_dronecan = _detected_modules[_detected_module].ap_dronecan;
676
if (ap_dronecan == nullptr) {
677
return false;
678
}
679
uint8_t node_id = _detected_modules[_detected_module].node_id;
680
681
switch(cfg_step) {
682
case STEP_SET_TYPE:
683
// GPS_TYPE was renamed GPS1_TYPE. Request both and
684
// handle whichever we receive.
685
ap_dronecan->get_parameter_on_node(node_id, "GPS_TYPE", &param_int_cb);
686
ap_dronecan->get_parameter_on_node(node_id, "GPS1_TYPE", &param_int_cb);
687
break;
688
case STEP_SET_MB_CAN_TX:
689
if (role != AP_GPS::GPS_Role::GPS_ROLE_NORMAL) {
690
ap_dronecan->get_parameter_on_node(node_id, "GPS_MB_ONLY_PORT", &param_int_cb);
691
} else {
692
cfg_step++;
693
}
694
break;
695
case STEP_SAVE_AND_REBOOT:
696
if (requires_save_and_reboot) {
697
ap_dronecan->save_parameters_on_node(node_id, &param_save_cb);
698
} else {
699
cfg_step++;
700
}
701
break;
702
case STEP_FINISHED:
703
return true;
704
default:
705
break;
706
}
707
return false;
708
}
709
710
// Consume new data and mark it received
711
bool AP_GPS_DroneCAN::read(void)
712
{
713
if (gps._auto_config >= AP_GPS::GPS_AUTO_CONFIG_ENABLE_ALL) {
714
if (!do_config()) {
715
return false;
716
}
717
}
718
719
WITH_SEMAPHORE(sem);
720
721
send_rtcm();
722
723
if (_new_data) {
724
_new_data = false;
725
726
// the encoding of accuracies in DroneCAN can result in infinite
727
// values. These cause problems with blending. Use 1000m and 1000m/s instead
728
interim_state.horizontal_accuracy = MIN(interim_state.horizontal_accuracy, 1000.0);
729
interim_state.vertical_accuracy = MIN(interim_state.vertical_accuracy, 1000.0);
730
interim_state.speed_accuracy = MIN(interim_state.speed_accuracy, 1000.0);
731
732
// prevent announcing multiple times
733
interim_state.announced_detection = state.announced_detection;
734
735
state = interim_state;
736
if (interim_state.last_corrected_gps_time_us) {
737
// If we were able to get a valid last_corrected_gps_time_us
738
// we have had a valid GPS message time, from which we calculate
739
// the time of week.
740
_last_itow_ms = interim_state.time_week_ms;
741
_have_itow = true;
742
}
743
return true;
744
}
745
if (!seen_message) {
746
// start with NO_GPS until we get first packet
747
state.status = AP_GPS::GPS_Status::NO_GPS;
748
}
749
750
return false;
751
}
752
753
bool AP_GPS_DroneCAN::is_healthy(void) const
754
{
755
// if we don't have any health reports, assume it's healthy
756
if (!seen_status) {
757
return true;
758
}
759
return healthy;
760
}
761
762
bool AP_GPS_DroneCAN::logging_healthy(void) const
763
{
764
// if we don't have status, assume it's valid
765
if (!seen_status) {
766
return true;
767
}
768
769
return (status_flags & ARDUPILOT_GNSS_STATUS_STATUS_LOGGING) != 0;
770
}
771
772
bool AP_GPS_DroneCAN::is_configured(void) const
773
{
774
// if we don't have status assume it's configured
775
if (!seen_status) {
776
return true;
777
}
778
779
return (status_flags & ARDUPILOT_GNSS_STATUS_STATUS_ARMABLE) != 0;
780
}
781
782
/*
783
send pending RTCM data
784
*/
785
void AP_GPS_DroneCAN::send_rtcm(void)
786
{
787
if (_rtcm_stream.buf == nullptr) {
788
return;
789
}
790
WITH_SEMAPHORE(sem);
791
792
const uint32_t now = AP_HAL::millis();
793
if (now - _rtcm_stream.last_send_ms < 20) {
794
// don't send more than 50 per second
795
return;
796
}
797
uint32_t outlen = 0;
798
const uint8_t *ptr = _rtcm_stream.buf->readptr(outlen);
799
if (ptr == nullptr || outlen == 0) {
800
return;
801
}
802
uavcan_equipment_gnss_RTCMStream msg {};
803
outlen = MIN(outlen, sizeof(msg.data.data));
804
msg.protocol_id = UAVCAN_EQUIPMENT_GNSS_RTCMSTREAM_PROTOCOL_ID_RTCM3;
805
memcpy(msg.data.data, ptr, outlen);
806
msg.data.len = outlen;
807
if (_detected_modules[_detected_module].ap_dronecan->rtcm_stream.broadcast(msg)) {
808
_rtcm_stream.buf->advance(outlen);
809
_rtcm_stream.last_send_ms = now;
810
}
811
}
812
813
/*
814
handle RTCM data from MAVLink GPS_RTCM_DATA, forwarding it over MAVLink
815
*/
816
void AP_GPS_DroneCAN::inject_data(const uint8_t *data, uint16_t len)
817
{
818
// we only handle this if we are the first DroneCAN GPS or we are
819
// using a different uavcan instance than the first GPS, as we
820
// send the data as broadcast on all DroneCAN device ports and we
821
// don't want to send duplicates
822
const uint32_t now_ms = AP_HAL::millis();
823
if (_detected_module == 0 ||
824
_detected_modules[_detected_module].ap_dronecan != _detected_modules[0].ap_dronecan ||
825
now_ms - _detected_modules[0].last_inject_ms > 2000) {
826
if (_rtcm_stream.buf == nullptr) {
827
// give enough space for a full round from a NTRIP server with all
828
// constellations
829
_rtcm_stream.buf = NEW_NOTHROW ByteBuffer(2400);
830
if (_rtcm_stream.buf == nullptr) {
831
return;
832
}
833
}
834
_detected_modules[_detected_module].last_inject_ms = now_ms;
835
_rtcm_stream.buf->write(data, len);
836
send_rtcm();
837
}
838
}
839
840
/*
841
handle param get/set response
842
*/
843
bool AP_GPS_DroneCAN::handle_param_get_set_response_int(AP_DroneCAN* ap_dronecan, uint8_t node_id, const char* name, int32_t &value)
844
{
845
Debug("AP_GPS_DroneCAN: param set/get response from %d %s %ld\n", node_id, name, value);
846
if (((strcmp(name, "GPS_TYPE") == 0) || (strcmp(name, "GPS1_TYPE") == 0)) && (cfg_step == STEP_SET_TYPE)) {
847
if (role == AP_GPS::GPS_ROLE_MB_BASE && value != AP_GPS::GPS_TYPE_UBLOX_RTK_BASE) {
848
value = (int32_t)AP_GPS::GPS_TYPE_UBLOX_RTK_BASE;
849
requires_save_and_reboot = true;
850
return true;
851
} else if (role == AP_GPS::GPS_ROLE_MB_ROVER && value != AP_GPS::GPS_TYPE_UBLOX_RTK_ROVER) {
852
value = (int32_t)AP_GPS::GPS_TYPE_UBLOX_RTK_ROVER;
853
requires_save_and_reboot = true;
854
return true;
855
} else {
856
cfg_step++;
857
}
858
}
859
860
if (strcmp(name, "GPS_MB_ONLY_PORT") == 0 && cfg_step == STEP_SET_MB_CAN_TX) {
861
if (option_set(AP_GPS::UAVCAN_MBUseDedicatedBus) && !value) {
862
// set up so that another CAN port is used for the Moving Baseline Data
863
// setting this value will allow another CAN port to be used as dedicated
864
// line for the Moving Baseline Data
865
value = 1;
866
requires_save_and_reboot = true;
867
return true;
868
} else if (!option_set(AP_GPS::UAVCAN_MBUseDedicatedBus) && value) {
869
// set up so that all CAN ports are used for the Moving Baseline Data
870
value = 0;
871
requires_save_and_reboot = true;
872
return true;
873
} else {
874
cfg_step++;
875
}
876
}
877
return false;
878
}
879
880
bool AP_GPS_DroneCAN::handle_param_get_set_response_float(AP_DroneCAN* ap_dronecan, uint8_t node_id, const char* name, float &value)
881
{
882
Debug("AP_GPS_DroneCAN: param set/get response from %d %s %f\n", node_id, name, value);
883
return false;
884
}
885
886
void AP_GPS_DroneCAN::handle_param_save_response(AP_DroneCAN* ap_dronecan, const uint8_t node_id, bool success)
887
{
888
Debug("AP_GPS_DroneCAN: param save response from %d %s\n", node_id, success ? "success" : "failure");
889
890
if (cfg_step != STEP_SAVE_AND_REBOOT) {
891
return;
892
}
893
894
if (success) {
895
cfg_step++;
896
}
897
// Also send reboot command
898
// this is ok as we are sending from DroneCAN thread context
899
Debug("AP_GPS_DroneCAN: sending reboot command %d\n", node_id);
900
ap_dronecan->send_reboot_request(node_id);
901
}
902
903
#if AP_DRONECAN_SEND_GPS
904
bool AP_GPS_DroneCAN::instance_exists(const AP_DroneCAN* ap_dronecan)
905
{
906
for (uint8_t i=0; i<ARRAY_SIZE(_detected_modules); i++) {
907
if (ap_dronecan == _detected_modules[i].ap_dronecan) {
908
return true;
909
}
910
}
911
return false;
912
}
913
#endif // AP_DRONECAN_SEND_GPS
914
915
#endif // AP_GPS_DRONECAN_ENABLED
916
917