Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
gps_skytraq.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Antoine Drouin <poinix@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #include "subsystems/gps.h"
23 #include "subsystems/abi.h"
24 #include "led.h"
25 
26 #if GPS_USE_LATLONG
27 /* currently needed to get nav_utm_zone0 */
30 #endif
31 
33 
34 /* parser status */
35 #define UNINIT 0
36 #define GOT_SYNC1 1
37 #define GOT_SYNC2 2
38 #define GOT_LEN1 3
39 #define GOT_LEN2 4
40 #define GOT_ID 5
41 #define GOT_PAYLOAD 6
42 #define GOT_CHECKSUM 7
43 #define GOT_SYNC3 8
44 
45 #define SKYTRAQ_FIX_NONE 0x00
46 #define SKYTRAQ_FIX_2D 0x01
47 #define SKYTRAQ_FIX_3D 0x02
48 #define SKYTRAQ_FIX_3D_DGPS 0x03
49 
50 
51 #define SKYTRAQ_SYNC1 0xA0
52 #define SKYTRAQ_SYNC2 0xA1
53 
54 #define SKYTRAQ_SYNC3 0x0D
55 #define SKYTRAQ_SYNC4 0x0A
56 
57 
58 static inline uint16_t bswap16(uint16_t a)
59 {
60  return (a << 8) | (a >> 8);
61 }
62 
63 #define SKYTRAQ_NAVIGATION_DATA_FixMode(_payload) (uint8_t) (*((uint8_t*)_payload+2-2))
64 #define SKYTRAQ_NAVIGATION_DATA_NumSV(_payload) (uint8_t) (*((uint8_t*)_payload+3-2))
65 
66 #define SKYTRAQ_NAVIGATION_DATA_WEEK(_payload) bswap16(*(uint16_t*)&_payload[4-2])
67 #define SKYTRAQ_NAVIGATION_DATA_TOW(_payload) __builtin_bswap32(*(uint32_t*)&_payload[6-2])
68 
69 #define SKYTRAQ_NAVIGATION_DATA_LAT(_payload) (int32_t)__builtin_bswap32(*( int32_t*)&_payload[10-2])
70 #define SKYTRAQ_NAVIGATION_DATA_LON(_payload) (int32_t)__builtin_bswap32(*( int32_t*)&_payload[14-2])
71 
72 #define SKYTRAQ_NAVIGATION_DATA_AEL(_payload) __builtin_bswap32(*(uint32_t*)&_payload[18-2])
73 #define SKYTRAQ_NAVIGATION_DATA_ASL(_payload) __builtin_bswap32(*(uint32_t*)&_payload[22-2])
74 
75 #define SKYTRAQ_NAVIGATION_DATA_GDOP(_payload) bswap16(*(uint16_t*)&_payload[26-2])
76 #define SKYTRAQ_NAVIGATION_DATA_PDOP(_payload) bswap16(*(uint16_t*)&_payload[28-2])
77 #define SKYTRAQ_NAVIGATION_DATA_HDOP(_payload) bswap16(*(uint16_t*)&_payload[30-2])
78 #define SKYTRAQ_NAVIGATION_DATA_VDOP(_payload) bswap16(*(uint16_t*)&_payload[32-2])
79 #define SKYTRAQ_NAVIGATION_DATA_TDOP(_payload) bswap16(*(uint16_t*)&_payload[34-2])
80 
81 #define SKYTRAQ_NAVIGATION_DATA_ECEFX(_payload) (int32_t)__builtin_bswap32(*(uint32_t*)&_payload[36-2])
82 #define SKYTRAQ_NAVIGATION_DATA_ECEFY(_payload) (int32_t)__builtin_bswap32(*(uint32_t*)&_payload[40-2])
83 #define SKYTRAQ_NAVIGATION_DATA_ECEFZ(_payload) (int32_t)__builtin_bswap32(*(uint32_t*)&_payload[44-2])
84 #define SKYTRAQ_NAVIGATION_DATA_ECEFVX(_payload) (int32_t)__builtin_bswap32(*(uint32_t*)&_payload[48-2])
85 #define SKYTRAQ_NAVIGATION_DATA_ECEFVY(_payload) (int32_t)__builtin_bswap32(*(uint32_t*)&_payload[52-2])
86 #define SKYTRAQ_NAVIGATION_DATA_ECEFVZ(_payload) (int32_t)__builtin_bswap32(*(uint32_t*)&_payload[56-2])
87 
88 
89 // distance in cm (10km dist max any direction)
90 #define MAX_DISTANCE 1000000
91 
92 static int distance_too_great(struct EcefCoor_i *ecef_ref, struct EcefCoor_i *ecef_pos);
93 
94 void gps_impl_init(void)
95 {
96 
98 
99 }
100 
101 void gps_skytraq_msg(void)
102 {
103  // current timestamp
104  uint32_t now_ts = get_sys_time_usec();
109  if (gps.fix == GPS_FIX_3D) {
112  }
113  AbiSendMsgGPS(GPS_SKYTRAQ_ID, now_ts, &gps);
114  }
116 }
117 
118 
120 {
121 
133  // pacc;
134  // sacc;
138 
140  case SKYTRAQ_FIX_3D_DGPS:
141  case SKYTRAQ_FIX_3D:
142  gps.fix = GPS_FIX_3D;
143  break;
144  case SKYTRAQ_FIX_2D:
145  gps.fix = GPS_FIX_2D;
146  break;
147  default:
148  gps.fix = GPS_FIX_NONE;
149  }
150 
151 #if GPS_USE_LATLONG
152  /* Computes from (lat, long) in the referenced UTM zone */
153  struct LlaCoor_f lla_f;
154  LLA_FLOAT_OF_BFP(lla_f, gps.lla_pos);
155  struct UtmCoor_f utm_f;
156  utm_f.zone = nav_utm_zone0;
157  /* convert to utm */
158  utm_of_lla_f(&utm_f, &lla_f);
159  /* copy results of utm conversion */
160  gps.utm_pos.east = utm_f.east * 100;
161  gps.utm_pos.north = utm_f.north * 100;
164 #endif
165 
166  if (gps.fix == GPS_FIX_3D) {
168  // just grab current ecef_pos as reference.
170  }
171  // convert ecef velocity vector to NED vector.
173 
174  // ground course in radians
175  gps.course = (atan2f((float)gps.ned_vel.y, (float)gps.ned_vel.x)) * 1e7;
176  // GT: gps.cacc = ... ? what should course accuracy be?
177 
178  // ground speed
181 
182  // vertical speed (climb)
183  // solved by gps.ned.z?
184  }
185 
186 
187 #ifdef GPS_LED
188  if (gps.fix == GPS_FIX_3D) {
189  LED_ON(GPS_LED);
190  } else {
191  LED_TOGGLE(GPS_LED);
192  }
193 #endif
194  }
195 
196 }
197 
199 {
201  gps_skytraq.checksum ^= c;
202  }
203  switch (gps_skytraq.status) {
204  case UNINIT:
205  if (c == SKYTRAQ_SYNC1) {
207  }
208  break;
209  case GOT_SYNC1:
210  if (c != SKYTRAQ_SYNC2) {
212  goto error;
213  }
215  break;
216  case GOT_SYNC2:
217  gps_skytraq.len = c << 8;
219  break;
220  case GOT_LEN1:
221  gps_skytraq.len += c;
225  goto error;
226  }
227  break;
228  case GOT_LEN2:
229  gps_skytraq.msg_id = c;
230  gps_skytraq.msg_idx = 0;
231  gps_skytraq.checksum = c;
233  break;
234  case GOT_ID:
237  if (gps_skytraq.msg_idx >= gps_skytraq.len - 1) {
239  }
240  break;
241  case GOT_PAYLOAD:
242  if (c != gps_skytraq.checksum) {
244  goto error;
245  }
247  break;
248  case GOT_CHECKSUM:
249  if (c != SKYTRAQ_SYNC3) {
251  goto error;
252  }
254  break;
255  case GOT_SYNC3:
257  goto restart;
258  default:
260  goto error;
261  }
262  return;
263 error:
265 restart:
267  return;
268 }
269 
270 static int distance_too_great(struct EcefCoor_i *ecef_ref, struct EcefCoor_i *ecef_pos)
271 {
272  int32_t xdiff = abs(ecef_ref->x - ecef_pos->x);
273  if (xdiff > MAX_DISTANCE) {
274  return TRUE;
275  }
276  int32_t ydiff = abs(ecef_ref->y - ecef_pos->y);
277  if (ydiff > MAX_DISTANCE) {
278  return TRUE;
279  }
280  int32_t zdiff = abs(ecef_ref->z - ecef_pos->z);
281  if (zdiff > MAX_DISTANCE) {
282  return TRUE;
283  }
284 
285  return FALSE;
286 }
unsigned short uint16_t
Definition: types.h:16
int32_t z
in centimeters
#define GOT_CHECKSUM
Definition: gps_skytraq.c:42
int32_t north
in centimeters
#define GOT_SYNC3
Definition: gps_skytraq.c:43
struct LtpDef_i ref_ltp
Definition: gps_skytraq.h:52
#define SKYTRAQ_NAVIGATION_DATA_ASL(_payload)
Definition: gps_skytraq.c:73
#define SKYTRAQ_FIX_3D
Definition: gps_skytraq.c:47
float east
in meters
bool_t msg_available
Definition: gps_skytraq.h:42
void gps_impl_init(void)
GPS initialization.
Definition: gps_skytraq.c:94
float north
in meters
#define MAX_DISTANCE
Definition: gps_skytraq.c:90
enum GpsSkytraqError error_last
Definition: gps_skytraq.h:50
#define SKYTRAQ_SYNC1
Definition: gps_skytraq.c:51
vector in EarthCenteredEarthFixed coordinates
int32_t y
in centimeters
#define SKYTRAQ_NAVIGATION_DATA_ECEFVY(_payload)
Definition: gps_skytraq.c:85
uint8_t msg_buf[GPS_SKYTRAQ_MAX_PAYLOAD]
Definition: gps_skytraq.h:41
Main include for ABI (AirBorneInterface).
#define GPS_SKYTRAQ_ID
uint8_t nav_utm_zone0
Definition: common_nav.c:44
position in UTM coordinates Units: meters
int32_t east
in centimeters
#define GOT_ID
Definition: gps_skytraq.c:40
uint16_t speed_3d
norm of 3d speed in cm/s
Definition: gps.h:75
#define GPS_FIX_3D
3D GPS fix
Definition: gps.h:43
int32_t z
Down.
struct UtmCoor_i utm_pos
position in UTM (north,east: cm; alt: mm over ellipsoid)
Definition: gps.h:70
#define SKYTRAQ_NAVIGATION_DATA_LON(_payload)
Definition: gps_skytraq.c:70
void ned_of_ecef_vect_i(struct NedCoor_i *ned, struct LtpDef_i *def, struct EcefCoor_i *ecef)
Rotate a vector from ECEF to NED.
#define SKYTRAQ_NAVIGATION_DATA_AEL(_payload)
Definition: gps_skytraq.c:72
#define SKYTRAQ_NAVIGATION_DATA_ECEFY(_payload)
Definition: gps_skytraq.c:82
struct EcefCoor_i ecef
Reference point in ecef.
uint32_t last_3dfix_ticks
cpu time ticks at last valid 3D fix
Definition: gps.h:89
#define FALSE
Definition: std.h:5
int32_t alt
in millimeters above WGS84 reference ellipsoid
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
#define LED_TOGGLE(i)
Definition: led_hw.h:44
int32_t y
East.
uint32_t last_msg_time
cpu time in sec at last received GPS message
Definition: gps.h:92
#define GOT_SYNC2
Definition: gps_skytraq.c:37
uint8_t zone
UTM zone number.
Paparazzi floating point math for geodetic calculations.
vector in Latitude, Longitude and Altitude
#define TRUE
Definition: std.h:4
int32_t hmsl
height above mean sea level in mm
Definition: gps.h:71
#define GOT_LEN1
Definition: gps_skytraq.c:38
uint32_t tow
GPS time of week in ms.
Definition: gps.h:84
#define GPS_FIX_NONE
No GPS fix.
Definition: gps.h:41
#define GPS_FIX_2D
2D GPS fix
Definition: gps.h:42
#define GOT_PAYLOAD
Definition: gps_skytraq.c:41
#define SKYTRAQ_ID_NAVIGATION_DATA
Definition: gps_skytraq.h:27
Device independent GPS code (interface)
uint16_t pdop
position dilution of precision scaled by 100
Definition: gps.h:80
#define SKYTRAQ_NAVIGATION_DATA_ECEFX(_payload)
Definition: gps_skytraq.c:81
void gps_skytraq_read_message(void)
Definition: gps_skytraq.c:119
#define SKYTRAQ_SYNC3
Definition: gps_skytraq.c:54
#define SKYTRAQ_NAVIGATION_DATA_ECEFVZ(_payload)
Definition: gps_skytraq.c:86
int32_t x
North.
#define SKYTRAQ_FIX_3D_DGPS
Definition: gps_skytraq.c:48
unsigned long uint32_t
Definition: types.h:18
struct EcefCoor_i ecef_pos
position in ECEF in cm
Definition: gps.h:68
int32_t lon
in degrees*1e7
#define SKYTRAQ_FIX_2D
Definition: gps_skytraq.c:46
uint8_t zone
UTM zone number.
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:70
#define GPS_SKYTRAQ_MAX_PAYLOAD
Definition: gps_skytraq.h:39
signed long int32_t
Definition: types.h:19
#define SKYTRAQ_NAVIGATION_DATA_ECEFZ(_payload)
Definition: gps_skytraq.c:83
uint8_t msg_id
Definition: gps_skytraq.h:43
#define SKYTRAQ_SYNC2
Definition: gps_skytraq.c:52
#define LED_ON(i)
Definition: led_hw.h:42
uint32_t last_3dfix_time
cpu time in sec at last valid 3D fix
Definition: gps.h:90
int32_t alt
in millimeters above WGS84 reference ellipsoid
static int distance_too_great(struct EcefCoor_i *ecef_ref, struct EcefCoor_i *ecef_pos)
Definition: gps_skytraq.c:270
unsigned char uint8_t
Definition: types.h:14
int32_t course
GPS course over ground in rad*1e7, [0, 2*Pi]*1e7 (CW/north)
Definition: gps.h:76
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
#define GOT_SYNC1
Definition: gps_skytraq.c:36
#define SKYTRAQ_NAVIGATION_DATA_ECEFVX(_payload)
Definition: gps_skytraq.c:84
uint16_t len
Definition: gps_skytraq.h:46
#define SKYTRAQ_NAVIGATION_DATA_PDOP(_payload)
Definition: gps_skytraq.c:76
uint8_t status
Definition: gps_skytraq.h:45
void gps_skytraq_parse(uint8_t c)
Definition: gps_skytraq.c:198
#define UNINIT
Definition: gps_skytraq.c:35
#define SKYTRAQ_NAVIGATION_DATA_LAT(_payload)
Definition: gps_skytraq.c:69
#define SKYTRAQ_NAVIGATION_DATA_NumSV(_payload)
Definition: gps_skytraq.c:64
uint32_t last_msg_ticks
cpu time ticks at last received GPS message
Definition: gps.h:91
uint8_t num_sv
number of sat in fix
Definition: gps.h:81
arch independent LED (Light Emitting Diodes) API
int32_t x
in centimeters
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition: gps.h:74
uint8_t error_cnt
Definition: gps_skytraq.h:49
struct EcefCoor_i ecef_vel
speed ECEF in cm/s
Definition: gps.h:72
struct GpsSkytraq gps_skytraq
Definition: gps_skytraq.c:32
#define GOT_LEN2
Definition: gps_skytraq.c:39
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition: gps.h:69
static uint16_t bswap16(uint16_t a)
Definition: gps_skytraq.c:58
#define SKYTRAQ_NAVIGATION_DATA_FixMode(_payload)
Definition: gps_skytraq.c:63
int32_t lat
in degrees*1e7
uint8_t fix
status of fix
Definition: gps.h:82
void gps_skytraq_msg(void)
Definition: gps_skytraq.c:101
struct NedCoor_i ned_vel
speed NED in cm/s
Definition: gps.h:73
uint8_t checksum
Definition: gps_skytraq.h:48
struct GpsState gps
global GPS state
Definition: gps.c:41
#define LLA_FLOAT_OF_BFP(_o, _i)
uint8_t msg_idx
Definition: gps_skytraq.h:47
void ltp_def_from_ecef_i(struct LtpDef_i *def, struct EcefCoor_i *ecef)
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
#define SKYTRAQ_NAVIGATION_DATA_TOW(_payload)
Definition: gps_skytraq.c:67