2014-01-28 04:31:20 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2014-01-28 04:31:20 -04:00
|
|
|
/*
|
|
|
|
* control_drift.pde - init and run calls for drift flight mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DRIFT_SPEEDGAIN
|
2014-11-18 20:18:57 -04:00
|
|
|
# define DRIFT_SPEEDGAIN 8.0f
|
|
|
|
#endif
|
|
|
|
#ifndef DRIFT_SPEEDLIMIT
|
|
|
|
# define DRIFT_SPEEDLIMIT 560.0f
|
2014-03-30 11:00:23 -03:00
|
|
|
#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
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::drift_init(bool ignore_checks)
|
2014-01-28 04:31:20 -04:00
|
|
|
{
|
2015-01-02 07:43:50 -04:00
|
|
|
if (position_ok() || ignore_checks) {
|
2014-01-28 04:31:20 -04:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// drift_run - runs the drift controller
|
|
|
|
// should be called at 100hz or more
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::drift_run()
|
2014-01-28 04:31:20 -04:00
|
|
|
{
|
2015-04-24 02:19:45 -03:00
|
|
|
static float breaker = 0.0f;
|
|
|
|
static float roll_input = 0.0f;
|
2014-12-03 21:25:42 -04:00
|
|
|
float target_roll, target_pitch;
|
2014-01-28 04:31:20 -04:00
|
|
|
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
|
|
|
|
2015-04-17 14:49:08 -03:00
|
|
|
// if not armed or motor interlock not enabled or landed and throttle at zero, set throttle to zero and exit immediately
|
|
|
|
if(!motors.armed() || !motors.get_interlock() || (ap.land_complete && ap.throttle_zero)) {
|
2015-04-16 01:54:29 -03:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
2014-01-28 04:31:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert pilot input to lean angles
|
2015-09-06 09:23:30 -03:00
|
|
|
get_pilot_desired_lean_angles(channel_roll->control_in, channel_pitch->control_in, target_roll, target_pitch, aparm.angle_max);
|
2014-01-28 04:31:20 -04:00
|
|
|
|
2014-02-11 09:32:59 -04:00
|
|
|
// get pilot's desired throttle
|
2015-05-11 23:24:13 -03:00
|
|
|
pilot_throttle_scaled = get_pilot_desired_throttle(channel_throttle->control_in);
|
2014-02-11 09:32:59 -04:00
|
|
|
|
2014-01-28 04:31:20 -04:00
|
|
|
// Grab inertial velocity
|
2014-04-05 11:19:31 -03:00
|
|
|
const Vector3f& vel = inertial_nav.get_velocity();
|
2014-01-28 04:31:20 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-11-18 20:18:57 -04:00
|
|
|
// gain sceduling for Yaw
|
2015-05-08 15:40:08 -03:00
|
|
|
float pitch_vel2 = min(fabsf(pitch_vel), 2000);
|
2014-11-18 20:18:57 -04:00
|
|
|
target_yaw_rate = ((float)target_roll/1.0f) * (1.0f - (pitch_vel2 / 5000.0f)) * g.acro_yaw_p;
|
2014-01-28 04:31:20 -04:00
|
|
|
|
2014-11-18 20:18:57 -04:00
|
|
|
roll_vel = constrain_float(roll_vel, -DRIFT_SPEEDLIMIT, DRIFT_SPEEDLIMIT);
|
|
|
|
pitch_vel = constrain_float(pitch_vel, -DRIFT_SPEEDLIMIT, DRIFT_SPEEDLIMIT);
|
|
|
|
|
2015-05-11 23:24:13 -03:00
|
|
|
roll_input = roll_input * .96f + (float)channel_yaw->control_in * .04f;
|
2014-01-28 04:31:20 -04:00
|
|
|
|
2014-11-18 20:18:57 -04:00
|
|
|
//convert user input into desired roll velocity
|
|
|
|
float roll_vel_error = roll_vel - (roll_input / DRIFT_SPEEDGAIN);
|
2014-01-28 04:31:20 -04:00
|
|
|
|
2014-11-18 20:18:57 -04:00
|
|
|
// Roll velocity is feed into roll acceleration to minimize slip
|
|
|
|
target_roll = roll_vel_error * -DRIFT_SPEEDGAIN;
|
|
|
|
target_roll = constrain_int16(target_roll, -4500, 4500);
|
2014-01-28 04:31:20 -04:00
|
|
|
|
|
|
|
// If we let go of sticks, bring us to a stop
|
2015-05-04 23:34:21 -03:00
|
|
|
if(is_zero(target_pitch)){
|
2014-01-28 04:31:20 -04:00
|
|
|
// .14/ (.03 * 100) = 4.6 seconds till full breaking
|
2015-04-24 02:19:45 -03:00
|
|
|
breaker += .03f;
|
2014-01-28 04:31:20 -04:00
|
|
|
breaker = min(breaker, DRIFT_SPEEDGAIN);
|
|
|
|
target_pitch = pitch_vel * breaker;
|
|
|
|
}else{
|
2015-04-24 02:19:45 -03:00
|
|
|
breaker = 0.0f;
|
2014-01-28 04:31:20 -04:00
|
|
|
}
|
|
|
|
|
2014-08-29 01:41:08 -03:00
|
|
|
// call attitude controller
|
|
|
|
attitude_control.angle_ef_roll_pitch_rate_ef_yaw_smooth(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
|
|
|
|
|
|
|
// output pilot's throttle with angle boost
|
2015-04-16 01:54:29 -03:00
|
|
|
attitude_control.set_throttle_out(get_throttle_assist(vel.z, pilot_throttle_scaled), true, g.throttle_filt);
|
2014-08-29 01:41:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get_throttle_assist - return throttle output (range 0 ~ 1000) based on pilot input and z-axis velocity
|
2015-05-29 23:12:49 -03:00
|
|
|
int16_t Copter::get_throttle_assist(float velz, int16_t pilot_throttle_scaled)
|
2014-08-29 01:41:08 -03:00
|
|
|
{
|
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;
|
2015-03-16 01:35:57 -03:00
|
|
|
if (pilot_throttle_scaled > g.throttle_min && pilot_throttle_scaled < THR_MAX &&
|
2014-03-31 03:21:20 -03:00
|
|
|
pilot_throttle_scaled > DRIFT_THR_MIN && pilot_throttle_scaled < DRIFT_THR_MAX) {
|
|
|
|
// calculate throttle assist gain
|
2015-04-24 02:19:45 -03:00
|
|
|
thr_assist = 1.2f - ((float)abs(pilot_throttle_scaled - 500) / 240.0f);
|
2014-08-29 01:41:08 -03:00
|
|
|
thr_assist = constrain_float(thr_assist, 0.0f, 1.0f) * -DRIFT_THR_ASSIST_GAIN * velz;
|
2014-03-31 03:21:20 -03:00
|
|
|
|
|
|
|
// 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
|
2015-03-16 01:35:57 -03:00
|
|
|
thr_assist = constrain_float(thr_assist, g.throttle_min - pilot_throttle_scaled, THR_MAX - pilot_throttle_scaled);
|
2014-03-31 03:21:20 -03:00
|
|
|
}
|
2014-08-29 01:41:08 -03:00
|
|
|
|
|
|
|
return pilot_throttle_scaled + thr_assist;
|
2014-01-28 04:31:20 -04:00
|
|
|
}
|