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
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 MODULES_C
33 
37 #include "subsystems/abi.h"
38 
39 #include "generated/airframe.h"
40 #include "generated/settings.h"
41 #include "generated/modules.h"
42 
43 #include "std.h"
44 #include "mcu.h"
45 #include "mcu_periph/sys_time.h"
46 #include "led.h"
47 
48 #include "state.h"
49 #include "subsystems/imu.h"
50 #include "subsystems/ahrs.h"
52 
53 #include "subsystems/commands.h"
54 #include "subsystems/actuators.h"
55 #include "subsystems/settings.h"
56 
57 #include "pprz_version.h"
58 
59 #ifndef DEMO_MAX_ROLL
60 #define DEMO_MAX_ROLL RadOfDeg(65)
61 #endif
62 
63 #ifndef DEMO_MAX_PITCH
64 #define DEMO_MAX_PITCH RadOfDeg(65)
65 #endif
66 
67 static inline void main_init(void);
68 static inline void main_periodic_task(void);
69 static inline void main_event_task(void);
70 
71 static void send_alive(struct transport_tx *trans, struct link_device *dev);
72 static void send_autopilot_version(struct transport_tx *trans, struct link_device *dev);
73 static void send_actuators(struct transport_tx *trans, struct link_device *dev);
74 static void send_commands(struct transport_tx *trans, struct link_device *dev);
75 
76 int main(void)
77 {
78  main_init();
79  while (1) {
82  }
84  }
85  return 0;
86 }
87 
88 static inline void main_init(void)
89 {
90  mcu_init();
91  sys_time_register_timer((1. / PERIODIC_FREQUENCY), NULL);
92 
93  stateInit();
94  actuators_init();
95 
96  modules_init();
97 #if USE_AHRS_ALIGNER
99 #endif
100  ahrs_init();
101 
102  settings_init();
103 
104  mcu_int_enable();
105 
106  downlink_init();
107 
108  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AUTOPILOT_VERSION, send_autopilot_version);
112 
113  // send body_to_imu from here for now
114  AbiSendMsgBODY_TO_IMU_QUAT(1, orientationGetQuat_f(&imu.body_to_imu));
115 }
116 
117 static inline void main_periodic_task(void)
118 {
119  /* Simply set current roll/pitch as commands.
120  * Scale DEMO_MAX_ROLL/PITCH to MAX_PPRZ (the max commands)
121  */
124 
125  /* generated macro from airframe file, seconds AP_MODE param not used */
126  SetActuatorsFromCommands(commands, 0);
127 
128  if (sys_time.nb_sec > 1) {
129  modules_periodic_task();
130  }
131  RunOnceEvery(10, { LED_PERIODIC();});
132  RunOnceEvery(PERIODIC_FREQUENCY, { datalink_time++; });
133  periodic_telemetry_send_Main(DefaultPeriodic, &(DefaultChannel).trans_tx, &(DefaultDevice).device);
134 }
135 
136 static inline void main_event_task(void)
137 {
138  mcu_event();
139  modules_event_task();
140  DatalinkEvent();
141 }
142 
143 static void send_alive(struct transport_tx *trans, struct link_device *dev)
144 {
145  pprz_msg_send_ALIVE(trans, dev, AC_ID, 16, MD5SUM);
146 }
147 
148 void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
149 {
150  static uint32_t ap_version = PPRZ_VERSION_INT;
151  static char *ver_desc = PPRZ_VERSION_DESC;
152  pprz_msg_send_AUTOPILOT_VERSION(trans, dev, AC_ID, &ap_version, strlen(ver_desc), ver_desc);
153 }
154 
155 static void send_actuators(struct transport_tx *trans, struct link_device *dev)
156 {
157  pprz_msg_send_ACTUATORS(trans, dev, AC_ID , ACTUATORS_NB, actuators);
158 }
159 
160 static void send_commands(struct transport_tx *trans, struct link_device *dev)
161 {
162  pprz_msg_send_COMMANDS(trans, dev, AC_ID, COMMANDS_NB, commands);
163 }
int main(void)
Interface to align the AHRS via low-passed measurements at startup.
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:76
Dispatcher to register actual AHRS implementations.
float phi
in radians
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1125
#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)
struct Imu imu
global IMU state
Definition: imu.c:108
#define mcu_int_enable()
Definition: mcu_arch.h:38
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.
static void send_alive(struct transport_tx *trans, struct link_device *dev)
unsigned long uint32_t
Definition: types.h:18
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
Hardware independent code for commands handling.
Inertial Measurement Unit interface.
struct OrientationReps body_to_imu
rotation from body to imu frame
Definition: imu.h:49
uint16_t datalink_time
Definition: sim_ap.c:42
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
Arch independent mcu ( Micro Controller Unit ) utilities.
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)
static bool sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:114
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
static void send_commands(struct transport_tx *trans, struct link_device *dev)
#define LED_PERIODIC()
Definition: led_hw.h:52
arch independent LED (Light Emitting Diodes) API
void mcu_event(void)
MCU event functions.
Definition: mcu.c:217
#define MAX_PPRZ
Definition: paparazzi.h:8
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
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:43
static struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).