James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 1 | #include "frc971/input/drivetrain_input.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 2 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 3 | #include <cmath> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdio> |
| 5 | #include <cstring> |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/commonmath.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/logging/logging.h" |
Nikolai Sohmers | 0991b1f | 2024-10-20 14:24:34 -0700 | [diff] [blame^] | 9 | #include "drivetrain_input.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/control_loops_generated.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 13 | #include "frc971/input/driver_station_data.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 14 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 15 | using ::frc971::input::driver_station::ButtonLocation; |
| 16 | using ::frc971::input::driver_station::ControlBit; |
| 17 | using ::frc971::input::driver_station::JoystickAxis; |
| 18 | using ::frc971::input::driver_station::POVLocation; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 19 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | namespace drivetrain = frc971::control_loops::drivetrain; |
| 21 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 22 | namespace frc971::input { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 23 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 24 | const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1); |
| 25 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 26 | void DrivetrainInputReader::HandleDrivetrain( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 27 | const ::frc971::input::driver_station::Data &data) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 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 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 37 | drivetrain_status_fetcher_.Fetch(); |
| 38 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | robot_velocity_ = drivetrain_status_fetcher_->robot_speed(); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 42 | // If we have a vision align function, and it is in control, don't run the |
| 43 | // normal driving code. |
| 44 | if (vision_align_fn_) { |
| 45 | if (vision_align_fn_(data)) { |
| 46 | return; |
| 47 | } |
| 48 | } |
| 49 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 50 | bool is_control_loop_driving = false; |
| 51 | bool is_line_following = false; |
| 52 | |
| 53 | if (data.IsPressed(turn1_)) { |
| 54 | switch (turn1_use_) { |
| 55 | case TurnButtonUse::kControlLoopDriving: |
| 56 | is_control_loop_driving = true; |
| 57 | break; |
| 58 | case TurnButtonUse::kLineFollow: |
| 59 | is_line_following = true; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (data.IsPressed(turn2_)) { |
| 65 | switch (turn2_use_) { |
| 66 | case TurnButtonUse::kControlLoopDriving: |
| 67 | is_control_loop_driving = true; |
| 68 | break; |
| 69 | case TurnButtonUse::kLineFollow: |
| 70 | is_line_following = true; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 75 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 76 | if (is_control_loop_driving && !last_is_control_loop_driving_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 77 | left_goal_ = drivetrain_status_fetcher_->estimated_left_position() + |
Austin Schuh | f9202e8 | 2019-03-22 21:55:11 -0700 | [diff] [blame] | 78 | wheel * wheel_multiplier_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | right_goal_ = drivetrain_status_fetcher_->estimated_right_position() - |
Austin Schuh | f9202e8 | 2019-03-22 21:55:11 -0700 | [diff] [blame] | 80 | wheel * wheel_multiplier_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 83 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 84 | const double current_left_goal = |
| 85 | left_goal_ - wheel * wheel_multiplier_ + throttle * 0.3; |
| 86 | const double current_right_goal = |
| 87 | right_goal_ + wheel * wheel_multiplier_ + throttle * 0.3; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 89 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | frc971::ProfileParameters::Builder linear_builder = |
| 91 | builder.MakeBuilder<frc971::ProfileParameters>(); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 92 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | linear_builder.add_max_velocity(3.0); |
| 94 | linear_builder.add_max_acceleration(20.0); |
| 95 | |
| 96 | flatbuffers::Offset<frc971::ProfileParameters> linear_offset = |
| 97 | linear_builder.Finish(); |
| 98 | |
| 99 | auto goal_builder = builder.MakeBuilder<drivetrain::Goal>(); |
| 100 | goal_builder.add_wheel(wheel); |
| 101 | goal_builder.add_wheel_velocity(wheel_velocity); |
| 102 | goal_builder.add_wheel_torque(wheel_torque); |
| 103 | goal_builder.add_throttle(throttle); |
| 104 | goal_builder.add_throttle_velocity(throttle_velocity); |
| 105 | goal_builder.add_throttle_torque(throttle_torque); |
| 106 | goal_builder.add_highgear(high_gear); |
| 107 | goal_builder.add_quickturn(data.IsPressed(quick_turn_)); |
| 108 | goal_builder.add_controller_type( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 109 | is_line_following ? drivetrain::ControllerType::LINE_FOLLOWER |
| 110 | : (is_control_loop_driving |
| 111 | ? drivetrain::ControllerType::MOTION_PROFILE |
| 112 | : drivetrain::ControllerType::POLYDRIVE)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 113 | goal_builder.add_left_goal(current_left_goal); |
| 114 | goal_builder.add_right_goal(current_right_goal); |
| 115 | goal_builder.add_linear(linear_offset); |
| 116 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 117 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 118 | AOS_LOG(WARNING, "sending stick values failed\n"); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 119 | } |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 120 | |
| 121 | last_is_control_loop_driving_ = is_control_loop_driving; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Nikolai Sohmers | 0991b1f | 2024-10-20 14:24:34 -0700 | [diff] [blame^] | 124 | void SwerveDrivetrainInputReader::HandleDrivetrain( |
| 125 | const ::frc971::input::driver_station::Data &data) { |
| 126 | const auto swerve_goals = GetSwerveGoals(data); |
| 127 | const double vx = swerve_goals.vx; |
| 128 | const double vy = swerve_goals.vy; |
| 129 | const double omega = swerve_goals.omega; |
| 130 | |
| 131 | auto builder = goal_sender_.MakeStaticBuilder(); |
| 132 | |
| 133 | auto joystick_goal = builder->add_joystick_goal(); |
| 134 | |
| 135 | joystick_goal->set_vx(vx); |
| 136 | joystick_goal->set_vy(vy); |
| 137 | joystick_goal->set_omega(omega); |
| 138 | |
| 139 | builder.CheckOk(builder.Send()); |
| 140 | } |
| 141 | |
| 142 | std::unique_ptr<SwerveDrivetrainInputReader> SwerveDrivetrainInputReader::Make( |
| 143 | ::aos::EventLoop *event_loop) { |
| 144 | // Swerve Controller |
| 145 | // axis (2, 2) will give you alternative omega axis (controls with vertical |
| 146 | // movement) |
| 147 | const JoystickAxis kVxAxis(2, 1), kVyAxis(1, 1), kOmegaAxis(1, 2); |
| 148 | |
| 149 | std::unique_ptr<SwerveDrivetrainInputReader> result( |
| 150 | new SwerveDrivetrainInputReader(event_loop, kVxAxis, kVyAxis, |
| 151 | kOmegaAxis)); |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | SwerveDrivetrainInputReader::SwerveGoals |
| 156 | SwerveDrivetrainInputReader::GetSwerveGoals( |
| 157 | const ::frc971::input::driver_station::Data &data) { |
| 158 | // xbox |
| 159 | constexpr double kMovementDeadband = 0.05; |
| 160 | constexpr double kRotationDeadband = 0.05; |
| 161 | |
| 162 | const double omega = |
| 163 | -aos::Deadband(-data.GetAxis(omega_axis_), kRotationDeadband, 1.0); |
| 164 | |
| 165 | const double vx = |
| 166 | aos::Deadband(-data.GetAxis(vx_axis_), kMovementDeadband, 1.0); |
| 167 | |
| 168 | const double vy = |
| 169 | aos::Deadband(-data.GetAxis(vy_axis_), kMovementDeadband, 1.0); |
| 170 | |
| 171 | return SwerveDrivetrainInputReader::SwerveGoals{vx, vy, omega}; |
| 172 | } |
| 173 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 174 | DrivetrainInputReader::WheelAndThrottle |
| 175 | SteeringWheelDrivetrainInputReader::GetWheelAndThrottle( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 176 | const ::frc971::input::driver_station::Data &data) { |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 177 | const double wheel = -data.GetAxis(wheel_); |
| 178 | const double throttle = -data.GetAxis(throttle_); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 179 | |
| 180 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 181 | high_gear_ = default_high_gear_; |
| 182 | } |
| 183 | |
| 184 | if (data.PosEdge(kShiftLow)) { |
| 185 | high_gear_ = false; |
| 186 | } |
| 187 | |
| 188 | if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) { |
| 189 | high_gear_ = true; |
| 190 | } |
| 191 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 192 | return DrivetrainInputReader::WheelAndThrottle{ |
| 193 | wheel, 0.0, 0.0, throttle, 0.0, 0.0, high_gear_}; |
| 194 | } |
| 195 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 196 | double UnwrappedAxis(const ::frc971::input::driver_station::Data &data, |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 197 | const JoystickAxis &high_bits, |
| 198 | const JoystickAxis &low_bits) { |
| 199 | const float high_bits_data = data.GetAxis(high_bits); |
| 200 | const float low_bits_data = data.GetAxis(low_bits); |
| 201 | const int16_t high_bits_data_int = |
| 202 | (high_bits_data < 0.0f ? high_bits_data * 128.0f |
| 203 | : high_bits_data * 127.0f); |
| 204 | const int16_t low_bits_data_int = |
| 205 | (low_bits_data < 0.0f ? low_bits_data * 128.0f : low_bits_data * 127.0f); |
| 206 | |
| 207 | const uint16_t high_bits_data_uint = |
| 208 | ((static_cast<uint16_t>(high_bits_data_int) & 0xff) + 0x80) & 0xff; |
| 209 | const uint16_t low_bits_data_uint = |
| 210 | ((static_cast<uint16_t>(low_bits_data_int) & 0xff) + 0x80) & 0xff; |
| 211 | |
| 212 | const uint16_t data_uint = (high_bits_data_uint << 8) | low_bits_data_uint; |
| 213 | |
| 214 | const int32_t data_int = static_cast<int32_t>(data_uint) - 0x8000; |
| 215 | |
| 216 | if (data_int < 0) { |
| 217 | return static_cast<double>(data_int) / static_cast<double>(0x8000); |
| 218 | } else { |
| 219 | return static_cast<double>(data_int) / static_cast<double>(0x7fff); |
| 220 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | DrivetrainInputReader::WheelAndThrottle |
| 224 | PistolDrivetrainInputReader::GetWheelAndThrottle( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 225 | const ::frc971::input::driver_station::Data &data) { |
| 226 | const double wheel = -UnwrappedAxis(data, wheel_, wheel_low_); |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 227 | const double wheel_velocity = |
| 228 | -UnwrappedAxis(data, wheel_velocity_high_, wheel_velocity_low_) * 50.0; |
| 229 | const double wheel_torque = |
| 230 | -UnwrappedAxis(data, wheel_torque_high_, wheel_torque_low_) / 2.0; |
| 231 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 232 | double throttle = UnwrappedAxis(data, throttle_, throttle_low_); |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 233 | const double throttle_velocity = |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 234 | UnwrappedAxis(data, throttle_velocity_high_, throttle_velocity_low_) * |
| 235 | 50.0; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 236 | const double throttle_torque = |
| 237 | UnwrappedAxis(data, throttle_torque_high_, throttle_torque_low_) / 2.0; |
| 238 | |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 239 | // TODO(austin): Deal with haptics here. |
| 240 | if (throttle < 0) { |
| 241 | throttle = ::std::max(-1.0, throttle / 0.7); |
| 242 | } |
| 243 | |
James Kuszmaul | c4eb1b2 | 2019-04-13 15:48:34 -0700 | [diff] [blame] | 244 | if (data.IsPressed(slow_down_)) { |
| 245 | throttle *= 0.5; |
| 246 | } |
| 247 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 248 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 249 | high_gear_ = default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 252 | if (data.PosEdge(shift_low_)) { |
| 253 | high_gear_ = false; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 254 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 255 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 256 | if (data.PosEdge(shift_high_)) { |
| 257 | high_gear_ = true; |
| 258 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 259 | |
James Kuszmaul | f64bc43 | 2022-03-25 19:14:42 -0700 | [diff] [blame] | 260 | // Emprically, the current pistol grip tends towards steady-state errors at |
| 261 | // ~0.01-0.02 on both the wheel/throttle. Having the throttle correctly snap |
| 262 | // to zero is more important than the wheel for our internal logic, so force a |
| 263 | // deadband there. |
| 264 | constexpr double kThrottleDeadband = 0.05; |
| 265 | throttle = aos::Deadband(throttle, kThrottleDeadband, 1.0); |
| 266 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 267 | return DrivetrainInputReader::WheelAndThrottle{ |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 268 | wheel, wheel_velocity, wheel_torque, |
| 269 | throttle, throttle_velocity, throttle_torque, |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 270 | high_gear_}; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | DrivetrainInputReader::WheelAndThrottle |
| 274 | XboxDrivetrainInputReader::GetWheelAndThrottle( |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 275 | const ::frc971::input::driver_station::Data &data) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 276 | // xbox |
| 277 | constexpr double kWheelDeadband = 0.05; |
| 278 | constexpr double kThrottleDeadband = 0.05; |
| 279 | const double wheel = |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 280 | aos::Deadband(-data.GetAxis(wheel_), kWheelDeadband, 1.0); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 281 | |
| 282 | const double unmodified_throttle = |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 283 | aos::Deadband(-data.GetAxis(throttle_), kThrottleDeadband, 1.0); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 284 | |
| 285 | // Apply a sin function that's scaled to make it feel better. |
| 286 | constexpr double throttle_range = M_PI_2 * 0.9; |
| 287 | |
| 288 | double throttle = ::std::sin(throttle_range * unmodified_throttle) / |
| 289 | ::std::sin(throttle_range); |
| 290 | throttle = ::std::sin(throttle_range * throttle) / ::std::sin(throttle_range); |
| 291 | throttle = 2.0 * unmodified_throttle - throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 292 | return DrivetrainInputReader::WheelAndThrottle{wheel, 0.0, 0.0, throttle, |
| 293 | 0.0, 0.0, true}; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | std::unique_ptr<SteeringWheelDrivetrainInputReader> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 297 | SteeringWheelDrivetrainInputReader::Make(::aos::EventLoop *event_loop, |
| 298 | bool default_high_gear) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 299 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 300 | const ButtonLocation kQuickTurn(1, 5); |
| 301 | const ButtonLocation kTurn1(1, 7); |
| 302 | const ButtonLocation kTurn2(1, 11); |
| 303 | std::unique_ptr<SteeringWheelDrivetrainInputReader> result( |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 304 | new SteeringWheelDrivetrainInputReader( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 305 | event_loop, kSteeringWheel, kDriveThrottle, kQuickTurn, kTurn1, |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 306 | TurnButtonUse::kControlLoopDriving, kTurn2, |
| 307 | TurnButtonUse::kControlLoopDriving)); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 308 | result.get()->set_default_high_gear(default_high_gear); |
| 309 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 310 | return result; |
| 311 | } |
| 312 | |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 313 | std::unique_ptr<PistolDrivetrainInputReader> PistolDrivetrainInputReader::Make( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 314 | ::aos::EventLoop *event_loop, bool default_high_gear, |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 315 | PistolTopButtonUse top_button_use, PistolSecondButtonUse second_button_use, |
| 316 | PistolBottomButtonUse bottom_button_use) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 317 | // Pistol Grip controller |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 318 | const JoystickAxis kTriggerHigh(1, 1), kTriggerLow(1, 4), |
| 319 | kTriggerVelocityHigh(1, 2), kTriggerVelocityLow(1, 5), |
| 320 | kTriggerTorqueHigh(1, 3), kTriggerTorqueLow(1, 6); |
| 321 | |
| 322 | const JoystickAxis kWheelHigh(2, 1), kWheelLow(2, 4), |
| 323 | kWheelVelocityHigh(2, 2), kWheelVelocityLow(2, 5), kWheelTorqueHigh(2, 3), |
| 324 | kWheelTorqueLow(2, 6); |
| 325 | |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 326 | const ButtonLocation kQuickTurn(1, 3); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 327 | |
Austin Schuh | 91a4e55 | 2023-02-24 20:34:01 -0800 | [diff] [blame] | 328 | const ButtonLocation kTopButton(1, 1); |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 329 | |
Austin Schuh | 91a4e55 | 2023-02-24 20:34:01 -0800 | [diff] [blame] | 330 | const ButtonLocation kSecondButton(1, 2); |
| 331 | const ButtonLocation kBottomButton(1, 4); |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 332 | // Non-existant button for nops. |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 333 | const ButtonLocation kDummyButton(1, 15); |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 334 | |
| 335 | // TODO(james): Make a copy assignment operator for ButtonLocation so we don't |
| 336 | // have to shoehorn in these ternary operators. |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 337 | const ButtonLocation kTurn1 = |
| 338 | (second_button_use == PistolSecondButtonUse::kTurn1) ? kSecondButton |
| 339 | : kDummyButton; |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 340 | // Turn2 does closed loop driving. |
| 341 | const ButtonLocation kTurn2 = |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 342 | (top_button_use == PistolTopButtonUse::kLineFollow) ? kTopButton |
| 343 | : kDummyButton; |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 344 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 345 | const ButtonLocation kShiftHigh = |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 346 | (top_button_use == PistolTopButtonUse::kShift) ? kTopButton |
| 347 | : kDummyButton; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 348 | const ButtonLocation kShiftLow = |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 349 | (second_button_use == PistolSecondButtonUse::kShiftLow) ? kSecondButton |
| 350 | : kDummyButton; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 351 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 352 | std::unique_ptr<PistolDrivetrainInputReader> result( |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 353 | new PistolDrivetrainInputReader( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 354 | event_loop, kWheelHigh, kWheelLow, kTriggerVelocityHigh, |
| 355 | kTriggerVelocityLow, kTriggerTorqueHigh, kTriggerTorqueLow, |
| 356 | kTriggerHigh, kTriggerLow, kWheelVelocityHigh, kWheelVelocityLow, |
| 357 | kWheelTorqueHigh, kWheelTorqueLow, kQuickTurn, kShiftHigh, kShiftLow, |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 358 | kTurn1, |
| 359 | (bottom_button_use == PistolBottomButtonUse::kControlLoopDriving) |
| 360 | ? kBottomButton |
| 361 | : kTurn2, |
| 362 | (top_button_use == PistolTopButtonUse::kNone) ? kTopButton |
| 363 | : kBottomButton)); |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 364 | |
| 365 | result->set_default_high_gear(default_high_gear); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 366 | return result; |
| 367 | } |
| 368 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 369 | std::unique_ptr<XboxDrivetrainInputReader> XboxDrivetrainInputReader::Make( |
| 370 | ::aos::EventLoop *event_loop) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 371 | // xbox |
| 372 | const JoystickAxis kSteeringWheel(1, 5), kDriveThrottle(1, 2); |
| 373 | const ButtonLocation kQuickTurn(1, 5); |
| 374 | |
| 375 | // Nop |
| 376 | const ButtonLocation kTurn1(1, 1); |
| 377 | const ButtonLocation kTurn2(1, 2); |
| 378 | |
| 379 | std::unique_ptr<XboxDrivetrainInputReader> result( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 380 | new XboxDrivetrainInputReader(event_loop, kSteeringWheel, kDriveThrottle, |
| 381 | kQuickTurn, kTurn1, |
| 382 | TurnButtonUse::kControlLoopDriving, kTurn2, |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 383 | TurnButtonUse::kControlLoopDriving)); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 384 | return result; |
| 385 | } |
| 386 | ::std::unique_ptr<DrivetrainInputReader> DrivetrainInputReader::Make( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 387 | ::aos::EventLoop *event_loop, InputType type, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 388 | const drivetrain::DrivetrainConfig<double> &dt_config) { |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 389 | std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader; |
| 390 | |
| 391 | using InputType = DrivetrainInputReader::InputType; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 392 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 393 | switch (type) { |
| 394 | case InputType::kSteeringWheel: |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 395 | drivetrain_input_reader = SteeringWheelDrivetrainInputReader::Make( |
| 396 | event_loop, dt_config.default_high_gear); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 397 | break; |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 398 | case InputType::kPistol: { |
| 399 | // For backwards compatibility |
| 400 | PistolTopButtonUse top_button_use = |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 401 | dt_config.pistol_grip_shift_enables_line_follow |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 402 | ? PistolTopButtonUse::kLineFollow |
| 403 | : dt_config.top_button_use; |
| 404 | |
| 405 | PistolSecondButtonUse second_button_use = dt_config.second_button_use; |
| 406 | PistolBottomButtonUse bottom_button_use = dt_config.bottom_button_use; |
| 407 | |
| 408 | if (top_button_use == PistolTopButtonUse::kLineFollow) { |
| 409 | second_button_use = PistolSecondButtonUse::kTurn1; |
| 410 | } |
| 411 | |
| 412 | drivetrain_input_reader = PistolDrivetrainInputReader::Make( |
| 413 | event_loop, dt_config.default_high_gear, top_button_use, |
| 414 | second_button_use, bottom_button_use); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 415 | break; |
Maxwell Henderson | 24f89f3 | 2023-03-25 15:55:46 -0700 | [diff] [blame] | 416 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 417 | case InputType::kXbox: |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 418 | drivetrain_input_reader = XboxDrivetrainInputReader::Make(event_loop); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | return drivetrain_input_reader; |
| 422 | } |
| 423 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 424 | } // namespace frc971::input |