Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
guidance_v.c
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 
27 #include "generated/airframe.h"
30 
35 #include "modules/core/abi.h"
36 
37 #include "state.h"
38 
39 #include "math/pprz_algebra_int.h"
40 
41 
42 #ifndef GUIDANCE_V_NOMINAL_HOVER_THROTTLE
43 #define GUIDANCE_V_NOMINAL_HOVER_THROTTLE 0.4
44 #endif
46 
47 
48 #ifndef GUIDANCE_V_CLIMB_RC_DEADBAND
49 #define GUIDANCE_V_CLIMB_RC_DEADBAND MAX_PPRZ/10
50 #endif
51 
52 #ifndef GUIDANCE_V_MAX_RC_CLIMB_SPEED
53 #define GUIDANCE_V_MAX_RC_CLIMB_SPEED GUIDANCE_V_REF_MIN_ZD
54 #endif
55 
56 #ifndef GUIDANCE_V_MAX_RC_DESCENT_SPEED
57 #define GUIDANCE_V_MAX_RC_DESCENT_SPEED GUIDANCE_V_REF_MAX_ZD
58 #endif
59 
61 
62 static bool desired_zd_updated;
63 
64 #define GUIDANCE_V_GUIDED_MODE_ZHOLD 0
65 #define GUIDANCE_V_GUIDED_MODE_CLIMB 1
66 #define GUIDANCE_V_GUIDED_MODE_THROTTLE 2
67 
69 
70 #ifndef GUIDANCE_V_RC_ID
71 #define GUIDANCE_V_RC_ID ABI_BROADCAST
72 #endif
75 static void rc_cb(uint8_t sender_id UNUSED, struct RadioControl *rc);
76 
77 
78 #if PERIODIC_TELEMETRY
80 
81 static void send_tune_vert(struct transport_tx *trans, struct link_device *dev)
82 {
83  pprz_msg_send_TUNE_VERT(trans, dev, AC_ID,
85  &(stateGetPositionNed_i()->z),
88 }
89 #endif
90 
91 void guidance_v_init(void)
92 {
93 
97 
99  desired_zd_updated = false;
100 
101  gv_adapt_init();
102 
103  // bind ABI messages
104  AbiBindMsgRADIO_CONTROL(GUIDANCE_V_RC_ID, &rc_ev, rc_cb);
105 
106 #if PERIODIC_TELEMETRY
108 #endif
109 }
110 
111 static void rc_cb(uint8_t sender_id UNUSED, struct RadioControl *rc)
112 {
113  /* used in RC_DIRECT directly and as saturation in CLIMB and HOVER */
115 
116  /* used in RC_CLIMB */
119 
120  static const int32_t climb_scale = ABS(SPEED_BFP_OF_REAL(GUIDANCE_V_MAX_RC_CLIMB_SPEED) /
122  static const int32_t descent_scale = ABS(SPEED_BFP_OF_REAL(GUIDANCE_V_MAX_RC_DESCENT_SPEED) /
124 
125  if (guidance_v.rc_zd_sp > 0) {
126  guidance_v.rc_zd_sp *= descent_scale;
127  } else {
128  guidance_v.rc_zd_sp *= climb_scale;
129  }
130 }
131 
133 {
134 
135  if (new_mode == guidance_v.mode) {
136  return;
137  }
138 
139  switch (new_mode) {
143  break;
144 
147  guidance_v.zd_sp = 0;
148  /* Falls through. */
149  case GUIDANCE_V_MODE_NAV:
152  break;
153 
154  default:
155  break;
156 
157  }
158 
159  guidance_v.mode = new_mode;
160 
161 }
162 
163 void guidance_v_notify_in_flight(bool in_flight)
164 {
165  if (in_flight) {
166  gv_adapt_init();
167  }
168 }
169 
172 {
173  // cos(30°) = 0.8660254
174  static const int32_t max_bank_coef = BFP_OF_REAL(0.8660254f, INT32_TRIG_FRAC);
175 
176  struct Int32RMat *att = stateGetNedToBodyRMat_i();
177  /* thrust vector:
178  * int32_rmat_vmult(&thrust_vect, &att, &zaxis)
179  * same as last colum of rmat with INT32_TRIG_FRAC
180  * struct Int32Vect thrust_vect = {att.m[2], att.m[5], att.m[8]};
181  *
182  * Angle between two vectors v1 and v2:
183  * angle = acos(dot(v1, v2) / (norm(v1) * norm(v2)))
184  * since here both are already of unit length:
185  * angle = acos(dot(v1, v2))
186  * since we we want the cosine of the angle we simply need
187  * thrust_coeff = dot(v1, v2)
188  * also can be simplified considering: v1 is zaxis with (0,0,1)
189  * dot(v1, v2) = v1.z * v2.z = v2.z
190  */
191  int32_t coef = att->m[8];
192  if (coef < max_bank_coef) {
193  coef = max_bank_coef;
194  }
195  return coef;
196 }
197 
198 
199 void guidance_v_thrust_adapt(bool in_flight)
200 {
202 
203  if (in_flight) {
204  /* Only run adaptive throttle estimation if we are in flight and
205  * the desired vertical velocity (zd) was updated (i.e. we ran hover_loop before).
206  * This means that the estimation is not updated when using direct throttle commands.
207  *
208  * FIXME... SATURATIONS NOT TAKEN INTO ACCOUNT, AKA SUPERVISION and co
209  * FIXME get this out of here !!!! and don't get stab cmd directly
210  */
211  if (desired_zd_updated) {
212  int32_t vertical_thrust = (stabilization.cmd[COMMAND_THRUST] * guidance_v.thrust_coeff) >> INT32_TRIG_FRAC;
213  gv_adapt_run(stateGetAccelNed_i()->z, vertical_thrust, guidance_v.zd_ref);
214  }
215  } else {
216  /* reset estimate while not in_flight */
217  gv_adapt_init();
218  }
219 }
220 
221 struct ThrustSetpoint guidance_v_run(bool in_flight)
222 {
223  guidance_v_thrust_adapt(in_flight);
224 
225  /* reset flag indicating if desired zd was updated */
226  desired_zd_updated = false;
227  /* reset setpoint */
229 
230  switch (guidance_v.mode) {
231 
233  guidance_v.z_sp = stateGetPositionNed_i()->z; // for display only
235  break;
236 
241  break;
242 
246  break;
247 
250  /* Falls through. */
253  break;
254 
255  case GUIDANCE_V_MODE_NAV: {
257  break;
258  }
259 
260  default:
261  break;
262  }
263  return guidance_v.thrust;
264 }
265 
266 
268 {
269  /* set current altitude as setpoint */
271 
272  /* reset guidance reference */
275 
276  /* reset speed setting */
277  guidance_v.zd_sp = 0;
278 }
279 
280 void guidance_v_set_ref(int32_t pos, int32_t speed, int32_t accel)
281 {
282  gv_set_ref(pos, speed, accel);
283  guidance_v.z_ref = pos;
284  guidance_v.zd_ref = speed;
285  guidance_v.zdd_ref = accel;
286 }
287 
288 
290 {
291  /* convert our reference to generic representation */
292  int64_t tmp = gv_z_ref >> (GV_Z_REF_FRAC - INT32_POS_FRAC);
293  guidance_v.z_ref = (int32_t)tmp;
296  /* set flag to indicate that desired zd was updated */
297  desired_zd_updated = true;
298 }
299 
300 struct ThrustSetpoint guidance_v_from_nav(bool in_flight)
301 {
302  struct ThrustSetpoint sp;
306  guidance_v.zd_sp = 0;
309  sp = guidance_v_run_pos(in_flight, &guidance_v);
310  } else if (nav.vertical_mode == NAV_VERTICAL_MODE_CLIMB) {
315  sp = guidance_v_run_speed(in_flight, &guidance_v);
323  sp = guidance_v_guided_run(in_flight);
324  }
325  return sp;
326 }
327 
329 {
330  /* set current altitude as setpoint */
332 
333  /* reset guidance reference */
336 }
337 
338 struct ThrustSetpoint guidance_v_guided_run(bool in_flight)
339 {
340  struct ThrustSetpoint sp;
342  switch(guidance_v_guided_mode)
343  {
345  // Altitude Hold
346  guidance_v.zd_sp = 0;
349  sp = guidance_v_run_pos(in_flight, &guidance_v);
350  break;
352  // Climb
355  sp = guidance_v_run_speed(in_flight, &guidance_v);
356  break;
358  // Throttle
359  guidance_v.z_sp = stateGetPositionNed_i()->z; // for display only
361  break;
362  default:
363  break;
364  }
365  return sp;
366 }
367 
368 void guidance_v_set_z(float z)
369 {
370  /* disable vertical velocity setpoints */
372  /* set altitude setpoint */
374  /* reset speed setting */
375  guidance_v.zd_sp = 0;
376 }
377 
378 void guidance_v_set_vz(float vz)
379 {
380  /* enable vertical velocity setpoints */
382  /* set speed setting */
384 }
385 
386 void guidance_v_set_th(float th)
387 {
388  /* enable vertical velocity setpoints */
390 
391  /* reset guidance reference */
393  guidance_v.th_sp = (int32_t)(MAX_PPRZ * th);
394  Bound(guidance_v.th_sp, 0, MAX_PPRZ);
395 }
396 
397 
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
uint8_t last_wp UNUSED
int32_t m[3 *3]
#define INT32_POS_FRAC
#define INT32_TRIG_FRAC
#define INT32_SPEED_FRAC
#define INT32_ACCEL_FRAC
#define BFP_OF_REAL(_vr, _frac)
#define POS_BFP_OF_REAL(_af)
#define SPEED_BFP_OF_REAL(_af)
rotation matrix
int32_t z
Down.
static struct NedCoor_i * stateGetAccelNed_i(void)
Get acceleration in NED coordinates (int).
Definition: state.h:1020
static struct Int32RMat * stateGetNedToBodyRMat_i(void)
Get vehicle body attitude rotation matrix (int).
Definition: state.h:1119
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition: state.h:710
static struct NedCoor_i * stateGetPositionNed_i(void)
Get position in local NED coordinates (int).
Definition: state.h:665
static struct NedCoor_i * stateGetSpeedNed_i(void)
Get ground speed in local NED coordinates (int).
Definition: state.h:863
Guidance controllers (horizontal and vertical) for Hybrid UAV configurations.
Guidance in a module file.
void guidance_v_run_enter(void)
struct ThrustSetpoint guidance_v_run_pos(bool in_flight UNUSED, struct VerticalGuidance *gv)
struct ThrustSetpoint guidance_v_run_speed(bool in_flight UNUSED, struct VerticalGuidance *gv)
void gv_adapt_run(int32_t zdd_meas, int32_t thrust_applied, int32_t zd_ref)
Adaptation function.
void gv_adapt_init(void)
int32_t gv_zdd_ref
reference model vertical accel in meters/s^2 (output) fixed point representation with GV_ZDD_REF_FRAC...
int64_t gv_z_ref
reference model altitude in meters (output) fixed point representation with GV_Z_REF_FRAC Q37....
void gv_set_ref(int32_t alt, int32_t speed, int32_t accel)
void gv_update_ref_from_z_sp(int32_t z_sp)
void gv_update_ref_from_zd_sp(int32_t zd_sp, int32_t z_pos)
update vertical reference from speed setpoint.
int32_t gv_zd_ref
reference model vertical speed in meters/sec (output) fixed point representation with GV_ZD_REF_FRAC ...
#define GV_ZD_REF_FRAC
number of bits for the fractional part of gv_zd_ref
#define GV_Z_REF_FRAC
number of bits for the fractional part of gv_z_ref
#define GV_ZDD_REF_FRAC
number of bits for the fractional part of gv_zdd_ref
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
#define MAX_PPRZ
Definition: paparazzi.h:8
Paparazzi fixed point algebra.
Generic interface for radio control modules.
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:67
static void send_tune_vert(struct transport_tx *trans, struct link_device *dev)
Definition: guidance_v.c:81
void guidance_v_thrust_adapt(bool in_flight)
Definition: guidance_v.c:199
#define GUIDANCE_V_MAX_RC_DESCENT_SPEED
Definition: guidance_v.c:57
void guidance_v_set_vz(float vz)
Set z velocity setpoint.
Definition: guidance_v.c:378
void guidance_v_z_enter(void)
Definition: guidance_v.c:267
void guidance_v_notify_in_flight(bool in_flight)
Definition: guidance_v.c:163
struct ThrustSetpoint guidance_v_from_nav(bool in_flight)
Set guidance setpoint from NAV and run hover loop.
Definition: guidance_v.c:300
static void rc_cb(uint8_t sender_id UNUSED, struct RadioControl *rc)
Definition: guidance_v.c:111
#define GUIDANCE_V_MAX_RC_CLIMB_SPEED
Definition: guidance_v.c:53
static bool desired_zd_updated
Definition: guidance_v.c:62
#define GUIDANCE_V_NOMINAL_HOVER_THROTTLE
Definition: guidance_v.c:43
#define GUIDANCE_V_GUIDED_MODE_ZHOLD
Definition: guidance_v.c:64
void guidance_v_set_z(float z)
Set z position setpoint.
Definition: guidance_v.c:368
void guidance_v_guided_enter(void)
Enter GUIDED mode control.
Definition: guidance_v.c:328
#define GUIDANCE_V_GUIDED_MODE_THROTTLE
Definition: guidance_v.c:66
static int32_t get_vertical_thrust_coeff(void)
get the cosine of the angle between thrust vector and gravity vector
Definition: guidance_v.c:171
void guidance_v_set_ref(int32_t pos, int32_t speed, int32_t accel)
Set guidance ref parameters.
Definition: guidance_v.c:280
#define GUIDANCE_V_GUIDED_MODE_CLIMB
Definition: guidance_v.c:65
void guidance_v_set_th(float th)
Set throttle setpoint.
Definition: guidance_v.c:386
struct ThrustSetpoint guidance_v_guided_run(bool in_flight)
Run GUIDED mode control.
Definition: guidance_v.c:338
void guidance_v_mode_changed(uint8_t new_mode)
Definition: guidance_v.c:132
static abi_event rc_ev
Definition: guidance_v.c:74
struct VerticalGuidance guidance_v
Definition: guidance_v.c:60
#define GUIDANCE_V_CLIMB_RC_DEADBAND
Definition: guidance_v.c:49
void guidance_v_update_ref(void)
Definition: guidance_v.c:289
void guidance_v_init(void)
Definition: guidance_v.c:91
static int guidance_v_guided_mode
Definition: guidance_v.c:68
struct ThrustSetpoint guidance_v_run(bool in_flight)
Guidance vertical run functions.
Definition: guidance_v.c:221
#define GUIDANCE_V_RC_ID
Definition: guidance_v.c:71
Vertical guidance for rotorcrafts.
#define GuidanceVSetRef
Definition: guidance_v.h:129
float nominal_throttle
nominal throttle for hover.
Definition: guidance_v.h:102
int32_t z_sp
altitude setpoint in meters (input).
Definition: guidance_v.h:50
int32_t zd_sp
vertical speed setpoint in meter/s (input).
Definition: guidance_v.h:56
struct ThrustSetpoint thrust
Final thrust setpoint summation of feed-forward and feed-back commands, can be a total thrust or incr...
Definition: guidance_v.h:96
int32_t z_ref
altitude reference in meters.
Definition: guidance_v.h:62
#define GUIDANCE_V_MODE_CLIMB
Definition: guidance_v.h:38
#define GUIDANCE_V_MODE_RC_CLIMB
Definition: guidance_v.h:37
int32_t zd_ref
vertical speed reference in meter/s.
Definition: guidance_v.h:68
#define GUIDANCE_V_MODE_KILL
Definition: guidance_v.h:35
int32_t th_sp
input thrust setpoint.
Definition: guidance_v.h:90
int32_t rc_zd_sp
Vertical speed setpoint from radio control.
Definition: guidance_v.h:85
#define GUIDANCE_V_MODE_RC_DIRECT
Definition: guidance_v.h:36
#define GUIDANCE_V_MODE_NAV
Definition: guidance_v.h:40
#define GUIDANCE_V_MODE_HOVER
Definition: guidance_v.h:39
int32_t thrust_coeff
Definition: guidance_v.h:104
int32_t zdd_ref
vertical acceleration reference in meter/s^2.
Definition: guidance_v.h:74
#define GUIDANCE_V_MODE_GUIDED
Definition: guidance_v.h:41
int32_t rc_delta_t
Direct throttle from radio control.
Definition: guidance_v.h:79
struct RotorcraftNavigation nav
Definition: navigation.c:51
Rotorcraft navigation functions.
uint32_t throttle
throttle command (in pprz_t)
Definition: navigation.h:130
float climb
climb setpoint (in m/s)
Definition: navigation.h:137
#define NAV_VERTICAL_MODE_CLIMB
Definition: navigation.h:93
#define NAV_VERTICAL_MODE_MANUAL
Definition: navigation.h:92
float nav_altitude
current altitude setpoint (in meters): might differ from fp_altitude depending on altitude shift from...
Definition: navigation.h:139
#define NAV_VERTICAL_MODE_ALT
Definition: navigation.h:94
#define NAV_VERTICAL_MODE_GUIDED
Definition: navigation.h:95
struct Stabilization stabilization
Definition: stabilization.c:41
struct ThrustSetpoint th_sp_from_thrust_i(int32_t thrust, uint8_t axis)
General stabilization interface for rotorcrafts.
#define THRUST_SP_SET_ZERO(_sp)
#define THRUST_AXIS_Z
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
Thrust setpoint // TODO to a setpoint header Structure to store the desired thrust vector with differ...
Definition: stabilization.h:82
union ThrustSetpoint::@284 sp
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98