blob: e38320c5e79316c670f2807bee5a87492c77da5a [file] [log] [blame]
Sabina Davis92d2efa2017-11-04 22:35:25 -07001#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 Davis82b19182017-11-10 09:30:25 -080013#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -070014
15namespace aos {
16namespace 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.
36class DrivetrainInputReader {
37 public:
38 // Inputs driver station button and joystick locations
Austin Schuh2b1fce02018-03-02 20:05:20 -080039 DrivetrainInputReader(driver_station::JoystickAxis wheel,
40 driver_station::JoystickAxis throttle,
Sabina Davis82b19182017-11-10 09:30:25 -080041 driver_station::ButtonLocation quick_turn,
42 driver_station::ButtonLocation turn1,
43 driver_station::ButtonLocation turn2)
Austin Schuh2b1fce02018-03-02 20:05:20 -080044 : wheel_(wheel),
45 throttle_(throttle),
Sabina Davis82b19182017-11-10 09:30:25 -080046 quick_turn_(quick_turn),
47 turn1_(turn1),
48 turn2_(turn2) {}
Sabina Davis92d2efa2017-11-04 22:35:25 -070049
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 Davis82b19182017-11-10 09:30:25 -080060 static std::unique_ptr<DrivetrainInputReader> Make(
61 InputType type,
Austin Schuhbcce26a2018-03-26 23:41:24 -070062 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
63 &dt_config);
Sabina Davis92d2efa2017-11-04 22:35:25 -070064
65 // Processes new joystick data and publishes drivetrain goal messages.
66 void HandleDrivetrain(const ::aos::input::driver_station::Data &data);
67
68 // Sets the scalar for the steering wheel for closed loop mode converting
69 // steering ratio to meters displacement on the two wheels.
70 void set_wheel_multiplier(double wheel_multiplier) {
71 wheel_multiplier_ = wheel_multiplier;
72 }
73
74 // Returns the current robot velocity in m/s.
75 double robot_velocity() const { return robot_velocity_; }
76
77 protected:
Austin Schuh2b1fce02018-03-02 20:05:20 -080078 const driver_station::JoystickAxis wheel_;
79 const driver_station::JoystickAxis throttle_;
Sabina Davis82b19182017-11-10 09:30:25 -080080 const driver_station::ButtonLocation quick_turn_;
81 const driver_station::ButtonLocation turn1_;
82 const driver_station::ButtonLocation turn2_;
Sabina Davis92d2efa2017-11-04 22:35:25 -070083
84 // Structure containing the (potentially adjusted) steering and throttle
85 // values from the joysticks.
86 struct WheelAndThrottle {
87 double wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -080088 double wheel_velocity;
89 double wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -070090 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -080091 double throttle_velocity;
92 double throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -080093 bool high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -070094 };
95
96 private:
97 // Computes the steering and throttle from the provided driverstation data.
98 virtual WheelAndThrottle GetWheelAndThrottle(
99 const ::aos::input::driver_station::Data &data) = 0;
100
101 double robot_velocity_ = 0.0;
102 // Goals to send to the drivetrain in closed loop mode.
103 double left_goal_ = 0.0;
104 double right_goal_ = 0.0;
105 // The scale for the joysticks for closed loop mode converting
106 // joysticks to meters displacement on the two wheels.
107 double wheel_multiplier_ = 0.5;
108};
109
110// Implements DrivetrainInputReader for the original steering wheel.
111class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader {
112 public:
113 using DrivetrainInputReader::DrivetrainInputReader;
114
115 // Creates a DrivetrainInputReader with the corresponding joystick ports and
116 // axis for the big steering wheel and throttle stick.
Sabina Davis82b19182017-11-10 09:30:25 -0800117 static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(
118 bool default_high_gear);
119
120 // Sets the default shifter position
121 void set_default_high_gear(bool default_high_gear) {
122 high_gear_ = default_high_gear;
123 default_high_gear_ = default_high_gear;
124 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700125
126 private:
127 WheelAndThrottle GetWheelAndThrottle(
128 const ::aos::input::driver_station::Data &data) override;
Sabina Davis82b19182017-11-10 09:30:25 -0800129 bool high_gear_;
130 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700131};
132
133class PistolDrivetrainInputReader : public DrivetrainInputReader {
134 public:
135 using DrivetrainInputReader::DrivetrainInputReader;
136
137 // Creates a DrivetrainInputReader with the corresponding joystick ports and
138 // axis for the (cheap) pistol grip controller.
Austin Schuh2b1fce02018-03-02 20:05:20 -0800139 static std::unique_ptr<PistolDrivetrainInputReader> Make(bool default_high_gear);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700140
141 private:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800142 PistolDrivetrainInputReader(
143 driver_station::JoystickAxis wheel_high,
144 driver_station::JoystickAxis wheel_low,
145 driver_station::JoystickAxis wheel_velocity_high,
146 driver_station::JoystickAxis wheel_velocity_low,
147 driver_station::JoystickAxis wheel_torque_high,
148 driver_station::JoystickAxis wheel_torque_low,
149 driver_station::JoystickAxis throttle_high,
150 driver_station::JoystickAxis throttle_low,
151 driver_station::JoystickAxis throttle_velocity_high,
152 driver_station::JoystickAxis throttle_velocity_low,
153 driver_station::JoystickAxis throttle_torque_high,
154 driver_station::JoystickAxis throttle_torque_low,
155 driver_station::ButtonLocation quick_turn,
156 driver_station::ButtonLocation shift_high,
157 driver_station::ButtonLocation shift_low,
158 driver_station::ButtonLocation turn1,
159 driver_station::ButtonLocation turn2)
160 : DrivetrainInputReader(wheel_high, throttle_high, quick_turn, turn1,
161 turn2),
162 wheel_low_(wheel_low),
163 wheel_velocity_high_(wheel_velocity_high),
164 wheel_velocity_low_(wheel_velocity_low),
165 wheel_torque_high_(wheel_torque_high),
166 wheel_torque_low_(wheel_torque_low),
167 throttle_low_(throttle_low),
168 throttle_velocity_high_(throttle_velocity_high),
169 throttle_velocity_low_(throttle_velocity_low),
170 throttle_torque_high_(throttle_torque_high),
171 throttle_torque_low_(throttle_torque_low),
172 shift_high_(shift_high),
173 shift_low_(shift_low) {}
174
Sabina Davis92d2efa2017-11-04 22:35:25 -0700175 WheelAndThrottle GetWheelAndThrottle(
176 const ::aos::input::driver_station::Data &data) override;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800177
178 // Sets the default shifter position
179 void set_default_high_gear(bool default_high_gear) {
180 high_gear_ = default_high_gear;
181 default_high_gear_ = default_high_gear;
182 }
183
184 const driver_station::JoystickAxis wheel_low_;
185 const driver_station::JoystickAxis wheel_velocity_high_;
186 const driver_station::JoystickAxis wheel_velocity_low_;
187 const driver_station::JoystickAxis wheel_torque_high_;
188 const driver_station::JoystickAxis wheel_torque_low_;
189
190 const driver_station::JoystickAxis throttle_low_;
191 const driver_station::JoystickAxis throttle_velocity_high_;
192 const driver_station::JoystickAxis throttle_velocity_low_;
193 const driver_station::JoystickAxis throttle_torque_high_;
194 const driver_station::JoystickAxis throttle_torque_low_;
195
196 const driver_station::ButtonLocation shift_high_;
197 const driver_station::ButtonLocation shift_low_;
198
199 bool high_gear_;
200 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700201};
202
203class XboxDrivetrainInputReader : public DrivetrainInputReader {
204 public:
205 using DrivetrainInputReader::DrivetrainInputReader;
206
207 // Creates a DrivetrainInputReader with the corresponding joystick ports and
208 // axis for the Xbox controller.
209 static std::unique_ptr<XboxDrivetrainInputReader> Make();
210
211 private:
212 WheelAndThrottle GetWheelAndThrottle(
213 const ::aos::input::driver_station::Data &data) override;
214};
215
216} // namespace input
217} // namespace aos
218
219#endif // AOS_INPUT_DRIVETRAIN_INPUT_H_