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-08-08 21:24:30 -03:00
|
|
|
// convert pilot throttle input to desired speed (up to twice the cruise speed)
|
2017-08-15 23:32:55 -03:00
|
|
|
float target_speed = channel_throttle->get_control_in() * 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);
|
|
|
|
lateral_acceleration = 0.0f;
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 01:17:19 -03:00
|
|
|
|
2017-07-21 01:16:23 -03:00
|
|
|
// in steering mode we control lateral acceleration directly. We first calculate the maximum lateral
|
|
|
|
// acceleration at full steering lock for this speed. That is V^2/R where R is the radius of turn.
|
2017-08-08 21:24:30 -03:00
|
|
|
float 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
|
|
|
|
lateral_acceleration = max_g_force * (channel_steer->get_control_in() / 4500.0f);
|
|
|
|
|
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-07-21 01:17:19 -03:00
|
|
|
lateral_acceleration = -lateral_acceleration;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// run speed to throttle output controller
|
2017-08-10 00:06:43 -03:00
|
|
|
if (is_zero(target_speed)) {
|
|
|
|
stop_vehicle();
|
|
|
|
} else {
|
|
|
|
// run steering controller
|
|
|
|
calc_nav_steer(reversed);
|
|
|
|
calc_throttle(target_speed, false);
|
|
|
|
}
|
2017-07-18 23:17:45 -03:00
|
|
|
}
|