Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
guidance_h.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"
28
35#include "modules/core/abi.h"
36
37/* for guidance_v.thrust_coeff */
39
40#include "state.h"
41
44
46
51
53
54#ifndef GUIDANCE_H_RC_ID
55#define GUIDANCE_H_RC_ID ABI_BROADCAST
56#endif
60
61#if PERIODIC_TELEMETRY
63
64static void send_gh(struct transport_tx *trans, struct link_device *dev)
65{
66 struct NedCoor_i *pos = stateGetPositionNed_i();
70 &(pos->x), &(pos->y));
71}
72
83
84#endif
85
112
113
127
129{
130 if (new_mode == guidance_h.mode) {
131 return;
132 }
133
134 switch (new_mode) {
138 break;
141 break;
142 default:
143 break;
144 }
145
147
148}
149
150// If not defined, use attitude max yaw setpoint (or 60 deg/s) by default
151#ifndef GUIDANCE_H_SP_MAX_R
152#ifdef STABILIZATION_ATTITUDE_SP_MAX_R
153#define GUIDANCE_H_SP_MAX_R STABILIZATION_ATTITUDE_SP_MAX_R
154#else
155#define GUIDANCE_H_SP_MAX_R 60.f
156#endif
157#endif
158
159#ifndef GUIDANCE_H_DEADBAND_R
160#define GUIDANCE_H_DEADBAND_R 200
161#endif
162
163#define YAW_DEADBAND_EXCEEDED(_rc) \
164 (rc->values[RADIO_YAW] > GUIDANCE_H_DEADBAND_R || \
165 rc->values[RADIO_YAW] < -GUIDANCE_H_DEADBAND_R)
166
167static void read_rc_setpoint_heading(struct HorizontalGuidanceRCInput *rc_sp, bool in_flight, struct RadioControl *rc)
168{
169 if (in_flight) {
170 /* calculate dt for yaw integration */
171 float dt = get_sys_time_float() - rc_sp->last_ts;
172 /* make sure nothing drastically weird happens, bound dt to 0.5sec */
173 Bound(dt, 0, 0.5);
174
175 /* do not advance yaw setpoint if within a small deadband around stick center or if throttle is zero */
177 float heading_rate = (float) rc->values[RADIO_YAW] * GUIDANCE_H_SP_MAX_R / MAX_PPRZ;
178 rc_sp->heading += heading_rate * dt;
180 }
181 } else { /* if not flying, use current yaw as setpoint */
183 }
184 /* update timestamp for dt calculation */
185 rc_sp->last_ts = get_sys_time_float();
186}
187
189static void read_rc_setpoint_speed_i(struct Int32Vect2 *speed_sp, bool in_flight, struct RadioControl *rc)
190{
191 if (in_flight) {
192 // negative pitch is forward
193 int64_t rc_x = -rc->values[RADIO_PITCH];
194 int64_t rc_y = rc->values[RADIO_ROLL];
195 DeadBand(rc_x, MAX_PPRZ / 20);
196 DeadBand(rc_y, MAX_PPRZ / 20);
197
198 // convert input from MAX_PPRZ range to SPEED_BFP
201 //int32_t rc_norm = sqrtf(rc_x * rc_x + rc_y * rc_y);
202 //int32_t max_pprz = rc_norm * MAX_PPRZ / Max(abs(rc_x), abs(rc_y);
203 rc_x = rc_x * max_speed / MAX_PPRZ;
204 rc_y = rc_y * max_speed / MAX_PPRZ;
205
206 /* Rotate from body to NED frame by negative psi angle */
209 PPRZ_ITRIG_SIN(s_psi, psi);
210 PPRZ_ITRIG_COS(c_psi, psi);
211 speed_sp->x = (int32_t)(((int64_t)c_psi * rc_x + (int64_t)s_psi * rc_y) >> INT32_TRIG_FRAC);
212 speed_sp->y = (int32_t)((-(int64_t)s_psi * rc_x + (int64_t)c_psi * rc_y) >> INT32_TRIG_FRAC);
213 } else {
214 speed_sp->x = 0;
215 speed_sp->y = 0;
216 }
217}
218
220{
221 switch (guidance_h.mode) {
222
226#if GUIDANCE_H_USE_SPEED_REF
227 /* enable x,y velocity setpoints */
229 guidance_h.sp.h_mask = GUIDANCE_H_SP_SPEED;
230#endif
231 break;
234 if (radio_control.status == RC_OK) {
236 }
237 break;
238 default:
239 break;
240 }
241
242}
243
245{
248
249 switch (guidance_h.mode) {
250
252 /* set psi command from RC */
254 /* fall trough to GUIDED to update ref, run traj and set final attitude setpoint */
255
256 /* Falls through. */
258 sp = guidance_h_guided_run(in_flight);
259 break;
260
262 sp = guidance_h_from_nav(in_flight);
263 break;
264
265 default:
266 break;
267 }
268
269 return sp;
270}
271
272
274{
275 /* compute reference even if usage temporarily disabled via guidance_h_use_ref */
276#if GUIDANCE_H_USE_REF
277 if (guidance_h.sp.h_mask == GUIDANCE_H_SP_ACCEL) {
282 }
283 else if (guidance_h.sp.h_mask == GUIDANCE_H_SP_SPEED) {
284 struct FloatVect2 sp_speed;
288 } else {
290 }
291#endif
292
293 /* either use the reference or simply copy the pos setpoint */
294 if (guidance_h.use_ref) {
295 /* convert our reference to generic representation */
301 } else {
302 switch (nav.setpoint_mode) {
308 guidance_h.ref.accel.x = 0;
309 guidance_h.ref.accel.y = 0;
310 break;
311
319 break;
320
322 default: // Fallback is guidance by pos
326 break;
327 }
328 }
329
330#if GUIDANCE_H_USE_SPEED_REF
332 VECT2_COPY(guidance_h.sp.pos, guidance_h.ref.pos); // for display only
333 }
334#endif
335
336 /* update heading setpoint from rate */
337 if (guidance_h.sp.yaw_mask == GUIDANCE_H_SP_YAW_RATE) {
340 }
341}
342
344{
345 /* reset speed setting */
346 guidance_h.sp.speed.x = 0;
347 guidance_h.sp.speed.y = 0;
348
349 /* set horizontal setpoint to current position */
353 /* reset guidance reference */
355
356 /* set guidance to current heading and position */
359
360 /* call specific implementation */
362}
363
365{
366 /* horizontal position setpoint from navigation/flightplan */
369
370 /* call specific implementation */
373}
374
376{
377 if (!in_flight) {
379 }
380
381 if (nav.fp_max_speed > 0.f) {
383 }
387 return sp; // don't call guidance, still return attitude zero
391 }
392 else {
393 // it should be nav.setpoint_mode == NAV_SETPOINT_MODE_ATTITUDE
394 // TODO error handling ?
395 struct FloatEulers sp_cmd_f = {
396 .phi = nav.roll,
397 .theta = nav.pitch,
398 .psi = nav.heading
399 };
401 }
403 return guidance_h_guided_run(in_flight);
404 } else {
405 // update carrot for GCS display and convert ENU float -> NED int
406 // even if sp is changed later
409
410 switch (nav.setpoint_mode) {
412 guidance_h_set_pos(nav.carrot.y, nav.carrot.x); // nav pos is in ENU frame, convert to NED
416 break;
417
419 guidance_h_set_vel(nav.speed.y, nav.speed.x); // nav speed is in ENU frame, convert to NED
423 break;
424
426 guidance_h_set_acc(nav.accel.y, nav.accel.x); // nav acc is in ENU frame, convert to NED
430 break;
431
432 default:
433 // nothing to do for other cases at the moment
434 break;
435 }
436 /* return final attitude setpoint */
437 return guidance_h_cmd;
438 }
439}
440
442{
443 /* guidance_h.sp.pos and guidance_h.sp.heading need to be set from external source */
444 if (!in_flight) {
446 }
447
449
451 /* return final attitude setpoint */
452 return guidance_h_cmd;
453}
454
455void guidance_h_set_pos(float x, float y)
456{
457 if (guidance_h.sp.h_mask != GUIDANCE_H_SP_POS) {
459 }
460 guidance_h.sp.h_mask = GUIDANCE_H_SP_POS;
463}
464
471
472void guidance_h_set_body_vel(float vx, float vy)
473{
474 float psi = stateGetNedToBodyEulers_f()->psi;
475 float newvx = cosf(-psi) * vx + sinf(-psi) * vy;
476 float newvy = -sinf(-psi) * vx + cosf(-psi) * vy;
478}
479
480void guidance_h_set_vel(float vx, float vy)
481{
482 if (guidance_h.sp.h_mask != GUIDANCE_H_SP_SPEED) {
484 }
485 guidance_h.sp.h_mask = GUIDANCE_H_SP_SPEED;
488}
489
490void guidance_h_set_body_acc(float ax, float ay)
491{
492 float psi = stateGetNedToBodyEulers_f()->psi;
493 float newax = cosf(-psi) * ax + sinf(-psi) * ay;
494 float neway = -sinf(-psi) * ax + cosf(-psi) * ay;
496}
497
498void guidance_h_set_acc(float ax, float ay)
499{
500 if (guidance_h.sp.h_mask != GUIDANCE_H_SP_ACCEL) {
502 }
503 guidance_h.sp.h_mask = GUIDANCE_H_SP_ACCEL;
506}
507
509{
510 guidance_h.sp.yaw_mask = GUIDANCE_H_SP_YAW_RATE;
512}
513
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition abi_common.h:67
bool autopilot_in_flight(void)
get in_flight flag
Definition autopilot.c:340
#define UNUSED(x)
float phi
in radians
float psi
in radians
#define FLOAT_ANGLE_NORMALIZE(_a)
#define FLOAT_VECT2_ZERO(_v)
euler angles
#define VECT2_COPY(_a, _b)
int32_t psi
in rad with INT32_ANGLE_FRAC
#define INT32_POS_FRAC
#define ACCEL_BFP_OF_REAL(_af)
#define INT32_TRIG_FRAC
#define SPEED_FLOAT_OF_BFP(_ai)
#define INT_VECT2_ZERO(_v)
#define INT32_VECT2_RSHIFT(_o, _i, _r)
#define POS_BFP_OF_REAL(_af)
#define ACCEL_FLOAT_OF_BFP(_ai)
#define SPEED_BFP_OF_REAL(_af)
int32_t y
East.
int32_t x
North.
vector in North East Down coordinates
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition state.h:1288
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1306
static struct NedCoor_i * stateGetPositionNed_i(void)
Get position in local NED coordinates (int).
Definition state.h:794
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition state.h:839
static struct NedCoor_i * stateGetSpeedNed_i(void)
Get ground speed in local NED coordinates (int).
Definition state.h:1004
void gh_set_ref(struct Int32Vect2 pos, struct FloatVect2 speed, struct FloatVect2 accel)
void gh_update_ref_from_accel_sp(struct FloatVect2 accel_sp)
void gh_ref_init(void)
void gh_update_ref_from_pos_sp(struct Int32Vect2 pos_sp)
void gh_update_ref_from_speed_sp(struct FloatVect2 speed_sp)
struct GuidanceHRef gh_ref
#define GH_POS_REF_FRAC
fixedpoint representation: Q26.37 will give a range of 67e3 km and a resolution of 1....
struct FloatVect2 accel
Reference model acceleration.
#define GUIDANCE_H_REF_MAX_SPEED
Default speed saturation.
struct Int64Vect2 pos
Reference model position.
struct FloatVect2 speed
Reference model speed.
struct FloatVect3 speed_sp
Guidance in a module file.
uint16_t foo
Definition main_demo5.c:58
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
#define MAX_PPRZ
Definition paparazzi.h:8
float y
in meters
float x
in meters
#define PPRZ_ITRIG_SIN(_s, _a)
#define PPRZ_ITRIG_COS(_c, _a)
struct RadioControl radio_control
Generic interface for radio control modules.
#define RC_OK
Some helper functions to check RC sticks.
#define THROTTLE_STICK_DOWN_FROM_RC(_rc)
struct StabilizationSetpoint guidance_h_guided_run(bool in_flight)
Run GUIDED mode control.
Definition guidance_h.c:441
void guidance_h_set_acc(float ax, float ay)
Set horizontal velocity setpoint.
Definition guidance_h.c:498
struct StabilizationSetpoint guidance_h_from_nav(bool in_flight)
Set horizontal guidance from NAV and run control loop.
Definition guidance_h.c:375
struct StabilizationSetpoint guidance_h_cmd
horizontal guidance command.
Definition guidance_h.c:50
void guidance_h_set_pos(float x, float y)
Set horizontal position setpoint.
Definition guidance_h.c:455
void guidance_h_mode_changed(uint8_t new_mode)
Definition guidance_h.c:128
static void send_href(struct transport_tx *trans, struct link_device *dev)
Definition guidance_h.c:73
static void send_gh(struct transport_tx *trans, struct link_device *dev)
Definition guidance_h.c:64
void guidance_h_hover_enter(void)
Definition guidance_h.c:343
static void rc_cb(uint8_t sender_id UNUSED, struct RadioControl *rc)
Definition guidance_h.c:219
void guidance_h_set_body_acc(float ax, float ay)
Set body relative horizontal acceleration setpoint.
Definition guidance_h.c:490
void guidance_h_set_heading(float heading)
Set heading setpoint.
Definition guidance_h.c:465
void guidance_h_init(void)
Definition guidance_h.c:86
struct HorizontalGuidance guidance_h
Definition guidance_h.c:45
static void read_rc_setpoint_speed_i(struct Int32Vect2 *speed_sp, bool in_flight, struct RadioControl *rc)
read speed setpoint from RC
Definition guidance_h.c:189
static void read_rc_setpoint_heading(struct HorizontalGuidanceRCInput *rc_sp, bool in_flight, struct RadioControl *rc)
Definition guidance_h.c:167
#define GUIDANCE_H_RC_ID
Definition guidance_h.c:55
void guidance_h_nav_enter(void)
Definition guidance_h.c:364
static void guidance_h_update_reference(void)
Definition guidance_h.c:273
struct StabilizationSetpoint guidance_h_run(bool in_flight)
Definition guidance_h.c:244
void guidance_h_set_vel(float vx, float vy)
Set horizontal acceleration setpoint.
Definition guidance_h.c:480
void guidance_h_set_body_vel(float vx, float vy)
Set body relative horizontal velocity setpoint.
Definition guidance_h.c:472
#define YAW_DEADBAND_EXCEEDED(_rc)
Definition guidance_h.c:163
static abi_event rc_ev
Definition guidance_h.c:58
static void reset_guidance_reference_from_current_position(void)
Definition guidance_h.c:114
#define GUIDANCE_H_SP_MAX_R
Definition guidance_h.c:155
void guidance_h_set_heading_rate(float rate)
Set heading rate setpoint.
Definition guidance_h.c:508
Horizontal guidance for rotorcrafts.
#define GUIDANCE_H_MODE_NAV
Definition guidance_h.h:58
struct Int32Vect2 vect
Definition guidance_h.h:98
#define GUIDANCE_H_USE_REF
Use horizontal guidance reference trajectory.
Definition guidance_h.h:45
struct Int32Vect2 accel
with INT32_ACCEL_FRAC
Definition guidance_h.h:94
void guidance_h_run_enter(void)
#define GUIDANCE_H_MODE_HOVER
Definition guidance_h.h:57
#define GUIDANCE_H_MODE_NONE
Definition guidance_h.h:56
struct StabilizationSetpoint guidance_h_run_pos(bool in_flight, struct HorizontalGuidance *gh)
struct Int32Vect2 speed
with INT32_SPEED_FRAC
Definition guidance_h.h:93
struct StabilizationSetpoint guidance_h_run_speed(bool in_flight, struct HorizontalGuidance *gh)
struct HorizontalGuidanceRCInput rc_sp
remote control setpoint
Definition guidance_h.h:110
#define GUIDANCE_H_MODE_GUIDED
Definition guidance_h.h:59
static void guidance_h_SetMaxSpeed(float speed)
Definition guidance_h.h:181
struct Int32Vect2 pos
with INT32_POS_FRAC
Definition guidance_h.h:92
struct HorizontalGuidanceReference ref
reference calculated from setpoints
Definition guidance_h.h:109
#define GUIDANCE_H_USE_SPEED_REF
Use horizontal guidance speed reference.
Definition guidance_h.h:53
struct StabilizationSetpoint guidance_h_run_accel(bool in_flight, struct HorizontalGuidance *gh)
struct HorizontalGuidanceSetpoint sp
setpoints
Definition guidance_h.h:108
Vertical guidance for rotorcrafts.
struct RotorcraftNavigation nav
Definition navigation.c:51
Rotorcraft navigation functions.
#define NAV_SETPOINT_MODE_QUAT
Definition navigation.h:104
struct EnuCoor_f speed
speed setpoint (in m/s)
Definition navigation.h:128
float fp_max_speed
maximum speed setpoint from flight plan (in m/s), negative value means unset or invalid,...
Definition navigation.h:140
#define NAV_SETPOINT_MODE_SPEED
Definition navigation.h:101
struct FloatQuat quat
quaternion setpoint
Definition navigation.h:134
struct EnuCoor_f accel
accel setpoint (in m/s)
Definition navigation.h:129
#define NAV_SETPOINT_MODE_ACCEL
Definition navigation.h:102
#define NAV_HORIZONTAL_MODE_GUIDED
Definition navigation.h:90
#define NAV_HORIZONTAL_MODE_ATTITUDE
Definition navigation.h:88
float pitch
pitch angle (in radians)
Definition navigation.h:132
#define NAV_HORIZONTAL_MODE_NONE
Definition navigation.h:89
float heading
heading setpoint (in radians)
Definition navigation.h:133
struct EnuCoor_f carrot
carrot position (also used for GCS display)
Definition navigation.h:127
#define NAV_SETPOINT_MODE_POS
Nav setpoint modes these modes correspond to submodes defined by navigation routines to tell which se...
Definition navigation.h:100
float roll
roll angle (in radians)
Definition navigation.h:131
General stabilization interface for rotorcrafts.
struct StabilizationSetpoint stab_sp_from_quat_f(struct FloatQuat *quat)
struct StabilizationSetpoint stab_sp_from_eulers_f(struct FloatEulers *eulers)
#define STAB_SP_SET_EULERS_ZERO(_sp)
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
enum HorizontalGuidanceSetpoint::@275 yaw_mask
struct Int32Vect2 pos
horizontal position setpoint in NED.
Definition guidance_h.h:73
enum HorizontalGuidanceSetpoint::@274 h_mask
struct Int32Vect2 accel
For direct control of acceleration, if the guidance scheme is able to provide it.
Definition guidance_h.h:75
struct Int32Vect2 speed
only used in HOVER mode if GUIDANCE_H_USE_SPEED_REF or in GUIDED mode
Definition guidance_h.h:74
Stabilization setpoint.
union StabilizationSetpoint::@278 sp
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:138
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.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
float heading
Definition wedgebug.c:258