Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
nps_ins_vectornav.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2012 The Paparazzi Team
4  * Copyright (C) 2016 Michal Podhradsky <http://github.com/podhrmic>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
24 #include "nps_ins.h"
25 #include <sys/time.h>
26 #include "nps_fdm.h"
27 #include <time.h>
28 #include <stdio.h>
29 #include "nps_sensors.h"
30 #include <stdlib.h> /* srand, rand */
31 
32 /*
33  * Vectornav info
34  */
35 #define VN_DATA_START 10
36 #define VN_BUFFER_SIZE 512
37 #define GPS_SEC_IN_DAY 86400
38 
39 static uint8_t VN_SYNC = 0xFA;
40 static uint8_t VN_OUTPUT_GROUP = 0x39;
41 static uint16_t VN_GROUP_FIELD_1 = 0x01E9;
42 static uint16_t VN_GROUP_FIELD_2 = 0x061A;
43 static uint16_t VN_GROUP_FIELD_3 = 0x0140;
44 static uint16_t VN_GROUP_FIELD_4 = 0x0009;
45 
47 
49 
50 struct VectornavData {
52  float YawPitchRoll[3];
53  float AngularRate[3];
54  double Position[3];
55  float Velocity[3];
56  float Accel[3];
60  float PosU[3];
61  float VelU;
62  float LinearAccelBody[3];
63  float YprU[3];
65  float VelBody[3];
66 };
67 
69 
70 void ins_vectornav_init(void);
71 void ins_vectornav_init(void) {}
72 void ins_vectornav_event(void);
73 void ins_vectornav_event(void) {}
74 
80 static short vn_calculate_crc(unsigned char data[], unsigned int length)
81 {
82  unsigned int i;
83  unsigned short crc = 0;
84  for (i = 0; i < length; i++) {
85  crc = (unsigned char)(crc >> 8) | (crc << 8);
86  crc ^= data[i];
87  crc ^= (unsigned char)(crc & 0xff) >> 4;
88  crc ^= crc << 12;
89  crc ^= (crc & 0x00ff) << 5;
90  }
91  return crc;
92 }
93 
94 void nps_ins_init(void)
95 {
96  ins_buffer = &vn_buffer[0];
97 }
98 
99 
104 {
105  struct timeval curTime;
106  gettimeofday(&curTime, NULL);
107  int milli = curTime.tv_usec / 1000;
108  struct tm t_res;
109  localtime_r(&curTime.tv_sec, &t_res);
110  struct tm *tt = &t_res;
111 
112  uint64_t tow = (uint64_t)GPS_SEC_IN_DAY * tt->tm_wday + (uint64_t)3600 * tt->tm_hour + (uint64_t)60 * tt->tm_min + tt->tm_sec; // sec
113  tow = tow * 1000; // tow to ms
114  tow = tow + milli; // tow with added ms
115  tow = tow * 1e6; // tow in nanoseconds
116 
117  return tow;
118 }
119 
125 void nps_ins_fetch_data(struct NpsFdm *fdm_ins)
126 {
127  struct NpsFdm fdm_data;
128  memcpy(&fdm_data, fdm_ins, sizeof(struct NpsFdm));
129 
130  // Timestamp
131  vn_data.TimeStartup = (uint64_t)(fdm_data.time * 1000000000.0);
132 
133  //Attitude, float, [degrees], yaw, pitch, roll, NED frame
134  vn_data.YawPitchRoll[0] = DegOfRad((float)fdm_data.ltp_to_body_eulers.psi); // yaw
135  vn_data.YawPitchRoll[1] = DegOfRad((float)fdm_data.ltp_to_body_eulers.theta); // pitch
136  vn_data.YawPitchRoll[2] = DegOfRad((float)fdm_data.ltp_to_body_eulers.phi); // roll
137 
138  // Rates (imu frame), float, [rad/s]
139  vn_data.AngularRate[0] = (float)fdm_data.body_ecef_rotvel.p;
140  vn_data.AngularRate[1] = (float)fdm_data.body_ecef_rotvel.q;
141  vn_data.AngularRate[2] = (float)fdm_data.body_ecef_rotvel.r;
142 
143  //Pos LLA, double,[beg, deg, m]
144  //The estimated position given as latitude, longitude, and altitude given in [deg, deg, m] respectfully.
145  vn_data.Position[0] = DegOfRad(sensors.gps.lla_pos.lat);
146  vn_data.Position[1] = DegOfRad(sensors.gps.lla_pos.lon);
147  vn_data.Position[2] = sensors.gps.lla_pos.alt;
148 
149  //VelNed, float [m/s]
150  //The estimated velocity in the North East Down (NED) frame, given in m/s.
151  vn_data.Velocity[0] = (float)fdm_data.ltp_ecef_vel.x;
152  vn_data.Velocity[1] = (float)fdm_data.ltp_ecef_vel.y;
153  vn_data.Velocity[2] = (float)fdm_data.ltp_ecef_vel.z;
154 
155  // Accel (imu-frame), float, [m/s^-2]
156  vn_data.Accel[0] = (float)fdm_data.body_ecef_accel.x;
157  vn_data.Accel[1] = (float)fdm_data.body_ecef_accel.y;
158  vn_data.Accel[2] = (float)fdm_data.body_ecef_accel.z;
159 
160  // tow (in nanoseconds), uint64
162 
163  //num sats, uint8
164  vn_data.NumSats = 8; // random number
165 
166  //gps fix, uint8
167  // TODO: add warm-up time
168  vn_data.Fix = 3; // 3D fix
169 
170  //posU, float[3]
171  // TODO: use proper sensor simulation
172  vn_data.PosU[0] = 2.5+(((float)rand())/RAND_MAX)*0.1;
173  vn_data.PosU[1] = 2.5+(((float)rand())/RAND_MAX)*0.1;
174  vn_data.PosU[2] = 2.5+(((float)rand())/RAND_MAX)*0.1;
175 
176  //velU, float
177  // TODO: use proper sensor simulation
178  vn_data.VelU = 5.0+(((float)rand())/RAND_MAX)*0.1;
179 
180  //linear acceleration imu-body frame, float [m/s^2]
181  vn_data.LinearAccelBody[0] = (float)fdm_data.ltp_ecef_vel.x;
182  vn_data.LinearAccelBody[1] = (float)fdm_data.ltp_ecef_vel.y;
183  vn_data.LinearAccelBody[2] = (float)fdm_data.ltp_ecef_vel.z;
184 
185  //YprU, float[3]
186  // TODO: use proper sensor simulation
187  vn_data.YprU[0] = 2.5+(((float)rand())/RAND_MAX)*0.1;
188  vn_data.YprU[1] = 0.5+(((float)rand())/RAND_MAX)*0.1;
189  vn_data.YprU[2] = 0.5+(((float)rand())/RAND_MAX)*0.1;
190 
191  //instatus, uint16
192  vn_data.InsStatus = 0x02;
193 
194  //Vel body, float [m/s]
195  // The estimated velocity in the body (i.e. imu) frame, given in m/s.
196  vn_data.VelBody[0] = (float)fdm_data.body_accel.x;
197  vn_data.VelBody[1] = (float)fdm_data.body_accel.y;
198  vn_data.VelBody[2] = (float)fdm_data.body_accel.z;
199 }
200 
201 
203 {
204  static uint16_t idx;
205 
206  vn_buffer[0] = VN_SYNC;
208  vn_buffer[2] = (uint8_t)(VN_GROUP_FIELD_1 >> 8);
210  vn_buffer[4] = (uint8_t)(VN_GROUP_FIELD_2 >> 8);
212  vn_buffer[6] = (uint8_t)(VN_GROUP_FIELD_3 >> 8);
214  vn_buffer[8] = (uint8_t)(VN_GROUP_FIELD_4 >> 8);
216 
217  idx = VN_DATA_START;
218 
219  // Timestamp
220  memcpy(&vn_buffer[idx], &vn_data.TimeStartup, sizeof(uint64_t));
221  idx += sizeof(uint64_t);
222 
223  //Attitude, float, [degrees], yaw, pitch, roll, NED frame
224  memcpy(&vn_buffer[idx], &vn_data.YawPitchRoll, 3 * sizeof(float));
225  idx += 3 * sizeof(float);
226 
227  // Rates (imu frame), float, [rad/s]
228  memcpy(&vn_buffer[idx], &vn_data.AngularRate, 3 * sizeof(float));
229  idx += 3 * sizeof(float);
230 
231  //Pos LLA, double,[beg, deg, m]
232  //The estimated position given as latitude, longitude, and altitude given in [deg, deg, m] respectfully.
233  memcpy(&vn_buffer[idx], &vn_data.Position, 3 * sizeof(double));
234  idx += 3 * sizeof(double);
235 
236  //VelNed, float [m/s]
237  //The estimated velocity in the North East Down (NED) frame, given in m/s.
238  memcpy(&vn_buffer[idx], &vn_data.Velocity, 3 * sizeof(float));
239  idx += 3 * sizeof(float);
240 
241  // Accel (imu-frame), float, [m/s^-2]
242  memcpy(&vn_buffer[idx], &vn_data.Accel, 3 * sizeof(float));
243  idx += 3 * sizeof(float);
244 
245  // tow (in nanoseconds), uint64
246  memcpy(&vn_buffer[idx], &vn_data.Tow, sizeof(uint64_t));
247  idx += sizeof(uint64_t);
248 
249  //num sats, uint8
251  idx++;
252 
253  //gps fix, uint8
255  idx++;
256 
257  //posU, float[3]
258  memcpy(&vn_buffer[idx], &vn_data.PosU, 3 * sizeof(float));
259  idx += 3 * sizeof(float);
260 
261  //velU, float
262  memcpy(&vn_buffer[idx], &vn_data.VelU, sizeof(float));
263  idx += sizeof(float);
264 
265  //linear acceleration imu-body frame, float [m/s^2]
266  memcpy(&vn_buffer[idx], &vn_data.LinearAccelBody, 3 * sizeof(float));
267  idx += 3 * sizeof(float);
268 
269  //YprU, float[3]
270  memcpy(&vn_buffer[idx], &vn_data.YprU, 3 * sizeof(float));
271  idx += 3 * sizeof(float);
272 
273  //instatus, uint16
274  memcpy(&vn_buffer[idx], &vn_data.InsStatus, sizeof(uint16_t));
275  idx += sizeof(uint16_t);
276 
277  //Vel body, float [m/s]
278  // The estimated velocity in the body (i.e. imu) frame, given in m/s.
279  memcpy(&vn_buffer[idx], &vn_data.VelBody, 3 * sizeof(float));
280  idx += 3 * sizeof(float);
281 
282  // calculate checksum & send
283  uint16_t chk = vn_calculate_crc(&vn_buffer[1], idx - 1);
284  vn_buffer[idx] = (uint8_t)(chk >> 8);
285  idx++;
286  vn_buffer[idx] = (uint8_t)(chk & 0xFF);
287  idx++;
288 
289  return idx;
290 }
NedCoor_d::y
double y
in meters
Definition: pprz_geodetic_double.h:69
uint16_t
unsigned short uint16_t
Definition: types.h:16
GPS_SEC_IN_DAY
#define GPS_SEC_IN_DAY
Definition: nps_ins_vectornav.c:37
NpsFdm::body_ecef_accel
struct DoubleVect3 body_ecef_accel
acceleration in body frame, wrt ECEF frame
Definition: nps_fdm.h:71
DoubleVect3::z
double z
Definition: pprz_algebra_double.h:49
NpsFdm
Definition: nps_fdm.h:44
VN_GROUP_FIELD_4
static uint16_t VN_GROUP_FIELD_4
Definition: nps_ins_vectornav.c:44
vn_buffer
uint8_t vn_buffer[VN_BUFFER_SIZE]
Definition: nps_ins_vectornav.c:46
NedCoor_d::x
double x
in meters
Definition: pprz_geodetic_double.h:68
VectornavData::TimeStartup
uint64_t TimeStartup
Definition: nps_ins_vectornav.c:51
DoubleEulers::phi
double phi
in radians
Definition: pprz_algebra_double.h:77
DoubleRates::q
double q
in rad/s^2
Definition: pprz_algebra_double.h:87
DoubleEulers::theta
double theta
in radians
Definition: pprz_algebra_double.h:78
VN_SYNC
static uint8_t VN_SYNC
Definition: nps_ins_vectornav.c:39
VN_GROUP_FIELD_1
static uint16_t VN_GROUP_FIELD_1
Definition: nps_ins_vectornav.c:41
DoubleRates::r
double r
in rad/s^2
Definition: pprz_algebra_double.h:88
NpsFdm::body_accel
struct DoubleVect3 body_accel
acceleration in body frame as measured by an accelerometer (incl.
Definition: nps_fdm.h:87
VN_BUFFER_SIZE
#define VN_BUFFER_SIZE
Definition: nps_ins_vectornav.c:36
VN_GROUP_FIELD_3
static uint16_t VN_GROUP_FIELD_3
Definition: nps_ins_vectornav.c:43
DoubleEulers::psi
double psi
in radians
Definition: pprz_algebra_double.h:79
nps_fdm.h
VN_OUTPUT_GROUP
static uint8_t VN_OUTPUT_GROUP
Definition: nps_ins_vectornav.c:40
NpsFdm::ltp_ecef_vel
struct NedCoor_d ltp_ecef_vel
velocity in LTP frame, wrt ECEF frame
Definition: nps_fdm.h:74
idx
static uint32_t idx
Definition: nps_radio_control_spektrum.c:105
VN_GROUP_FIELD_2
static uint16_t VN_GROUP_FIELD_2
Definition: nps_ins_vectornav.c:42
NpsFdm::body_ecef_rotvel
struct DoubleRates body_ecef_rotvel
Definition: nps_fdm.h:97
NpsFdm::time
double time
Definition: nps_fdm.h:46
VectornavData::InsStatus
uint16_t InsStatus
Definition: nps_ins_vectornav.c:64
VectornavData::AngularRate
float AngularRate[3]
Definition: nps_ins_vectornav.c:53
DoubleVect3::x
double x
Definition: pprz_algebra_double.h:47
VectornavData::LinearAccelBody
float LinearAccelBody[3]
Definition: nps_ins_vectornav.c:62
VectornavData::Position
double Position[3]
Definition: nps_ins_vectornav.c:54
VectornavData::VelU
float VelU
Definition: nps_ins_vectornav.c:61
VectornavData
Definition: nps_ins_vectornav.c:50
vn_get_time_of_week
static uint64_t vn_get_time_of_week(void)
Definition: nps_ins_vectornav.c:103
VectornavData::YprU
float YprU[3]
Definition: nps_ins_vectornav.c:63
uint8_t
unsigned char uint8_t
Definition: types.h:14
NedCoor_d::z
double z
in meters
Definition: pprz_geodetic_double.h:70
VN_DATA_START
#define VN_DATA_START
Definition: nps_ins_vectornav.c:35
ins_vectornav_event
void ins_vectornav_event(void)
Event handling for Vectornav.
Definition: nps_ins_vectornav.c:73
nps_sensors.h
vn_data
struct VectornavData vn_data
Definition: nps_ins_vectornav.c:68
nps_ins_init
void nps_ins_init(void)
Definition: nps_ins_vectornav.c:94
ins_vectornav_init
void ins_vectornav_init(void)
Initialize Vectornav struct.
Definition: nps_ins_vectornav.c:71
NpsFdm::ltp_to_body_eulers
struct DoubleEulers ltp_to_body_eulers
Definition: nps_fdm.h:92
VectornavData::YawPitchRoll
float YawPitchRoll[3]
Definition: nps_ins_vectornav.c:52
VectornavData::Fix
uint8_t Fix
Definition: nps_ins_vectornav.c:59
vn_calculate_crc
static short vn_calculate_crc(unsigned char data[], unsigned int length)
Calculates the 16-bit CRC for the given ASCII or binary message.
Definition: nps_ins_vectornav.c:80
VectornavData::Accel
float Accel[3]
Definition: nps_ins_vectornav.c:56
VectornavData::Velocity
float Velocity[3]
Definition: nps_ins_vectornav.c:55
DoubleVect3::y
double y
Definition: pprz_algebra_double.h:48
VectornavData::VelBody
float VelBody[3]
Definition: nps_ins_vectornav.c:65
ins_buffer
uint8_t * ins_buffer
Definition: nps_ins_vectornav.c:48
nps_ins_fetch_data
void nps_ins_fetch_data(struct NpsFdm *fdm_ins)
Fetch data from FDM and store them into vectornav packet NOTE: some noise is being added,...
Definition: nps_ins_vectornav.c:125
nps_ins_fill_buffer
uint16_t nps_ins_fill_buffer(void)
Definition: nps_ins_vectornav.c:202
VectornavData::NumSats
uint8_t NumSats
Definition: nps_ins_vectornav.c:58
DoubleRates::p
double p
in rad/s^2
Definition: pprz_algebra_double.h:86
VectornavData::PosU
float PosU[3]
Definition: nps_ins_vectornav.c:60
sensors
#define sensors(...)
Definition: cc2500_compat.h:68
nps_ins.h
uint64_t
unsigned long long uint64_t
Definition: types.h:20
VectornavData::Tow
uint64_t Tow
Definition: nps_ins_vectornav.c:57