2017-02-03 00:18:27 -04:00
|
|
|
// ArduSub transect flight mode
|
|
|
|
// Sub follows a line according to current crosstrack error supplied by XTE NMEA sentence
|
|
|
|
// Requires GPS providing crosstrack error
|
|
|
|
// Jacob Walser August 2016
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2017-02-03 00:18:27 -04:00
|
|
|
#include "version.h"
|
|
|
|
#include "GCS_Mavlink.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
#if TRANSECT_ENABLED == ENABLED
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
namespace {
|
2017-02-03 17:33:27 -04:00
|
|
|
static uint32_t last_transect_message_ms = 0;
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
float des_velx = 0; // inav earth-frame desired velocity +/- = north/south
|
|
|
|
float des_vely = 0; // inav earth-frame desired velocity +/- = east/west
|
|
|
|
float des_velf = 0; // pilot body-frame desired velocity +/- = forward/backward
|
|
|
|
float des_velr = 0; // pilot body-frame desired velocity +/- = right/left
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// Heading PID controller update interval
|
|
|
|
uint32_t last_pid_ms = 0;
|
|
|
|
uint8_t pid_dt = 1000/20;
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// Initialize transect controller
|
|
|
|
bool Sub::transect_init(bool ignore_checks)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2017-02-03 00:18:27 -04:00
|
|
|
// fail to initialise transect mode if no GPS lock
|
|
|
|
if (!position_ok() && !ignore_checks) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
pos_control.init_xy_controller();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// set speed and acceleration from wpnav's speed and acceleration
|
|
|
|
pos_control.set_speed_xy(wp_nav.get_speed_xy());
|
|
|
|
pos_control.set_accel_xy(wp_nav.get_wp_acceleration());
|
|
|
|
pos_control.set_jerk_xy_to_default();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
const Vector3f& curr_pos = inertial_nav.get_position();
|
|
|
|
const Vector3f& curr_vel = inertial_nav.get_velocity();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// set target position and velocity to current position and velocity
|
|
|
|
pos_control.set_xy_target(curr_pos.x, curr_pos.y);
|
|
|
|
pos_control.set_desired_velocity_xy(curr_vel.x, curr_vel.y);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// 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);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// 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());
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
last_pilot_heading = ahrs.yaw_sensor;
|
|
|
|
des_velf = 0;
|
|
|
|
des_velr = 0;
|
|
|
|
des_velx = 0;
|
|
|
|
des_vely = 0;
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// should be called at 100hz or more
|
|
|
|
void Sub::transect_run()
|
|
|
|
{
|
2017-02-03 17:33:27 -04:00
|
|
|
uint32_t tnow = millis();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// convert inertial nav earth-frame velocities to body-frame
|
|
|
|
const Vector3f& vel = inertial_nav.get_velocity();
|
|
|
|
float vel_fw = vel.x*ahrs.cos_yaw() + vel.y*ahrs.sin_yaw();
|
|
|
|
float vel_right = -vel.x*ahrs.sin_yaw() + vel.y*ahrs.cos_yaw();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
// if not auto armed or motor interlock not enabled set throttle to zero and exit immediately
|
|
|
|
if (!motors.armed() || !ap.auto_armed || !motors.get_interlock()) {
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
//reset targets
|
2017-02-03 00:18:27 -04:00
|
|
|
des_velf = 0;
|
|
|
|
des_velr = 0;
|
|
|
|
des_velx = 0;
|
|
|
|
des_vely = 0;
|
|
|
|
last_pilot_heading = ahrs.yaw_sensor;
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-02-03 00:18:27 -04:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
|
|
|
pos_control.relax_alt_hold_controllers(motors.get_throttle_hover());
|
|
|
|
pos_control.set_pos_target(inertial_nav.get_position());
|
|
|
|
pos_control.set_desired_velocity(Vector3f(0,0,0));
|
|
|
|
return;
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// apply SIMPLE mode transform to pilot inputs
|
|
|
|
update_simple_mode();
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// set motors to full range
|
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// get pilot's desired yaw rate in centidegrees per second
|
|
|
|
//float target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
|
|
|
//int16_t xtrack_error = constrain_int16(-gps.crosstrack_error(), -4500, 4500);
|
|
|
|
int16_t xtrack_error = -channel_lateral->get_control_in() / 10;
|
|
|
|
double target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// get pilot desired climb rate
|
|
|
|
float target_climb_rate = get_pilot_desired_climb_rate(channel_throttle->get_control_in());
|
|
|
|
target_climb_rate = constrain_float(target_climb_rate, -g.pilot_velocity_z_max, g.pilot_velocity_z_max);
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
int16_t pilot_lateral = channel_lateral->get_control_in();
|
|
|
|
int16_t pilot_forward = channel_forward->get_control_in();
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
float lateral_out = 0;
|
|
|
|
float forward_out = 0;
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// Pilot adjusts desired velocities to maintain during transect
|
|
|
|
if (pilot_lateral > 1000 || pilot_lateral < -1000 || pilot_forward > 1000 || pilot_forward < -1000) {
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
des_velf += pilot_forward * 0.0001;
|
|
|
|
des_velr += pilot_lateral * 0.0001;
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// desired forward and right speeds in body-frame
|
|
|
|
des_velf = constrain_float(des_velf, -25.0, 25.0);
|
|
|
|
des_velr = constrain_float(des_velr, -25.0, 25.0);
|
|
|
|
}
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// rotate pilot desired velocities to earth-frame
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// forward velocity only (maintain zero lateral velocity)
|
|
|
|
des_vely = des_velf * ahrs.sin_yaw(); // +East / -West
|
|
|
|
des_velx = des_velf * ahrs.cos_yaw(); // +North / -South
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// lateral velocity only (maintain zero forward velocity)
|
|
|
|
// des_vely = des_velr * ahrs.cos_yaw(); // +East / -West
|
|
|
|
// des_velx = -des_velr * ahrs.sin_yaw(); // +North / -South
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
//combined forward/lateral velocities
|
|
|
|
// des_vely = des_velf * ahrs.sin_yaw() + des_velr * ahrs.cos_yaw(); // +East / -West
|
|
|
|
// des_velx = des_velf * ahrs.cos_yaw() - des_velr * ahrs.sin_yaw(); // +North / -South
|
2017-02-03 00:18:27 -04:00
|
|
|
|
|
|
|
// set target position and velocity to current position and velocity
|
|
|
|
pos_control.set_desired_velocity_xy(des_velx, des_vely);
|
|
|
|
|
|
|
|
// run position controller
|
|
|
|
pos_control.update_xy_controller(AC_PosControl::XY_MODE_POS_AND_VEL_FF, ekfNavVelGainScaler, false);
|
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// get pos_control forward and lateral outputs from wp_nav pitch and roll (from copter code)
|
|
|
|
float poscontrol_lateral = pos_control.get_roll(); //
|
|
|
|
float poscontrol_forward = -pos_control.get_pitch(); // output is reversed
|
|
|
|
|
|
|
|
// constrain target forward/lateral values
|
|
|
|
poscontrol_lateral = constrain_int16(poscontrol_lateral, -aparm.angle_max, aparm.angle_max);
|
|
|
|
poscontrol_forward = constrain_int16(poscontrol_forward, -aparm.angle_max, aparm.angle_max);
|
|
|
|
|
|
|
|
lateral_out = poscontrol_lateral/(float)aparm.angle_max;
|
|
|
|
forward_out = poscontrol_forward/(float)aparm.angle_max;
|
|
|
|
|
|
|
|
motors.set_lateral(lateral_out);
|
|
|
|
motors.set_forward(forward_out);
|
|
|
|
|
|
|
|
// convert pilot input to lean angles
|
|
|
|
// To-Do: convert get_pilot_desired_lean_angles to return angles as floats
|
|
|
|
float target_roll, target_pitch;
|
|
|
|
get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, aparm.angle_max);
|
|
|
|
|
|
|
|
int32_t error_heading = 0;
|
|
|
|
if (!is_zero(target_yaw_rate)) {
|
|
|
|
// Allow pilot to set approximate heading to maintain during transect
|
|
|
|
last_pilot_heading = ahrs.yaw_sensor;
|
|
|
|
last_pilot_yaw_input_ms = tnow;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Set target heading after we have slowed to a stop after pilot input
|
|
|
|
if (tnow < last_pilot_yaw_input_ms + 250) {
|
|
|
|
target_yaw_rate = 0;
|
|
|
|
last_pilot_heading = ahrs.yaw_sensor;
|
|
|
|
} else {
|
|
|
|
// We are holding the current heading
|
|
|
|
error_heading = last_pilot_heading - ahrs.yaw_sensor;
|
|
|
|
|
|
|
|
// Wrap error 0~360 degrees
|
|
|
|
if (error_heading > 18000) {
|
|
|
|
error_heading = error_heading - 36000;
|
|
|
|
} else if (error_heading < -18000) {
|
|
|
|
error_heading = error_heading + 36000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust heading to correct for crosstrack error
|
|
|
|
target_yaw_rate = g.pid_heading_control.get_pid() + g.pid_crosstrack_control.get_pid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update crosstrack and heading controllers
|
|
|
|
if (tnow > last_pid_ms + pid_dt) {
|
|
|
|
last_pid_ms = tnow;
|
|
|
|
g.pid_heading_control.set_input_filter_all(error_heading);
|
|
|
|
//g.pid_crosstrack_control.set_input_filter_all(-channel_lateral->get_control_in());
|
|
|
|
g.pid_crosstrack_control.set_input_filter_all(xtrack_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update attitude controller targets
|
|
|
|
attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
|
|
|
|
|
|
|
|
// adjust climb rate using rangefinder
|
|
|
|
if (rangefinder_alt_ok()) {
|
|
|
|
// if rangefinder is ok, use surface tracking
|
|
|
|
target_climb_rate = get_surface_tracking_climb_rate(target_climb_rate, pos_control.get_alt_target(), G_Dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// call z axis position controller
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
if (inertial_nav.get_altitude() < g.surface_depth) { // pilot allowed to move up or down freely
|
|
|
|
pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
|
|
|
|
} else if (target_climb_rate < 0) { // pilot allowed to move only down freely
|
|
|
|
if (pos_control.get_vel_target_z() > 0) {
|
|
|
|
pos_control.relax_alt_hold_controllers(0); // reset target velocity and acceleration
|
|
|
|
}
|
|
|
|
pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
|
|
|
|
} else if (pos_control.get_alt_target() > g.surface_depth) { // hold depth at surface level.
|
|
|
|
pos_control.set_alt_target(g.surface_depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos_control.update_z_controller();
|
|
|
|
|
|
|
|
if (tnow > last_transect_message_ms + 200) {
|
|
|
|
last_transect_message_ms = tnow;
|
|
|
|
mavlink_msg_command_long_send(
|
|
|
|
(mavlink_channel_t)0, //channel
|
|
|
|
0, //target system
|
|
|
|
0, //target component
|
|
|
|
47, //command id
|
|
|
|
0, //confirmation
|
|
|
|
des_velf,//1
|
|
|
|
des_velr,
|
|
|
|
vel_fw,
|
|
|
|
vel_right,
|
|
|
|
forward_out,
|
|
|
|
lateral_out,
|
|
|
|
poscontrol_forward
|
|
|
|
);
|
|
|
|
//gcs_send_text_fmt(MAV_SEVERITY_INFO, "%ld, %ld, %ld, %f, %d", error_heading, ahrs.yaw_sensor, last_pilot_heading, target_yaw_rate, channel_lateral->get_control_in());
|
|
|
|
//gcs_send_text_fmt(MAV_SEVERITY_INFO, "%f, %f", g.pid_heading_control.get_pid(), g.pid_crosstrack_control.get_pid());
|
|
|
|
//gcs_send_text_fmt(MAV_SEVERITY_INFO, "%f, %ld, %ld, %f, %d", vel_fw, ahrs.yaw_sensor, last_pilot_heading, des_velf, gps.crosstrack_error());
|
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
2017-02-03 00:18:27 -04:00
|
|
|
#endif
|