-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ins_vn100.c
314 lines (278 loc) · 10.3 KB
/
ins_vn100.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (C) 2010 ENAC
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/**
* \brief driver for the VectorNav VN100
*/
#include "modules/ins/ins_vn100.h"
#include "generated/airframe.h"
#include "mcu_periph/spi.h"
#include "state.h"
// for telemetry report
#include "mcu_periph/uart.h"
#include "subsystems/datalink/downlink.h"
#include "messages.h"
#ifndef INS_YAW_NEUTRAL_DEFAULT
#define INS_YAW_NEUTRAL_DEFAULT 0.
#endif
// default spi device
#ifndef VN100_SPI_DEV
#define VN100_SPI_DEV spi1
#endif
// default slave number
#ifndef VN100_SLAVE_IDX
#define VN100_SLAVE_IDX 0
#endif
/* neutrals */
float ins_roll_neutral;
float ins_pitch_neutral;
float ins_yaw_neutral;
struct FloatEulers ins_eulers;
struct FloatQuat ins_quat;
struct FloatRates ins_rates;
struct FloatRMat ins_rmat;
struct FloatVect3 ins_accel;
struct FloatVect3 ins_mag;
volatile uint8_t ins_msg_received;
/* last received SPI packet */
VN100_Res_Packet last_received_packet;
/* last send packet */
VN100_Req_Packet last_send_packet;
/* output mode */
uint32_t ins_ador;
uint32_t ins_adof;
uint32_t ins_baud;
uint8_t ins_init_status;
// parsing function
static inline void parse_ins_msg( void );
/* spi transaction */
struct spi_transaction vn100_trans;
/* init vn100 */
void vn100_init( void ) {
//ins_roll_neutral = INS_ROLL_NEUTRAL_DEFAULT;
//ins_pitch_neutral = INS_PITCH_NEUTRAL_DEFAULT;
ins_yaw_neutral = INS_YAW_NEUTRAL_DEFAULT;
vn100_trans.slave_idx = VN100_SLAVE_IDX;
vn100_trans.cpol = SPICpolIdleHigh;
vn100_trans.cpha = SPICphaEdge2;
vn100_trans.dss = SPIDss8bit;
vn100_trans.select = SPISelectUnselect;
vn100_trans.output_buf = (uint8_t*)&last_send_packet;
vn100_trans.input_buf = (uint8_t*)&last_received_packet;
vn100_trans.status = SPITransDone;
ins_ador = VN100_ADOR;
ins_adof = VN100_ADOF;
ins_baud = VN100_BAUD;
ins_init_status = INS_VN100_SET_BAUD;
}
static inline bool_t ins_configure( void ) {
// nothing to receive during conf
vn100_trans.input_length = 0;
switch (ins_init_status) {
case INS_VN100_SET_BAUD :
last_send_packet.RegID = VN100_REG_SBAUD;
vn100_trans.output_length = 4+VN100_REG_SBAUD_SIZE;
ins_init_status++;
break;
case INS_VN100_SET_ADOR :
last_send_packet.RegID = VN100_REG_ADOR;
vn100_trans.output_length = 4+VN100_REG_ADOR_SIZE;
ins_init_status++;
break;
case INS_VN100_SET_ADOF :
last_send_packet.RegID = VN100_REG_ADOF;
vn100_trans.output_length = 4+VN100_REG_ADOF_SIZE;
ins_init_status++;
break;
case INS_VN100_READY :
return TRUE;
}
last_send_packet.CmdID = VN100_CmdID_WriteRegister;
spi_submit(&(VN100_SPI_DEV),&vn100_trans);
return FALSE;
}
void vn100_periodic_task( void ) {
// only send config or request when last transaction is done
if (vn100_trans.status != SPITransDone) { return; }
// send request when configuration is done
if (ins_configure() == TRUE) {
// Fill request for QMR
last_send_packet.CmdID = VN100_CmdID_ReadRegister;
last_send_packet.RegID = VN100_REG_YMR;
// Set IO length
vn100_trans.output_length = 2; // Only 2 ?
vn100_trans.input_length = 4+VN100_REG_YMR_SIZE;
// submit
spi_submit(&(VN100_SPI_DEV),&vn100_trans);
}
}
void vn100_event_task( void ) {
if (vn100_trans.status == SPITransSuccess) {
parse_ins_msg();
#ifndef INS_VN100_READ_ONLY
// Update estimator
// FIXME Use a proper rotation matrix here
struct FloatEulers att = {
ins_eulers.phi - ins_roll_neutral,
ins_eulers.theta - ins_pitch_neutral,
ins_eulers.psi
};
stateSetNedToBodyEulers_f(&att);
stateSetBodyRates_f(&ins_rates);
#endif
//uint8_t s = 4+VN100_REG_QMR_SIZE;
//DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice,s,spi_buffer_input);
vn100_trans.status = SPITransDone;
}
if (vn100_trans.status == SPITransFailed) {
vn100_trans.status = SPITransDone;
// FIXME retry config if not done ?
}
}
static inline void parse_ins_msg( void ) {
if (last_received_packet.ErrID != VN100_Error_None) {
//TODO send error
return;
}
// parse message (will work only with read and write register)
switch (last_received_packet.RegID) {
case VN100_REG_ADOR :
ins_ador = last_received_packet.Data[0].UInt;
break;
case VN100_REG_ADOF :
ins_adof = last_received_packet.Data[0].UInt;
break;
case VN100_REG_SBAUD :
ins_baud = last_received_packet.Data[0].UInt;
break;
case VN100_REG_YPR :
ins_eulers.phi = RadOfDeg(last_received_packet.Data[2].Float);
ins_eulers.theta = RadOfDeg(last_received_packet.Data[1].Float);
ins_eulers.psi = RadOfDeg(last_received_packet.Data[0].Float);
break;
case VN100_REG_QTN :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
break;
case VN100_REG_QTM :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
ins_mag.x = last_received_packet.Data[4].Float;
ins_mag.y = last_received_packet.Data[5].Float;
ins_mag.z = last_received_packet.Data[6].Float;
break;
case VN100_REG_QTA :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
ins_accel.x = last_received_packet.Data[4].Float;
ins_accel.y = last_received_packet.Data[5].Float;
ins_accel.z = last_received_packet.Data[6].Float;
break;
case VN100_REG_QTR :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
ins_rates.p = last_received_packet.Data[4].Float;
ins_rates.q = last_received_packet.Data[5].Float;
ins_rates.r = last_received_packet.Data[6].Float;
break;
case VN100_REG_QMA :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
ins_mag.x = last_received_packet.Data[4].Float;
ins_mag.y = last_received_packet.Data[5].Float;
ins_mag.z = last_received_packet.Data[6].Float;
ins_accel.x = last_received_packet.Data[7].Float;
ins_accel.y = last_received_packet.Data[8].Float;
ins_accel.z = last_received_packet.Data[9].Float;
break;
case VN100_REG_QAR :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
ins_accel.x = last_received_packet.Data[4].Float;
ins_accel.y = last_received_packet.Data[5].Float;
ins_accel.z = last_received_packet.Data[6].Float;
ins_rates.p = last_received_packet.Data[7].Float;
ins_rates.q = last_received_packet.Data[8].Float;
ins_rates.r = last_received_packet.Data[9].Float;
break;
case VN100_REG_QMR :
ins_quat.qi = last_received_packet.Data[0].Float;
ins_quat.qx = last_received_packet.Data[1].Float;
ins_quat.qy = last_received_packet.Data[2].Float;
ins_quat.qz = last_received_packet.Data[3].Float;
float_eulers_of_quat(&ins_eulers, &ins_quat);
ins_mag.x = last_received_packet.Data[4].Float;
ins_mag.y = last_received_packet.Data[5].Float;
ins_mag.z = last_received_packet.Data[6].Float;
ins_accel.x = last_received_packet.Data[7].Float;
ins_accel.y = last_received_packet.Data[8].Float;
ins_accel.z = last_received_packet.Data[9].Float;
ins_rates.p = last_received_packet.Data[10].Float;
ins_rates.q = last_received_packet.Data[11].Float;
ins_rates.r = last_received_packet.Data[12].Float;
break;
case VN100_REG_YMR :
ins_eulers.phi = RadOfDeg(last_received_packet.Data[2].Float);
ins_eulers.theta = RadOfDeg(last_received_packet.Data[1].Float);
ins_eulers.psi = RadOfDeg(last_received_packet.Data[0].Float);
ins_mag.x = last_received_packet.Data[3].Float;
ins_mag.y = last_received_packet.Data[4].Float;
ins_mag.z = last_received_packet.Data[5].Float;
ins_accel.x = last_received_packet.Data[6].Float;
ins_accel.y = last_received_packet.Data[7].Float;
ins_accel.z = last_received_packet.Data[8].Float;
ins_rates.p = last_received_packet.Data[9].Float;
ins_rates.q = last_received_packet.Data[10].Float;
ins_rates.r = last_received_packet.Data[11].Float;
break;
default:
break;
}
}
#include "mcu_periph/uart.h"
#include "messages.h"
#include "subsystems/datalink/downlink.h"
extern void vn100_report_task( void ) {
DOWNLINK_SEND_AHRS_LKF(DefaultChannel, DefaultDevice,
&ins_eulers.phi, &ins_eulers.theta, &ins_eulers.psi,
&ins_quat.qi, &ins_quat.qx, &ins_quat.qy, &ins_quat.qz,
&ins_rates.p, &ins_rates.q, &ins_rates.r,
&ins_accel.x, &ins_accel.y, &ins_accel.z,
&ins_mag.x, &ins_mag.y, &ins_mag.z);
}