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 | |
| 88 | protected: |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 89 | const driver_station::JoystickAxis wheel_; |
| 90 | const driver_station::JoystickAxis throttle_; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 91 | const driver_station::ButtonLocation quick_turn_; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 92 | // Button for enabling control loop driving. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 93 | const driver_station::ButtonLocation turn1_; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 94 | const TurnButtonUse turn1_use_; |
| 95 | // But for enabling line following. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 96 | const driver_station::ButtonLocation turn2_; |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 97 | const TurnButtonUse turn2_use_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 98 | |
| 99 | // Structure containing the (potentially adjusted) steering and throttle |
| 100 | // values from the joysticks. |
| 101 | struct WheelAndThrottle { |
| 102 | double wheel; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 103 | double wheel_velocity; |
| 104 | double wheel_torque; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 105 | double throttle; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 106 | double throttle_velocity; |
| 107 | double throttle_torque; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 108 | bool high_gear; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | private: |
| 112 | // Computes the steering and throttle from the provided driverstation data. |
| 113 | virtual WheelAndThrottle GetWheelAndThrottle( |
| 114 | const ::aos::input::driver_station::Data &data) = 0; |
| 115 | |
| 116 | double robot_velocity_ = 0.0; |
| 117 | // Goals to send to the drivetrain in closed loop mode. |
| 118 | double left_goal_ = 0.0; |
| 119 | double right_goal_ = 0.0; |
| 120 | // The scale for the joysticks for closed loop mode converting |
| 121 | // joysticks to meters displacement on the two wheels. |
| 122 | double wheel_multiplier_ = 0.5; |
| 123 | }; |
| 124 | |
| 125 | // Implements DrivetrainInputReader for the original steering wheel. |
| 126 | class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader { |
| 127 | public: |
| 128 | using DrivetrainInputReader::DrivetrainInputReader; |
| 129 | |
| 130 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 131 | // axis for the big steering wheel and throttle stick. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 132 | static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make( |
| 133 | bool default_high_gear); |
| 134 | |
| 135 | // Sets the default shifter position |
| 136 | void set_default_high_gear(bool default_high_gear) { |
| 137 | high_gear_ = default_high_gear; |
| 138 | default_high_gear_ = default_high_gear; |
| 139 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 140 | |
| 141 | private: |
| 142 | WheelAndThrottle GetWheelAndThrottle( |
| 143 | const ::aos::input::driver_station::Data &data) override; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 144 | bool high_gear_; |
| 145 | bool default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | class PistolDrivetrainInputReader : public DrivetrainInputReader { |
| 149 | public: |
| 150 | using DrivetrainInputReader::DrivetrainInputReader; |
| 151 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 152 | // What to use the top two buttons for on the pistol grip. |
| 153 | enum class TopButtonUse { |
| 154 | // Normal shifting. |
| 155 | kShift, |
| 156 | // Line following (currently just uses top button). |
| 157 | kLineFollow, |
| 158 | }; |
| 159 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 160 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 161 | // axis for the (cheap) pistol grip controller. |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 162 | static std::unique_ptr<PistolDrivetrainInputReader> Make( |
| 163 | bool default_high_gear, TopButtonUse top_button_use); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 164 | |
| 165 | private: |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 166 | PistolDrivetrainInputReader( |
| 167 | driver_station::JoystickAxis wheel_high, |
| 168 | driver_station::JoystickAxis wheel_low, |
| 169 | driver_station::JoystickAxis wheel_velocity_high, |
| 170 | driver_station::JoystickAxis wheel_velocity_low, |
| 171 | driver_station::JoystickAxis wheel_torque_high, |
| 172 | driver_station::JoystickAxis wheel_torque_low, |
| 173 | driver_station::JoystickAxis throttle_high, |
| 174 | driver_station::JoystickAxis throttle_low, |
| 175 | driver_station::JoystickAxis throttle_velocity_high, |
| 176 | driver_station::JoystickAxis throttle_velocity_low, |
| 177 | driver_station::JoystickAxis throttle_torque_high, |
| 178 | driver_station::JoystickAxis throttle_torque_low, |
| 179 | driver_station::ButtonLocation quick_turn, |
| 180 | driver_station::ButtonLocation shift_high, |
| 181 | driver_station::ButtonLocation shift_low, |
| 182 | driver_station::ButtonLocation turn1, |
| 183 | driver_station::ButtonLocation turn2) |
| 184 | : DrivetrainInputReader(wheel_high, throttle_high, quick_turn, turn1, |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 185 | TurnButtonUse::kLineFollow, turn2, |
| 186 | TurnButtonUse::kLineFollow), |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 187 | wheel_low_(wheel_low), |
| 188 | wheel_velocity_high_(wheel_velocity_high), |
| 189 | wheel_velocity_low_(wheel_velocity_low), |
| 190 | wheel_torque_high_(wheel_torque_high), |
| 191 | wheel_torque_low_(wheel_torque_low), |
| 192 | throttle_low_(throttle_low), |
| 193 | throttle_velocity_high_(throttle_velocity_high), |
| 194 | throttle_velocity_low_(throttle_velocity_low), |
| 195 | throttle_torque_high_(throttle_torque_high), |
| 196 | throttle_torque_low_(throttle_torque_low), |
| 197 | shift_high_(shift_high), |
| 198 | shift_low_(shift_low) {} |
| 199 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 200 | WheelAndThrottle GetWheelAndThrottle( |
| 201 | const ::aos::input::driver_station::Data &data) override; |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 202 | |
| 203 | // Sets the default shifter position |
| 204 | void set_default_high_gear(bool default_high_gear) { |
| 205 | high_gear_ = default_high_gear; |
| 206 | default_high_gear_ = default_high_gear; |
| 207 | } |
| 208 | |
| 209 | const driver_station::JoystickAxis wheel_low_; |
| 210 | const driver_station::JoystickAxis wheel_velocity_high_; |
| 211 | const driver_station::JoystickAxis wheel_velocity_low_; |
| 212 | const driver_station::JoystickAxis wheel_torque_high_; |
| 213 | const driver_station::JoystickAxis wheel_torque_low_; |
| 214 | |
| 215 | const driver_station::JoystickAxis throttle_low_; |
| 216 | const driver_station::JoystickAxis throttle_velocity_high_; |
| 217 | const driver_station::JoystickAxis throttle_velocity_low_; |
| 218 | const driver_station::JoystickAxis throttle_torque_high_; |
| 219 | const driver_station::JoystickAxis throttle_torque_low_; |
| 220 | |
| 221 | const driver_station::ButtonLocation shift_high_; |
| 222 | const driver_station::ButtonLocation shift_low_; |
| 223 | |
| 224 | bool high_gear_; |
| 225 | bool default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | class XboxDrivetrainInputReader : public DrivetrainInputReader { |
| 229 | public: |
| 230 | using DrivetrainInputReader::DrivetrainInputReader; |
| 231 | |
| 232 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 233 | // axis for the Xbox controller. |
| 234 | static std::unique_ptr<XboxDrivetrainInputReader> Make(); |
| 235 | |
| 236 | private: |
| 237 | WheelAndThrottle GetWheelAndThrottle( |
| 238 | const ::aos::input::driver_station::Data &data) override; |
| 239 | }; |
| 240 | |
| 241 | } // namespace input |
| 242 | } // namespace aos |
| 243 | |
| 244 | #endif // AOS_INPUT_DRIVETRAIN_INPUT_H_ |