John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_INPUT_DRIVER_STATION_DATA_H_ |
| 2 | #define AOS_INPUT_DRIVER_STATION_DATA_H_ |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 3 | |
| 4 | // This file defines several types to support nicely looking at the data |
| 5 | // received from the driver's station. |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 7 | #include <array> |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 10 | #include "aos/robot_state/joystick_state_generated.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | namespace input { |
| 14 | namespace driver_station { |
| 15 | |
| 16 | // Represents a feature of a joystick (a button or an axis). |
| 17 | // All indices are 1-based. |
| 18 | class JoystickFeature { |
| 19 | public: |
| 20 | JoystickFeature(int joystick, int number) |
| 21 | : joystick_(joystick), number_(number) {} |
| 22 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 23 | // How many joysticks there are. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 24 | static const int kJoysticks = 6; |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 25 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 26 | // Which joystick number this is (1-based). |
| 27 | int joystick() const { return joystick_; } |
| 28 | // Which feature on joystick() this is (1-based). |
| 29 | int number() const { return number_; } |
| 30 | |
| 31 | private: |
| 32 | const int joystick_, number_; |
| 33 | }; |
| 34 | |
| 35 | // Represents the location of a button. |
| 36 | // Use Data to actually get the value. |
| 37 | // Safe for static initialization. |
| 38 | class ButtonLocation : public JoystickFeature { |
| 39 | public: |
| 40 | ButtonLocation(int joystick, int number) |
| 41 | : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 42 | |
| 43 | // How many buttons there are available on each joystick. |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 44 | static const int kButtons = 16; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 47 | // Represents the direction of a POV on a joystick. |
| 48 | // Use Data to actually get the value. |
| 49 | // Safe for static initialization. |
| 50 | class POVLocation : public JoystickFeature { |
| 51 | public: |
| 52 | POVLocation(int joystick, int number) |
| 53 | : JoystickFeature(joystick, number) {} |
| 54 | }; |
| 55 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 56 | // Represents various bits of control information that the DS sends. |
| 57 | // Use Data to actually get the value. |
| 58 | enum class ControlBit { |
| 59 | kTestMode, kFmsAttached, kAutonomous, kEnabled |
| 60 | }; |
| 61 | |
| 62 | // Represents a single axis of a joystick. |
| 63 | // Use Data to actually get the value. |
| 64 | // Safe for static initialization. |
| 65 | class JoystickAxis : public JoystickFeature { |
| 66 | public: |
| 67 | JoystickAxis(int joystick, int number) |
| 68 | : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 69 | |
| 70 | // How many axes there are available on each joystick. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 71 | static const int kAxes = 6; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | class Data { |
| 75 | public: |
| 76 | // Initializes the data to all buttons and control bits off and all joysticks |
| 77 | // at 0. |
| 78 | Data(); |
| 79 | |
| 80 | // Updates the current information with a new set of values. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 81 | void Update(const JoystickState *new_values); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 82 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 83 | bool IsPressed(POVLocation location) const; |
| 84 | bool PosEdge(POVLocation location) const; |
| 85 | bool NegEdge(POVLocation location) const; |
| 86 | |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame] | 87 | // Returns the current and previous "values" for the POV. |
| 88 | int32_t GetPOV(int joystick) const; |
| 89 | int32_t GetOldPOV(int joystick) const; |
| 90 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 91 | bool IsPressed(ButtonLocation location) const; |
| 92 | bool PosEdge(ButtonLocation location) const; |
| 93 | bool NegEdge(ButtonLocation location) const; |
| 94 | |
| 95 | bool GetControlBit(ControlBit bit) const; |
| 96 | bool PosEdge(ControlBit bit) const; |
| 97 | bool NegEdge(ControlBit bit) const; |
| 98 | |
| 99 | // Returns the value in the range [-1.0, 1.0]. |
| 100 | float GetAxis(JoystickAxis axis) const; |
| 101 | |
| 102 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 103 | struct SavedJoystickState { |
| 104 | struct SavedJoystick { |
| 105 | uint16_t buttons = 0; |
| 106 | std::array<double, JoystickAxis::kAxes> axis; |
| 107 | int pov = 0; |
| 108 | }; |
| 109 | |
| 110 | std::array<SavedJoystick, JoystickFeature::kJoysticks> joysticks; |
| 111 | bool test_mode = false; |
| 112 | bool fms_attached = false; |
| 113 | bool enabled = false; |
| 114 | bool autonomous = false; |
| 115 | uint16_t team_id = 0; |
| 116 | |
| 117 | // 2018 scale and switch positions. |
| 118 | bool switch_left = false; |
| 119 | bool scale_left = false; |
| 120 | }; |
| 121 | |
| 122 | static bool GetButton(const ButtonLocation location, |
| 123 | const Data::SavedJoystickState &values); |
| 124 | static bool DoGetPOV(const POVLocation location, |
| 125 | const Data::SavedJoystickState &values); |
| 126 | |
| 127 | static bool GetControlBitValue(const ControlBit bit, |
| 128 | const Data::SavedJoystickState &values); |
| 129 | |
| 130 | SavedJoystickState current_values_, old_values_; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace driver_station |
| 134 | } // namespace input |
| 135 | } // namespace aos |
| 136 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 137 | #endif // AOS_INPUT_DRIVER_STATION_DATA_H_ |