blob: 173c551fa640d6c4f6d42dd9742ebda259afe945 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080016namespace frc971::input {
Sabina Davis92d2efa2017-11-04 22:35:25 -070017
Maxwell Henderson24f89f32023-03-25 15:55:46 -070018using control_loops::drivetrain::PistolBottomButtonUse;
19using control_loops::drivetrain::PistolSecondButtonUse;
20using control_loops::drivetrain::PistolTopButtonUse;
21
Sabina Davis92d2efa2017-11-04 22:35:25 -070022// We have a couple different joystick configurations used to drive our skid
23// steer robots. These configurations don't change very often, and there are a
24// small, discrete, set of them. The interface to the drivetrain is the same
25// across all of our drivetrains, so we can abstract that away from our users.
26//
27// We want to be able to re-use that code across years, and switch joystick
28// types quickly and efficiently.
29//
30// DrivetrainInputReader is the abstract base class which provides a consistent
31// API to control drivetrains.
32//
33// To construct the appropriate DrivetrainInputReader, call
34// DrivetrainInputReader::Make with the input type enum.
35// To use it, call HandleDrivetrain(data) every time a joystick packet is
36// received.
37//
38// Base class for the interface to the drivetrain implemented by multiple
39// joystick types.
40class DrivetrainInputReader {
41 public:
James Kuszmaul8bad2412019-03-10 10:47:56 -070042 // What to use the turn1/2 buttons for.
43 enum class TurnButtonUse {
44 // Use the button to enable control loop driving.
45 kControlLoopDriving,
46 // Use the button to set line following mode.
47 kLineFollow,
48 };
Sabina Davis92d2efa2017-11-04 22:35:25 -070049 // Inputs driver station button and joystick locations
Austin Schuhbd0a40f2019-06-30 14:56:31 -070050 DrivetrainInputReader(::aos::EventLoop *event_loop,
51 driver_station::JoystickAxis wheel,
Austin Schuh2b1fce02018-03-02 20:05:20 -080052 driver_station::JoystickAxis throttle,
Sabina Davis82b19182017-11-10 09:30:25 -080053 driver_station::ButtonLocation quick_turn,
54 driver_station::ButtonLocation turn1,
James Kuszmaul8bad2412019-03-10 10:47:56 -070055 TurnButtonUse turn1_use,
56 driver_station::ButtonLocation turn2,
57 TurnButtonUse turn2_use)
Austin Schuh2b1fce02018-03-02 20:05:20 -080058 : wheel_(wheel),
59 throttle_(throttle),
Sabina Davis82b19182017-11-10 09:30:25 -080060 quick_turn_(quick_turn),
61 turn1_(turn1),
James Kuszmaul8bad2412019-03-10 10:47:56 -070062 turn1_use_(turn1_use),
63 turn2_(turn2),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070064 turn2_use_(turn2_use),
65 drivetrain_status_fetcher_(
66 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070067 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
68 "/drivetrain")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070069 drivetrain_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 event_loop->MakeSender<::frc971::control_loops::drivetrain::Goal>(
71 "/drivetrain")) {}
Sabina Davis92d2efa2017-11-04 22:35:25 -070072
73 virtual ~DrivetrainInputReader() = default;
74
75 // List of driver joystick types.
76 enum class InputType {
77 kSteeringWheel,
78 kPistol,
79 kXbox,
80 };
81
82 // Constructs the appropriate DrivetrainInputReader.
Sabina Davis82b19182017-11-10 09:30:25 -080083 static std::unique_ptr<DrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -070084 ::aos::EventLoop *event_loop, InputType type,
Austin Schuhbcce26a2018-03-26 23:41:24 -070085 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
86 &dt_config);
Sabina Davis92d2efa2017-11-04 22:35:25 -070087
88 // Processes new joystick data and publishes drivetrain goal messages.
James Kuszmaul7077d342021-06-09 20:23:58 -070089 void HandleDrivetrain(const ::frc971::input::driver_station::Data &data);
Sabina Davis92d2efa2017-11-04 22:35:25 -070090
91 // Sets the scalar for the steering wheel for closed loop mode converting
92 // steering ratio to meters displacement on the two wheels.
93 void set_wheel_multiplier(double wheel_multiplier) {
94 wheel_multiplier_ = wheel_multiplier;
95 }
96
97 // Returns the current robot velocity in m/s.
98 double robot_velocity() const { return robot_velocity_; }
99
Austin Schuh0b00c862021-10-17 17:39:10 -0700100 // Returns the current drivetrain status.
101 const control_loops::drivetrain::Status *drivetrain_status() const {
102 return drivetrain_status_fetcher_.get();
103 }
104
Austin Schuha250b2d2019-05-27 16:14:02 -0700105 void set_vision_align_fn(
James Kuszmaul7077d342021-06-09 20:23:58 -0700106 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -0700107 vision_align_fn) {
108 vision_align_fn_ = vision_align_fn;
109 }
110
Sabina Davis92d2efa2017-11-04 22:35:25 -0700111 protected:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800112 const driver_station::JoystickAxis wheel_;
113 const driver_station::JoystickAxis throttle_;
Sabina Davis82b19182017-11-10 09:30:25 -0800114 const driver_station::ButtonLocation quick_turn_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700115 // Button for enabling control loop driving.
Sabina Davis82b19182017-11-10 09:30:25 -0800116 const driver_station::ButtonLocation turn1_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700117 const TurnButtonUse turn1_use_;
118 // But for enabling line following.
Sabina Davis82b19182017-11-10 09:30:25 -0800119 const driver_station::ButtonLocation turn2_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700120 const TurnButtonUse turn2_use_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700121
Austin Schuh48d3a962019-03-17 18:12:32 -0700122 bool last_is_control_loop_driving_ = false;
123
Sabina Davis92d2efa2017-11-04 22:35:25 -0700124 // Structure containing the (potentially adjusted) steering and throttle
125 // values from the joysticks.
126 struct WheelAndThrottle {
127 double wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800128 double wheel_velocity;
129 double wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700130 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800131 double throttle_velocity;
132 double throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -0800133 bool high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700134 };
135
136 private:
137 // Computes the steering and throttle from the provided driverstation data.
138 virtual WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700139 const ::frc971::input::driver_station::Data &data) = 0;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700140
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700142 drivetrain_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 ::aos::Sender<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700144 drivetrain_goal_sender_;
145
Sabina Davis92d2efa2017-11-04 22:35:25 -0700146 double robot_velocity_ = 0.0;
147 // Goals to send to the drivetrain in closed loop mode.
148 double left_goal_ = 0.0;
149 double right_goal_ = 0.0;
150 // The scale for the joysticks for closed loop mode converting
151 // joysticks to meters displacement on the two wheels.
152 double wheel_multiplier_ = 0.5;
Austin Schuha250b2d2019-05-27 16:14:02 -0700153
James Kuszmaul7077d342021-06-09 20:23:58 -0700154 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -0700155 vision_align_fn_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700156};
157
158// Implements DrivetrainInputReader for the original steering wheel.
159class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader {
160 public:
161 using DrivetrainInputReader::DrivetrainInputReader;
162
163 // Creates a DrivetrainInputReader with the corresponding joystick ports and
164 // axis for the big steering wheel and throttle stick.
Sabina Davis82b19182017-11-10 09:30:25 -0800165 static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700166 ::aos::EventLoop *event_loop, bool default_high_gear);
Sabina Davis82b19182017-11-10 09:30:25 -0800167
168 // Sets the default shifter position
169 void set_default_high_gear(bool default_high_gear) {
170 high_gear_ = default_high_gear;
171 default_high_gear_ = default_high_gear;
172 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700173
174 private:
175 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700176 const ::frc971::input::driver_station::Data &data) override;
Sabina Davis82b19182017-11-10 09:30:25 -0800177 bool high_gear_;
178 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700179};
180
181class PistolDrivetrainInputReader : public DrivetrainInputReader {
182 public:
183 using DrivetrainInputReader::DrivetrainInputReader;
184
185 // Creates a DrivetrainInputReader with the corresponding joystick ports and
186 // axis for the (cheap) pistol grip controller.
James Kuszmaul8bad2412019-03-10 10:47:56 -0700187 static std::unique_ptr<PistolDrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700188 ::aos::EventLoop *event_loop, bool default_high_gear,
Maxwell Henderson24f89f32023-03-25 15:55:46 -0700189 PistolTopButtonUse top_button_use,
190 PistolSecondButtonUse second_button_use,
191 PistolBottomButtonUse bottom_button_use);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700192
193 private:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800194 PistolDrivetrainInputReader(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700195 ::aos::EventLoop *event_loop, driver_station::JoystickAxis wheel_high,
Austin Schuh2b1fce02018-03-02 20:05:20 -0800196 driver_station::JoystickAxis wheel_low,
197 driver_station::JoystickAxis wheel_velocity_high,
198 driver_station::JoystickAxis wheel_velocity_low,
199 driver_station::JoystickAxis wheel_torque_high,
200 driver_station::JoystickAxis wheel_torque_low,
201 driver_station::JoystickAxis throttle_high,
202 driver_station::JoystickAxis throttle_low,
203 driver_station::JoystickAxis throttle_velocity_high,
204 driver_station::JoystickAxis throttle_velocity_low,
205 driver_station::JoystickAxis throttle_torque_high,
206 driver_station::JoystickAxis throttle_torque_low,
207 driver_station::ButtonLocation quick_turn,
208 driver_station::ButtonLocation shift_high,
209 driver_station::ButtonLocation shift_low,
210 driver_station::ButtonLocation turn1,
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700211 driver_station::ButtonLocation turn2,
212 driver_station::ButtonLocation slow_down)
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700213 : DrivetrainInputReader(event_loop, wheel_high, throttle_high, quick_turn,
214 turn1, TurnButtonUse::kLineFollow, turn2,
Austin Schuh48d3a962019-03-17 18:12:32 -0700215 TurnButtonUse::kControlLoopDriving),
Austin Schuh2b1fce02018-03-02 20:05:20 -0800216 wheel_low_(wheel_low),
217 wheel_velocity_high_(wheel_velocity_high),
218 wheel_velocity_low_(wheel_velocity_low),
219 wheel_torque_high_(wheel_torque_high),
220 wheel_torque_low_(wheel_torque_low),
221 throttle_low_(throttle_low),
222 throttle_velocity_high_(throttle_velocity_high),
223 throttle_velocity_low_(throttle_velocity_low),
224 throttle_torque_high_(throttle_torque_high),
225 throttle_torque_low_(throttle_torque_low),
226 shift_high_(shift_high),
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700227 shift_low_(shift_low),
228 slow_down_(slow_down) {}
Austin Schuh2b1fce02018-03-02 20:05:20 -0800229
Sabina Davis92d2efa2017-11-04 22:35:25 -0700230 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700231 const ::frc971::input::driver_station::Data &data) override;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800232
233 // Sets the default shifter position
234 void set_default_high_gear(bool default_high_gear) {
235 high_gear_ = default_high_gear;
236 default_high_gear_ = default_high_gear;
237 }
238
239 const driver_station::JoystickAxis wheel_low_;
240 const driver_station::JoystickAxis wheel_velocity_high_;
241 const driver_station::JoystickAxis wheel_velocity_low_;
242 const driver_station::JoystickAxis wheel_torque_high_;
243 const driver_station::JoystickAxis wheel_torque_low_;
244
245 const driver_station::JoystickAxis throttle_low_;
246 const driver_station::JoystickAxis throttle_velocity_high_;
247 const driver_station::JoystickAxis throttle_velocity_low_;
248 const driver_station::JoystickAxis throttle_torque_high_;
249 const driver_station::JoystickAxis throttle_torque_low_;
250
251 const driver_station::ButtonLocation shift_high_;
252 const driver_station::ButtonLocation shift_low_;
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700253 const driver_station::ButtonLocation slow_down_;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800254
255 bool high_gear_;
256 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700257};
258
259class XboxDrivetrainInputReader : public DrivetrainInputReader {
260 public:
261 using DrivetrainInputReader::DrivetrainInputReader;
262
263 // Creates a DrivetrainInputReader with the corresponding joystick ports and
264 // axis for the Xbox controller.
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700265 static std::unique_ptr<XboxDrivetrainInputReader> Make(
266 ::aos::EventLoop *event_loop);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700267
268 private:
269 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700270 const ::frc971::input::driver_station::Data &data) override;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700271};
272
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800273} // namespace frc971::input
Sabina Davis92d2efa2017-11-04 22:35:25 -0700274
275#endif // AOS_INPUT_DRIVETRAIN_INPUT_H_