2018-04-28 03:31:31 -03:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Rover.h"
|
|
|
|
|
|
|
|
bool ModeLoiter::_enter()
|
|
|
|
{
|
|
|
|
// set _destination to reasonable stopping point
|
|
|
|
calc_stopping_location(_destination);
|
|
|
|
|
|
|
|
// initialise desired speed to current speed
|
|
|
|
if (!attitude_control.get_forward_speed(_desired_speed)) {
|
|
|
|
_desired_speed = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise heading to current heading
|
|
|
|
_desired_yaw_cd = ahrs.yaw_sensor;
|
|
|
|
_yaw_error_cd = 0.0f;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeLoiter::update()
|
|
|
|
{
|
|
|
|
// get distance (in meters) to destination
|
2019-02-24 20:10:39 -04:00
|
|
|
_distance_to_destination = rover.current_loc.get_distance(_destination);
|
2018-04-28 03:31:31 -03:00
|
|
|
|
2018-09-27 21:11:09 -03:00
|
|
|
// if within loiter radius slew desired speed towards zero and use existing desired heading
|
2018-09-25 10:09:21 -03:00
|
|
|
if (_distance_to_destination <= g2.loit_radius) {
|
2018-09-25 10:09:47 -03:00
|
|
|
// sailboats do not stop
|
|
|
|
const float desired_speed_within_radius = g2.motors.has_sail() ? 0.1f : 0.0f;
|
|
|
|
_desired_speed = attitude_control.get_desired_speed_accel_limited(desired_speed_within_radius, rover.G_Dt);
|
2018-04-28 03:31:31 -03:00
|
|
|
_yaw_error_cd = 0.0f;
|
|
|
|
} else {
|
|
|
|
// P controller with hard-coded gain to convert distance to desired speed
|
|
|
|
// To-Do: make gain configurable or calculate from attitude controller's maximum accelearation
|
2018-09-25 10:09:21 -03:00
|
|
|
_desired_speed = MIN((_distance_to_destination - g2.loit_radius) * 0.5f, g.speed_cruise);
|
2018-04-28 03:31:31 -03:00
|
|
|
|
|
|
|
// calculate bearing to destination
|
|
|
|
_desired_yaw_cd = get_bearing_cd(rover.current_loc, _destination);
|
|
|
|
_yaw_error_cd = wrap_180_cd(_desired_yaw_cd - ahrs.yaw_sensor);
|
|
|
|
// if destination is behind vehicle, reverse towards it
|
2018-08-07 01:22:51 -03:00
|
|
|
if (fabsf(_yaw_error_cd) > 9000 && g2.loit_type == 0) {
|
2018-04-28 03:31:31 -03:00
|
|
|
_desired_yaw_cd = wrap_180_cd(_desired_yaw_cd + 18000);
|
|
|
|
_yaw_error_cd = wrap_180_cd(_desired_yaw_cd - ahrs.yaw_sensor);
|
|
|
|
_desired_speed = -_desired_speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reduce desired speed if yaw_error is large
|
2018-06-05 00:33:13 -03:00
|
|
|
// 45deg of error reduces speed to 75%, 90deg of error reduces speed to 50%
|
|
|
|
float yaw_error_ratio = 1.0f - constrain_float(fabsf(_yaw_error_cd / 9000.0f), 0.0f, 1.0f) * 0.5f;
|
2018-04-28 03:31:31 -03:00
|
|
|
_desired_speed *= yaw_error_ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
// run steering and throttle controllers
|
2018-09-10 04:25:15 -03:00
|
|
|
calc_steering_to_heading(_desired_yaw_cd);
|
2018-04-28 03:31:31 -03:00
|
|
|
calc_throttle(_desired_speed, false, true);
|
|
|
|
}
|