Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mag_pitot_uart.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) C. De Wagter
3  * Copyright (C) 2015 Freek van Tienen <freek.v.tienen@gmail.com>
4  *
5  * This file is part of paparazzi
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
28 
29 #include "pprzlink/pprz_transport.h"
30 #include "pprzlink/intermcu_msg.h"
31 #include "mcu_periph/uart.h"
32 #include "subsystems/abi.h"
33 #include "subsystems/imu.h"
34 #include "state.h"
35 
36 /* Main magneto structure */
37 static struct mag_pitot_t mag_pitot = {
38  .device = (&((MAG_PITOT_PORT).device)),
39  .msg_available = false
40 };
41 static uint8_t mp_msg_buf[128] __attribute__((aligned));
42 
43 
44 #if PERIODIC_TELEMETRY
46 
47 static void mag_pitot_raw_downlink(struct transport_tx *trans, struct link_device *dev)
48 {
49  pprz_msg_send_IMU_MAG_RAW(trans, dev, AC_ID, &imu.mag_unscaled.x, &imu.mag_unscaled.y,
50  &imu.mag_unscaled.z);
51 }
52 #endif
53 
54 /* Initialize the magneto and pitot */
56  // Initialize transport protocol
57  pprz_transport_init(&mag_pitot.transport);
58 
59  // Set the Imu to Magneto rotation
60  struct FloatEulers imu_to_mag_eulers =
62  orientationSetEulers_f(&mag_pitot.imu_to_mag, &imu_to_mag_eulers);
63 
64 #if PERIODIC_TELEMETRY
66 #endif
67 }
68 
69 /* Parse the InterMCU message */
70 static inline void mag_pitot_parse_msg(void)
71 {
72  uint32_t now_ts = get_sys_time_usec();
73 
74  /* Parse the mag-pitot message */
75  uint8_t msg_id = mp_msg_buf[1];
76  switch (msg_id) {
77 
78  /* Got a magneto message */
79  case DL_IMCU_REMOTE_MAG: {
80  struct Int32Vect3 raw_mag;
81 
82  // Read the raw magneto information
83  raw_mag.x = DL_IMCU_REMOTE_MAG_mag_x(mp_msg_buf);
84  raw_mag.y = DL_IMCU_REMOTE_MAG_mag_y(mp_msg_buf);
85  raw_mag.z = DL_IMCU_REMOTE_MAG_mag_z(mp_msg_buf);
86 
87  // Rotate the magneto
88  struct Int32RMat *imu_to_mag_rmat = orientationGetRMat_i(&mag_pitot.imu_to_mag);
89  int32_rmat_vmult(&imu.mag_unscaled, imu_to_mag_rmat, &raw_mag);
90 
91  // Send and set the magneto IMU data
93  AbiSendMsgIMU_MAG_INT32(IMU_MAG_PITOT_ID, now_ts, &imu.mag);
94  break;
95  }
96 
97  /* Got a barometer message */
98  case DL_IMCU_REMOTE_BARO: {
99  float pitot_stat = DL_IMCU_REMOTE_BARO_pitot_stat(mp_msg_buf);
100  float pitot_temp = DL_IMCU_REMOTE_BARO_pitot_temp(mp_msg_buf);
101 
102  AbiSendMsgBARO_ABS(IMU_MAG_PITOT_ID, pitot_stat);
103  AbiSendMsgTEMPERATURE(IMU_MAG_PITOT_ID, pitot_temp);
104  break;
105  }
106 
107  /* Got an airspeed message */
108  case DL_IMCU_REMOTE_AIRSPEED: {
109  // Should be updated to differential pressure
110  float pitot_ias = DL_IMCU_REMOTE_AIRSPEED_pitot_IAS(mp_msg_buf);
111  AbiSendMsgAIRSPEED(IMU_MAG_PITOT_ID, pitot_ias);
112  break;
113  }
114 
115  default:
116  break;
117  }
118 }
119 
120 /* We need to wait for incomming messages */
122  // Check if we got some message from the Magneto or Pitot
123  pprz_check_and_parse(mag_pitot.device, &mag_pitot.transport, mp_msg_buf, &mag_pitot.msg_available);
124 
125  // If we have a message we should parse it
126  if (mag_pitot.msg_available) {
128  mag_pitot.msg_available = false;
129  }
130 }
131 
132 
#define IMU_TO_MAG_PHI
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
static struct Int32RMat * orientationGetRMat_i(struct OrientationReps *orientation)
Get vehicle body attitude rotation matrix (int).
#define IMU_MAG_PITOT_ID
#define IMU_TO_MAG_PSI
struct OrientationReps imu_to_mag
IMU to magneto translation.
static void mag_pitot_raw_downlink(struct transport_tx *trans, struct link_device *dev)
Periodic telemetry system header (includes downlink utility and generated code).
#define IMU_TO_MAG_THETA
void mag_pitot_event()
Main include for ABI (AirBorneInterface).
struct Imu imu
global IMU state
Definition: imu.c:108
euler angles
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
void int32_rmat_vmult(struct Int32Vect3 *vb, struct Int32RMat *m_a2b, struct Int32Vect3 *va)
rotate 3D vector by rotation matrix.
unsigned long uint32_t
Definition: types.h:18
static void mag_pitot_parse_msg(void)
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
static struct mag_pitot_t mag_pitot
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Inertial Measurement Unit interface.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
bool msg_available
If we received a message.
unsigned char uint8_t
Definition: types.h:14
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
API to get/set the generic vehicle states.
struct pprz_transport transport
The transport layer (PPRZ)
void mag_pitot_init()
rotation matrix
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
struct link_device * device
The device which is uses for communication.
static uint8_t mp_msg_buf[128]
The message buffer for the Magneto and pitot.
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
static void orientationSetEulers_f(struct OrientationReps *orientation, struct FloatEulers *eulers)
Set vehicle body attitude from euler angles (float).