Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 1 | #include "aos/input/drivetrain_input.h" |
| 2 | |
| 3 | #include <math.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <cmath> |
| 7 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/commonmath.h" |
| 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/logging/logging.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | |
| 13 | using ::frc971::control_loops::drivetrain_queue; |
| 14 | using ::aos::input::driver_station::ButtonLocation; |
| 15 | using ::aos::input::driver_station::ControlBit; |
| 16 | using ::aos::input::driver_station::JoystickAxis; |
| 17 | using ::aos::input::driver_station::POVLocation; |
| 18 | |
| 19 | namespace aos { |
| 20 | namespace input { |
| 21 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 22 | const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1); |
| 23 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 24 | void DrivetrainInputReader::HandleDrivetrain( |
| 25 | const ::aos::input::driver_station::Data &data) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 26 | const auto wheel_and_throttle = GetWheelAndThrottle(data); |
| 27 | const double wheel = wheel_and_throttle.wheel; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 28 | const double wheel_velocity = wheel_and_throttle.wheel_velocity; |
| 29 | const double wheel_torque = wheel_and_throttle.wheel_torque; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 30 | const double throttle = wheel_and_throttle.throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 31 | const double throttle_velocity = wheel_and_throttle.throttle_velocity; |
| 32 | const double throttle_torque = wheel_and_throttle.throttle_torque; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 33 | const bool high_gear = wheel_and_throttle.high_gear; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 34 | |
| 35 | drivetrain_queue.status.FetchLatest(); |
| 36 | if (drivetrain_queue.status.get()) { |
| 37 | robot_velocity_ = drivetrain_queue.status->robot_speed; |
| 38 | } |
| 39 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 40 | bool is_control_loop_driving = false; |
| 41 | bool is_line_following = false; |
| 42 | |
| 43 | if (data.IsPressed(turn1_)) { |
| 44 | switch (turn1_use_) { |
| 45 | case TurnButtonUse::kControlLoopDriving: |
| 46 | is_control_loop_driving = true; |
| 47 | break; |
| 48 | case TurnButtonUse::kLineFollow: |
| 49 | is_line_following = true; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (data.IsPressed(turn2_)) { |
| 55 | switch (turn2_use_) { |
| 56 | case TurnButtonUse::kControlLoopDriving: |
| 57 | is_control_loop_driving = true; |
| 58 | break; |
| 59 | case TurnButtonUse::kLineFollow: |
| 60 | is_line_following = true; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (is_control_loop_driving) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 66 | if (drivetrain_queue.status.get()) { |
| 67 | left_goal_ = drivetrain_queue.status->estimated_left_position; |
| 68 | right_goal_ = drivetrain_queue.status->estimated_right_position; |
| 69 | } |
| 70 | } |
| 71 | const double current_left_goal = |
| 72 | left_goal_ - wheel * wheel_multiplier_ + throttle * 0.3; |
| 73 | const double current_right_goal = |
| 74 | right_goal_ + wheel * wheel_multiplier_ + throttle * 0.3; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 75 | auto new_drivetrain_goal = drivetrain_queue.goal.MakeMessage(); |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 76 | new_drivetrain_goal->wheel = wheel; |
| 77 | new_drivetrain_goal->wheel_velocity = wheel_velocity; |
| 78 | new_drivetrain_goal->wheel_torque = wheel_torque; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 79 | new_drivetrain_goal->throttle = throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 80 | new_drivetrain_goal->throttle_velocity = throttle_velocity; |
| 81 | new_drivetrain_goal->throttle_torque = throttle_torque; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 82 | new_drivetrain_goal->highgear = high_gear; |
| 83 | new_drivetrain_goal->quickturn = data.IsPressed(quick_turn_); |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 84 | new_drivetrain_goal->controller_type = |
| 85 | is_line_following ? 3 : (is_control_loop_driving ? 1 : 0); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 86 | new_drivetrain_goal->left_goal = current_left_goal; |
| 87 | new_drivetrain_goal->right_goal = current_right_goal; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 88 | |
| 89 | new_drivetrain_goal->linear.max_velocity = 3.0; |
| 90 | new_drivetrain_goal->linear.max_acceleration = 20.0; |
| 91 | |
| 92 | if (!new_drivetrain_goal.Send()) { |
| 93 | LOG(WARNING, "sending stick values failed\n"); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | DrivetrainInputReader::WheelAndThrottle |
| 98 | SteeringWheelDrivetrainInputReader::GetWheelAndThrottle( |
| 99 | const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 100 | const double wheel = -data.GetAxis(wheel_); |
| 101 | const double throttle = -data.GetAxis(throttle_); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 102 | |
| 103 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 104 | high_gear_ = default_high_gear_; |
| 105 | } |
| 106 | |
| 107 | if (data.PosEdge(kShiftLow)) { |
| 108 | high_gear_ = false; |
| 109 | } |
| 110 | |
| 111 | if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) { |
| 112 | high_gear_ = true; |
| 113 | } |
| 114 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 115 | return DrivetrainInputReader::WheelAndThrottle{ |
| 116 | wheel, 0.0, 0.0, throttle, 0.0, 0.0, high_gear_}; |
| 117 | } |
| 118 | |
| 119 | double UnwrappedAxis(const ::aos::input::driver_station::Data &data, |
| 120 | const JoystickAxis &high_bits, |
| 121 | const JoystickAxis &low_bits) { |
| 122 | const float high_bits_data = data.GetAxis(high_bits); |
| 123 | const float low_bits_data = data.GetAxis(low_bits); |
| 124 | const int16_t high_bits_data_int = |
| 125 | (high_bits_data < 0.0f ? high_bits_data * 128.0f |
| 126 | : high_bits_data * 127.0f); |
| 127 | const int16_t low_bits_data_int = |
| 128 | (low_bits_data < 0.0f ? low_bits_data * 128.0f : low_bits_data * 127.0f); |
| 129 | |
| 130 | const uint16_t high_bits_data_uint = |
| 131 | ((static_cast<uint16_t>(high_bits_data_int) & 0xff) + 0x80) & 0xff; |
| 132 | const uint16_t low_bits_data_uint = |
| 133 | ((static_cast<uint16_t>(low_bits_data_int) & 0xff) + 0x80) & 0xff; |
| 134 | |
| 135 | const uint16_t data_uint = (high_bits_data_uint << 8) | low_bits_data_uint; |
| 136 | |
| 137 | const int32_t data_int = static_cast<int32_t>(data_uint) - 0x8000; |
| 138 | |
| 139 | if (data_int < 0) { |
| 140 | return static_cast<double>(data_int) / static_cast<double>(0x8000); |
| 141 | } else { |
| 142 | return static_cast<double>(data_int) / static_cast<double>(0x7fff); |
| 143 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | DrivetrainInputReader::WheelAndThrottle |
| 147 | PistolDrivetrainInputReader::GetWheelAndThrottle( |
| 148 | const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 149 | const double wheel = |
| 150 | -UnwrappedAxis(data, wheel_, wheel_low_); |
| 151 | const double wheel_velocity = |
| 152 | -UnwrappedAxis(data, wheel_velocity_high_, wheel_velocity_low_) * 50.0; |
| 153 | const double wheel_torque = |
| 154 | -UnwrappedAxis(data, wheel_torque_high_, wheel_torque_low_) / 2.0; |
| 155 | |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 156 | double throttle = |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 157 | UnwrappedAxis(data, throttle_, throttle_low_); |
| 158 | const double throttle_velocity = |
| 159 | UnwrappedAxis(data, throttle_velocity_high_, throttle_velocity_low_) * 50.0; |
| 160 | const double throttle_torque = |
| 161 | UnwrappedAxis(data, throttle_torque_high_, throttle_torque_low_) / 2.0; |
| 162 | |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 163 | // TODO(austin): Deal with haptics here. |
| 164 | if (throttle < 0) { |
| 165 | throttle = ::std::max(-1.0, throttle / 0.7); |
| 166 | } |
| 167 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 168 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 169 | high_gear_ = default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 172 | if (data.PosEdge(shift_low_)) { |
| 173 | high_gear_ = false; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 174 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 175 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 176 | if (data.PosEdge(shift_high_)) { |
| 177 | high_gear_ = true; |
| 178 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 179 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 180 | return DrivetrainInputReader::WheelAndThrottle{ |
| 181 | wheel, wheel_velocity, wheel_torque, |
| 182 | throttle, throttle_velocity, throttle_torque, |
| 183 | high_gear_}; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | DrivetrainInputReader::WheelAndThrottle |
| 187 | XboxDrivetrainInputReader::GetWheelAndThrottle( |
| 188 | const ::aos::input::driver_station::Data &data) { |
| 189 | // xbox |
| 190 | constexpr double kWheelDeadband = 0.05; |
| 191 | constexpr double kThrottleDeadband = 0.05; |
| 192 | const double wheel = |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 193 | aos::Deadband(-data.GetAxis(wheel_), kWheelDeadband, 1.0); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 194 | |
| 195 | const double unmodified_throttle = |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 196 | aos::Deadband(-data.GetAxis(throttle_), kThrottleDeadband, 1.0); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 197 | |
| 198 | // Apply a sin function that's scaled to make it feel better. |
| 199 | constexpr double throttle_range = M_PI_2 * 0.9; |
| 200 | |
| 201 | double throttle = ::std::sin(throttle_range * unmodified_throttle) / |
| 202 | ::std::sin(throttle_range); |
| 203 | throttle = ::std::sin(throttle_range * throttle) / ::std::sin(throttle_range); |
| 204 | throttle = 2.0 * unmodified_throttle - throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 205 | return DrivetrainInputReader::WheelAndThrottle{wheel, 0.0, 0.0, throttle, |
| 206 | 0.0, 0.0, true}; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | std::unique_ptr<SteeringWheelDrivetrainInputReader> |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 210 | SteeringWheelDrivetrainInputReader::Make(bool default_high_gear) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 211 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 212 | const ButtonLocation kQuickTurn(1, 5); |
| 213 | const ButtonLocation kTurn1(1, 7); |
| 214 | const ButtonLocation kTurn2(1, 11); |
| 215 | std::unique_ptr<SteeringWheelDrivetrainInputReader> result( |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 216 | new SteeringWheelDrivetrainInputReader( |
| 217 | kSteeringWheel, kDriveThrottle, kQuickTurn, kTurn1, |
| 218 | TurnButtonUse::kControlLoopDriving, kTurn2, |
| 219 | TurnButtonUse::kControlLoopDriving)); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 220 | result.get()->set_default_high_gear(default_high_gear); |
| 221 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 222 | return result; |
| 223 | } |
| 224 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 225 | std::unique_ptr<PistolDrivetrainInputReader> PistolDrivetrainInputReader::Make( |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 226 | bool default_high_gear, TopButtonUse top_button_use) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 227 | // Pistol Grip controller |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 228 | const JoystickAxis kTriggerHigh(1, 1), kTriggerLow(1, 4), |
| 229 | kTriggerVelocityHigh(1, 2), kTriggerVelocityLow(1, 5), |
| 230 | kTriggerTorqueHigh(1, 3), kTriggerTorqueLow(1, 6); |
| 231 | |
| 232 | const JoystickAxis kWheelHigh(2, 1), kWheelLow(2, 4), |
| 233 | kWheelVelocityHigh(2, 2), kWheelVelocityLow(2, 5), kWheelTorqueHigh(2, 3), |
| 234 | kWheelTorqueLow(2, 6); |
| 235 | |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 236 | const ButtonLocation kQuickTurn(1, 3); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 237 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 238 | const ButtonLocation TopButton(1, 1); |
| 239 | const ButtonLocation SecondButton(1, 2); |
| 240 | // Non-existant button for nops. |
| 241 | const ButtonLocation DummyButton(1, 10); |
| 242 | |
| 243 | // TODO(james): Make a copy assignment operator for ButtonLocation so we don't |
| 244 | // have to shoehorn in these ternary operators. |
| 245 | const ButtonLocation kTurn1 = |
| 246 | (top_button_use == TopButtonUse::kLineFollow) ? TopButton : DummyButton; |
| 247 | // Turn2 currently does nothing on the pistol grip, ever. |
| 248 | const ButtonLocation kTurn2 = DummyButton; |
| 249 | const ButtonLocation kShiftHigh = |
| 250 | (top_button_use == TopButtonUse::kShift) ? TopButton : DummyButton; |
| 251 | const ButtonLocation kShiftLow = |
| 252 | (top_button_use == TopButtonUse::kShift) ? SecondButton : DummyButton; |
| 253 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 254 | std::unique_ptr<PistolDrivetrainInputReader> result( |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 255 | new PistolDrivetrainInputReader( |
| 256 | kWheelHigh, kWheelLow, kTriggerVelocityHigh, kTriggerVelocityLow, |
| 257 | kTriggerTorqueHigh, kTriggerTorqueLow, kTriggerHigh, kTriggerLow, |
| 258 | kWheelVelocityHigh, kWheelVelocityLow, kWheelTorqueHigh, |
| 259 | kWheelTorqueLow, kQuickTurn, kShiftHigh, kShiftLow, kTurn1, kTurn2)); |
| 260 | |
| 261 | result->set_default_high_gear(default_high_gear); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 262 | return result; |
| 263 | } |
| 264 | |
| 265 | std::unique_ptr<XboxDrivetrainInputReader> XboxDrivetrainInputReader::Make() { |
| 266 | // xbox |
| 267 | const JoystickAxis kSteeringWheel(1, 5), kDriveThrottle(1, 2); |
| 268 | const ButtonLocation kQuickTurn(1, 5); |
| 269 | |
| 270 | // Nop |
| 271 | const ButtonLocation kTurn1(1, 1); |
| 272 | const ButtonLocation kTurn2(1, 2); |
| 273 | |
| 274 | std::unique_ptr<XboxDrivetrainInputReader> result( |
| 275 | new XboxDrivetrainInputReader(kSteeringWheel, kDriveThrottle, kQuickTurn, |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 276 | kTurn1, TurnButtonUse::kControlLoopDriving, |
| 277 | kTurn2, |
| 278 | TurnButtonUse::kControlLoopDriving)); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 279 | return result; |
| 280 | } |
| 281 | ::std::unique_ptr<DrivetrainInputReader> DrivetrainInputReader::Make( |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 282 | InputType type, |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 283 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 284 | &dt_config) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 285 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader; |
| 286 | |
| 287 | using InputType = DrivetrainInputReader::InputType; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 288 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 289 | switch (type) { |
| 290 | case InputType::kSteeringWheel: |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 291 | drivetrain_input_reader = |
| 292 | SteeringWheelDrivetrainInputReader::Make(dt_config.default_high_gear); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 293 | break; |
| 294 | case InputType::kPistol: |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame^] | 295 | drivetrain_input_reader = PistolDrivetrainInputReader::Make( |
| 296 | dt_config.default_high_gear, |
| 297 | dt_config.pistol_grip_shift_enables_line_follow |
| 298 | ? PistolDrivetrainInputReader::TopButtonUse::kLineFollow |
| 299 | : PistolDrivetrainInputReader::TopButtonUse::kShift); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 300 | break; |
| 301 | case InputType::kXbox: |
| 302 | drivetrain_input_reader = XboxDrivetrainInputReader::Make(); |
| 303 | break; |
| 304 | } |
| 305 | return drivetrain_input_reader; |
| 306 | } |
| 307 | |
| 308 | } // namespace input |
| 309 | } // namespace aos |