blob: 18eb579e73c41971958a9f3a5e2ca61b1120af22 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_INPUT_DRIVER_STATION_DATA_H_
2#define AOS_INPUT_DRIVER_STATION_DATA_H_
Brian Silvermanba3de7e2013-05-08 16:18:15 -07003
4// This file defines several types to support nicely looking at the data
5// received from the driver's station.
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include <array>
Brian Silvermanba3de7e2013-05-08 16:18:15 -07008#include <memory>
9
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/robot_state/joystick_state_generated.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -070011
12namespace aos {
13namespace input {
14namespace driver_station {
15
16// Represents a feature of a joystick (a button or an axis).
17// All indices are 1-based.
18class JoystickFeature {
19 public:
20 JoystickFeature(int joystick, int number)
21 : joystick_(joystick), number_(number) {}
22
Brian Silvermanc25bc892013-05-09 19:09:34 -070023 // How many joysticks there are.
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 static const int kJoysticks = 6;
Brian Silvermanc25bc892013-05-09 19:09:34 -070025
Brian Silvermanba3de7e2013-05-08 16:18:15 -070026 // 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.
38class ButtonLocation : public JoystickFeature {
39 public:
40 ButtonLocation(int joystick, int number)
41 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070042
43 // How many buttons there are available on each joystick.
Austin Schuh75c6af42018-03-09 21:16:07 -080044 static const int kButtons = 16;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070045};
46
Austin Schuhfee2e602015-03-08 18:26:05 -070047// Represents the direction of a POV on a joystick.
48// Use Data to actually get the value.
49// Safe for static initialization.
50class POVLocation : public JoystickFeature {
51 public:
52 POVLocation(int joystick, int number)
53 : JoystickFeature(joystick, number) {}
54};
55
Brian Silvermanba3de7e2013-05-08 16:18:15 -070056// Represents various bits of control information that the DS sends.
57// Use Data to actually get the value.
58enum 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.
65class JoystickAxis : public JoystickFeature {
66 public:
67 JoystickAxis(int joystick, int number)
68 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070069
70 // How many axes there are available on each joystick.
Alex Perrycb7da4b2019-08-28 19:35:56 -070071 static const int kAxes = 6;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070072};
73
74class 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 Perrycb7da4b2019-08-28 19:35:56 -070081 void Update(const JoystickState *new_values);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070082
Austin Schuhfee2e602015-03-08 18:26:05 -070083 bool IsPressed(POVLocation location) const;
84 bool PosEdge(POVLocation location) const;
85 bool NegEdge(POVLocation location) const;
86
Brian Silverman7b9ab672015-03-14 23:41:41 -070087 // 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 Silvermanba3de7e2013-05-08 16:18:15 -070091 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 Perrycb7da4b2019-08-28 19:35:56 -0700103 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 Silvermanba3de7e2013-05-08 16:18:15 -0700131};
132
133} // namespace driver_station
134} // namespace input
135} // namespace aos
136
John Park33858a32018-09-28 23:05:48 -0700137#endif // AOS_INPUT_DRIVER_STATION_DATA_H_