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" |
| 13 | |
| 14 | namespace aos { |
| 15 | namespace input { |
| 16 | |
| 17 | // We have a couple different joystick configurations used to drive our skid |
| 18 | // steer robots. These configurations don't change very often, and there are a |
| 19 | // small, discrete, set of them. The interface to the drivetrain is the same |
| 20 | // across all of our drivetrains, so we can abstract that away from our users. |
| 21 | // |
| 22 | // We want to be able to re-use that code across years, and switch joystick |
| 23 | // types quickly and efficiently. |
| 24 | // |
| 25 | // DrivetrainInputReader is the abstract base class which provides a consistent |
| 26 | // API to control drivetrains. |
| 27 | // |
| 28 | // To construct the appropriate DrivetrainInputReader, call |
| 29 | // DrivetrainInputReader::Make with the input type enum. |
| 30 | // To use it, call HandleDrivetrain(data) every time a joystick packet is |
| 31 | // received. |
| 32 | // |
| 33 | // Base class for the interface to the drivetrain implemented by multiple |
| 34 | // joystick types. |
| 35 | class DrivetrainInputReader { |
| 36 | public: |
| 37 | // Inputs driver station button and joystick locations |
| 38 | DrivetrainInputReader(driver_station::JoystickAxis kSteeringWheel, |
| 39 | driver_station::JoystickAxis kDriveThrottle, |
| 40 | driver_station::ButtonLocation kQuickTurn, |
| 41 | driver_station::ButtonLocation kTurn1, |
| 42 | driver_station::ButtonLocation kTurn2) |
| 43 | : kSteeringWheel(kSteeringWheel), |
| 44 | kDriveThrottle(kDriveThrottle), |
| 45 | kQuickTurn(kQuickTurn), |
| 46 | kTurn1(kTurn1), |
| 47 | kTurn2(kTurn2) {} |
| 48 | |
| 49 | virtual ~DrivetrainInputReader() = default; |
| 50 | |
| 51 | // List of driver joystick types. |
| 52 | enum class InputType { |
| 53 | kSteeringWheel, |
| 54 | kPistol, |
| 55 | kXbox, |
| 56 | }; |
| 57 | |
| 58 | // Constructs the appropriate DrivetrainInputReader. |
| 59 | static std::unique_ptr<DrivetrainInputReader> Make(InputType type); |
| 60 | |
| 61 | // Processes new joystick data and publishes drivetrain goal messages. |
| 62 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data); |
| 63 | |
| 64 | // Sets the scalar for the steering wheel for closed loop mode converting |
| 65 | // steering ratio to meters displacement on the two wheels. |
| 66 | void set_wheel_multiplier(double wheel_multiplier) { |
| 67 | wheel_multiplier_ = wheel_multiplier; |
| 68 | } |
| 69 | |
| 70 | // Returns the current robot velocity in m/s. |
| 71 | double robot_velocity() const { return robot_velocity_; } |
| 72 | |
| 73 | protected: |
| 74 | const driver_station::JoystickAxis kSteeringWheel; |
| 75 | const driver_station::JoystickAxis kDriveThrottle; |
| 76 | const driver_station::ButtonLocation kQuickTurn; |
| 77 | const driver_station::ButtonLocation kTurn1; |
| 78 | const driver_station::ButtonLocation kTurn2; |
| 79 | |
| 80 | // Structure containing the (potentially adjusted) steering and throttle |
| 81 | // values from the joysticks. |
| 82 | struct WheelAndThrottle { |
| 83 | double wheel; |
| 84 | double throttle; |
| 85 | }; |
| 86 | |
| 87 | private: |
| 88 | // Computes the steering and throttle from the provided driverstation data. |
| 89 | virtual WheelAndThrottle GetWheelAndThrottle( |
| 90 | const ::aos::input::driver_station::Data &data) = 0; |
| 91 | |
| 92 | double robot_velocity_ = 0.0; |
| 93 | // Goals to send to the drivetrain in closed loop mode. |
| 94 | double left_goal_ = 0.0; |
| 95 | double right_goal_ = 0.0; |
| 96 | // The scale for the joysticks for closed loop mode converting |
| 97 | // joysticks to meters displacement on the two wheels. |
| 98 | double wheel_multiplier_ = 0.5; |
| 99 | }; |
| 100 | |
| 101 | // Implements DrivetrainInputReader for the original steering wheel. |
| 102 | class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader { |
| 103 | public: |
| 104 | using DrivetrainInputReader::DrivetrainInputReader; |
| 105 | |
| 106 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 107 | // axis for the big steering wheel and throttle stick. |
| 108 | static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(); |
| 109 | |
| 110 | private: |
| 111 | WheelAndThrottle GetWheelAndThrottle( |
| 112 | const ::aos::input::driver_station::Data &data) override; |
| 113 | }; |
| 114 | |
| 115 | class PistolDrivetrainInputReader : public DrivetrainInputReader { |
| 116 | public: |
| 117 | using DrivetrainInputReader::DrivetrainInputReader; |
| 118 | |
| 119 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 120 | // axis for the (cheap) pistol grip controller. |
| 121 | static std::unique_ptr<PistolDrivetrainInputReader> Make(); |
| 122 | |
| 123 | private: |
| 124 | WheelAndThrottle GetWheelAndThrottle( |
| 125 | const ::aos::input::driver_station::Data &data) override; |
| 126 | }; |
| 127 | |
| 128 | class XboxDrivetrainInputReader : public DrivetrainInputReader { |
| 129 | public: |
| 130 | using DrivetrainInputReader::DrivetrainInputReader; |
| 131 | |
| 132 | // Creates a DrivetrainInputReader with the corresponding joystick ports and |
| 133 | // axis for the Xbox controller. |
| 134 | static std::unique_ptr<XboxDrivetrainInputReader> Make(); |
| 135 | |
| 136 | private: |
| 137 | WheelAndThrottle GetWheelAndThrottle( |
| 138 | const ::aos::input::driver_station::Data &data) override; |
| 139 | }; |
| 140 | |
| 141 | } // namespace input |
| 142 | } // namespace aos |
| 143 | |
| 144 | #endif // AOS_INPUT_DRIVETRAIN_INPUT_H_ |