Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ahrs_chimu_uart.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 // Output
9 #include "state.h"
10 
11 // For centripedal corrections
12 #include "subsystems/gps.h"
13 #include "subsystems/ahrs.h"
14 
15 #include "generated/airframe.h"
16 
17 #if CHIMU_DOWNLINK_IMMEDIATE
18 #include "mcu_periph/uart.h"
19 #include "pprzlink/messages.h"
21 #endif
22 
23 #include "ins_module.h"
24 #include "imu_chimu.h"
25 
26 #include "led.h"
27 
29 
32 
34 
35 static bool ahrs_chimu_enable_output(bool enable)
36 {
37  ahrs_chimu.is_enabled = enable;
38  return ahrs_chimu.is_enabled;
39 }
40 
42 {
45 }
46 
47 void ahrs_chimu_init(void)
48 {
49  ahrs_chimu.is_enabled = true;
50  ahrs_chimu.is_aligned = false;
51 
52  uint8_t ping[7] = {CHIMU_STX, CHIMU_STX, 0x01, CHIMU_BROADCAST, MSG00_PING, 0x00, 0xE6 };
53  uint8_t rate[12] = {CHIMU_STX, CHIMU_STX, 0x06, CHIMU_BROADCAST, MSG10_UARTSETTINGS, 0x05, 0xff, 0x79, 0x00, 0x00, 0x01, 0x76 }; // 50Hz attitude only + SPI
54  uint8_t quaternions[7] = {CHIMU_STX, CHIMU_STX, 0x01, CHIMU_BROADCAST, MSG09_ESTIMATOR, 0x01, 0x39 }; // 25Hz attitude only + SPI
55  // uint8_t rate[12] = {CHIMU_STX, CHIMU_STX, 0x06, CHIMU_BROADCAST, MSG10_UARTSETTINGS, 0x04, 0xff, 0x79, 0x00, 0x00, 0x01, 0xd3 }; // 25Hz attitude only + SPI
56  // uint8_t euler[7] = {CHIMU_STX, CHIMU_STX, 0x01, CHIMU_BROADCAST, MSG09_ESTIMATOR, 0x00, 0xaf }; // 25Hz attitude only + SPI
57 
58  new_ins_attitude = 0;
59 
62 
63  CHIMU_Init(&CHIMU_DATA);
64 
65  // Request Software version
66  for (int i = 0; i < 7; i++) {
67  InsUartSend1(ping[i]);
68  }
69 
70  // Quat Filter
71  for (int i = 0; i < 7; i++) {
72  InsUartSend1(quaternions[i]);
73  }
74 
75  // 50Hz
76  CHIMU_Checksum(rate, 12);
77  InsSend(rate, 12);
78 }
79 
80 
81 void parse_ins_msg(void)
82 {
83  struct link_device *dev = InsLinkDevice;
84  while (dev->char_available(dev->periph)) {
85  uint8_t ch = dev->get_byte(dev->periph);
86 
87  if (CHIMU_Parse(ch, 0, &CHIMU_DATA)) {
88  if (CHIMU_DATA.m_MsgID == 0x03) {
89  new_ins_attitude = 1;
90  RunOnceEvery(25, LED_TOGGLE(3));
91  if (CHIMU_DATA.m_attitude.euler.phi > M_PI) {
92  CHIMU_DATA.m_attitude.euler.phi -= 2 * M_PI;
93  }
94 
95  ahrs_chimu.is_aligned = true;
96 
97  if (ahrs_chimu.is_enabled) {
98  struct FloatEulers att = {
99  CHIMU_DATA.m_attitude.euler.phi,
100  CHIMU_DATA.m_attitude.euler.theta,
101  CHIMU_DATA.m_attitude.euler.psi
102  };
104  }
105 
106 #if CHIMU_DOWNLINK_IMMEDIATE
107  static uint8_t ahrs_chimu_id = AHRS_COMP_ID_CHIMU;
108  DOWNLINK_SEND_AHRS_EULER(DefaultChannel, DefaultDevice,
109  &CHIMU_DATA.m_attitude.euler.phi,
110  &CHIMU_DATA.m_attitude.euler.theta,
111  &CHIMU_DATA.m_attitude.euler.psi,
112  &ahrs_chimu_id);
113 #endif
114 
115  }
116  }
117  }
118 }
static void stateSetNedToBodyEulers_f(struct FloatEulers *ned_to_body_eulers)
Set vehicle body attitude from euler angles (float).
Definition: state.h:1105
bool is_aligned
Definition: ahrs_chimu.h:34
bool is_enabled
Definition: ahrs_chimu.h:33
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
Dispatcher to register actual AHRS implementations.
CHIMU_attitude_data m_attitude
Definition: imu_chimu.h:168
#define AHRS_COMP_ID_CHIMU
Definition: ahrs.h:42
INS_FORMAT ins_roll_neutral
float phi
Definition: imu_chimu.h:118
float theta
Definition: imu_chimu.h:119
struct AhrsChimu ahrs_chimu
#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
#define INS_ROLL_NEUTRAL_DEFAULT
Definition: ahrs_sim.c:43
void CHIMU_Checksum(unsigned char *data, unsigned char buflen)
Definition: imu_chimu.c:82
void parse_ins_msg(void)
euler angles
#define InsLinkDevice
Definition: ins_module.h:76
void ahrs_register_impl(AhrsEnableOutput enable)
Register an AHRS implementation.
Definition: ahrs.c:62
void ahrs_chimu_register(void)
#define MSG00_PING
Definition: imu_chimu.h:51
void CHIMU_Init(CHIMU_PARSER_DATA *pstData)
Definition: imu_chimu.c:108
Device independent GPS code (interface)
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
#define INS_PITCH_NEUTRAL_DEFAULT
Definition: ahrs_sim.c:46
#define LED_TOGGLE(i)
Definition: led_hw.h:52
#define INS_FORMAT
Definition: ins_module.h:35
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
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
CHIMU_PARSER_DATA CHIMU_DATA
void ahrs_chimu_init(void)
arch independent LED (Light Emitting Diodes) API
#define MSG09_ESTIMATOR
Definition: imu_chimu.h:60
float psi
Definition: imu_chimu.h:120
#define InsUartSend1(c)
Definition: ins_module.h:83
static bool ahrs_chimu_enable_output(bool enable)
Device independent INS code.
INS_FORMAT ins_pitch_neutral