Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 1 | #ifndef AOS_INPUT_DRIVETRAIN_INPUT_H_ |
| 2 | #define AOS_INPUT_DRIVETRAIN_INPUT_H_ |
| 3 | |
| 4 | #include <math.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <cmath> |
| 8 | #include <memory> |
| 9 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/input/driver_station_data.h" |
| 11 | #include "aos/logging/logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "frc971/control_loops/control_loops_generated.h" |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 15 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
| 18 | namespace input { |
| 19 | |
| 20 | // We have a couple different joystick configurations used to drive our skid |
| 21 | // steer robots. These configurations don't change very often, and there are a |
| 22 | // small, discrete, set of them. The interface to the drivetrain is the same |
| 23 | // across all of our drivetrains, so we can abstract that away from our users. |
| 24 | // |
| 25 | // We want to be able to re-use that code across years, and switch joystick |
| 26 | // types quickly and efficiently. |
| 27 | // |
| 28 | // DrivetrainInputReader is the abstract base class which provides a consistent |
| 29 | // API to control drivetrains. |
| 30 | // |
| 31 | // To construct the appropriate DrivetrainInputReader, call |
| 32 | // DrivetrainInputReader::Make with the input type enum. |
| 33 | // To use it, call HandleDrivetrain(data) every time a joystick packet is |
| 34 | // received. |
| 35 | // |
| 36 | // Base class for the interface to the drivetrain implemented by multiple |
| 37 | // joystick types. |
| 38 | class DrivetrainInputReader { |
| 39 | public: |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 40 | // What to use the turn1/2 buttons for. |
| 41 | enum class TurnButtonUse { |
| 42 | // Use the button to enable control loop driving. |
| 43 | kControlLoopDriving, |
| 44 | // Use the button to set line following mode. |
| 45 | kLineFollow, |
| 46 | }; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 47 | // Inputs driver station button and joystick locations |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 48 | DrivetrainInputReader(::aos::EventLoop *event_loop, |
| 49 | driver_station::JoystickAxis wheel, |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 50 | driver_station::JoystickAxis throttle, |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 51 | driver_station::ButtonLocation quick_turn, |
| 52 | driver_station::ButtonLocation turn1, |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 53 | TurnButtonUse turn1_use, |
| 54 | driver_station::ButtonLocation turn2, |
| 55 | TurnButtonUse turn2_use) |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 56 | : wheel_(wheel), |
| 57 | throttle_(throttle), |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 58 | quick_turn_(quick_turn), |
| 59 | turn1_(turn1), |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 60 | turn1_use_(turn1_use), |
| 61 | turn2_(turn2), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 62 | turn2_use_(turn2_use), |
| 63 | drivetrain_status_fetcher_( |
| 64 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | ->MakeFetcher<::frc971::control_loops::drivetrain::Status>( |
| 66 | "/drivetrain")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 67 | drivetrain_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | event_loop->MakeSender<::frc971::control_loops::drivetrain::Goal>( |
| 69 | "/drivetrain")) {} |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 70 | |
| 71 | virtual ~DrivetrainInputReader() = default; |
| 72 | |
| 73 | // List of driver joystick types. |
| 74 | enum class InputType { |
| 75 | kSteeringWheel, |
| 76 | kPistol, |
| 77 | kXbox, |
| 78 | }; |
| 79 | |
| 80 | // Constructs the appropriate DrivetrainInputReader. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 81 | static std::unique_ptr<DrivetrainInputReader> Make( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 82 | ::aos::EventLoop *event_loop, InputType type, |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 83 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 84 | &dt_config); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 85 | |
| 86 | // Processes new joystick data and publishes drivetrain goal messages. |
| 87 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data); |
| 88 | |
| 89 | // Sets the scalar for the steering wheel for closed loop mode converting |
| 90 | // steering ratio to meters displacement on the two wheels. |
| 91 | void set_wheel_multiplier(double wheel_multiplier) { |
| 92 | wheel_multiplier_ = wheel_multiplier; |
| 93 | } |
| 94 | |
| 95 | // Returns the current robot velocity in m/s. |
| 96 | double robot_velocity() const { return robot_velocity_; } |
| 97 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 98 | void set_vision_align_fn( |
| 99 | ::std::function<bool(const ::aos::input::driver_station::Data &data)> |
| 100 | vision_align_fn) { |
| 101 | vision_align_fn_ = vision_align_fn; |
| 102 | } |
| 103 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 104 | protected: |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 105 | const driver_station::JoystickAxis wheel_; |
| 106 | const driver_station::JoystickAxis throttle_; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 107 | const driver_station::ButtonLocation quick_turn_; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 108 | // Button for enabling control loop driving. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 109 | const driver_station::ButtonLocation turn1_; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 110 | const TurnButtonUse turn1_use_; |
| 111 | // But for enabling line following. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 112 | const driver_station::ButtonLocation turn2_; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 113 | const TurnButtonUse turn2_use_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 114 | |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 115 | bool last_is_control_loop_driving_ = false; |
| 116 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 117 | // Structure containing the (potentially adjusted) steering and throttle |
| 118 | // values from the joysticks. |
| 119 | struct WheelAndThrottle { |
| 120 | double wheel; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 121 | double wheel_velocity; |
| 122 | double wheel_torque; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 123 | double throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 124 | double throttle_velocity; |
| 125 | double throttle_torque; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 126 | bool high_gear; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | private: |
| 130 | // Computes the steering and throttle from the provided driverstation data. |
| 131 | virtual WheelAndThrottle GetWheelAndThrottle( |
| 132 | const ::aos::input::driver_station::Data &data) = 0; |
| 133 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 134 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Status> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 135 | drivetrain_status_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | ::aos::Sender<::frc971::control_loops::drivetrain::Goal> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 137 | drivetrain_goal_sender_; |
| 138 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 139 | double robot_velocity_ = 0.0; |
| 140 | // Goals to send to the drivetrain in closed loop mode. |
| 141 | double left_goal_ = 0.0; |
| 142 | double right_goal_ = 0.0; |
| 143 | // The scale for the joysticks for closed loop mode converting |
| 144 | // joysticks to meters displacement on the two wheels. |
| 145 | double wheel_multiplier_ = 0.5; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 146 | |
| 147 | ::std::function<bool(const ::aos::input::driver_station::Data &data)> |
| 148 | vision_align_fn_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Implements DrivetrainInputReader for the original steering wheel. |
| 152 | class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader { |
| 153 | public: |
| 154 | using DrivetrainInputReader::DrivetrainInputReader; |
| 155 | |
| 156 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 157 | // axis for the big steering wheel and throttle stick. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 158 | static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 159 | ::aos::EventLoop *event_loop, bool default_high_gear); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 160 | |
| 161 | // Sets the default shifter position |
| 162 | void set_default_high_gear(bool default_high_gear) { |
| 163 | high_gear_ = default_high_gear; |
| 164 | default_high_gear_ = default_high_gear; |
| 165 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 166 | |
| 167 | private: |
| 168 | WheelAndThrottle GetWheelAndThrottle( |
| 169 | const ::aos::input::driver_station::Data &data) override; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 170 | bool high_gear_; |
| 171 | bool default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | class PistolDrivetrainInputReader : public DrivetrainInputReader { |
| 175 | public: |
| 176 | using DrivetrainInputReader::DrivetrainInputReader; |
| 177 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 178 | // What to use the top two buttons for on the pistol grip. |
| 179 | enum class TopButtonUse { |
| 180 | // Normal shifting. |
| 181 | kShift, |
| 182 | // Line following (currently just uses top button). |
| 183 | kLineFollow, |
| 184 | }; |
| 185 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 186 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 187 | // axis for the (cheap) pistol grip controller. |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 188 | static std::unique_ptr<PistolDrivetrainInputReader> Make( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 189 | ::aos::EventLoop *event_loop, bool default_high_gear, |
| 190 | TopButtonUse top_button_use); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 191 | |
| 192 | private: |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 193 | PistolDrivetrainInputReader( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 194 | ::aos::EventLoop *event_loop, driver_station::JoystickAxis wheel_high, |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 195 | driver_station::JoystickAxis wheel_low, |
| 196 | driver_station::JoystickAxis wheel_velocity_high, |
| 197 | driver_station::JoystickAxis wheel_velocity_low, |
| 198 | driver_station::JoystickAxis wheel_torque_high, |
| 199 | driver_station::JoystickAxis wheel_torque_low, |
| 200 | driver_station::JoystickAxis throttle_high, |
| 201 | driver_station::JoystickAxis throttle_low, |
| 202 | driver_station::JoystickAxis throttle_velocity_high, |
| 203 | driver_station::JoystickAxis throttle_velocity_low, |
| 204 | driver_station::JoystickAxis throttle_torque_high, |
| 205 | driver_station::JoystickAxis throttle_torque_low, |
| 206 | driver_station::ButtonLocation quick_turn, |
| 207 | driver_station::ButtonLocation shift_high, |
| 208 | driver_station::ButtonLocation shift_low, |
| 209 | driver_station::ButtonLocation turn1, |
James Kuszmaul | c4eb1b2 | 2019-04-13 15:48:34 -0700 | [diff] [blame] | 210 | driver_station::ButtonLocation turn2, |
| 211 | driver_station::ButtonLocation slow_down) |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 212 | : DrivetrainInputReader(event_loop, wheel_high, throttle_high, quick_turn, |
| 213 | turn1, TurnButtonUse::kLineFollow, turn2, |
Austin Schuh | 48d3a96 | 2019-03-17 18:12:32 -0700 | [diff] [blame] | 214 | TurnButtonUse::kControlLoopDriving), |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 215 | wheel_low_(wheel_low), |
| 216 | wheel_velocity_high_(wheel_velocity_high), |
| 217 | wheel_velocity_low_(wheel_velocity_low), |
| 218 | wheel_torque_high_(wheel_torque_high), |
| 219 | wheel_torque_low_(wheel_torque_low), |
| 220 | throttle_low_(throttle_low), |
| 221 | throttle_velocity_high_(throttle_velocity_high), |
| 222 | throttle_velocity_low_(throttle_velocity_low), |
| 223 | throttle_torque_high_(throttle_torque_high), |
| 224 | throttle_torque_low_(throttle_torque_low), |
| 225 | shift_high_(shift_high), |
James Kuszmaul | c4eb1b2 | 2019-04-13 15:48:34 -0700 | [diff] [blame] | 226 | shift_low_(shift_low), |
| 227 | slow_down_(slow_down) {} |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 228 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 229 | WheelAndThrottle GetWheelAndThrottle( |
| 230 | const ::aos::input::driver_station::Data &data) override; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 231 | |
| 232 | // Sets the default shifter position |
| 233 | void set_default_high_gear(bool default_high_gear) { |
| 234 | high_gear_ = default_high_gear; |
| 235 | default_high_gear_ = default_high_gear; |
| 236 | } |
| 237 | |
| 238 | const driver_station::JoystickAxis wheel_low_; |
| 239 | const driver_station::JoystickAxis wheel_velocity_high_; |
| 240 | const driver_station::JoystickAxis wheel_velocity_low_; |
| 241 | const driver_station::JoystickAxis wheel_torque_high_; |
| 242 | const driver_station::JoystickAxis wheel_torque_low_; |
| 243 | |
| 244 | const driver_station::JoystickAxis throttle_low_; |
| 245 | const driver_station::JoystickAxis throttle_velocity_high_; |
| 246 | const driver_station::JoystickAxis throttle_velocity_low_; |
| 247 | const driver_station::JoystickAxis throttle_torque_high_; |
| 248 | const driver_station::JoystickAxis throttle_torque_low_; |
| 249 | |
| 250 | const driver_station::ButtonLocation shift_high_; |
| 251 | const driver_station::ButtonLocation shift_low_; |
James Kuszmaul | c4eb1b2 | 2019-04-13 15:48:34 -0700 | [diff] [blame] | 252 | const driver_station::ButtonLocation slow_down_; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 253 | |
| 254 | bool high_gear_; |
| 255 | bool default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | class XboxDrivetrainInputReader : public DrivetrainInputReader { |
| 259 | public: |
| 260 | using DrivetrainInputReader::DrivetrainInputReader; |
| 261 | |
| 262 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 263 | // axis for the Xbox controller. |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 264 | static std::unique_ptr<XboxDrivetrainInputReader> Make( |
| 265 | ::aos::EventLoop *event_loop); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 266 | |
| 267 | private: |
| 268 | WheelAndThrottle GetWheelAndThrottle( |
| 269 | const ::aos::input::driver_station::Data &data) override; |
| 270 | }; |
| 271 | |
| 272 | } // namespace input |
| 273 | } // namespace aos |
| 274 | |
| 275 | #endif // AOS_INPUT_DRIVETRAIN_INPUT_H_ |