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.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2011 The Paparazzi Team
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 
27 #include "subsystems/gps.h"
28 
29 #include "led.h"
30 
31 #ifdef GPS_POWER_GPIO
32 #include "mcu_periph/gpio.h"
33 
34 #ifndef GPS_POWER_GPIO_ON
35 #define GPS_POWER_GPIO_ON gpio_set
36 #endif
37 #endif
38 
39 #define MSEC_PER_WEEK (1000*60*60*24*7)
40 
41 struct GpsState gps;
42 
44 
45 #if PERIODIC_TELEMETRY
47 
48 static void send_svinfo_id(struct transport_tx *trans, struct link_device *dev,
49  uint8_t svid)
50 {
51  if (svid < GPS_NB_CHANNELS) {
52  pprz_msg_send_SVINFO(trans, dev, AC_ID, &svid,
53  &gps.svinfos[svid].svid, &gps.svinfos[svid].flags,
54  &gps.svinfos[svid].qi, &gps.svinfos[svid].cno,
55  &gps.svinfos[svid].elev, &gps.svinfos[svid].azim);
56  }
57 }
58 
60 static void send_svinfo(struct transport_tx *trans, struct link_device *dev)
61 {
62  static uint8_t i = 0;
63  if (i == gps.nb_channels) { i = 0; }
64  send_svinfo_id(trans, dev, i);
65  i++;
66 }
67 
72 static inline void send_svinfo_available(struct transport_tx *trans, struct link_device *dev)
73 {
74  static uint8_t i = 0;
75  static uint8_t last_cnos[GPS_NB_CHANNELS];
76  if (i >= gps.nb_channels) { i = 0; }
77  // send SVINFO for all satellites while no GPS fix,
78  // after 3D fix, send avialable sats if they were updated
79  if (gps.fix < GPS_FIX_3D) {
80  send_svinfo_id(trans, dev, i);
81  } else if (gps.svinfos[i].cno != last_cnos[i]) {
82  send_svinfo_id(trans, dev, i);
83  last_cnos[i] = gps.svinfos[i].cno;
84  }
85  i++;
86 }
87 
88 static void send_gps(struct transport_tx *trans, struct link_device *dev)
89 {
90  uint8_t zero = 0;
91  int16_t climb = -gps.ned_vel.z;
92  int16_t course = (DegOfRad(gps.course) / ((int32_t)1e6));
93  pprz_msg_send_GPS(trans, dev, AC_ID, &gps.fix,
95  &course, &gps.hmsl, &gps.gspeed, &climb,
96  &gps.week, &gps.tow, &gps.utm_pos.zone, &zero);
97  // send SVINFO for available satellites that have new data
98  send_svinfo_available(trans, dev);
99 }
100 
101 static void send_gps_int(struct transport_tx *trans, struct link_device *dev)
102 {
103  pprz_msg_send_GPS_INT(trans, dev, AC_ID,
106  &gps.hmsl,
108  &gps.pacc, &gps.sacc,
109  &gps.tow,
110  &gps.pdop,
111  &gps.num_sv,
112  &gps.fix);
113  // send SVINFO for available satellites that have new data
114  send_svinfo_available(trans, dev);
115 }
116 
117 static void send_gps_lla(struct transport_tx *trans, struct link_device *dev)
118 {
119  uint8_t err = 0;
120  int16_t climb = -gps.ned_vel.z;
121  int16_t course = (DegOfRad(gps.course) / ((int32_t)1e6));
122  pprz_msg_send_GPS_LLA(trans, dev, AC_ID,
124  &gps.hmsl, &course, &gps.gspeed, &climb,
125  &gps.week, &gps.tow,
126  &gps.fix, &err);
127 }
128 
129 static void send_gps_sol(struct transport_tx *trans, struct link_device *dev)
130 {
131  pprz_msg_send_GPS_SOL(trans, dev, AC_ID, &gps.pacc, &gps.sacc, &gps.pdop, &gps.num_sv);
132 }
133 #endif
134 
135 void gps_init(void)
136 {
137  gps.fix = GPS_FIX_NONE;
138  gps.week = 0;
139  gps.tow = 0;
140  gps.cacc = 0;
141 
142  gps.last_3dfix_ticks = 0;
143  gps.last_3dfix_time = 0;
144  gps.last_msg_ticks = 0;
145  gps.last_msg_time = 0;
146 #ifdef GPS_POWER_GPIO
148  GPS_POWER_GPIO_ON(GPS_POWER_GPIO);
149 #endif
150 #ifdef GPS_LED
151  LED_OFF(GPS_LED);
152 #endif
153 #ifdef GPS_TYPE_H
154  gps_impl_init();
155 #endif
156 
157 #if PERIODIC_TELEMETRY
158  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GPS, send_gps);
159  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GPS_INT, send_gps_int);
160  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GPS_LLA, send_gps_lla);
161  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GPS_SOL, send_gps_sol);
162  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_SVINFO, send_svinfo);
163 #endif
164 }
165 
167 {
169  gps.fix = GPS_FIX_NONE;
170  }
171 }
172 
174 {
175  uint32_t clock_delta;
176  uint32_t time_delta;
177  uint32_t itow_now;
178 
179  if (sys_ticks < gps_time_sync.t0_ticks) {
180  clock_delta = (0xFFFFFFFF - sys_ticks) + gps_time_sync.t0_ticks + 1;
181  } else {
182  clock_delta = sys_ticks - gps_time_sync.t0_ticks;
183  }
184 
185  time_delta = msec_of_sys_time_ticks(clock_delta);
186 
187  itow_now = gps_time_sync.t0_tow + time_delta;
188  if (itow_now > MSEC_PER_WEEK) {
189  itow_now %= MSEC_PER_WEEK;
190  }
191 
192  return itow_now;
193 }
194 
198 void WEAK gps_inject_data(uint8_t packet_id __attribute__((unused)), uint8_t length __attribute__((unused)), uint8_t *data __attribute__((unused))){
199 
200 }
uint8_t qi
quality bitfield (GPS receiver specific)
Definition: gps.h:60
int32_t z
in centimeters
uint32_t t0_tow
GPS time of week in ms from last message.
Definition: gps.h:98
int32_t north
in centimeters
struct SVinfo svinfos[GPS_NB_CHANNELS]
holds information from the Space Vehicles (Satellites)
Definition: gps.h:87
int16_t azim
azimuth in deg
Definition: gps.h:63
uint32_t pacc
position accuracy in cm
Definition: gps.h:77
Generic transmission transport header.
Definition: transport.h:89
uint8_t nb_channels
Number of scanned satellites.
Definition: gps.h:86
uint32_t t0_ticks
hw clock ticks when GPS message is received
Definition: gps.h:100
Periodic telemetry system header (includes downlink utility and generated code).
int32_t y
in centimeters
uint16_t week
GPS week.
Definition: gps.h:83
Some architecture independent helper functions for GPIOs.
static uint32_t msec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:153
int32_t east
in centimeters
#define MSEC_PER_WEEK
Definition: gps.c:39
uint8_t svid
Satellite ID.
Definition: gps.h:58
#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
uint32_t last_3dfix_ticks
cpu time ticks at last valid 3D fix
Definition: gps.h:89
int8_t elev
elevation in deg
Definition: gps.h:62
int32_t alt
in millimeters above WGS84 reference ellipsoid
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.
int32_t hmsl
height above mean sea level in mm
Definition: gps.h:71
void WEAK gps_inject_data(uint8_t packet_id, uint8_t length, uint8_t *data)
Default parser for GPS injected data.
Definition: gps.c:198
uint8_t cno
Carrier to Noise Ratio (Signal Strength) in dbHz.
Definition: gps.h:61
data structure for GPS information
Definition: gps.h:67
uint32_t tow
GPS time of week in ms.
Definition: gps.h:84
#define GPS_FIX_NONE
No GPS fix.
Definition: gps.h:41
Device independent GPS code (interface)
uint16_t pdop
position dilution of precision scaled by 100
Definition: gps.h:80
unsigned long uint32_t
Definition: types.h:18
struct EcefCoor_i ecef_pos
position in ECEF in cm
Definition: gps.h:68
signed short int16_t
Definition: types.h:17
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
int32_t lon
in degrees*1e7
signed long int32_t
Definition: types.h:19
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
uint32_t last_3dfix_time
cpu time in sec at last valid 3D fix
Definition: gps.h:90
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
void gps_periodic_check(void)
Periodic GPS check.
Definition: gps.c:166
uint8_t flags
bitfield with GPS receiver specific flags
Definition: gps.h:59
struct GpsTimeSync gps_time_sync
Definition: gps.c:43
uint32_t last_msg_ticks
cpu time ticks at last received GPS message
Definition: gps.h:91
uint32_t gps_tow_from_sys_ticks(uint32_t sys_ticks)
Convert time in sys_time ticks to GPS time of week.
Definition: gps.c:173
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
struct EcefCoor_i ecef_vel
speed ECEF in cm/s
Definition: gps.h:72
data structure for GPS time sync
Definition: gps.h:97
#define GPS_TIMEOUT
GPS timeout in seconds.
Definition: gps.h:117
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition: gps.h:69
#define LED_OFF(i)
Definition: led_hw.h:43
int32_t lat
in degrees*1e7
uint8_t fix
status of fix
Definition: gps.h:82
struct NedCoor_i ned_vel
speed NED in cm/s
Definition: gps.h:73
struct GpsState gps
global GPS state
Definition: gps.c:41
void gpio_setup_output(uint32_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_ardrone.c:102
void gps_init(void)
initialize the global GPS state
Definition: gps.c:135
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46