blob: a33d449dddec79a8eba038b6ccc9ba08ecab186a [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
Sabina Davis92d2efa2017-11-04 22:35:25 -07004#include <cmath>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cstdio>
6#include <cstring>
Sabina Davis92d2efa2017-11-04 22:35:25 -07007#include <memory>
8
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "frc971/control_loops/control_loops_generated.h"
Sabina Davis82b19182017-11-10 09:30:25 -080011#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070014#include "frc971/input/driver_station_data.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -070015
James Kuszmaul7077d342021-06-09 20:23:58 -070016namespace frc971 {
Sabina Davis92d2efa2017-11-04 22:35:25 -070017namespace input {
18
19// We have a couple different joystick configurations used to drive our skid
20// steer robots. These configurations don't change very often, and there are a
21// small, discrete, set of them. The interface to the drivetrain is the same
22// across all of our drivetrains, so we can abstract that away from our users.
23//
24// We want to be able to re-use that code across years, and switch joystick
25// types quickly and efficiently.
26//
27// DrivetrainInputReader is the abstract base class which provides a consistent
28// API to control drivetrains.
29//
30// To construct the appropriate DrivetrainInputReader, call
31// DrivetrainInputReader::Make with the input type enum.
32// To use it, call HandleDrivetrain(data) every time a joystick packet is
33// received.
34//
35// Base class for the interface to the drivetrain implemented by multiple
36// joystick types.
37class DrivetrainInputReader {
38 public:
James Kuszmaul8bad2412019-03-10 10:47:56 -070039 // What to use the turn1/2 buttons for.
40 enum class TurnButtonUse {
41 // Use the button to enable control loop driving.
42 kControlLoopDriving,
43 // Use the button to set line following mode.
44 kLineFollow,
45 };
Sabina Davis92d2efa2017-11-04 22:35:25 -070046 // Inputs driver station button and joystick locations
Austin Schuhbd0a40f2019-06-30 14:56:31 -070047 DrivetrainInputReader(::aos::EventLoop *event_loop,
48 driver_station::JoystickAxis wheel,
Austin Schuh2b1fce02018-03-02 20:05:20 -080049 driver_station::JoystickAxis throttle,
Sabina Davis82b19182017-11-10 09:30:25 -080050 driver_station::ButtonLocation quick_turn,
51 driver_station::ButtonLocation turn1,
James Kuszmaul8bad2412019-03-10 10:47:56 -070052 TurnButtonUse turn1_use,
53 driver_station::ButtonLocation turn2,
54 TurnButtonUse turn2_use)
Austin Schuh2b1fce02018-03-02 20:05:20 -080055 : wheel_(wheel),
56 throttle_(throttle),
Sabina Davis82b19182017-11-10 09:30:25 -080057 quick_turn_(quick_turn),
58 turn1_(turn1),
James Kuszmaul8bad2412019-03-10 10:47:56 -070059 turn1_use_(turn1_use),
60 turn2_(turn2),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070061 turn2_use_(turn2_use),
62 drivetrain_status_fetcher_(
63 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070064 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
65 "/drivetrain")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070066 drivetrain_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070067 event_loop->MakeSender<::frc971::control_loops::drivetrain::Goal>(
68 "/drivetrain")) {}
Sabina Davis92d2efa2017-11-04 22:35:25 -070069
70 virtual ~DrivetrainInputReader() = default;
71
72 // List of driver joystick types.
73 enum class InputType {
74 kSteeringWheel,
75 kPistol,
76 kXbox,
77 };
78
79 // Constructs the appropriate DrivetrainInputReader.
Sabina Davis82b19182017-11-10 09:30:25 -080080 static std::unique_ptr<DrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -070081 ::aos::EventLoop *event_loop, InputType type,
Austin Schuhbcce26a2018-03-26 23:41:24 -070082 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
83 &dt_config);
Sabina Davis92d2efa2017-11-04 22:35:25 -070084
85 // Processes new joystick data and publishes drivetrain goal messages.
James Kuszmaul7077d342021-06-09 20:23:58 -070086 void HandleDrivetrain(const ::frc971::input::driver_station::Data &data);
Sabina Davis92d2efa2017-11-04 22:35:25 -070087
88 // Sets the scalar for the steering wheel for closed loop mode converting
89 // steering ratio to meters displacement on the two wheels.
90 void set_wheel_multiplier(double wheel_multiplier) {
91 wheel_multiplier_ = wheel_multiplier;
92 }
93
94 // Returns the current robot velocity in m/s.
95 double robot_velocity() const { return robot_velocity_; }
96
Austin Schuh0b00c862021-10-17 17:39:10 -070097 // Returns the current drivetrain status.
98 const control_loops::drivetrain::Status *drivetrain_status() const {
99 return drivetrain_status_fetcher_.get();
100 }
101
Austin Schuha250b2d2019-05-27 16:14:02 -0700102 void set_vision_align_fn(
James Kuszmaul7077d342021-06-09 20:23:58 -0700103 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -0700104 vision_align_fn) {
105 vision_align_fn_ = vision_align_fn;
106 }
107
Sabina Davis92d2efa2017-11-04 22:35:25 -0700108 protected:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800109 const driver_station::JoystickAxis wheel_;
110 const driver_station::JoystickAxis throttle_;
Sabina Davis82b19182017-11-10 09:30:25 -0800111 const driver_station::ButtonLocation quick_turn_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700112 // Button for enabling control loop driving.
Sabina Davis82b19182017-11-10 09:30:25 -0800113 const driver_station::ButtonLocation turn1_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700114 const TurnButtonUse turn1_use_;
115 // But for enabling line following.
Sabina Davis82b19182017-11-10 09:30:25 -0800116 const driver_station::ButtonLocation turn2_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700117 const TurnButtonUse turn2_use_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700118
Austin Schuh48d3a962019-03-17 18:12:32 -0700119 bool last_is_control_loop_driving_ = false;
120
Sabina Davis92d2efa2017-11-04 22:35:25 -0700121 // Structure containing the (potentially adjusted) steering and throttle
122 // values from the joysticks.
123 struct WheelAndThrottle {
124 double wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800125 double wheel_velocity;
126 double wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700127 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800128 double throttle_velocity;
129 double throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -0800130 bool high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700131 };
132
133 private:
134 // Computes the steering and throttle from the provided driverstation data.
135 virtual WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700136 const ::frc971::input::driver_station::Data &data) = 0;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700137
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700139 drivetrain_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 ::aos::Sender<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700141 drivetrain_goal_sender_;
142
Sabina Davis92d2efa2017-11-04 22:35:25 -0700143 double robot_velocity_ = 0.0;
144 // Goals to send to the drivetrain in closed loop mode.
145 double left_goal_ = 0.0;
146 double right_goal_ = 0.0;
147 // The scale for the joysticks for closed loop mode converting
148 // joysticks to meters displacement on the two wheels.
149 double wheel_multiplier_ = 0.5;
Austin Schuha250b2d2019-05-27 16:14:02 -0700150
James Kuszmaul7077d342021-06-09 20:23:58 -0700151 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -0700152 vision_align_fn_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700153};
154
155// Implements DrivetrainInputReader for the original steering wheel.
156class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader {
157 public:
158 using DrivetrainInputReader::DrivetrainInputReader;
159
160 // Creates a DrivetrainInputReader with the corresponding joystick ports and
161 // axis for the big steering wheel and throttle stick.
Sabina Davis82b19182017-11-10 09:30:25 -0800162 static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700163 ::aos::EventLoop *event_loop, bool default_high_gear);
Sabina Davis82b19182017-11-10 09:30:25 -0800164
165 // Sets the default shifter position
166 void set_default_high_gear(bool default_high_gear) {
167 high_gear_ = default_high_gear;
168 default_high_gear_ = default_high_gear;
169 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700170
171 private:
172 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700173 const ::frc971::input::driver_station::Data &data) override;
Sabina Davis82b19182017-11-10 09:30:25 -0800174 bool high_gear_;
175 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700176};
177
178class PistolDrivetrainInputReader : public DrivetrainInputReader {
179 public:
180 using DrivetrainInputReader::DrivetrainInputReader;
181
James Kuszmaul8bad2412019-03-10 10:47:56 -0700182 // What to use the top two buttons for on the pistol grip.
183 enum class TopButtonUse {
184 // Normal shifting.
185 kShift,
186 // Line following (currently just uses top button).
187 kLineFollow,
188 };
189
Sabina Davis92d2efa2017-11-04 22:35:25 -0700190 // Creates a DrivetrainInputReader with the corresponding joystick ports and
191 // axis for the (cheap) pistol grip controller.
James Kuszmaul8bad2412019-03-10 10:47:56 -0700192 static std::unique_ptr<PistolDrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700193 ::aos::EventLoop *event_loop, bool default_high_gear,
194 TopButtonUse top_button_use);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700195
196 private:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800197 PistolDrivetrainInputReader(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700198 ::aos::EventLoop *event_loop, driver_station::JoystickAxis wheel_high,
Austin Schuh2b1fce02018-03-02 20:05:20 -0800199 driver_station::JoystickAxis wheel_low,
200 driver_station::JoystickAxis wheel_velocity_high,
201 driver_station::JoystickAxis wheel_velocity_low,
202 driver_station::JoystickAxis wheel_torque_high,
203 driver_station::JoystickAxis wheel_torque_low,
204 driver_station::JoystickAxis throttle_high,
205 driver_station::JoystickAxis throttle_low,
206 driver_station::JoystickAxis throttle_velocity_high,
207 driver_station::JoystickAxis throttle_velocity_low,
208 driver_station::JoystickAxis throttle_torque_high,
209 driver_station::JoystickAxis throttle_torque_low,
210 driver_station::ButtonLocation quick_turn,
211 driver_station::ButtonLocation shift_high,
212 driver_station::ButtonLocation shift_low,
213 driver_station::ButtonLocation turn1,
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700214 driver_station::ButtonLocation turn2,
215 driver_station::ButtonLocation slow_down)
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700216 : DrivetrainInputReader(event_loop, wheel_high, throttle_high, quick_turn,
217 turn1, TurnButtonUse::kLineFollow, turn2,
Austin Schuh48d3a962019-03-17 18:12:32 -0700218 TurnButtonUse::kControlLoopDriving),
Austin Schuh2b1fce02018-03-02 20:05:20 -0800219 wheel_low_(wheel_low),
220 wheel_velocity_high_(wheel_velocity_high),
221 wheel_velocity_low_(wheel_velocity_low),
222 wheel_torque_high_(wheel_torque_high),
223 wheel_torque_low_(wheel_torque_low),
224 throttle_low_(throttle_low),
225 throttle_velocity_high_(throttle_velocity_high),
226 throttle_velocity_low_(throttle_velocity_low),
227 throttle_torque_high_(throttle_torque_high),
228 throttle_torque_low_(throttle_torque_low),
229 shift_high_(shift_high),
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700230 shift_low_(shift_low),
231 slow_down_(slow_down) {}
Austin Schuh2b1fce02018-03-02 20:05:20 -0800232
Sabina Davis92d2efa2017-11-04 22:35:25 -0700233 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700234 const ::frc971::input::driver_station::Data &data) override;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800235
236 // Sets the default shifter position
237 void set_default_high_gear(bool default_high_gear) {
238 high_gear_ = default_high_gear;
239 default_high_gear_ = default_high_gear;
240 }
241
242 const driver_station::JoystickAxis wheel_low_;
243 const driver_station::JoystickAxis wheel_velocity_high_;
244 const driver_station::JoystickAxis wheel_velocity_low_;
245 const driver_station::JoystickAxis wheel_torque_high_;
246 const driver_station::JoystickAxis wheel_torque_low_;
247
248 const driver_station::JoystickAxis throttle_low_;
249 const driver_station::JoystickAxis throttle_velocity_high_;
250 const driver_station::JoystickAxis throttle_velocity_low_;
251 const driver_station::JoystickAxis throttle_torque_high_;
252 const driver_station::JoystickAxis throttle_torque_low_;
253
254 const driver_station::ButtonLocation shift_high_;
255 const driver_station::ButtonLocation shift_low_;
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700256 const driver_station::ButtonLocation slow_down_;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800257
258 bool high_gear_;
259 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700260};
261
262class XboxDrivetrainInputReader : public DrivetrainInputReader {
263 public:
264 using DrivetrainInputReader::DrivetrainInputReader;
265
266 // Creates a DrivetrainInputReader with the corresponding joystick ports and
267 // axis for the Xbox controller.
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700268 static std::unique_ptr<XboxDrivetrainInputReader> Make(
269 ::aos::EventLoop *event_loop);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700270
271 private:
272 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700273 const ::frc971::input::driver_station::Data &data) override;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700274};
275
276} // namespace input
James Kuszmaul7077d342021-06-09 20:23:58 -0700277} // namespace frc971
Sabina Davis92d2efa2017-11-04 22:35:25 -0700278
279#endif // AOS_INPUT_DRIVETRAIN_INPUT_H_