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
autopilot.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@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, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #ifndef AUTOPILOT_H
30 #define AUTOPILOT_H
31 
32 #include "std.h"
33 #include "generated/airframe.h"
34 #include "state.h"
35 
36 #define AP_MODE_KILL 0
37 #define AP_MODE_FAILSAFE 1
38 #define AP_MODE_HOME 2
39 #define AP_MODE_RATE_DIRECT 3
40 #define AP_MODE_ATTITUDE_DIRECT 4
41 #define AP_MODE_RATE_RC_CLIMB 5
42 #define AP_MODE_ATTITUDE_RC_CLIMB 6
43 #define AP_MODE_ATTITUDE_CLIMB 7
44 #define AP_MODE_RATE_Z_HOLD 8
45 #define AP_MODE_ATTITUDE_Z_HOLD 9
46 #define AP_MODE_HOVER_DIRECT 10
47 #define AP_MODE_HOVER_CLIMB 11
48 #define AP_MODE_HOVER_Z_HOLD 12
49 #define AP_MODE_NAV 13
50 #define AP_MODE_RC_DIRECT 14 // Safety Pilot Direct Commands for helicopter direct control
51 #define AP_MODE_CARE_FREE_DIRECT 15
52 #define AP_MODE_FORWARD 16
53 #define AP_MODE_MODULE 17
54 #define AP_MODE_FLIP 18
55 #define AP_MODE_GUIDED 19
56 
57 extern uint8_t autopilot_mode;
59 extern bool_t autopilot_motors_on;
60 extern bool_t autopilot_in_flight;
61 extern bool_t kill_throttle;
62 extern bool_t autopilot_rc;
63 
64 extern bool_t autopilot_power_switch;
65 
66 extern void autopilot_init(void);
67 extern void autopilot_periodic(void);
68 extern void autopilot_on_rc_frame(void);
69 extern void autopilot_set_mode(uint8_t new_autopilot_mode);
70 extern void autopilot_set_motors_on(bool_t motors_on);
71 extern void autopilot_check_in_flight(bool_t motors_on);
72 
73 extern bool_t autopilot_ground_detected;
74 extern bool_t autopilot_detect_ground_once;
75 
77 
80 #ifndef MODE_MANUAL
81 #define MODE_MANUAL AP_MODE_ATTITUDE_DIRECT
82 #endif
83 #ifndef MODE_AUTO1
84 #define MODE_AUTO1 AP_MODE_HOVER_Z_HOLD
85 #endif
86 #ifndef MODE_AUTO2
87 #define MODE_AUTO2 AP_MODE_NAV
88 #endif
89 
90 
91 #define autopilot_KillThrottle(_kill) { \
92  if (_kill) \
93  autopilot_set_motors_on(FALSE); \
94  else \
95  autopilot_set_motors_on(TRUE); \
96  }
97 
98 #ifdef POWER_SWITCH_GPIO
99 #include "mcu_periph/gpio.h"
100 #define autopilot_SetPowerSwitch(_v) { \
101  autopilot_power_switch = _v; \
102  if (_v) { gpio_set(POWER_SWITCH_GPIO); } \
103  else { gpio_clear(POWER_SWITCH_GPIO); } \
104  }
105 #else
106 #define autopilot_SetPowerSwitch(_v) { \
107  autopilot_power_switch = _v; \
108  }
109 #endif
110 
115 #ifdef ROTORCRAFT_IS_HELI
116 #define SetRotorcraftCommands(_cmd, _in_flight, _motor_on) { \
117  commands[COMMAND_ROLL] = _cmd[COMMAND_ROLL]; \
118  commands[COMMAND_PITCH] = _cmd[COMMAND_PITCH]; \
119  commands[COMMAND_YAW] = _cmd[COMMAND_YAW]; \
120  commands[COMMAND_THRUST] = _cmd[COMMAND_THRUST]; \
121  }
122 #else
123 
124 #ifndef ROTORCRAFT_COMMANDS_YAW_ALWAYS_ENABLED
125 #define SetRotorcraftCommands(_cmd, _in_flight, _motor_on) { \
126  if (!(_in_flight)) { _cmd[COMMAND_YAW] = 0; } \
127  if (!(_motor_on)) { _cmd[COMMAND_THRUST] = 0; } \
128  commands[COMMAND_ROLL] = _cmd[COMMAND_ROLL]; \
129  commands[COMMAND_PITCH] = _cmd[COMMAND_PITCH]; \
130  commands[COMMAND_YAW] = _cmd[COMMAND_YAW]; \
131  commands[COMMAND_THRUST] = _cmd[COMMAND_THRUST]; \
132  }
133 #else
134 #define SetRotorcraftCommands(_cmd, _in_flight, _motor_on) { \
135  if (!(_motor_on)) { _cmd[COMMAND_THRUST] = 0; } \
136  commands[COMMAND_ROLL] = _cmd[COMMAND_ROLL]; \
137  commands[COMMAND_PITCH] = _cmd[COMMAND_PITCH]; \
138  commands[COMMAND_YAW] = _cmd[COMMAND_YAW]; \
139  commands[COMMAND_THRUST] = _cmd[COMMAND_THRUST]; \
140  }
141 #endif
142 #endif
143 
145 #ifndef THRESHOLD_GROUND_DETECT
146 #define THRESHOLD_GROUND_DETECT 25.0
147 #endif
148 
150 static inline void DetectGroundEvent(void)
151 {
153  struct NedCoor_f *accel = stateGetAccelNed_f();
154  if (accel->z < -THRESHOLD_GROUND_DETECT ||
155  accel->z > THRESHOLD_GROUND_DETECT) {
158  }
159  }
160 }
161 
162 #include "subsystems/settings.h"
163 
164 /* try to make sure that we don't write to flash while flying */
165 static inline void autopilot_StoreSettings(float store)
166 {
167  if (kill_throttle && store) {
168  settings_store_flag = store;
169  settings_store();
170  }
171 }
172 
173 static inline void autopilot_ClearSettings(float clear)
174 {
175  if (kill_throttle && clear) {
176  settings_clear_flag = clear;
177  settings_clear();
178  }
179 }
180 
181 #if DOWNLINK
183 extern void send_autopilot_version(struct transport_tx *trans, struct link_device *dev);
184 #endif
185 
193 extern bool_t autopilot_guided_goto_ned(float x, float y, float z, float heading);
194 
202 extern bool_t autopilot_guided_goto_ned_relative(float dx, float dy, float dz, float dyaw);
203 
211 extern bool_t autopilot_guided_goto_body_relative(float dx, float dy, float dz, float dyaw);
212 
213 
214 
215 #endif /* AUTOPILOT_H */
static void autopilot_ClearSettings(float clear)
Definition: autopilot.h:173
unsigned short uint16_t
Definition: types.h:16
void autopilot_periodic(void)
Definition: autopilot.c:334
bool_t autopilot_power_switch
Definition: autopilot.c:76
float y
in meters
Generic transmission transport header.
Definition: transport.h:89
uint16_t autopilot_flight_time
flight time in seconds.
Definition: autopilot.c:48
void send_autopilot_version(struct transport_tx *trans, struct link_device *dev)
Definition: autopilot.c:158
generic transport header
void autopilot_set_mode(uint8_t new_autopilot_mode)
Definition: autopilot.c:395
bool_t autopilot_rc
Definition: autopilot.c:75
Some architecture independent helper functions for GPIOs.
bool_t autopilot_in_flight
Definition: autopilot.c:68
int32_t settings_store(void)
store settings marked as persistent to flash
Definition: settings.c:57
bool_t settings_store_flag
flag for setting feedback.
Definition: settings.c:38
#define FALSE
Definition: std.h:5
static void autopilot_StoreSettings(float store)
Definition: autopilot.h:165
float z
in meters
bool_t autopilot_motors_on
Definition: autopilot.c:72
#define TRUE
Definition: std.h:4
static float heading
Definition: ahrs_infrared.c:45
int32_t settings_clear(void)
clear all persistent settings from flash
Definition: settings.c:77
vector in North East Down coordinates Units: meters
#define THRESHOLD_GROUND_DETECT
Z-acceleration threshold to detect ground in m/s^2.
Definition: autopilot.h:146
bool_t autopilot_detect_ground_once
Definition: autopilot.c:79
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1009
bool_t kill_throttle
Definition: autopilot.c:42
void autopilot_init(void)
Autopilot inititalization.
Definition: autopilot.c:175
bool_t settings_clear_flag
Definition: settings.c:40
bool_t autopilot_guided_goto_body_relative(float dx, float dy, float dz, float dyaw)
Set position and heading setpoints wrt.
Definition: autopilot.c:539
void autopilot_check_in_flight(bool_t motors_on)
Definition: autopilot.c:552
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
bool_t autopilot_guided_goto_ned_relative(float dx, float dy, float dz, float dyaw)
Set position and heading setpoints wrt.
Definition: autopilot.c:527
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
Persistent settings interface.
void autopilot_on_rc_frame(void)
Get autopilot mode from two 2way switches.
Definition: autopilot.c:644
bool_t autopilot_guided_goto_ned(float x, float y, float z, float heading)
Set position and heading setpoints in GUIDED mode.
Definition: autopilot.c:516
#define AP_MODE_FAILSAFE
Definition: autopilot.h:37
void autopilot_set_motors_on(bool_t motors_on)
Definition: autopilot.c:587
uint8_t autopilot_mode_auto2
Definition: autopilot.c:66
bool_t autopilot_ground_detected
Definition: autopilot.c:78
uint8_t autopilot_mode
Definition: autopilot.c:65
static void DetectGroundEvent(void)
Ground detection based on vertical acceleration.
Definition: autopilot.h:150
float x
in meters