MAV'RIC
gps_ublox.h
1 /*
2 * gps_ublox.h
3 *
4 * created on March 28 2013
5 *
6 * Author: N.Dousse
7 *
8 */
9 
10 
11 #ifndef GPS_UBLOX_H__ND
12 #define GPS_UBLOX_H__ND
13 
14 //#include "gps_maveric.h"
15 #include "stdint.h"
16 #include "stdbool.h"
17 
18 /*
19  * try to put a UBlox into binary mode. This is in two parts. First we
20  * send a PUBX asking the UBlox to receive NMEA and UBX, and send UBX,
21  * with a baudrate of 38400. Then we send a UBX message setting rate 1
22  * for the NAV_SOL message. The setup of NAV_SOL is to cope with
23  * configurations where all UBX binary message types are disabled.
24  * 0001: UBX
25  * 0002: NMEA
26  * 0003: NMEA + UBX
27  */
28 //#define UBLOX_SET_BINARY "$PUBX,41,1,0003,0001,38400,0*26\n\265\142\006\001\003\000\001\006\001\022\117"
29 
30 // changed by ndousse
31 #define UBLOX_SET_BINARY "$PUBX,41,1,0003,0001,38400,0*25\r\n"//\265\142\006\001\003\000\001\006\001\022\117"
32 
33 /*
34 Structure of an UBlox binary message
35 
36 PREAMBLE1 PREAMBLE2 CLASS MSG_ID LENGTH1 LENGTH2 MESSAGE CHECKSUMA CHECKSUMB
37 1 byte 1 byte 1 byte 1 byte Lowest byte Highest byte LENGTH size 1 byte 1 byte
38 
39 The information is received in the Little endian format (least significant byte first).
40 */
41 
42 #define UBX_PREAMBLE1 0xb5
43 #define UBX_PREAMBLE2 0x62
44 
45 /*
46 Name Class Description
47 NAV 0x01 Navigation Results: Position, Speed, Time, Acc, Heading, DOP, SVs used
48 RXM 0x02 Receiver Manager Messages: Satellite Status, RTC Status
49 INF 0x04 Information Messages: Printf-Style Messages, with IDs such as Error, Warning, Notice
50 ACK 0x05 Ack/Nack Messages: as replies to CFG Input Messages
51 CFG 0x06 Configuration Input Messages: Set Dynamic Model, Set DOP Mask, Set Baud Rate, etc.
52 MON 0x0A Monitoring Messages: Comunication Status, CPU Load, Stack Usage, Task Status
53 AID 0x0B AssistNow Aiding Messages: Ephemeris, Almanac, other A-GPS data input
54 TIM 0x0D Timing Messages: Timepulse Output, Timemark Results
55 */
56 #define UBX_CLASS_NAV 0x01
57 #define UBX_CLASS_RXM 0x02
58 #define UBX_CLASS_INF 0x04
59 #define UBX_CLASS_ACK 0x05
60 #define UBX_CLASS_CFG 0x06
61 #define UBX_CLASS_MON 0x0A
62 #define UBX_CLASS_AID 0x0B
63 #define UBX_CLASS_TIM 0x0D
64 #define UBX_CLASS_ESF 0x10
65 
66 // Type of Messages that can be received in each class
67 #define MSG_ACK_NACK 0x00
68 #define MSG_ACK_ACK 0x01
69 
70 #define MSG_INF_ERROR 0x00
71 #define MSG_INF_WARNING 0x01
72 #define MSG_INF_NOTICE 0x02
73 #define MSG_INF_TEST 0x03
74 #define MSG_INF_DEBUG 0x04
75 
76 #define MSG_NAV_POSLLH 0x02
77 #define MSG_NAV_STATUS 0x03
78 #define MSG_NAV_AOPSTATUS 0x60
79 #define MSG_NAV_CLOCK 0x22
80 #define MSG_NAV_SOL 0x06
81 #define MSG_NAV_VELNED 0x12
82 #define MSG_NAV_VELCEF 0x11
83 #define MSG_NAV_TIMEGPS 0x20
84 #define MSG_NAV_TIMEUTC 0x21
85 #define MSG_NAV_SVINFO 0x30
86 
87 #define MSG_CFG_PRT 0x00
88 #define MSG_CFG_RATE 0x08
89 #define MSG_CFG_SET_RATE 0x01
90 #define MSG_CFG_NAV_SETTINGS 0x24
91 #define MSG_CFG_NMEA 0x17
92 
93 #define MSG_MON_RXR 0x21
94 
95 #define MSG_TIM_TP 0x01
96 #define MSG_TIM_VRFY 0x06
97 
98 #define UBX_PLATFORM_PORTABLE 0x00
99 #define UBX_PLATFORM_STATIONARY 0x02
100 #define UBX_PLATFORM_PEDESTRIAN 0x03
101 #define UBX_PLATFORM_AUTO 0x04
102 #define UBX_PLATFORM_SEA 0x05
103 #define UBX_PLATFORM_1GAIR 0x06
104 #define UBX_PLATFORM_2GAIR 0x07
105 #define UBX_PLATFORM_4GAIR 0x08
106 
107 #define GPS_FIX_TYPE_NOFIX 0x00
108 #define GPS_FIX_TYPE_DEADRECK 0x01
109 #define GPS_FIX_TYPE_2DFIX 0x02
110 #define GPS_FIX_TYPE_3DFIX 0x03
111 #define GPS_FIX_TYPE_GPSDEADRECK 0x04
112 #define GPS_FIX_TYPE_TIMEONLY 0x05
113 
114 // For binary message
115 #define UBX_CFG_MSG 0xF1
116 #define UBX_CFG_MSG_ID 0x41
117 
118 // Sizes
119 #define UBX_SIZE_NAV_POSLLH 28
120 #define UBX_SIZE_NAV_STATUS 16
121 #define UBX_SIZE_NAV_SOL 52
122 #define UBX_SIZE_NAV_VELNED 36
123 #define UBX_SIZE_NAV_SVINFO 30 //8 + 12*num_channel
124 #define UBX_SIZE_NAV_SETTINGS 36
125 
126 #define UBX_SIZE_CFG_RATE 6
127 #define UBX_SIZE_CFG_GETSET_RATE 3
128 
129 #define UBX_SIZE_MON_RXR 1
130 
131 #define UBX_SIZE_TIM_TP 16
132 #define UBX_SIZE_TIM_VRFY 20
133 
134 #define NAV_STATUS_FIX_NVALID 0
135 #define NAV_STATUS_FIX_VALID 1
136 
137 //epoch
138 #define TIME_OF_DAY 0 //<
139 #define TIME_OF_WEEK 1 //< Ublox
140 #define TIME_OF_YEAR 2 //< MTK, NMEA
141 #define UNIX_EPOCH 3
142 
143 #ifndef PI
144 #define PI 3.141592
145 #endif
146 
147 #define DEG2RAD PI/180
148 
149 // The UART bytes are sent in a little endian format from the GPS, if the processor is big endian, define BIG_ENDIAN
150 // Otherwise comment the following line
151 #define BIG_ENDIAN
152 
153 #ifdef BIG_ENDIAN
154  typedef struct {
155  uint16_t length;
156  uint8_t msg_id_header;
157  uint8_t msg_class;
158  uint8_t preamble2;
159  uint8_t preamble1;
160  }ubx_header_t;
161 
162  typedef struct {
163  uint16_t timeref;
164  uint16_t nav_rate;
165  uint16_t measure_rate_ms;
167 
168  // We still have to send to message in the correct order to the GPS
169  typedef struct {
170  uint16_t measure_rate_ms;
171  uint16_t nav_rate;
172  uint16_t timeref; // 0:UTC time, 1:GPS time
174 
175  typedef struct {
176  uint8_t rate;
177  uint8_t msg_id_rate;
178  uint8_t msg_class;
180 
181  // We still have to send to message in the correct order to the GPS
182  typedef struct {
183  uint8_t msg_class;
184  uint8_t msg_id_rate;
185  uint8_t rate;
187 
188  typedef struct {
189  uint32_t res4;
190  uint32_t res3;
191  uint32_t res2;
192  uint8_t dgps_timeout; // s
193  uint8_t static_hold_thresh; // cm/s
194  uint16_t t_acc; // m
195  uint16_t p_acc; // m
196  uint16_t t_dop;
197  uint16_t p_dop;
198  uint8_t dr_limit; // s
199  int8_t min_elev; // deg
200  uint32_t fixed_alt_var; // m^2
201  int32_t fixed_alt; // m
202  uint8_t fix_mode;
203  uint8_t dyn_model; // UBX_PLATFORM_... type
204  uint16_t mask;
206 
207  typedef struct {
208  uint32_t vertical_accuracy; // mm
209  uint32_t horizontal_accuracy; // mm
210  int32_t altitude_msl; // mm
211  int32_t altitude_ellipsoid; // mm
212  int32_t latitude; // deg 1e-7
213  int32_t longitude; // deg 1e-7
214  uint32_t itow; // GPS msToW
216 
217  typedef struct {
218  uint32_t uptime; // milliseconds
219  uint32_t time_to_first_fix; // milliseconds
220  uint8_t res;
221  uint8_t differential_status;
222  uint8_t fix_status;
223  uint8_t fix_type;
224  uint32_t itow; // GPS msToW
226 
227  typedef struct {
228  uint32_t res2;
229  uint8_t satellites;
230  uint8_t res;
231  uint16_t position_DOP; // scaling 0.01
232  uint32_t speed_accuracy; // cm/s
233  int32_t ecef_z_velocity; // cm/s
234  int32_t ecef_y_velocity; // cm/s
235  int32_t ecef_x_velocity; // cm/s
236  uint32_t position_accuracy_3d; // cm
237  int32_t ecef_z; // cm
238  int32_t ecef_y; // cm
239  int32_t ecef_x; // cm
240  uint8_t fix_status;
241  uint8_t fix_type;
242  int16_t week;
243  int32_t time_nsec; // nanoseconds
244  uint32_t itow; // milliseconds
246 
247  typedef struct {
248  uint32_t heading_accuracy; // deg
249  uint32_t speed_accuracy; // cm/s
250  int32_t heading_2d; // deg
251  uint32_t ground_speed_2d; // cm/s
252  uint32_t speed_3d; // cm/s
253  int32_t ned_down; // cm/s
254  int32_t ned_east; // cm/s
255  int32_t ned_north; // cm/s
256  uint32_t itow; // milliseconds GPS msToW
258 
259  typedef struct
260  {
261  struct
262  {
263  int32_t pr_res;
264  int16_t azim;
265  int8_t elev;
266  uint8_t cno;
267  uint8_t quality;
268  uint8_t flags;
269  uint8_t svid;
270  uint8_t chn;
271  } channel_data[16];
272 
273  uint16_t reserved;
274  uint8_t global_flags;
275  uint8_t num_ch;
276  uint32_t itow;
278 
279  typedef struct{
280  uint8_t awake_flag;
282 
283  typedef struct{
284  uint8_t res;
285  uint8_t flags;
286  uint16_t week;
287  int32_t q_err;
288  uint32_t tow_sub_ms; // ms scaling: 2^-32
289  uint32_t tow_ms; // ms
290  }ubx_tim_tp_t;
291 
292  typedef struct
293  {
294  uint8_t res;
295  uint8_t flags;
296  uint16_t wno; // week number
297  int32_t delta_ns; // ns
298  int32_t delta_ms; // ms
299  int32_t frac; // ns
300  int32_t itow; // ms
302 
303 #else
304 
305  typedef struct {
306  uint8_t preamble1;
307  uint8_t preamble2;
308  uint8_t msg_class;
309  uint8_t msg_id_header;
310  uint16_t length;
311  }ubx_header_t;
312 
313  typedef struct {
314  uint16_t measure_rate_ms;
315  uint16_t nav_rate;
316  uint16_t timeref;
318 
319  typedef struct {
320  uint8_t msg_class;
321  uint8_t msg_id_rate;
322  uint8_t rate;
324 
325  typedef struct {
326  uint16_t mask;
327  uint8_t dyn_model; // UBX_PLATFORM_... type
328  uint8_t fix_mode;
329  int32_t fixed_alt; // m
330  uint32_t fixed_alt_var; // m^2
331  int8_t min_elev; // deg
332  uint8_t dr_limit; // s
333  uint16_t p_dop;
334  uint16_t t_dop;
335  uint16_t p_acc; // m
336  uint16_t t_acc; // m
337  uint8_t static_hold_thresh; // cm/s
338  uint8_t dgps_timeout; // s
339  uint32_t res2;
340  uint32_t res3;
341  uint32_t res4;
343 
344  typedef struct {
345  uint32_t itow; // GPS msToW
346  int32_t longitude; // deg 1E-7
347  int32_t latitude; // deg 1E-7
348  int32_t altitude_ellipsoid; // mm
349  int32_t altitude_msl; // mm
350  uint32_t horizontal_accuracy; // mm
351  uint32_t vertical_accuracy; // mm
353 
354  typedef struct {
355  uint32_t itow; // GPS msToW
356  uint8_t fix_type;
357  uint8_t fix_status;
358  uint8_t differential_status;
359  uint8_t res;
360  uint32_t time_to_first_fix; // milliseconds
361  uint32_t uptime; // milliseconds
363 
364  typedef struct {
365  uint32_t itow; // milliseconds
366  int32_t time_nsec; // nanoseconds
367  int16_t week;
368  uint8_t fix_type;
369  uint8_t fix_status;
370  int32_t ecef_x; // cm
371  int32_t ecef_y; // cm
372  int32_t ecef_z; // cm
373  uint32_t position_accuracy_3d; // cm
374  int32_t ecef_x_velocity; // cm/s
375  int32_t ecef_y_velocity; // cm/s
376  int32_t ecef_z_velocity; // cm/s
377  uint32_t speed_accuracy; // cm/s
378  uint16_t position_DOP; // scaling 0.01
379  uint8_t res;
380  uint8_t satellites;
381  uint32_t res2;
383 
384  typedef struct {
385  uint32_t itow; // milliseconds GPS msToW
386  int32_t ned_north; // cm/s
387  int32_t ned_east; // cm/s
388  int32_t ned_down; // cm/s
389  uint32_t speed_3d; // cm/s
390  uint32_t ground_speed_2d; // cm/s
391  int32_t heading_2d; // deg
392  uint32_t speed_accuracy; // cm/s
393  uint32_t heading_accuracy; // deg
395 
396  typedef struct
397  {
398  uint32_t itow;
399  uint8_t num_ch;
400  uint8_t global_flags;
401  uint16_t reserved;
402 
403  struct
404  {
405  uint8_t chn;
406  uint8_t svid;
407  uint8_t flags;
408  uint8_t quality;
409  uint8_t cno;
410  int8_t elev;
411  int16_t azim;
412  int32_t pr_res;
413  } channel_data[16];
415 
416  typedef struct{
417  uint8_t awake_flag;
419 
420  typedef struct
421  {
422  uint32_t tow_ms; // ms
423  uint32_t tow_sub_ms; // ms scaling: 2^-32
424  int32_t q_err;
425  uint16_t week;
426  uint8_t flags;
427  uint8_t res;
428  }ubx_tim_tp_t;
429 
430  typedef struct
431  {
432  int32_t itow;
433  int32_t frac;
434  int32_t delta_ms;
435  int32_t delta_ns;
436  uint16_t wno;
437  uint8_t flags;
438  uint8_t res;
440 
441 #endif
442 
443 ubx_cfg_nav_settings_t nav_settings;
444 
445 uint8_t cksum_a;
446 uint8_t cksum_b;
447 
448 // State machine state
449 uint8_t step;
450 uint8_t msg_id;
451 uint16_t payload_length;
452 uint16_t payload_counter;
453 
454 // 8 bit count of fix messages processed, used for periodic
455 // processing
456 uint8_t fix_count;
457 
458 uint8_t ubx_class;
459 
460 // do we have new position and speed information?
461 bool new_position;
462 bool new_speed;
463 
464 uint8_t disable_counter;
465 
466 bool fix;
467 bool next_fix;
468 bool have_raw_velocity;
469 // bool valid_read;
470 // bool new_data;
471 
472 uint32_t last_ground_speed_cm;
473 
474 #define NO_GPS 0
475 #define NO_FIX 1
476 #define GPS_OK 2
477 
478 enum gps_engine_setting_t{
479  GPS_ENGINE_NONE = -1,
480  GPS_ENGINE_PORTABLE = 0,
481  GPS_ENGINE_STATIONARY = 2,
482  GPS_ENGINE_PEDESTRIAN = 3,
483  GPS_ENGINE_AUTOMOTIVE = 4,
484  GPS_ENGINE_SEA = 5,
485  GPS_ENGINE_AIRBORNE_1G = 6,
486  GPS_ENGINE_AIRBORNE_2G = 7,
487  GPS_ENGINE_AIRBORNE_4G = 8
488 };
489 
490 enum gps_engine_setting_t engine_nav_setting;
491 
493 #define UBX_TIMEOUT_CYCLES 2
494 #define UBX_POSITION_PRECISION 20
496 #define UBX_ALTITUDE_PRECISION 20
498 #define UBX_SPEED_PRECISION 5
500 #define UBX_HEADING_PRECISION 5000000//3000000
502 
503 // Type definition for GPS data
504 typedef struct
505 {
506  double latitude;
507  double longitude;
508  float altitude;
509  float alt_elips;
510  float speed;
511  float ground_speed;
512  float north_speed;
513  float east_speed;
514  float vertical_speed;
515  float course;
516 
517  float horizontal_accuracy;
518  float vertical_accuracy;
519 
520  float speed_accuracy;
521  float heading_accuracy;
522 
523  uint8_t num_sats;
524  uint16_t hdop;
525 
526  unsigned long time_last_msg;
527  unsigned long time_gps;
528 
529  unsigned char status;
530 
531  unsigned char horizontal_status;
532 
533 // unsigned char latitude_status;
534 // unsigned char longitude_status;
535  unsigned char altitude_status;
536  unsigned char speed_status;
537 // unsigned char ground_speed_status;
538 // unsigned char north_speed_status;
539 // unsigned char east_speed_status;
540 // unsigned char vertical_speed_status;
541  unsigned char course_status;
542  unsigned char accuracy_status;
543 } gps_t;
544 
546 uint32_t idle_timer;
547 
553 uint32_t idle_timeout;
554 
555 uint32_t last_fix_time;
556 
557 float velocity_north;
558 float velocity_east;
559 float velocity_down;
560 
561 //float get_lag() { return 0.5; };
562 
563 void gps_ublox_init(enum gps_engine_setting_t _engine_nav_setting);
564 
565 bool gps_ublox_message_decode(void);
566 bool gps_ublox_process_data(void);
567 
568 void update_checksum(uint8_t *data, uint8_t len, uint8_t *ck_a, uint8_t *ck_b);
569 uint8_t endian_lower_bytes_uint16(uint16_t bytes);
570 uint8_t endian_higher_bytes_uint16(uint16_t bytes);
571 void ubx_send_header(uint8_t msg_class, uint8_t _msg_id, uint8_t size);
572 void ubx_send_cksum(uint8_t ck_sum_a, uint8_t ck_sum_b);
573 
574 void ubx_send_message(uint8_t msg_class, uint8_t msg_id, void *msg, uint8_t size);
575 void ubx_send_message_CFG_nav_rate(uint8_t msg_class, uint8_t _msg_id, ubx_cfg_nav_rate_send_t msg, uint8_t size);
576 void ubx_send_message_nav_settings(uint8_t msg_class, uint8_t _msg_id, enum gps_engine_setting_t *engine_settings, uint8_t size);
577 void ubx_configure_message_rate(uint8_t msg_class, uint8_t msg_id, uint8_t rate);
578 
579 void gps_ublox_configure_gps(void);
580 
581 void gps_ublox_update(void);
582 bool gps_ublox_newValidGpsMsg(uint32_t *prev_gps_msg_time);
583 
584 ubx_nav_pos_llh_t * ubx_get_pos_llh(void);
585 ubx_nav_status_t * ubx_get_status(void);
586 ubx_nav_solution_t * ubx_get_solution(void);
587 ubx_nav_vel_ned_t * ubx_get_vel_ned(void);
588 ubx_nav_sv_info_t * ubx_get_sv_info(void);
589 ubx_cfg_nav_settings_t * ubx_get_nav_settings(void);
590 ubx_cfg_msg_rate_t * ubx_get_msg_rate(void);
591 ubx_mon_rxr_struct_t * ubx_get_mon_rxr(void);
592 ubx_tim_tp_t * ubx_get_tim_tp(void);
593 ubx_tim_vrfy_t * ubx_get_tim_vrfy(void);
594 
595 float to_rad(float numdeg);
596 
597 #endif //GPS_UBLOX_H__
The U-Blox CFG_MSG rate structure definition.
Definition: gps_ublox.h:225
unsigned long time_gps
time reference in ms of gps
Definition: gps_ublox.h:527
The U-Blox header structure definition.
Definition: gps_ublox.h:192
The U-Blox CFG_NAV rate send structure definition.
Definition: gps_ublox.h:215
The U-Blox NAV-SOL message structure definition.
Definition: gps_ublox.h:297
The U-Blox TIM-VRFY message structure definition.
Definition: gps_ublox.h:384
The U-Blox NAV-VELNED message structure definition.
Definition: gps_ublox.h:321
The U-Blox MON-RXR message structure definition.
Definition: gps_ublox.h:363
The U-Blox NAV-STATUS message structure definition.
Definition: gps_ublox.h:283
Type definition for GPS data.
Definition: gps_ublox.h:635
unsigned long time_last_msg
time reference in ms of microcontroller
Definition: gps_ublox.h:526
The U-Blox CFG_NAV structure definition.
Definition: gps_ublox.h:204
The U-Blox CFG_MSG rate send structure definition.
Definition: gps_ublox.h:236
The U-Blox NAV-SVINFO message structure definition.
Definition: gps_ublox.h:337
The U-Blox TIM-TP message structure definition.
Definition: gps_ublox.h:371
The U-Blox CFG_NAV settings structure definition.
Definition: gps_ublox.h:246
The U-Blox NAV-POSLLH message structure definition.
Definition: gps_ublox.h:269