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
ahrs_chimu_spi.c
Go to the documentation of this file.
1 /*
2  C code to connect a CHIMU using uart
3 */
4 
5 
6 #include <stdbool.h>
7 
8 // SPI
9 #include "mcu_periph/spi.h"
11 
12 // Output
13 #include "state.h"
14 
15 // For centripedal corrections
16 #include "subsystems/gps.h"
17 #include "subsystems/ahrs.h"
18 
19 #include "generated/airframe.h"
20 
21 #if CHIMU_DOWNLINK_IMMEDIATE
22 #include "mcu_periph/uart.h"
23 #include "messages.h"
25 #endif
26 
27 #include "ins_module.h"
28 #include "imu_chimu.h"
29 #include "ahrs_chimu.h"
30 
31 #include "led.h"
32 
34 
37 
39 
40 void ahrs_chimu_update_gps(uint8_t gps_fix, uint16_t gps_speed_3d);
41 
42 #include "subsystems/abi.h"
44 static void gps_cb(uint8_t sender_id __attribute__((unused)),
45  uint32_t stamp __attribute__((unused)),
46  struct GpsState *gps_s)
47 {
48  ahrs_chimu_update_gps(gps_s->fix, gps_s->speed_3d);
49 }
50 
51 static bool_t ahrs_chimu_enable_output(bool_t enable)
52 {
53  ahrs_chimu.is_enabled = enable;
54  return ahrs_chimu.is_enabled;
55 }
56 
58 {
61  AbiBindMsgGPS(ABI_BROADCAST, &gps_ev, gps_cb);
62 }
63 
64 void ahrs_chimu_init(void)
65 {
68 
69  // uint8_t ping[7] = {CHIMU_STX, CHIMU_STX, 0x01, CHIMU_BROADCAST, MSG00_PING, 0x00, 0xE6 };
70  uint8_t rate[12] = {CHIMU_STX, CHIMU_STX, 0x06, CHIMU_BROADCAST, MSG10_UARTSETTINGS, 0x05, 0xff, 0x79, 0x00, 0x00, 0x01, 0x76 }; // 50Hz attitude only + SPI
71  uint8_t quaternions[7] = {CHIMU_STX, CHIMU_STX, 0x01, CHIMU_BROADCAST, MSG09_ESTIMATOR, 0x01, 0x39 }; // 25Hz attitude only + SPI
72  // uint8_t rate[12] = {CHIMU_STX, CHIMU_STX, 0x06, CHIMU_BROADCAST, MSG10_UARTSETTINGS, 0x04, 0xff, 0x79, 0x00, 0x00, 0x01, 0xd3 }; // 25Hz attitude only + SPI
73  // uint8_t euler[7] = {CHIMU_STX, CHIMU_STX, 0x01, CHIMU_BROADCAST, MSG09_ESTIMATOR, 0x00, 0xaf }; // 25Hz attitude only + SPI
74 
75  new_ins_attitude = 0;
76 
79 
80  // Init
81  CHIMU_Init(&CHIMU_DATA);
82 
83  // Quat Filter
84  CHIMU_Checksum(quaternions, 7);
85  InsSend(quaternions, 7);
86 
87  // Wait a bit (SPI send zero)
88  InsSend1(0);
89  InsSend1(0);
90  InsSend1(0);
91  InsSend1(0);
92  InsSend1(0);
93 
94  // 50Hz data: attitude only
95  CHIMU_Checksum(rate, 12);
96  InsSend(rate, 12);
97 }
98 
99 
100 void parse_ins_msg(void)
101 {
102  struct link_device *dev = InsLinkDevice;
103  while (dev->char_available(dev->periph)) {
104  uint8_t ch = dev->get_byte(dev->periph);
105 
106  if (CHIMU_Parse(ch, 0, &CHIMU_DATA)) {
107  RunOnceEvery(25, LED_TOGGLE(3));
108  if (CHIMU_DATA.m_MsgID == CHIMU_Msg_3_IMU_Attitude) {
109  // TODO: remove this flag as it doesn't work with multiple AHRS
110  new_ins_attitude = 1;
111  if (CHIMU_DATA.m_attitude.euler.phi > M_PI) {
112  CHIMU_DATA.m_attitude.euler.phi -= 2 * M_PI;
113  }
114 
115  //FIXME
117 
118  if (ahrs_chimu.is_enabled) {
119  struct FloatEulers att = {
120  CHIMU_DATA.m_attitude.euler.phi,
121  CHIMU_DATA.m_attitude.euler.theta,
122  CHIMU_DATA.m_attitude.euler.psi
123  };
125  struct FloatRates rates = {
126  CHIMU_DATA.m_sensor.rate[0],
127  CHIMU_DATA.m_attrates.euler.theta,
128  0.
129  }; // FIXME rate r
130  stateSetBodyRates_f(&rates);
131  }
132  } else if (CHIMU_DATA.m_MsgID == 0x02) {
133 #if CHIMU_DOWNLINK_IMMEDIATE
134  RunOnceEvery(25, DOWNLINK_SEND_AHRS_EULER(DefaultChannel, DefaultDevice, &CHIMU_DATA.m_sensor.rate[0],
135  &CHIMU_DATA.m_sensor.rate[1], &CHIMU_DATA.m_sensor.rate[2]));
136 #endif
137  }
138  }
139  }
140 }
141 
142 
143 void ahrs_chimu_update_gps(uint8_t gps_fix __attribute__((unused)), uint16_t gps_speed_3d)
144 {
145  // Send SW Centripetal Corrections
146  uint8_t centripedal[19] = {0xae, 0xae, 0x0d, 0xaa, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2 };
147 
148  float gps_speed = 0;
149 
150  if (GpsFixValid()) {
151  gps_speed = gps_speed_3d / 100.;
152  }
153  gps_speed = FloatSwap(gps_speed);
154 
155  memmove(&centripedal[6], &gps_speed, 4);
156 
157  // Fill X-speed
158 
159  CHIMU_Checksum(centripedal, 19);
160  InsSend(centripedal, 19);
161 
162  // Downlink Send
163 }
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
unsigned short uint16_t
Definition: types.h:16
static void stateSetNedToBodyEulers_f(struct FloatEulers *ned_to_body_eulers)
Set vehicle body attitude from euler angles (float).
Definition: state.h:1076
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
static abi_event gps_ev
#define FloatSwap(X)
Definition: imu_chimu.h:112
Dispatcher to register actual AHRS implementations.
#define CHIMU_Msg_3_IMU_Attitude
Definition: imu_chimu.h:74
CHIMU_attitude_data m_attitude
Definition: imu_chimu.h:168
struct AhrsChimu ahrs_chimu
float phi
Definition: imu_chimu.h:118
Main include for ABI (AirBorneInterface).
float theta
Definition: imu_chimu.h:119
#define MSG10_UARTSETTINGS
Definition: imu_chimu.h:67
#define CHIMU_STX
Definition: imu_chimu.h:47
unsigned char m_MsgID
Definition: imu_chimu.h:162
uint16_t speed_3d
norm of 3d speed in cm/s
Definition: gps.h:75
#define INS_ROLL_NEUTRAL_DEFAULT
Definition: ahrs_sim.c:43
void CHIMU_Checksum(unsigned char *data, unsigned char buflen)
Definition: imu_chimu.c:82
#define FALSE
Definition: std.h:5
bool_t is_aligned
Definition: ahrs_chimu.h:34
euler angles
#define LED_TOGGLE(i)
Definition: led_hw.h:44
Architecture independent SPI (Serial Peripheral Interface) API.
INS_FORMAT ins_roll_neutral
#define InsLinkDevice
Definition: ins_module.h:76
static bool_t ahrs_chimu_enable_output(bool_t enable)
void ahrs_chimu_init(void)
#define TRUE
Definition: std.h:4
void ahrs_register_impl(AhrsEnableOutput enable)
Register an AHRS implementation.
Definition: ahrs.c:57
data structure for GPS information
Definition: gps.h:67
void CHIMU_Init(CHIMU_PARSER_DATA *pstData)
Definition: imu_chimu.c:108
Device independent GPS code (interface)
unsigned long uint32_t
Definition: types.h:18
INS_FORMAT ins_pitch_neutral
CHIMU_Euler euler
Definition: imu_chimu.h:135
unsigned char CHIMU_Parse(unsigned char btData, unsigned char bInputType, CHIMU_PARSER_DATA *pstData)
Definition: imu_chimu.c:149
#define InsSend(_dat, _len)
Definition: ins_module.h:84
CHIMU_sensor_data m_sensor
Definition: imu_chimu.h:170
void parse_ins_msg(void)
static void gps_cb(uint8_t sender_id, uint32_t stamp, struct GpsState *gps_s)
#define INS_PITCH_NEUTRAL_DEFAULT
Definition: ahrs_sim.c:46
#define INS_FORMAT
Definition: ins_module.h:35
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
CHIMU_PARSER_DATA CHIMU_DATA
void ahrs_chimu_update_gps(uint8_t gps_fix, uint16_t gps_speed_3d)
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
volatile uint8_t new_ins_attitude
#define CHIMU_BROADCAST
Definition: imu_chimu.h:48
void ahrs_chimu_register(void)
CHIMU_attitude_data m_attrates
Definition: imu_chimu.h:169
arch independent LED (Light Emitting Diodes) API
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:56
#define MSG09_ESTIMATOR
Definition: imu_chimu.h:60
Highspeed SPI Slave Interface.
float psi
Definition: imu_chimu.h:120
static void stateSetBodyRates_f(struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1152
uint8_t fix
status of fix
Definition: gps.h:82
#define InsSend1(c)
Definition: ins_module.h:82
bool_t is_enabled
Definition: ahrs_chimu.h:33
angular rates
Device independent INS code.
#define GpsFixValid()
Definition: gps.h:47