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