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 | |
| 10 | #include "aos/common/input/driver_station_data.h" |
| 11 | #include "aos/common/logging/logging.h" |
| 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: |
| 38 | // Inputs driver station button and joystick locations |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 39 | DrivetrainInputReader(driver_station::JoystickAxis steering_wheel, |
| 40 | driver_station::JoystickAxis drive_throttle, |
| 41 | driver_station::ButtonLocation quick_turn, |
| 42 | driver_station::ButtonLocation turn1, |
| 43 | driver_station::ButtonLocation turn2) |
| 44 | : steering_wheel_(steering_wheel), |
| 45 | drive_throttle_(drive_throttle), |
| 46 | quick_turn_(quick_turn), |
| 47 | turn1_(turn1), |
| 48 | turn2_(turn2) {} |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 49 | |
| 50 | virtual ~DrivetrainInputReader() = default; |
| 51 | |
| 52 | // List of driver joystick types. |
| 53 | enum class InputType { |
| 54 | kSteeringWheel, |
| 55 | kPistol, |
| 56 | kXbox, |
| 57 | }; |
| 58 | |
| 59 | // Constructs the appropriate DrivetrainInputReader. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 60 | static std::unique_ptr<DrivetrainInputReader> Make( |
| 61 | InputType type, |
| 62 | const ::frc971::control_loops::drivetrain::DrivetrainConfig &dt_config); |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 63 | |
| 64 | // Processes new joystick data and publishes drivetrain goal messages. |
| 65 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data); |
| 66 | |
| 67 | // Sets the scalar for the steering wheel for closed loop mode converting |
| 68 | // steering ratio to meters displacement on the two wheels. |
| 69 | void set_wheel_multiplier(double wheel_multiplier) { |
| 70 | wheel_multiplier_ = wheel_multiplier; |
| 71 | } |
| 72 | |
| 73 | // Returns the current robot velocity in m/s. |
| 74 | double robot_velocity() const { return robot_velocity_; } |
| 75 | |
| 76 | protected: |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 77 | const driver_station::JoystickAxis steering_wheel_; |
| 78 | const driver_station::JoystickAxis drive_throttle_; |
| 79 | const driver_station::ButtonLocation quick_turn_; |
| 80 | const driver_station::ButtonLocation turn1_; |
| 81 | const driver_station::ButtonLocation turn2_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 82 | |
| 83 | // Structure containing the (potentially adjusted) steering and throttle |
| 84 | // values from the joysticks. |
| 85 | struct WheelAndThrottle { |
| 86 | double wheel; |
| 87 | double throttle; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 88 | bool high_gear; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | private: |
| 92 | // Computes the steering and throttle from the provided driverstation data. |
| 93 | virtual WheelAndThrottle GetWheelAndThrottle( |
| 94 | const ::aos::input::driver_station::Data &data) = 0; |
| 95 | |
| 96 | double robot_velocity_ = 0.0; |
| 97 | // Goals to send to the drivetrain in closed loop mode. |
| 98 | double left_goal_ = 0.0; |
| 99 | double right_goal_ = 0.0; |
| 100 | // The scale for the joysticks for closed loop mode converting |
| 101 | // joysticks to meters displacement on the two wheels. |
| 102 | double wheel_multiplier_ = 0.5; |
| 103 | }; |
| 104 | |
| 105 | // Implements DrivetrainInputReader for the original steering wheel. |
| 106 | class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader { |
| 107 | public: |
| 108 | using DrivetrainInputReader::DrivetrainInputReader; |
| 109 | |
| 110 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 111 | // axis for the big steering wheel and throttle stick. |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 112 | static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make( |
| 113 | bool default_high_gear); |
| 114 | |
| 115 | // Sets the default shifter position |
| 116 | void set_default_high_gear(bool default_high_gear) { |
| 117 | high_gear_ = default_high_gear; |
| 118 | default_high_gear_ = default_high_gear; |
| 119 | } |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | WheelAndThrottle GetWheelAndThrottle( |
| 123 | const ::aos::input::driver_station::Data &data) override; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 124 | bool high_gear_; |
| 125 | bool default_high_gear_; |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | class PistolDrivetrainInputReader : public DrivetrainInputReader { |
| 129 | public: |
| 130 | using DrivetrainInputReader::DrivetrainInputReader; |
| 131 | |
| 132 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 133 | // axis for the (cheap) pistol grip controller. |
| 134 | static std::unique_ptr<PistolDrivetrainInputReader> Make(); |
| 135 | |
| 136 | private: |
| 137 | WheelAndThrottle GetWheelAndThrottle( |
| 138 | const ::aos::input::driver_station::Data &data) override; |
| 139 | }; |
| 140 | |
| 141 | class XboxDrivetrainInputReader : public DrivetrainInputReader { |
| 142 | public: |
| 143 | using DrivetrainInputReader::DrivetrainInputReader; |
| 144 | |
| 145 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 146 | // axis for the Xbox controller. |
| 147 | static std::unique_ptr<XboxDrivetrainInputReader> Make(); |
| 148 | |
| 149 | private: |
| 150 | WheelAndThrottle GetWheelAndThrottle( |
| 151 | const ::aos::input::driver_station::Data &data) override; |
| 152 | }; |
| 153 | |
| 154 | } // namespace input |
| 155 | } // namespace aos |
| 156 | |
| 157 | #endif // AOS_INPUT_DRIVETRAIN_INPUT_H_ |