blob: ec52368b58d612d58ac1426bee2d51e839269665 [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
John Park33858a32018-09-28 23:05:48 -070010#include "aos/input/driver_station_data.h"
11#include "aos/logging/logging.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -070012#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:
James Kuszmaul8bad2412019-03-10 10:47:56 -070038 // What to use the turn1/2 buttons for.
39 enum class TurnButtonUse {
40 // Use the button to enable control loop driving.
41 kControlLoopDriving,
42 // Use the button to set line following mode.
43 kLineFollow,
44 };
Sabina Davis92d2efa2017-11-04 22:35:25 -070045 // Inputs driver station button and joystick locations
Austin Schuh2b1fce02018-03-02 20:05:20 -080046 DrivetrainInputReader(driver_station::JoystickAxis wheel,
47 driver_station::JoystickAxis throttle,
Sabina Davis82b19182017-11-10 09:30:25 -080048 driver_station::ButtonLocation quick_turn,
49 driver_station::ButtonLocation turn1,
James Kuszmaul8bad2412019-03-10 10:47:56 -070050 TurnButtonUse turn1_use,
51 driver_station::ButtonLocation turn2,
52 TurnButtonUse turn2_use)
Austin Schuh2b1fce02018-03-02 20:05:20 -080053 : wheel_(wheel),
54 throttle_(throttle),
Sabina Davis82b19182017-11-10 09:30:25 -080055 quick_turn_(quick_turn),
56 turn1_(turn1),
James Kuszmaul8bad2412019-03-10 10:47:56 -070057 turn1_use_(turn1_use),
58 turn2_(turn2),
59 turn2_use_(turn2_use) {}
Sabina Davis92d2efa2017-11-04 22:35:25 -070060
61 virtual ~DrivetrainInputReader() = default;
62
63 // List of driver joystick types.
64 enum class InputType {
65 kSteeringWheel,
66 kPistol,
67 kXbox,
68 };
69
70 // Constructs the appropriate DrivetrainInputReader.
Sabina Davis82b19182017-11-10 09:30:25 -080071 static std::unique_ptr<DrivetrainInputReader> Make(
72 InputType type,
Austin Schuhbcce26a2018-03-26 23:41:24 -070073 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
74 &dt_config);
Sabina Davis92d2efa2017-11-04 22:35:25 -070075
76 // Processes new joystick data and publishes drivetrain goal messages.
77 void HandleDrivetrain(const ::aos::input::driver_station::Data &data);
78
79 // Sets the scalar for the steering wheel for closed loop mode converting
80 // steering ratio to meters displacement on the two wheels.
81 void set_wheel_multiplier(double wheel_multiplier) {
82 wheel_multiplier_ = wheel_multiplier;
83 }
84
85 // Returns the current robot velocity in m/s.
86 double robot_velocity() const { return robot_velocity_; }
87
88 protected:
Austin Schuh2b1fce02018-03-02 20:05:20 -080089 const driver_station::JoystickAxis wheel_;
90 const driver_station::JoystickAxis throttle_;
Sabina Davis82b19182017-11-10 09:30:25 -080091 const driver_station::ButtonLocation quick_turn_;
James Kuszmaul8bad2412019-03-10 10:47:56 -070092 // Button for enabling control loop driving.
Sabina Davis82b19182017-11-10 09:30:25 -080093 const driver_station::ButtonLocation turn1_;
James Kuszmaul8bad2412019-03-10 10:47:56 -070094 const TurnButtonUse turn1_use_;
95 // But for enabling line following.
Sabina Davis82b19182017-11-10 09:30:25 -080096 const driver_station::ButtonLocation turn2_;
James Kuszmaul8bad2412019-03-10 10:47:56 -070097 const TurnButtonUse turn2_use_;
Sabina Davis92d2efa2017-11-04 22:35:25 -070098
Austin Schuh48d3a962019-03-17 18:12:32 -070099 bool last_is_control_loop_driving_ = false;
100
Sabina Davis92d2efa2017-11-04 22:35:25 -0700101 // Structure containing the (potentially adjusted) steering and throttle
102 // values from the joysticks.
103 struct WheelAndThrottle {
104 double wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800105 double wheel_velocity;
106 double wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700107 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800108 double throttle_velocity;
109 double throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -0800110 bool high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700111 };
112
113 private:
114 // Computes the steering and throttle from the provided driverstation data.
115 virtual WheelAndThrottle GetWheelAndThrottle(
116 const ::aos::input::driver_station::Data &data) = 0;
117
118 double robot_velocity_ = 0.0;
119 // Goals to send to the drivetrain in closed loop mode.
120 double left_goal_ = 0.0;
121 double right_goal_ = 0.0;
122 // The scale for the joysticks for closed loop mode converting
123 // joysticks to meters displacement on the two wheels.
124 double wheel_multiplier_ = 0.5;
125};
126
127// Implements DrivetrainInputReader for the original steering wheel.
128class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader {
129 public:
130 using DrivetrainInputReader::DrivetrainInputReader;
131
132 // Creates a DrivetrainInputReader with the corresponding joystick ports and
133 // axis for the big steering wheel and throttle stick.
Sabina Davis82b19182017-11-10 09:30:25 -0800134 static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(
135 bool default_high_gear);
136
137 // Sets the default shifter position
138 void set_default_high_gear(bool default_high_gear) {
139 high_gear_ = default_high_gear;
140 default_high_gear_ = default_high_gear;
141 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700142
143 private:
144 WheelAndThrottle GetWheelAndThrottle(
145 const ::aos::input::driver_station::Data &data) override;
Sabina Davis82b19182017-11-10 09:30:25 -0800146 bool high_gear_;
147 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700148};
149
150class PistolDrivetrainInputReader : public DrivetrainInputReader {
151 public:
152 using DrivetrainInputReader::DrivetrainInputReader;
153
James Kuszmaul8bad2412019-03-10 10:47:56 -0700154 // What to use the top two buttons for on the pistol grip.
155 enum class TopButtonUse {
156 // Normal shifting.
157 kShift,
158 // Line following (currently just uses top button).
159 kLineFollow,
160 };
161
Sabina Davis92d2efa2017-11-04 22:35:25 -0700162 // Creates a DrivetrainInputReader with the corresponding joystick ports and
163 // axis for the (cheap) pistol grip controller.
James Kuszmaul8bad2412019-03-10 10:47:56 -0700164 static std::unique_ptr<PistolDrivetrainInputReader> Make(
165 bool default_high_gear, TopButtonUse top_button_use);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700166
167 private:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800168 PistolDrivetrainInputReader(
169 driver_station::JoystickAxis wheel_high,
170 driver_station::JoystickAxis wheel_low,
171 driver_station::JoystickAxis wheel_velocity_high,
172 driver_station::JoystickAxis wheel_velocity_low,
173 driver_station::JoystickAxis wheel_torque_high,
174 driver_station::JoystickAxis wheel_torque_low,
175 driver_station::JoystickAxis throttle_high,
176 driver_station::JoystickAxis throttle_low,
177 driver_station::JoystickAxis throttle_velocity_high,
178 driver_station::JoystickAxis throttle_velocity_low,
179 driver_station::JoystickAxis throttle_torque_high,
180 driver_station::JoystickAxis throttle_torque_low,
181 driver_station::ButtonLocation quick_turn,
182 driver_station::ButtonLocation shift_high,
183 driver_station::ButtonLocation shift_low,
184 driver_station::ButtonLocation turn1,
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700185 driver_station::ButtonLocation turn2,
186 driver_station::ButtonLocation slow_down)
Austin Schuh2b1fce02018-03-02 20:05:20 -0800187 : DrivetrainInputReader(wheel_high, throttle_high, quick_turn, turn1,
James Kuszmaul8bad2412019-03-10 10:47:56 -0700188 TurnButtonUse::kLineFollow, turn2,
Austin Schuh48d3a962019-03-17 18:12:32 -0700189 TurnButtonUse::kControlLoopDriving),
Austin Schuh2b1fce02018-03-02 20:05:20 -0800190 wheel_low_(wheel_low),
191 wheel_velocity_high_(wheel_velocity_high),
192 wheel_velocity_low_(wheel_velocity_low),
193 wheel_torque_high_(wheel_torque_high),
194 wheel_torque_low_(wheel_torque_low),
195 throttle_low_(throttle_low),
196 throttle_velocity_high_(throttle_velocity_high),
197 throttle_velocity_low_(throttle_velocity_low),
198 throttle_torque_high_(throttle_torque_high),
199 throttle_torque_low_(throttle_torque_low),
200 shift_high_(shift_high),
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700201 shift_low_(shift_low),
202 slow_down_(slow_down) {}
Austin Schuh2b1fce02018-03-02 20:05:20 -0800203
Sabina Davis92d2efa2017-11-04 22:35:25 -0700204 WheelAndThrottle GetWheelAndThrottle(
205 const ::aos::input::driver_station::Data &data) override;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800206
207 // Sets the default shifter position
208 void set_default_high_gear(bool default_high_gear) {
209 high_gear_ = default_high_gear;
210 default_high_gear_ = default_high_gear;
211 }
212
213 const driver_station::JoystickAxis wheel_low_;
214 const driver_station::JoystickAxis wheel_velocity_high_;
215 const driver_station::JoystickAxis wheel_velocity_low_;
216 const driver_station::JoystickAxis wheel_torque_high_;
217 const driver_station::JoystickAxis wheel_torque_low_;
218
219 const driver_station::JoystickAxis throttle_low_;
220 const driver_station::JoystickAxis throttle_velocity_high_;
221 const driver_station::JoystickAxis throttle_velocity_low_;
222 const driver_station::JoystickAxis throttle_torque_high_;
223 const driver_station::JoystickAxis throttle_torque_low_;
224
225 const driver_station::ButtonLocation shift_high_;
226 const driver_station::ButtonLocation shift_low_;
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700227 const driver_station::ButtonLocation slow_down_;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800228
229 bool high_gear_;
230 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700231};
232
233class XboxDrivetrainInputReader : public DrivetrainInputReader {
234 public:
235 using DrivetrainInputReader::DrivetrainInputReader;
236
237 // Creates a DrivetrainInputReader with the corresponding joystick ports and
238 // axis for the Xbox controller.
239 static std::unique_ptr<XboxDrivetrainInputReader> Make();
240
241 private:
242 WheelAndThrottle GetWheelAndThrottle(
243 const ::aos::input::driver_station::Data &data) override;
244};
245
246} // namespace input
247} // namespace aos
248
249#endif // AOS_INPUT_DRIVETRAIN_INPUT_H_