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_sirf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Freek van Tienen
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 
23 
24 #include "subsystems/gps.h"
25 #include "subsystems/abi.h"
26 #include "led.h"
27 
28 #if GPS_USE_LATLONG
29 /* currently needed to get nav_utm_zone0 */
31 #endif
33 
34 #include <inttypes.h>
35 #include <math.h>
36 #include <stdio.h>
37 #include "gps_sirf.h"
38 
40 void sirf_parse_2(void);
41 void sirf_parse_41(void);
42 
43 void gps_impl_init(void)
44 {
47  gps_sirf.msg_len = 0;
48  gps_sirf.read_state = 0;
49 }
50 
51 void gps_sirf_msg(void)
52 {
53  // current timestamp
54  uint32_t now_ts = get_sys_time_usec();
58  if (gps_sirf.pos_available) {
59  if (gps.fix == GPS_FIX_3D) {
62  }
63  AbiSendMsgGPS(GPS_SIRF_ID, now_ts, &gps);
64  }
66 }
67 
69 {
70  switch (gps_sirf.read_state) {
71  case UNINIT:
72  if (c == 0xA0) {
73  gps_sirf.msg_len = 0;
75  gps_sirf.msg_len++;
77  }
78  break;
79  case GOT_A0:
80  if (c == 0xA2) {
82  gps_sirf.msg_len++;
84  } else {
85  goto restart;
86  }
87  break;
88  case GOT_A2:
90  gps_sirf.msg_len++;
91  if (c == 0xB0) {
93  }
94  break;
95  case GOT_B0:
96  if (c == 0xB3) {
98  gps_sirf.msg_len++;
100  } else {
101  goto restart;
102  }
103  break;
104  default:
105  break;
106  }
107  return;
108 
109 restart:
111 }
112 
113 int start_time = 0;
114 int ticks = 0;
115 int start_time2 = 0;
116 int ticks2 = 0;
117 
118 void sirf_parse_41(void)
119 {
120  struct sirf_msg_41 *p = (struct sirf_msg_41 *)&gps_sirf.msg_buf[4];
121 
122  gps.tow = Invert4Bytes(p->tow);
123  gps.hmsl = Invert4Bytes(p->alt_msl) * 10;
124  gps.num_sv = p->num_sat;
125  gps.nb_channels = p ->num_sat;
126 
127  /* read latitude, longitude and altitude from packet */
131 
132 #if GPS_USE_LATLONG
133  /* convert to utm */
134  struct LlaCoor_f lla_f;
135  LLA_FLOAT_OF_BFP(lla_f, gps.lla_pos);
136  struct UtmCoor_f utm_f;
137  utm_f.zone = nav_utm_zone0;
138  utm_of_lla_f(&utm_f, &lla_f);
139 
140  /* copy results of utm conversion */
141  gps.utm_pos.east = utm_f.east * 100;
142  gps.utm_pos.north = utm_f.north * 100;
145 #endif
146 
147  gps.sacc = (Invert2Bytes(p->ehve) >> 16);
148  gps.course = RadOfDeg(Invert2Bytes(p->cog)) * pow(10, 5);
149  gps.gspeed = RadOfDeg(Invert2Bytes(p->sog)) * pow(10, 5);
150  gps.cacc = RadOfDeg(Invert2Bytes(p->heading_err)) * pow(10, 5);
151  gps.pacc = Invert4Bytes(p->ehpe);
152  gps.pdop = p->hdop * 20;
153 
154  if ((p->nav_type >> 8 & 0x7) >= 0x4) {
155  gps.fix = GPS_FIX_3D;
156  } else if ((p->nav_type >> 8 & 0x7) >= 0x1) {
157  gps.fix = GPS_FIX_2D;
158  } else {
159  gps.fix = GPS_FIX_NONE;
160  }
161 
162 
163  //Let gps_sirf know we have a position update
165 }
166 
167 void sirf_parse_2(void)
168 {
169  struct sirf_msg_2 *p = (struct sirf_msg_2 *)&gps_sirf.msg_buf[4];
170 
171  gps.week = Invert2Bytes(p->week);
172 
173  gps.ecef_pos.x = Invert4Bytes(p->x_pos) * 100;
174  gps.ecef_pos.y = Invert4Bytes(p->y_pos) * 100;
175  gps.ecef_pos.z = Invert4Bytes(p->z_pos) * 100;
176 
177  gps.ecef_vel.x = (Invert2Bytes(p->vx) >> 16) * 100 / 8;
178  gps.ecef_vel.y = (Invert2Bytes(p->vy) >> 16) * 100 / 8;
179  gps.ecef_vel.z = (Invert2Bytes(p->vz) >> 16) * 100 / 8;
180 
181  if (gps.fix == GPS_FIX_3D) {
182  ticks++;
183 #if DEBUG_SIRF
184  printf("GPS %i %i %i %i\n", ticks, (sys_time.nb_sec - start_time), ticks2, (sys_time.nb_sec - start_time2));
185 #endif
186  } else if (sys_time.nb_sec - gps.last_3dfix_time > 10) {
188  ticks = 0;
189  }
190 
191 }
192 
193 void sirf_parse_msg(void)
194 {
195  //Set position available to false and check if it is a valid message
197  if (gps_sirf.msg_len < 8) {
198  return;
199  }
200 
201  if (start_time2 == 0) {
203  }
204  ticks2++;
205 
206  //Check the message id and parse the message
207  uint8_t message_id = gps_sirf.msg_buf[4];
208  switch (message_id) {
209  case 0x29:
210  sirf_parse_41();
211  break;
212  case 0x02:
213  sirf_parse_2();
214  break;
215  }
216 
217 }
int32_t z
in centimeters
#define GOT_B0
Definition: gps_sirf.h:41
int32_t north
in centimeters
void gps_sirf_msg(void)
Definition: gps_sirf.c:51
int msg_len
Definition: gps_sirf.h:47
float east
in meters
uint32_t tow
time of week in seconds *10^3]
Definition: gps_sirf.h:93
uint32_t pacc
position accuracy in cm
Definition: gps.h:77
float north
in meters
bool_t msg_available
Definition: gps_sirf.h:44
uint8_t nb_channels
Number of scanned satellites.
Definition: gps.h:86
int16_t vx
x-velocity * 8 in m/s
Definition: gps_sirf.h:63
int32_t y
in centimeters
uint16_t week
GPS week.
Definition: gps.h:83
int32_t longitude
in degrees (+= East) *10*7
Definition: gps_sirf.h:102
int16_t vy
y-velocity * 8 in m/s
Definition: gps_sirf.h:64
Main include for ABI (AirBorneInterface).
Sirf protocol specific code.
uint8_t nav_utm_zone0
Definition: common_nav.c:44
#define Invert4Bytes(x)
Definition: gps_sirf.h:55
position in UTM coordinates Units: meters
uint32_t ehpe
estimated horizontal position error, in meters * 10^2
Definition: gps_sirf.h:111
int32_t east
in centimeters
void sirf_parse_msg(void)
Definition: gps_sirf.c:193
void gps_impl_init(void)
GPS initialization.
Definition: gps_sirf.c:43
#define GPS_FIX_3D
3D GPS fix
Definition: gps.h:43
struct UtmCoor_i utm_pos
position in UTM (north,east: cm; alt: mm over ellipsoid)
Definition: gps.h:70
#define GPS_SIRF_ID
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
uint32_t sacc
speed accuracy in cm/s
Definition: gps.h:78
uint32_t last_msg_time
cpu time in sec at last received GPS message
Definition: gps.h:92
uint32_t cacc
course accuracy in rad*1e7
Definition: gps.h:79
uint8_t zone
UTM zone number.
Paparazzi floating point math for geodetic calculations.
int32_t x_pos
x-position in m
Definition: gps_sirf.h:60
int16_t vz
z-velocity * 8 in m/s
Definition: gps_sirf.h:65
vector in Latitude, Longitude and Altitude
int32_t z_pos
z-position in m
Definition: gps_sirf.h:62
#define TRUE
Definition: std.h:4
int32_t hmsl
height above mean sea level in mm
Definition: gps.h:71
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
Message ID 2 from GPS.
Definition: gps_sirf.h:58
uint16_t sog
speed over ground, in m/s * 10^2
Definition: gps_sirf.h:106
Device independent GPS code (interface)
bool_t pos_available
Definition: gps_sirf.h:45
uint16_t pdop
position dilution of precision scaled by 100
Definition: gps.h:80
uint16_t ehve
estimated horizontal velocity error in m/s * 10^2
Definition: gps_sirf.h:114
unsigned long uint32_t
Definition: types.h:18
uint16_t week
Definition: gps_sirf.h:69
struct EcefCoor_i ecef_pos
position in ECEF in cm
Definition: gps.h:68
uint8_t num_sat
Number of satellites used for solution.
Definition: gps_sirf.h:122
void sirf_parse_2(void)
Definition: gps_sirf.c:167
int32_t lon
in degrees*1e7
struct GpsSirf gps_sirf
Definition: gps_sirf.c:39
uint16_t nav_type
Definition: gps_sirf.h:91
Message ID 41 from GPS.
Definition: gps_sirf.h:88
int ticks
Definition: gps_sirf.c:114
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 Invert2Bytes(x)
Definition: gps_sirf.h:54
char msg_buf[SIRF_MAXLEN]
buffer for storing one nmea-line
Definition: gps_sirf.h:46
uint16_t cog
course over ground, in degrees clockwise from true north * 10^2
Definition: gps_sirf.h:107
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
int start_time
Definition: gps_sirf.c:113
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_A0
Definition: gps_sirf.h:39
int32_t latitude
in degrees (+= North) *10^7
Definition: gps_sirf.h:101
void sirf_parse_char(uint8_t c)
Definition: gps_sirf.c:68
uint16_t heading_err
in degrees * 10^2
Definition: gps_sirf.h:121
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
static float p[2][2]
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition: gps.h:74
struct EcefCoor_i ecef_vel
speed ECEF in cm/s
Definition: gps.h:72
int32_t y_pos
y-position in m
Definition: gps_sirf.h:61
uint8_t hdop
Horizontal dilution of precision x 5 (0.2 precision)
Definition: gps_sirf.h:123
int read_state
Definition: gps_sirf.h:48
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition: gps.h:69
int32_t alt_ellipsoid
in meters *10^2
Definition: gps_sirf.h:103
int32_t alt_msl
in meters *10^2
Definition: gps_sirf.h:104
void sirf_parse_41(void)
Definition: gps_sirf.c:118
int32_t lat
in degrees*1e7
uint8_t fix
status of fix
Definition: gps.h:82
struct GpsState gps
global GPS state
Definition: gps.c:41
int ticks2
Definition: gps_sirf.c:116
#define LLA_FLOAT_OF_BFP(_o, _i)
#define UNINIT
Receiving pprz messages.
Definition: protocol.c:11
int start_time2
Definition: gps_sirf.c:115
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
#define GOT_A2
Definition: gps_sirf.h:40