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
demo_ahrs_actuators.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Felix Ruess <felix.ruess@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <inttypes.h>
26 
27 /* PERIODIC_C_MAIN is defined before generated/periodic_telemetry.h
28  * in order to implement telemetry_mode_Main_*
29  */
30 #define PERIODIC_C_MAIN
31 #define ABI_C
32 #define DATALINK_C
33 
37 #include "subsystems/abi.h"
38 
39 #include "generated/airframe.h"
40 #include "generated/settings.h"
41 
42 #include "std.h"
43 #include "mcu.h"
44 #include "mcu_periph/sys_time.h"
45 #include "led.h"
46 
47 #include "state.h"
48 #include "subsystems/imu.h"
49 #include "subsystems/ahrs.h"
51 
52 #include "subsystems/commands.h"
53 #include "subsystems/actuators.h"
54 #include "subsystems/settings.h"
55 
56 #include "pprz_version.h"
57 
58 #ifndef DEMO_MAX_ROLL
59 #define DEMO_MAX_ROLL RadOfDeg(65)
60 #endif
61 
62 #ifndef DEMO_MAX_PITCH
63 #define DEMO_MAX_PITCH RadOfDeg(65)
64 #endif
65 
66 static inline void main_init(void);
67 static inline void main_periodic_task(void);
68 static inline void main_event_task(void);
69 
70 static void send_alive(struct transport_tx *trans, struct link_device *dev);
71 static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev);
72 static void send_actuators(struct transport_tx *trans, struct link_device *dev);
73 static void send_commands(struct transport_tx *trans, struct link_device *dev);
74 
76 
77 int main(void)
78 {
79  main_init();
80  while (1) {
83  }
85  }
86  return 0;
87 }
88 
89 static inline void main_init(void)
90 {
91  mcu_init();
93 
94  stateInit();
95  actuators_init();
96 
97  imu_init();
98 #if USE_AHRS_ALIGNER
100 #endif
101  ahrs_init();
102 
103  settings_init();
104 
105  mcu_int_enable();
106 
107  downlink_init();
108 
109  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
113 
114  // send body_to_imu from here for now
115  AbiSendMsgBODY_TO_IMU_QUAT(1, orientationGetQuat_f(&imu.body_to_imu));
116 }
117 
118 static inline void main_periodic_task(void)
119 {
120  /* Simply set current roll/pitch as commands.
121  * Scale DEMO_MAX_ROLL/PITCH to MAX_PPRZ (the max commands)
122  */
125 
126  /* generated macro from airframe file, seconds AP_MODE param not used */
127  SetActuatorsFromCommands(commands, 0);
128 
129  if (sys_time.nb_sec > 1) {
130  imu_periodic();
131  }
132  RunOnceEvery(10, { LED_PERIODIC();});
133  RunOnceEvery(PERIODIC_FREQUENCY, { datalink_time++; });
134  periodic_telemetry_send_Main(DefaultPeriodic, &(DefaultChannel).trans_tx, &(DefaultDevice).device);
135 }
136 
137 static inline void main_event_task(void)
138 {
139  mcu_event();
140  ImuEvent();
141  DatalinkEvent();
142 }
143 
144 
145 void dl_parse_msg(void)
146 {
147  uint8_t msg_id = dl_buffer[1];
148  switch (msg_id) {
149 
150  case DL_PING: {
151  DOWNLINK_SEND_PONG(DefaultChannel, DefaultDevice);
152  }
153  break;
154  case DL_SETTING:
155  if (DL_SETTING_ac_id(dl_buffer) == AC_ID) {
156  uint8_t i = DL_SETTING_index(dl_buffer);
157  float val = DL_SETTING_value(dl_buffer);
158  DlSetting(i, val);
159  DOWNLINK_SEND_DL_VALUE(DefaultChannel, DefaultDevice, &i, &val);
160  }
161  break;
162  case DL_GET_SETTING : {
163  if (DL_GET_SETTING_ac_id(dl_buffer) != AC_ID) { break; }
164  uint8_t i = DL_GET_SETTING_index(dl_buffer);
165  float val = settings_get_value(i);
166  DOWNLINK_SEND_DL_VALUE(DefaultChannel, DefaultDevice, &i, &val);
167  }
168  break;
169  default:
170  break;
171  }
172 }
173 
174 static void send_alive(struct transport_tx *trans, struct link_device *dev)
175 {
176  pprz_msg_send_ALIVE(trans, dev, AC_ID, 16, MD5SUM);
177 }
178 
180 {
181  static uint32_t ap_version = PPRZ_VERSION_INT;
182  static char *ver_desc = PPRZ_VERSION_DESC;
183  pprz_msg_send_AUTOPILOT_VERSION(trans, dev, AC_ID, &ap_version, strlen(ver_desc), ver_desc);
184 }
185 
186 static void send_actuators(struct transport_tx *trans, struct link_device *dev)
187 {
188  pprz_msg_send_ACTUATORS(trans, dev, AC_ID , ACTUATORS_NB, actuators);
189 }
190 
191 static void send_commands(struct transport_tx *trans, struct link_device *dev)
192 {
193  pprz_msg_send_COMMANDS(trans, dev, AC_ID, COMMANDS_NB, commands);
194 }
int main(void)
Interface to align the AHRS via low-passed measurements at startup.
unsigned short uint16_t
Definition: types.h:16
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:71
Dispatcher to register actual AHRS implementations.
float phi
in radians
Generic transmission transport header.
Definition: transport.h:89
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1114
#define DEMO_MAX_ROLL
Periodic telemetry system header (includes downlink utility and generated code).
Main include for ABI (AirBorneInterface).
static void main_periodic_task(void)
void imu_periodic(void)
optional.
Definition: imu_apogee.c:95
void dl_parse_msg(void)
Should be called when chars are available in dl_buffer.
uint16_t datalink_time
static void send_actuators(struct transport_tx *trans, struct link_device *dev)
void ahrs_init(void)
AHRS initialization.
Definition: ahrs.c:68
static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
void settings_init(void)
Definition: settings.c:43
void stateInit(void)
Definition: state.c:43
float theta
in radians
Hardware independent API for actuators (servos, motor controllers).
static void main_event_task(void)
Architecture independent timing functions.
uint16_t val[TCOUPLE_NB]
static void send_alive(struct transport_tx *trans, struct link_device *dev)
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
unsigned long uint32_t
Definition: types.h:18
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define ImuEvent
Definition: imu_apogee.h:113
Hardware independent code for commands handling.
Inertial Measurement Unit interface.
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:37
static bool_t sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:111
struct OrientationReps body_to_imu
rotation from body to imu frame
Definition: imu.h:53
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
Arch independent mcu ( Micro Controller Unit ) utilities.
#define mcu_int_enable()
Definition: mcu_arch.h:35
unsigned char uint8_t
Definition: types.h:14
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
API to get/set the generic vehicle states.
Persistent settings interface.
static void main_init(void)
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
static void send_commands(struct transport_tx *trans, struct link_device *dev)
#define LED_PERIODIC()
Definition: led_hw.h:47
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:64
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:47
arch independent LED (Light Emitting Diodes) API
void mcu_event(void)
MCU event functions.
Definition: mcu.c:192
#define MAX_PPRZ
Definition: paparazzi.h:8
void imu_init(void)
Definition: imu.c:110
void ahrs_aligner_init(void)
Definition: ahrs_aligner.c:79
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 struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).