CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_GPS/AP_GPS_SBP2.cpp
Views: 1798
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
// Swift Navigation SBP GPS driver for ArduPilot.
18
// Code by Niels Joubert
19
//
20
// Swift Binary Protocol format: http://docs.swift-nav.com/
21
//
22
23
24
#include "AP_GPS.h"
25
#include "AP_GPS_SBP2.h"
26
#include <AP_Logger/AP_Logger.h>
27
#include <GCS_MAVLink/GCS_MAVLink.h>
28
#include <GCS_MAVLink/GCS.h>
29
30
#if AP_GPS_SBP2_ENABLED
31
32
extern const AP_HAL::HAL& hal;
33
34
#define SBP_DEBUGGING 0
35
#define SBP_INFOREPORTING 1
36
37
//INVARIANT: We expect SBP to give us a heartbeat in less than 2 seconds.
38
// This is more lax than the default Piksi settings,
39
// and we assume the user hasn't reconfigured their Piksi to longer heartbeat intervals
40
#define SBP_TIMEOUT_HEARTBEAT 2000
41
42
#if SBP_DEBUGGING
43
# define Debug(fmt, args ...) \
44
do { \
45
hal.console->printf("%s:%d: " fmt "\n", \
46
__FUNCTION__, __LINE__, \
47
## args); \
48
hal.scheduler->delay(1); \
49
} while(0)
50
#else
51
# define Debug(fmt, args ...)
52
#endif
53
54
#if SBP_INFOREPORTING
55
# define Info(fmt, args ...) \
56
do { \
57
GCS_SEND_TEXT(MAV_SEVERITY_INFO, fmt "\n", ## args); \
58
} while(0)
59
#else
60
# define Info(fmt, args ...)
61
#endif
62
63
64
AP_GPS_SBP2::AP_GPS_SBP2(AP_GPS &_gps,
65
AP_GPS::Params &_params,
66
AP_GPS::GPS_State &_state,
67
AP_HAL::UARTDriver *_port) :
68
AP_GPS_Backend(_gps, _params, _state, _port)
69
{
70
Debug("SBP Driver Initialized");
71
parser_state.state = sbp_parser_state_t::WAITING;
72
}
73
74
// Process all bytes available from the stream
75
//
76
bool
77
AP_GPS_SBP2::read(void)
78
{
79
//Invariant: Calling this function processes *all* data current in the UART buffer.
80
//
81
//IMPORTANT NOTICE: This function is NOT CALLED for several seconds
82
// during arming. That should not cause the driver to die. Process *all* waiting messages
83
84
_sbp_process();
85
return _attempt_state_update();
86
}
87
88
void
89
AP_GPS_SBP2::inject_data(const uint8_t *data, uint16_t len)
90
{
91
if (port->txspace() > len) {
92
last_injected_data_ms = AP_HAL::millis();
93
port->write(data, len);
94
} else {
95
Debug("PIKSI: Not enough TXSPACE");
96
}
97
}
98
99
//This attempts to reads all SBP messages from the incoming port.
100
//Returns true if a new message was read, false if we failed to read a message.
101
void
102
AP_GPS_SBP2::_sbp_process()
103
{
104
uint32_t nleft = port->available();
105
while (nleft > 0) {
106
nleft--;
107
uint8_t temp = port->read();
108
#if AP_GPS_DEBUG_LOGGING_ENABLED
109
log_data(&temp, 1);
110
#endif
111
uint16_t crc;
112
113
//This switch reads one character at a time,
114
//parsing it into buffers until a full message is dispatched
115
switch (parser_state.state) {
116
case sbp_parser_state_t::WAITING:
117
if (temp == SBP_PREAMBLE) {
118
parser_state.n_read = 0;
119
parser_state.state = sbp_parser_state_t::GET_TYPE;
120
}
121
break;
122
123
case sbp_parser_state_t::GET_TYPE:
124
*((uint8_t*)&(parser_state.msg_type) + parser_state.n_read) = temp;
125
parser_state.n_read += 1;
126
if (parser_state.n_read >= 2) {
127
parser_state.n_read = 0;
128
parser_state.state = sbp_parser_state_t::GET_SENDER;
129
}
130
break;
131
132
case sbp_parser_state_t::GET_SENDER:
133
*((uint8_t*)&(parser_state.sender_id) + parser_state.n_read) = temp;
134
parser_state.n_read += 1;
135
if (parser_state.n_read >= 2) {
136
parser_state.n_read = 0;
137
parser_state.state = sbp_parser_state_t::GET_LEN;
138
}
139
break;
140
141
case sbp_parser_state_t::GET_LEN:
142
parser_state.msg_len = temp;
143
parser_state.n_read = 0;
144
parser_state.state = sbp_parser_state_t::GET_MSG;
145
break;
146
147
case sbp_parser_state_t::GET_MSG:
148
*((uint8_t*)&(parser_state.msg_buff) + parser_state.n_read) = temp;
149
parser_state.n_read += 1;
150
if (parser_state.n_read >= parser_state.msg_len) {
151
parser_state.n_read = 0;
152
parser_state.state = sbp_parser_state_t::GET_CRC;
153
}
154
break;
155
156
case sbp_parser_state_t::GET_CRC:
157
*((uint8_t*)&(parser_state.crc) + parser_state.n_read) = temp;
158
parser_state.n_read += 1;
159
if (parser_state.n_read >= 2) {
160
parser_state.state = sbp_parser_state_t::WAITING;
161
162
crc = crc16_ccitt((uint8_t*)&(parser_state.msg_type), 2, 0);
163
crc = crc16_ccitt((uint8_t*)&(parser_state.sender_id), 2, crc);
164
crc = crc16_ccitt(&(parser_state.msg_len), 1, crc);
165
crc = crc16_ccitt(parser_state.msg_buff, parser_state.msg_len, crc);
166
if (parser_state.crc == crc) {
167
_sbp_process_message();
168
} else {
169
Debug("CRC Error Occurred!");
170
crc_error_counter += 1;
171
}
172
}
173
break;
174
175
default:
176
parser_state.state = sbp_parser_state_t::WAITING;
177
break;
178
}
179
}
180
}
181
182
183
//INVARIANT: A fully received message with correct CRC is currently in parser_state
184
void
185
AP_GPS_SBP2::_sbp_process_message() {
186
//Here, we copy messages into local structs.
187
switch (parser_state.msg_type) {
188
case SBP_HEARTBEAT_MSGTYPE:
189
memcpy(&last_heartbeat, parser_state.msg_buff, sizeof(struct sbp_heartbeat_t));
190
last_heartbeat_received_ms = AP_HAL::millis();
191
break;
192
193
case SBP_GPS_TIME_MSGTYPE:
194
memcpy(&last_gps_time, parser_state.msg_buff, sizeof(struct sbp_gps_time_t));
195
check_new_itow(last_gps_time.tow, parser_state.msg_len);
196
break;
197
198
case SBP_VEL_NED_MSGTYPE:
199
memcpy(&last_vel_ned, parser_state.msg_buff, sizeof(struct sbp_vel_ned_t));
200
check_new_itow(last_vel_ned.tow, parser_state.msg_len);
201
break;
202
203
case SBP_POS_LLH_MSGTYPE:
204
memcpy(&last_pos_llh, parser_state.msg_buff, sizeof(struct sbp_pos_llh_t));
205
check_new_itow(last_pos_llh.tow, parser_state.msg_len);
206
break;
207
208
case SBP_DOPS_MSGTYPE:
209
memcpy(&last_dops, parser_state.msg_buff, sizeof(struct sbp_dops_t));
210
check_new_itow(last_dops.tow, parser_state.msg_len);
211
break;
212
213
case SBP_EXT_EVENT_MSGTYPE:
214
memcpy(&last_event, parser_state.msg_buff, sizeof(struct sbp_ext_event_t));
215
check_new_itow(last_event.tow, parser_state.msg_len);
216
#if HAL_LOGGING_ENABLED
217
logging_ext_event();
218
#endif
219
break;
220
221
default:
222
break;
223
}
224
225
#if HAL_LOGGING_ENABLED
226
// send all messages we receive to log, even if it's an unsupported message,
227
// so we can do additional post-processing from logs.
228
// The log mask will be used to adjust or suppress logging
229
logging_log_raw_sbp(parser_state.msg_type, parser_state.sender_id, parser_state.msg_len, parser_state.msg_buff);
230
#endif
231
}
232
233
int32_t
234
AP_GPS_SBP2::distMod(int32_t tow1_ms, int32_t tow2_ms, int32_t mod) {
235
return MIN(abs(tow1_ms - tow2_ms), mod - abs(tow1_ms - tow2_ms));
236
}
237
238
bool
239
AP_GPS_SBP2::_attempt_state_update()
240
{
241
if (last_heartbeat_received_ms == 0)
242
return false;
243
244
uint32_t now = AP_HAL::millis();
245
246
if (now - last_heartbeat_received_ms > SBP_TIMEOUT_HEARTBEAT) {
247
248
state.status = AP_GPS::NO_FIX;
249
Info("No Heartbeats from Piksi! Status to NO_FIX.");
250
return false;
251
252
} else if (last_heartbeat.protocol_major != 2) {
253
254
state.status = AP_GPS::NO_FIX;
255
Info("Received a heartbeat from non-SBPv2 device. Current driver only supports SBPv2. Status to NO_FIX.");
256
return false;
257
258
} else if (last_heartbeat.nap_error == 1 ||
259
last_heartbeat.io_error == 1 ||
260
last_heartbeat.sys_error == 1) {
261
262
state.status = AP_GPS::NO_FIX;
263
264
Info("Piksi reported an error. Status to NO_FIX.");
265
Debug(" ext_antenna: %d", last_heartbeat.ext_antenna);
266
Debug(" res2: %d", last_heartbeat.res2);
267
Debug(" protocol_major: %d", last_heartbeat.protocol_major);
268
Debug(" protocol_minor: %d", last_heartbeat.protocol_minor);
269
Debug(" res: %d", last_heartbeat.res);
270
Debug(" nap_error: %d", last_heartbeat.nap_error);
271
Debug(" io_error: %d", last_heartbeat.io_error);
272
Debug(" sys_error: %d", last_heartbeat.sys_error);
273
274
return false;
275
276
} else if (last_pos_llh.tow == last_vel_ned.tow
277
&& (distMod(last_gps_time.tow, last_vel_ned.tow, AP_MSEC_PER_WEEK) < 10000)
278
&& (distMod(last_dops.tow, last_vel_ned.tow, AP_MSEC_PER_WEEK) < 60000)
279
&& (last_vel_ned.tow > last_full_update_tow || (last_gps_time.wn > last_full_update_wn && last_vel_ned.tow < last_full_update_tow))) {
280
281
//We have an aligned VEL and LLH, and a recent DOPS and TIME.
282
283
//
284
// Check Flags for Valid Messages
285
//
286
if (last_gps_time.flags.time_src == 0 ||
287
last_vel_ned.flags.vel_mode == 0 ||
288
last_pos_llh.flags.fix_mode == 0 ||
289
last_dops.flags.fix_mode == 0) {
290
291
Debug("Message Marked as Invalid. NO FIX! Flags: {GPS_TIME: %d, VEL_NED: %d, POS_LLH: %d, DOPS: %d}",
292
last_gps_time.flags.time_src,
293
last_vel_ned.flags.vel_mode,
294
last_pos_llh.flags.fix_mode,
295
last_dops.flags.fix_mode);
296
297
state.status = AP_GPS::NO_FIX;
298
return false;
299
}
300
301
//
302
// Update external time and accuracy state
303
//
304
state.time_week = last_gps_time.wn;
305
state.time_week_ms = last_vel_ned.tow;
306
state.hdop = last_dops.hdop;
307
state.vdop = last_dops.vdop;
308
state.last_gps_time_ms = now;
309
310
//
311
// Update velocity state
312
//
313
state.velocity[0] = (float)(last_vel_ned.n * 1.0e-3);
314
state.velocity[1] = (float)(last_vel_ned.e * 1.0e-3);
315
state.velocity[2] = (float)(last_vel_ned.d * 1.0e-3);
316
317
velocity_to_speed_course(state);
318
319
state.speed_accuracy = safe_sqrt(
320
powf((float)last_vel_ned.h_accuracy * 1.0e-3f, 2) +
321
powf((float)last_vel_ned.v_accuracy * 1.0e-3f, 2));
322
state.horizontal_accuracy = (float) last_pos_llh.h_accuracy * 1.0e-3f;
323
state.vertical_accuracy = (float) last_pos_llh.v_accuracy * 1.0e-3f;
324
325
//
326
// Set flags appropriately
327
//
328
state.have_vertical_velocity = true;
329
state.have_speed_accuracy = !is_zero(state.speed_accuracy);
330
state.have_horizontal_accuracy = !is_zero(state.horizontal_accuracy);
331
state.have_vertical_accuracy = !is_zero(state.vertical_accuracy);
332
333
//
334
// Update position state
335
//
336
state.location.lat = (int32_t) (last_pos_llh.lat * (double)1e7);
337
state.location.lng = (int32_t) (last_pos_llh.lon * (double)1e7);
338
state.location.alt = (int32_t) (last_pos_llh.height * 100);
339
state.num_sats = last_pos_llh.n_sats;
340
341
switch (last_pos_llh.flags.fix_mode) {
342
case 1:
343
state.status = AP_GPS::GPS_OK_FIX_3D;
344
break;
345
case 2:
346
state.status = AP_GPS::GPS_OK_FIX_3D_DGPS;
347
break;
348
case 3:
349
state.status = AP_GPS::GPS_OK_FIX_3D_RTK_FLOAT;
350
break;
351
case 4:
352
state.status = AP_GPS::GPS_OK_FIX_3D_RTK_FIXED;
353
break;
354
case 6:
355
state.status = AP_GPS::GPS_OK_FIX_3D_DGPS;
356
break;
357
default:
358
state.status = AP_GPS::NO_FIX;
359
break;
360
}
361
362
//
363
// Update Internal Timing
364
//
365
last_full_update_tow = last_vel_ned.tow;
366
last_full_update_wn = last_gps_time.wn;
367
368
return true;
369
}
370
return false;
371
}
372
373
374
375
bool
376
AP_GPS_SBP2::_detect(struct SBP2_detect_state &state, uint8_t data)
377
{
378
// This switch reads one character at a time, if we find something that
379
// looks like our preamble we'll try to read the full message length,
380
// calculating the CRC. If the CRC matches, we have an SBP GPS!
381
switch (state.state) {
382
case SBP2_detect_state::WAITING:
383
if (data == SBP_PREAMBLE) {
384
state.n_read = 0;
385
state.crc_so_far = 0;
386
state.state = SBP2_detect_state::GET_TYPE;
387
}
388
break;
389
390
case SBP2_detect_state::GET_TYPE:
391
*((uint8_t*)&(state.msg_type) + state.n_read) = data;
392
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
393
state.n_read += 1;
394
if (state.n_read >= 2) {
395
state.n_read = 0;
396
state.state = SBP2_detect_state::GET_SENDER;
397
}
398
break;
399
400
case SBP2_detect_state::GET_SENDER:
401
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
402
state.n_read += 1;
403
if (state.n_read >= 2) {
404
state.n_read = 0;
405
state.state = SBP2_detect_state::GET_LEN;
406
}
407
break;
408
409
case SBP2_detect_state::GET_LEN:
410
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
411
state.msg_len = data;
412
state.n_read = 0;
413
state.state = SBP2_detect_state::GET_MSG;
414
break;
415
416
case SBP2_detect_state::GET_MSG:
417
if (state.msg_type == SBP_HEARTBEAT_MSGTYPE && state.n_read < 4) {
418
*((uint8_t*)&(state.heartbeat_buff) + state.n_read) = data;
419
}
420
state.crc_so_far = crc16_ccitt(&data, 1, state.crc_so_far);
421
state.n_read += 1;
422
if (state.n_read >= state.msg_len) {
423
state.n_read = 0;
424
state.state = SBP2_detect_state::GET_CRC;
425
}
426
break;
427
428
case SBP2_detect_state::GET_CRC:
429
*((uint8_t*)&(state.crc) + state.n_read) = data;
430
state.n_read += 1;
431
if (state.n_read >= 2) {
432
state.state = SBP2_detect_state::WAITING;
433
if (state.crc == state.crc_so_far
434
&& state.msg_type == SBP_HEARTBEAT_MSGTYPE) {
435
struct sbp_heartbeat_t* heartbeat = ((struct sbp_heartbeat_t*)state.heartbeat_buff);
436
return heartbeat->protocol_major == 2;
437
}
438
return false;
439
}
440
break;
441
442
default:
443
state.state = SBP2_detect_state::WAITING;
444
break;
445
}
446
return false;
447
}
448
449
#if HAL_LOGGING_ENABLED
450
void
451
AP_GPS_SBP2::logging_log_full_update()
452
{
453
if (!should_log()) {
454
return;
455
}
456
457
//TODO: Expand with heartbeat info.
458
//TODO: Get rid of IAR NUM HYPO
459
460
struct log_SbpHealth pkt = {
461
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPHEALTH),
462
time_us : AP_HAL::micros64(),
463
crc_error_counter : crc_error_counter,
464
last_injected_data_ms : last_injected_data_ms,
465
last_iar_num_hypotheses : 0,
466
};
467
AP::logger().WriteBlock(&pkt, sizeof(pkt));
468
};
469
470
void
471
AP_GPS_SBP2::logging_log_raw_sbp(uint16_t msg_type,
472
uint16_t sender_id,
473
uint8_t msg_len,
474
uint8_t *msg_buff) {
475
if (!should_log()) {
476
return;
477
}
478
479
//MASK OUT MESSAGES WE DON'T WANT TO LOG
480
if (( ((uint16_t) gps._sbp_logmask) & msg_type) == 0) {
481
return;
482
}
483
484
uint64_t time_us = AP_HAL::micros64();
485
uint8_t pages = 1;
486
487
if (msg_len > 48) {
488
pages += (msg_len - 48) / 104 + 1;
489
}
490
491
struct log_SbpRAWH pkt = {
492
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPRAWH),
493
time_us : time_us,
494
msg_type : msg_type,
495
sender_id : sender_id,
496
index : 1,
497
pages : pages,
498
msg_len : msg_len,
499
};
500
memcpy(pkt.data, msg_buff, MIN(msg_len, 48));
501
AP::logger().WriteBlock(&pkt, sizeof(pkt));
502
503
for (uint8_t i = 0; i < pages - 1; i++) {
504
struct log_SbpRAWM pkt2 = {
505
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPRAWM),
506
time_us : time_us,
507
msg_type : msg_type,
508
sender_id : sender_id,
509
index : uint8_t(i + 2),
510
pages : pages,
511
msg_len : msg_len,
512
};
513
memcpy(pkt2.data, &msg_buff[48 + i * 104], MIN(msg_len - (48 + i * 104), 104));
514
AP::logger().WriteBlock(&pkt2, sizeof(pkt2));
515
}
516
};
517
518
void
519
AP_GPS_SBP2::logging_ext_event() {
520
if (!should_log()) {
521
return;
522
}
523
524
struct log_SbpEvent pkt = {
525
LOG_PACKET_HEADER_INIT(LOG_MSG_SBPEVENT),
526
time_us : AP_HAL::micros64(),
527
wn : last_event.wn,
528
tow : last_event.tow,
529
ns_residual : last_event.ns_residual,
530
level : last_event.flags.level,
531
quality : last_event.flags.quality,
532
};
533
AP::logger().WriteBlock(&pkt, sizeof(pkt));
534
};
535
#endif // HAL_LOGGING_ENABLED
536
#endif //AP_GPS_SBP2_ENABLED
537
538