2015-12-18 05:46:56 -04:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2018-03-14 17:16:16 -03:00
|
|
|
#if MODE_THROW_ENABLED == ENABLED
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// throw_init - initialise throw controller
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeThrow::init(bool ignore_checks)
|
2015-12-18 05:46:56 -04:00
|
|
|
{
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
// do not allow helis to use throw to start
|
|
|
|
return false;
|
|
|
|
#endif
|
2015-12-19 01:59:44 -04:00
|
|
|
|
2016-01-21 22:59:47 -04:00
|
|
|
// do not enter the mode when already armed or when flying
|
2017-01-09 03:31:26 -04:00
|
|
|
if (motors->armed()) {
|
2015-12-19 01:59:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-01 09:42:09 -03:00
|
|
|
// init state
|
2016-08-02 23:30:27 -03:00
|
|
|
stage = Throw_Disarmed;
|
|
|
|
nextmode_attempted = false;
|
2016-08-01 09:42:09 -03:00
|
|
|
|
2016-03-22 04:21:58 -03:00
|
|
|
return true;
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// runs the throw to start controller
|
|
|
|
// should be called at 100hz or more
|
2019-05-09 23:18:49 -03:00
|
|
|
void ModeThrow::run()
|
2015-12-18 05:46:56 -04:00
|
|
|
{
|
|
|
|
/* Throw State Machine
|
|
|
|
Throw_Disarmed - motors are off
|
|
|
|
Throw_Detecting - motors are on and we are waiting for the throw
|
|
|
|
Throw_Uprighting - the throw has been detected and the copter is being uprighted
|
|
|
|
Throw_HgtStabilise - the copter is kept level and height is stabilised about the target height
|
|
|
|
Throw_PosHold - the copter is kept at a constant position and height
|
|
|
|
*/
|
|
|
|
|
2019-02-28 05:03:23 -04:00
|
|
|
if (!motors->armed()) {
|
2015-12-18 05:46:56 -04:00
|
|
|
// state machine entry is always from a disarmed state
|
2016-08-02 23:30:27 -03:00
|
|
|
stage = Throw_Disarmed;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
2016-08-02 23:30:27 -03:00
|
|
|
} else if (stage == Throw_Disarmed && motors->armed()) {
|
2017-07-08 21:56:49 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"waiting for throw");
|
2016-08-02 23:30:27 -03:00
|
|
|
stage = Throw_Detecting;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
2016-08-02 23:30:27 -03:00
|
|
|
} else if (stage == Throw_Detecting && throw_detected()){
|
2017-07-08 21:56:49 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"throw detected - uprighting");
|
2016-08-02 23:30:27 -03:00
|
|
|
stage = Throw_Uprighting;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// Cancel the waiting for throw tone sequence
|
|
|
|
AP_Notify::flags.waiting_for_throw = false;
|
|
|
|
|
2016-08-02 23:30:27 -03:00
|
|
|
} else if (stage == Throw_Uprighting && throw_attitude_good()) {
|
2017-07-08 21:56:49 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"uprighted - controlling height");
|
2016-08-02 23:30:27 -03:00
|
|
|
stage = Throw_HgtStabilise;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// initialize vertical speed and acceleration limits
|
|
|
|
// use brake mode values for rapid response
|
2018-09-20 02:45:02 -03:00
|
|
|
pos_control->set_max_speed_z(BRAKE_MODE_SPEED_Z, BRAKE_MODE_SPEED_Z);
|
|
|
|
pos_control->set_max_accel_z(BRAKE_MODE_DECEL_RATE);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// initialise the demanded height to 3m above the throw height
|
|
|
|
// we want to rapidly clear surrounding obstacles
|
2020-12-05 03:10:00 -04:00
|
|
|
if (g2.throw_type == ThrowType::Drop) {
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_alt_target(inertial_nav.get_altitude() - 100);
|
2016-08-01 23:26:48 -03:00
|
|
|
} else {
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_alt_target(inertial_nav.get_altitude() + 300);
|
2016-08-01 23:26:48 -03:00
|
|
|
}
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// set the initial velocity of the height controller demand to the measured velocity if it is going up
|
|
|
|
// if it is going down, set it to zero to enforce a very hard stop
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_desired_velocity_z(fmaxf(inertial_nav.get_velocity_z(),0.0f));
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// Set the auto_arm status to true to avoid a possible automatic disarm caused by selection of an auto mode with throttle at minimum
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.set_auto_armed(true);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
2016-08-02 23:30:27 -03:00
|
|
|
} else if (stage == Throw_HgtStabilise && throw_height_good()) {
|
2017-07-08 21:56:49 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO,"height achieved - controlling position");
|
2016-08-02 23:30:27 -03:00
|
|
|
stage = Throw_PosHold;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
2018-09-30 22:39:29 -03:00
|
|
|
// initialise the loiter target to the current position and velocity
|
2018-09-18 05:06:24 -03:00
|
|
|
loiter_nav->clear_pilot_desired_acceleration();
|
2018-03-27 23:13:37 -03:00
|
|
|
loiter_nav->init_target();
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// Set the auto_arm status to true to avoid a possible automatic disarm caused by selection of an auto mode with throttle at minimum
|
2018-02-07 22:21:09 -04:00
|
|
|
copter.set_auto_armed(true);
|
2016-08-02 23:30:27 -03:00
|
|
|
} else if (stage == Throw_PosHold && throw_position_good()) {
|
|
|
|
if (!nextmode_attempted) {
|
2019-09-07 20:33:56 -03:00
|
|
|
switch ((Mode::Number)g2.throw_nextmode.get()) {
|
|
|
|
case Mode::Number::AUTO:
|
|
|
|
case Mode::Number::GUIDED:
|
|
|
|
case Mode::Number::RTL:
|
|
|
|
case Mode::Number::LAND:
|
|
|
|
case Mode::Number::BRAKE:
|
|
|
|
case Mode::Number::LOITER:
|
2019-10-17 00:49:22 -03:00
|
|
|
set_mode((Mode::Number)g2.throw_nextmode.get(), ModeReason::THROW_COMPLETE);
|
2016-08-01 09:42:09 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
}
|
2016-08-02 23:30:27 -03:00
|
|
|
nextmode_attempted = true;
|
2016-08-01 09:42:09 -03:00
|
|
|
}
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Throw State Processing
|
2016-08-02 23:30:27 -03:00
|
|
|
switch (stage) {
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
case Throw_Disarmed:
|
|
|
|
|
2016-08-02 04:07:48 -03:00
|
|
|
// prevent motors from rotating before the throw is detected unless enabled by the user
|
2020-12-05 03:18:28 -04:00
|
|
|
if (g.throw_motor_start == PreThrowMotorState::RUNNING) {
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
|
2016-08-02 04:07:48 -03:00
|
|
|
} else {
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::SHUT_DOWN);
|
2016-08-02 04:07:48 -03:00
|
|
|
}
|
|
|
|
|
2015-12-18 05:46:56 -04:00
|
|
|
// demand zero throttle (motors will be stopped anyway) and continually reset the attitude controller
|
2019-04-05 07:38:17 -03:00
|
|
|
attitude_control->set_yaw_target_to_current_heading();
|
|
|
|
attitude_control->reset_rate_controller_I_terms();
|
2019-02-28 05:03:23 -04:00
|
|
|
attitude_control->set_throttle_out(0,true,g.throttle_filt);
|
2015-12-18 05:46:56 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Throw_Detecting:
|
|
|
|
|
2016-08-02 04:07:48 -03:00
|
|
|
// prevent motors from rotating before the throw is detected unless enabled by the user
|
2020-12-05 03:18:28 -04:00
|
|
|
if (g.throw_motor_start == PreThrowMotorState::RUNNING) {
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
|
2016-08-02 04:07:48 -03:00
|
|
|
} else {
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::SHUT_DOWN);
|
2016-08-02 04:07:48 -03:00
|
|
|
}
|
|
|
|
|
2015-12-18 05:46:56 -04:00
|
|
|
// Hold throttle at zero during the throw and continually reset the attitude controller
|
2019-04-05 07:38:17 -03:00
|
|
|
attitude_control->set_yaw_target_to_current_heading();
|
|
|
|
attitude_control->reset_rate_controller_I_terms();
|
2019-02-28 05:03:23 -04:00
|
|
|
attitude_control->set_throttle_out(0,true,g.throttle_filt);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// Play the waiting for throw tone sequence to alert the user
|
|
|
|
AP_Notify::flags.waiting_for_throw = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Throw_Uprighting:
|
|
|
|
|
2016-08-02 04:07:48 -03:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-08-02 04:07:48 -03:00
|
|
|
|
2015-12-18 05:46:56 -04:00
|
|
|
// demand a level roll/pitch attitude with zero yaw rate
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(0.0f, 0.0f, 0.0f);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// output 50% throttle and turn off angle boost to maximise righting moment
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->set_throttle_out(0.5f, false, g.throttle_filt);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Throw_HgtStabilise:
|
|
|
|
|
2016-08-02 04:07:48 -03:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-08-02 04:07:48 -03:00
|
|
|
|
2015-12-18 05:46:56 -04:00
|
|
|
// call attitude controller
|
2017-06-26 05:48:04 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(0.0f, 0.0f, 0.0f);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// call height controller
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_alt_target_from_climb_rate_ff(0.0f, G_Dt, false);
|
|
|
|
pos_control->update_z_controller();
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Throw_PosHold:
|
|
|
|
|
2016-08-02 04:07:48 -03:00
|
|
|
// set motors to full range
|
2019-04-09 09:16:58 -03:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2016-08-02 04:07:48 -03:00
|
|
|
|
2015-12-18 05:46:56 -04:00
|
|
|
// run loiter controller
|
2018-09-05 03:47:50 -03:00
|
|
|
loiter_nav->update();
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// call attitude controller
|
2018-03-27 23:13:37 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(loiter_nav->get_roll(), loiter_nav->get_pitch(), 0.0f);
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// call height controller
|
2017-01-09 03:31:26 -04:00
|
|
|
pos_control->set_alt_target_from_climb_rate_ff(0.0f, G_Dt, false);
|
|
|
|
pos_control->update_z_controller();
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-08-02 02:41:39 -03:00
|
|
|
|
|
|
|
// log at 10hz or if stage changes
|
|
|
|
uint32_t now = AP_HAL::millis();
|
2016-08-02 23:30:27 -03:00
|
|
|
if ((stage != prev_stage) || (now - last_log_ms) > 100) {
|
|
|
|
prev_stage = stage;
|
|
|
|
last_log_ms = now;
|
2018-04-17 12:38:47 -03:00
|
|
|
const float velocity = inertial_nav.get_velocity().length();
|
|
|
|
const float velocity_z = inertial_nav.get_velocity().z;
|
|
|
|
const float accel = copter.ins.get_accel().length();
|
|
|
|
const float ef_accel_z = ahrs.get_accel_ef().z;
|
|
|
|
const bool throw_detect = (stage > Throw_Detecting) || throw_detected();
|
|
|
|
const bool attitude_ok = (stage > Throw_Uprighting) || throw_attitude_good();
|
|
|
|
const bool height_ok = (stage > Throw_HgtStabilise) || throw_height_good();
|
|
|
|
const bool pos_ok = (stage > Throw_PosHold) || throw_position_good();
|
2020-04-04 06:01:45 -03:00
|
|
|
|
|
|
|
// @LoggerMessage: THRO
|
|
|
|
// @Description: Throw Mode messages
|
|
|
|
// @URL: https://ardupilot.org/copter/docs/throw-mode.html
|
|
|
|
// @Field: TimeUS: Time since system startup
|
|
|
|
// @Field: Stage: Current stage of the Throw Mode
|
|
|
|
// @Field: Vel: Magnitude of the velocity vector
|
|
|
|
// @Field: VelZ: Vertical Velocity
|
|
|
|
// @Field: Acc: Magnitude of the vector of the current acceleration
|
|
|
|
// @Field: AccEfZ: Vertical earth frame accelerometer value
|
|
|
|
// @Field: Throw: True if a throw has been detected since entering this mode
|
|
|
|
// @Field: AttOk: True if the vehicle is upright
|
|
|
|
// @Field: HgtOk: True if the vehicle is within 50cm of the demanded height
|
|
|
|
// @Field: PosOk: True if the vehicle is within 50cm of the demanded horizontal position
|
|
|
|
|
2019-01-18 00:24:08 -04:00
|
|
|
AP::logger().Write(
|
2017-12-13 05:57:38 -04:00
|
|
|
"THRO",
|
|
|
|
"TimeUS,Stage,Vel,VelZ,Acc,AccEfZ,Throw,AttOk,HgtOk,PosOk",
|
|
|
|
"s-nnoo----",
|
|
|
|
"F-0000----",
|
|
|
|
"QBffffbbbb",
|
|
|
|
AP_HAL::micros64(),
|
|
|
|
(uint8_t)stage,
|
2018-04-17 12:38:47 -03:00
|
|
|
(double)velocity,
|
|
|
|
(double)velocity_z,
|
|
|
|
(double)accel,
|
|
|
|
(double)ef_accel_z,
|
2017-12-13 05:57:38 -04:00
|
|
|
throw_detect,
|
|
|
|
attitude_ok,
|
|
|
|
height_ok,
|
|
|
|
pos_ok);
|
2016-08-02 02:41:39 -03:00
|
|
|
}
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeThrow::throw_detected()
|
2015-12-18 05:46:56 -04:00
|
|
|
{
|
|
|
|
// Check that we have a valid navigation solution
|
|
|
|
nav_filter_status filt_status = inertial_nav.get_filter_status();
|
|
|
|
if (!filt_status.flags.attitude || !filt_status.flags.horiz_pos_abs || !filt_status.flags.vert_pos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-02 02:30:32 -03:00
|
|
|
// Check for high speed (>500 cm/s)
|
|
|
|
bool high_speed = inertial_nav.get_velocity().length() > THROW_HIGH_SPEED;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
2016-08-02 02:30:32 -03:00
|
|
|
// check for upwards or downwards trajectory (airdrop) of 50cm/s
|
2016-08-01 23:26:48 -03:00
|
|
|
bool changing_height;
|
2020-12-05 03:10:00 -04:00
|
|
|
if (g2.throw_type == ThrowType::Drop) {
|
2016-08-02 02:30:32 -03:00
|
|
|
changing_height = inertial_nav.get_velocity().z < -THROW_VERTICAL_SPEED;
|
2016-08-01 23:26:48 -03:00
|
|
|
} else {
|
2016-08-02 02:30:32 -03:00
|
|
|
changing_height = inertial_nav.get_velocity().z > THROW_VERTICAL_SPEED;
|
2016-08-01 23:26:48 -03:00
|
|
|
}
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// Check the vertical acceleraton is greater than 0.25g
|
|
|
|
bool free_falling = ahrs.get_accel_ef().z > -0.25 * GRAVITY_MSS;
|
|
|
|
|
|
|
|
// Check if the accel length is < 1.0g indicating that any throw action is complete and the copter has been released
|
2018-02-07 22:21:09 -04:00
|
|
|
bool no_throw_action = copter.ins.get_accel().length() < 1.0f * GRAVITY_MSS;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
2016-08-02 02:32:27 -03:00
|
|
|
// High velocity or free-fall combined with increasing height indicate a possible air-drop or throw release
|
2016-08-01 23:26:48 -03:00
|
|
|
bool possible_throw_detected = (free_falling || high_speed) && changing_height && no_throw_action;
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// Record time and vertical velocity when we detect the possible throw
|
2016-08-02 23:30:27 -03:00
|
|
|
if (possible_throw_detected && ((AP_HAL::millis() - free_fall_start_ms) > 500)) {
|
|
|
|
free_fall_start_ms = AP_HAL::millis();
|
|
|
|
free_fall_start_velz = inertial_nav.get_velocity().z;
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once a possible throw condition has been detected, we check for 2.5 m/s of downwards velocity change in less than 0.5 seconds to confirm
|
2016-08-02 23:30:27 -03:00
|
|
|
bool throw_condition_confirmed = ((AP_HAL::millis() - free_fall_start_ms < 500) && ((inertial_nav.get_velocity().z - free_fall_start_velz) < -250.0f));
|
2015-12-18 05:46:56 -04:00
|
|
|
|
|
|
|
// start motors and enter the control mode if we are in continuous freefall
|
2021-02-03 10:04:38 -04:00
|
|
|
return throw_condition_confirmed;
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeThrow::throw_attitude_good()
|
2015-12-18 05:46:56 -04:00
|
|
|
{
|
|
|
|
// Check that we have uprighted the copter
|
|
|
|
const Matrix3f &rotMat = ahrs.get_rotation_body_to_ned();
|
2016-11-02 21:23:26 -03:00
|
|
|
return (rotMat.c.z > 0.866f); // is_upright
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeThrow::throw_height_good()
|
2015-12-18 05:46:56 -04:00
|
|
|
{
|
2017-12-21 19:40:15 -04:00
|
|
|
// Check that we are within 0.5m of the demanded height
|
2017-01-09 03:31:26 -04:00
|
|
|
return (pos_control->get_alt_error() < 50.0f);
|
2015-12-18 05:46:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:18:49 -03:00
|
|
|
bool ModeThrow::throw_position_good()
|
2016-08-01 09:42:09 -03:00
|
|
|
{
|
|
|
|
// check that our horizontal position error is within 50cm
|
2017-01-09 03:31:26 -04:00
|
|
|
return (pos_control->get_horizontal_error() < 50.0f);
|
2016-08-01 09:42:09 -03:00
|
|
|
}
|
2018-03-14 17:16:16 -03:00
|
|
|
#endif
|