2015-12-30 18:57:56 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* control_althold.pde - init and run calls for althold, flight mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
// althold_init - initialise althold controller
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::althold_init(bool ignore_checks)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2016-08-24 18:50:20 -03:00
|
|
|
if(!ap.depth_sensor_present) { // can't hold depth without a depth sensor, exit immediately.
|
|
|
|
gcs_send_text(MAV_SEVERITY_WARNING, "Depth hold requires external pressure sensor.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// initialize vertical speeds and leash lengths
|
2016-01-20 23:29:51 -04:00
|
|
|
// sets the maximum speed up and down returned by position controller
|
2015-12-30 18:57:56 -04:00
|
|
|
pos_control.set_speed_z(-g.pilot_velocity_z_max, g.pilot_velocity_z_max);
|
|
|
|
pos_control.set_accel_z(g.pilot_accel_z);
|
|
|
|
|
|
|
|
// initialise position and desired velocity
|
|
|
|
pos_control.set_alt_target(inertial_nav.get_altitude());
|
|
|
|
pos_control.set_desired_velocity_z(inertial_nav.get_velocity_z());
|
|
|
|
|
|
|
|
// stop takeoff if running
|
|
|
|
takeoff_stop();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// althold_run - runs the althold controller
|
|
|
|
// should be called at 100hz or more
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::althold_run()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
AltHoldModeState althold_state;
|
|
|
|
float takeoff_climb_rate = 0.0f;
|
|
|
|
|
|
|
|
// initialize vertical speeds and acceleration
|
|
|
|
pos_control.set_speed_z(-g.pilot_velocity_z_max, g.pilot_velocity_z_max);
|
|
|
|
pos_control.set_accel_z(g.pilot_accel_z);
|
|
|
|
|
|
|
|
// apply SIMPLE mode transform to pilot inputs
|
|
|
|
update_simple_mode();
|
|
|
|
|
|
|
|
// get pilot desired lean angles
|
|
|
|
float target_roll, target_pitch;
|
|
|
|
get_pilot_desired_lean_angles(channel_roll->control_in, channel_pitch->control_in, target_roll, target_pitch, attitude_control.get_althold_lean_angle_max());
|
|
|
|
|
|
|
|
// get pilot's desired yaw rate
|
|
|
|
float target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->control_in);
|
|
|
|
|
|
|
|
// get pilot desired climb rate
|
|
|
|
float target_climb_rate = get_pilot_desired_climb_rate(channel_throttle->control_in);
|
|
|
|
target_climb_rate = constrain_float(target_climb_rate, -g.pilot_velocity_z_max, g.pilot_velocity_z_max);
|
|
|
|
|
2016-04-05 00:17:39 -03:00
|
|
|
//bool takeoff_triggered = (ap.land_complete && (channel_throttle->control_in > get_takeoff_trigger_throttle()) && motors.spool_up_complete());
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2016-01-20 23:29:51 -04:00
|
|
|
// // Alt Hold State Machine Determination
|
2016-03-06 18:37:19 -04:00
|
|
|
if(!ap.auto_armed) {
|
|
|
|
althold_state = AltHold_Disarmed;
|
2016-04-05 00:17:39 -03:00
|
|
|
// if (!motors.armed() || !motors.get_interlock()) {
|
|
|
|
// althold_state = AltHold_MotorStop;
|
|
|
|
// } else if (!ap.auto_armed){
|
|
|
|
// althold_state = AltHold_Disarmed;
|
2016-01-20 23:29:51 -04:00
|
|
|
// } else if (takeoff_state.running || takeoff_triggered){
|
|
|
|
// althold_state = AltHold_Takeoff;
|
|
|
|
// } else if (ap.land_complete){
|
|
|
|
// althold_state = AltHold_Landed;
|
2016-03-06 18:37:19 -04:00
|
|
|
} else {
|
2015-12-30 18:57:56 -04:00
|
|
|
althold_state = AltHold_Flying;
|
2016-03-06 18:37:19 -04:00
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// Alt Hold State Machine
|
|
|
|
switch (althold_state) {
|
|
|
|
|
|
|
|
case AltHold_Disarmed:
|
2016-04-05 00:17:39 -03:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
|
2016-02-23 02:15:15 -04:00
|
|
|
// Multicopter do not stabilize roll/pitch/yaw when disarmed
|
2015-12-30 18:57:56 -04:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
2016-02-23 02:15:15 -04:00
|
|
|
|
2016-03-06 18:37:19 -04:00
|
|
|
pos_control.relax_alt_hold_controllers(0);
|
2015-12-30 18:57:56 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AltHold_MotorStop:
|
2016-02-23 02:15:15 -04:00
|
|
|
// Multicopter do not stabilize roll/pitch/yaw when motor are stopped
|
2016-04-05 00:17:39 -03:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_SHUT_DOWN);
|
2015-12-30 18:57:56 -04:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
|
|
|
pos_control.relax_alt_hold_controllers(get_throttle_pre_takeoff(channel_throttle->control_in)-throttle_average);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AltHold_Takeoff:
|
|
|
|
|
|
|
|
// initiate take-off
|
|
|
|
if (!takeoff_state.running) {
|
|
|
|
takeoff_timer_start(constrain_float(g.pilot_takeoff_alt,0.0f,1000.0f));
|
|
|
|
// indicate we are taking off
|
|
|
|
set_land_complete(false);
|
|
|
|
// clear i terms
|
|
|
|
set_throttle_takeoff();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get take-off adjusted pilot and takeoff climb rates
|
|
|
|
takeoff_get_climb_rates(target_climb_rate, takeoff_climb_rate);
|
|
|
|
|
2016-04-05 00:17:39 -03:00
|
|
|
// set motors to full range
|
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// call attitude controller
|
|
|
|
attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw_smooth(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
|
|
|
|
|
|
|
// call position controller
|
|
|
|
pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
|
|
|
|
pos_control.add_takeoff_climb_rate(takeoff_climb_rate, G_Dt);
|
|
|
|
pos_control.update_z_controller();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AltHold_Landed:
|
2016-02-23 02:15:15 -04:00
|
|
|
// Multicopter do not stabilize roll/pitch/yaw when disarmed
|
2016-04-05 00:17:39 -03:00
|
|
|
attitude_control.set_throttle_out(get_throttle_pre_takeoff(channel_throttle->control_in),false,g.throttle_filt);
|
|
|
|
// if throttle zero reset attitude and exit immediately
|
|
|
|
if (ap.throttle_zero) {
|
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
|
|
|
|
} else {
|
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
|
|
|
}
|
2016-02-23 02:15:15 -04:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
pos_control.relax_alt_hold_controllers(get_throttle_pre_takeoff(channel_throttle->control_in)-throttle_average);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AltHold_Flying:
|
2016-04-05 00:17:39 -03:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
2015-12-30 18:57:56 -04:00
|
|
|
// call attitude controller
|
|
|
|
attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw_smooth(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
|
|
|
|
2016-05-22 21:50:44 -03:00
|
|
|
// adjust climb rate using rangefinder
|
|
|
|
if (rangefinder_alt_ok()) {
|
|
|
|
// if rangefinder is ok, use surface tracking
|
2015-12-30 18:57:56 -04:00
|
|
|
target_climb_rate = get_surface_tracking_climb_rate(target_climb_rate, pos_control.get_alt_target(), G_Dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// call position controller
|
2016-02-24 18:18:48 -04:00
|
|
|
if(ap.at_bottom) {
|
|
|
|
pos_control.relax_alt_hold_controllers(0.0); // clear velocity and position targets, and integrator
|
|
|
|
pos_control.set_alt_target(inertial_nav.get_altitude() + 10.0f); // set target to 10 cm above bottom
|
2016-02-28 20:13:06 -04:00
|
|
|
} else if(ap.at_surface) {
|
2016-03-06 18:37:19 -04:00
|
|
|
if(target_climb_rate < 0.0) { // Dive if the pilot wants to
|
2016-02-28 20:13:06 -04:00
|
|
|
pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
|
2016-03-06 18:37:19 -04:00
|
|
|
} else if(pos_control.get_vel_target_z() > 0.0) {
|
|
|
|
pos_control.relax_alt_hold_controllers(0.0); // clear velocity and position targets, and integrator
|
2016-03-14 20:51:53 -03:00
|
|
|
pos_control.set_alt_target(g.surface_depth); // set alt target to the same depth that triggers the surface detector.
|
2016-02-28 20:13:06 -04:00
|
|
|
}
|
2016-02-24 18:18:48 -04:00
|
|
|
} else {
|
|
|
|
pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
|
|
|
|
}
|
2016-02-28 20:13:06 -04:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
pos_control.update_z_controller();
|
|
|
|
break;
|
|
|
|
}
|
2016-01-08 19:25:59 -04:00
|
|
|
|
|
|
|
//control_in is range 0-1000
|
|
|
|
//radio_in is raw pwm value
|
2016-10-13 16:12:39 -03:00
|
|
|
motors.set_forward(channel_forward->norm_input_dz());
|
|
|
|
motors.set_lateral(channel_lateral->norm_input_dz());
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|