2017-07-18 23:17:45 -03:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Rover.h"
|
|
|
|
|
2017-07-21 01:16:23 -03:00
|
|
|
void ModeSteering::update()
|
|
|
|
{
|
2017-11-27 09:11:45 -04:00
|
|
|
float desired_steering, desired_throttle;
|
|
|
|
get_pilot_desired_steering_and_throttle(desired_steering, desired_throttle);
|
|
|
|
|
2017-08-08 21:24:30 -03:00
|
|
|
// convert pilot throttle input to desired speed (up to twice the cruise speed)
|
2017-12-13 05:17:42 -04:00
|
|
|
float target_speed = desired_throttle * 0.01f * calc_speed_max(g.speed_cruise, g.throttle_cruise * 0.01f);
|
2017-08-08 21:24:30 -03:00
|
|
|
|
|
|
|
// get speed forward
|
|
|
|
float speed;
|
|
|
|
if (!attitude_control.get_forward_speed(speed)) {
|
2017-08-10 00:06:43 -03:00
|
|
|
// no valid speed so stop
|
2017-08-15 22:32:56 -03:00
|
|
|
g2.motors.set_throttle(0.0f);
|
2017-08-08 21:24:30 -03:00
|
|
|
g2.motors.set_steering(0.0f);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 01:17:19 -03:00
|
|
|
|
2017-11-13 17:13:03 -04:00
|
|
|
// determine if pilot is requesting pivot turn
|
2017-11-27 09:11:45 -04:00
|
|
|
bool is_pivot_turning = g2.motors.have_skid_steering() && is_zero(target_speed) && (!is_zero(desired_steering));
|
2017-11-13 17:13:03 -04:00
|
|
|
|
|
|
|
// In steering mode we control lateral acceleration directly.
|
|
|
|
// For pivot steering vehicles we use the TURN_MAX_G parameter
|
|
|
|
// For regular steering vehicles we use the maximum lateral acceleration at full steering lock for this speed: V^2/R where R is the radius of turn.
|
|
|
|
float max_g_force;
|
|
|
|
if (is_pivot_turning) {
|
|
|
|
max_g_force = g.turn_max_g * GRAVITY_MSS;
|
|
|
|
} else {
|
|
|
|
max_g_force = speed * speed / MAX(g2.turn_radius, 0.1f);
|
|
|
|
}
|
2017-07-18 23:17:45 -03:00
|
|
|
|
|
|
|
// constrain to user set TURN_MAX_G
|
|
|
|
max_g_force = constrain_float(max_g_force, 0.1f, g.turn_max_g * GRAVITY_MSS);
|
|
|
|
|
|
|
|
// convert pilot steering input to desired lateral acceleration
|
2017-12-23 02:17:49 -04:00
|
|
|
float desired_lat_accel = max_g_force * (desired_steering / 4500.0f);
|
2017-07-18 23:17:45 -03:00
|
|
|
|
2017-07-21 01:17:19 -03:00
|
|
|
// reverse target lateral acceleration if backing up
|
2017-08-03 03:19:57 -03:00
|
|
|
bool reversed = false;
|
2017-07-21 01:17:19 -03:00
|
|
|
if (is_negative(target_speed)) {
|
2017-08-03 03:19:57 -03:00
|
|
|
reversed = true;
|
2017-12-23 02:17:49 -04:00
|
|
|
desired_lat_accel = -desired_lat_accel;
|
2017-07-21 01:17:19 -03:00
|
|
|
}
|
2017-07-18 23:17:45 -03:00
|
|
|
|
2017-08-03 03:19:57 -03:00
|
|
|
// mark us as in_reverse when using a negative throttle
|
|
|
|
rover.set_reverse(reversed);
|
2017-07-18 23:17:45 -03:00
|
|
|
|
2018-02-18 21:29:49 -04:00
|
|
|
// run lateral acceleration to steering controller
|
|
|
|
calc_steering_from_lateral_acceleration(desired_lat_accel, reversed);
|
2017-12-13 05:17:42 -04:00
|
|
|
|
2018-02-18 21:29:49 -04:00
|
|
|
// run speed to throttle controller
|
2018-04-21 03:45:12 -03:00
|
|
|
calc_throttle(target_speed, false, true);
|
2017-07-18 23:17:45 -03:00
|
|
|
}
|