2014-01-28 04:31:20 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
/*
|
|
|
|
* control_drift.pde - init and run calls for drift flight mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DRIFT_SPEEDGAIN
|
2014-03-30 11:00:23 -03:00
|
|
|
# define DRIFT_SPEEDGAIN 14.0f
|
|
|
|
#endif
|
|
|
|
|
2014-03-31 03:21:20 -03:00
|
|
|
#ifndef DRIFT_THR_ASSIST_GAIN
|
|
|
|
# define DRIFT_THR_ASSIST_GAIN 1.8f // gain controlling amount of throttle assistance
|
2014-03-31 01:05:43 -03:00
|
|
|
#endif
|
|
|
|
|
2014-03-31 03:21:20 -03:00
|
|
|
#ifndef DRIFT_THR_ASSIST_MAX
|
|
|
|
# define DRIFT_THR_ASSIST_MAX 300.0f // maximum assistance throttle assist will provide
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DRIFT_THR_MIN
|
|
|
|
# define DRIFT_THR_MIN 213 // throttle assist will be active when pilot's throttle is above this value
|
|
|
|
#endif
|
|
|
|
#ifndef DRIFT_THR_MAX
|
|
|
|
# define DRIFT_THR_MAX 787 // throttle assist will be active when pilot's throttle is below this value
|
2014-01-28 04:31:20 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// drift_init - initialise drift controller
|
|
|
|
static bool drift_init(bool ignore_checks)
|
|
|
|
{
|
|
|
|
if (GPS_ok() || ignore_checks) {
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// drift_run - runs the drift controller
|
|
|
|
// should be called at 100hz or more
|
|
|
|
static void drift_run()
|
|
|
|
{
|
|
|
|
static float breaker = 0.0;
|
|
|
|
int16_t target_roll, target_pitch;
|
|
|
|
float target_yaw_rate;
|
2014-02-11 09:32:59 -04:00
|
|
|
int16_t pilot_throttle_scaled;
|
2014-01-28 04:31:20 -04:00
|
|
|
|
2014-03-30 11:00:23 -03:00
|
|
|
// if not armed or landed and throttle at zero, set throttle to zero and exit immediately
|
|
|
|
if(!motors.armed() || (ap.land_complete && g.rc_3.control_in <= 0)) {
|
2014-01-28 04:31:20 -04:00
|
|
|
attitude_control.init_targets();
|
|
|
|
attitude_control.set_throttle_out(0, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert pilot input to lean angles
|
|
|
|
get_pilot_desired_lean_angles(g.rc_1.control_in, g.rc_2.control_in, target_roll, target_pitch);
|
|
|
|
|
2014-02-11 09:32:59 -04:00
|
|
|
// get pilot's desired throttle
|
|
|
|
pilot_throttle_scaled = get_pilot_desired_throttle(g.rc_3.control_in);
|
|
|
|
|
2014-01-28 04:31:20 -04:00
|
|
|
// Grab inertial velocity
|
|
|
|
Vector3f vel = inertial_nav.get_velocity();
|
|
|
|
|
|
|
|
// rotate roll, pitch input from north facing to vehicle's perspective
|
2014-02-08 23:35:22 -04:00
|
|
|
float roll_vel = vel.y * ahrs.cos_yaw() - vel.x * ahrs.sin_yaw(); // body roll vel
|
|
|
|
float pitch_vel = vel.y * ahrs.sin_yaw() + vel.x * ahrs.cos_yaw(); // body pitch vel
|
2014-01-28 04:31:20 -04:00
|
|
|
|
|
|
|
float pitch_vel2 = min(fabs(pitch_vel), 800);
|
|
|
|
|
|
|
|
// simple gain scheduling for yaw input
|
|
|
|
target_yaw_rate = (float)(target_roll/2) * (1.0 - (pitch_vel2 / 2400.0));
|
|
|
|
|
|
|
|
roll_vel = constrain_float(roll_vel, -322, 322);
|
|
|
|
pitch_vel = constrain_float(pitch_vel, -322, 322);
|
|
|
|
|
|
|
|
// always limit roll
|
|
|
|
target_roll = roll_vel * -DRIFT_SPEEDGAIN;
|
|
|
|
|
|
|
|
// If we let go of sticks, bring us to a stop
|
|
|
|
if(target_pitch == 0){
|
|
|
|
// .14/ (.03 * 100) = 4.6 seconds till full breaking
|
|
|
|
breaker += .03;
|
|
|
|
breaker = min(breaker, DRIFT_SPEEDGAIN);
|
|
|
|
target_pitch = pitch_vel * breaker;
|
|
|
|
}else{
|
|
|
|
breaker = 0.0;
|
|
|
|
}
|
|
|
|
|
2014-03-31 01:05:43 -03:00
|
|
|
// throttle assist - adjusts throttle to slow the vehicle's vertical velocity
|
|
|
|
// Only active when pilot's throttle is between 213 ~ 787
|
|
|
|
// Assistance is strongest when throttle is at mid, drops linearly to no assistance at 213 and 787
|
2014-03-31 03:21:20 -03:00
|
|
|
float thr_assist = 0.0f;
|
|
|
|
if (pilot_throttle_scaled > g.throttle_min && pilot_throttle_scaled < g.throttle_max &&
|
|
|
|
pilot_throttle_scaled > DRIFT_THR_MIN && pilot_throttle_scaled < DRIFT_THR_MAX) {
|
|
|
|
// calculate throttle assist gain
|
|
|
|
thr_assist = 1.2 - ((float)abs(pilot_throttle_scaled - 500) / 240.0f);
|
|
|
|
thr_assist = constrain_float(thr_assist, 0.0f, 1.0f) * -DRIFT_THR_ASSIST_GAIN * vel.z;
|
|
|
|
|
|
|
|
// ensure throttle assist never adjusts the throttle by more than 300 pwm
|
|
|
|
thr_assist = constrain_float(thr_assist, -DRIFT_THR_ASSIST_MAX, DRIFT_THR_ASSIST_MAX);
|
|
|
|
|
|
|
|
// ensure throttle assist never pushes throttle below throttle_min or above throttle_max
|
|
|
|
thr_assist = constrain_float(thr_assist, g.throttle_min - pilot_throttle_scaled, g.throttle_max - pilot_throttle_scaled);
|
|
|
|
}
|
2014-03-30 11:00:23 -03:00
|
|
|
|
2014-01-28 04:31:20 -04:00
|
|
|
// call attitude controller
|
2014-02-19 07:52:00 -04:00
|
|
|
attitude_control.angle_ef_roll_pitch_rate_ef_yaw_smooth(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
2014-02-11 09:32:59 -04:00
|
|
|
|
|
|
|
// output pilot's throttle with angle boost
|
2014-03-30 11:00:23 -03:00
|
|
|
attitude_control.set_throttle_out(pilot_throttle_scaled + thr_assist, true);
|
2014-01-28 04:31:20 -04:00
|
|
|
}
|