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