blob: 0cb724df3522b33af9a7205574e02e548f3b21b6 [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
Maxwell Henderson24f89f32023-03-25 15:55:46 -070019using control_loops::drivetrain::PistolBottomButtonUse;
20using control_loops::drivetrain::PistolSecondButtonUse;
21using control_loops::drivetrain::PistolTopButtonUse;
22
Sabina Davis92d2efa2017-11-04 22:35:25 -070023// We have a couple different joystick configurations used to drive our skid
24// steer robots. These configurations don't change very often, and there are a
25// small, discrete, set of them. The interface to the drivetrain is the same
26// across all of our drivetrains, so we can abstract that away from our users.
27//
28// We want to be able to re-use that code across years, and switch joystick
29// types quickly and efficiently.
30//
31// DrivetrainInputReader is the abstract base class which provides a consistent
32// API to control drivetrains.
33//
34// To construct the appropriate DrivetrainInputReader, call
35// DrivetrainInputReader::Make with the input type enum.
36// To use it, call HandleDrivetrain(data) every time a joystick packet is
37// received.
38//
39// Base class for the interface to the drivetrain implemented by multiple
40// joystick types.
41class DrivetrainInputReader {
42 public:
James Kuszmaul8bad2412019-03-10 10:47:56 -070043 // What to use the turn1/2 buttons for.
44 enum class TurnButtonUse {
45 // Use the button to enable control loop driving.
46 kControlLoopDriving,
47 // Use the button to set line following mode.
48 kLineFollow,
49 };
Sabina Davis92d2efa2017-11-04 22:35:25 -070050 // Inputs driver station button and joystick locations
Austin Schuhbd0a40f2019-06-30 14:56:31 -070051 DrivetrainInputReader(::aos::EventLoop *event_loop,
52 driver_station::JoystickAxis wheel,
Austin Schuh2b1fce02018-03-02 20:05:20 -080053 driver_station::JoystickAxis throttle,
Sabina Davis82b19182017-11-10 09:30:25 -080054 driver_station::ButtonLocation quick_turn,
55 driver_station::ButtonLocation turn1,
James Kuszmaul8bad2412019-03-10 10:47:56 -070056 TurnButtonUse turn1_use,
57 driver_station::ButtonLocation turn2,
58 TurnButtonUse turn2_use)
Austin Schuh2b1fce02018-03-02 20:05:20 -080059 : wheel_(wheel),
60 throttle_(throttle),
Sabina Davis82b19182017-11-10 09:30:25 -080061 quick_turn_(quick_turn),
62 turn1_(turn1),
James Kuszmaul8bad2412019-03-10 10:47:56 -070063 turn1_use_(turn1_use),
64 turn2_(turn2),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070065 turn2_use_(turn2_use),
66 drivetrain_status_fetcher_(
67 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
69 "/drivetrain")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070070 drivetrain_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070071 event_loop->MakeSender<::frc971::control_loops::drivetrain::Goal>(
72 "/drivetrain")) {}
Sabina Davis92d2efa2017-11-04 22:35:25 -070073
74 virtual ~DrivetrainInputReader() = default;
75
76 // List of driver joystick types.
77 enum class InputType {
78 kSteeringWheel,
79 kPistol,
80 kXbox,
81 };
82
83 // Constructs the appropriate DrivetrainInputReader.
Sabina Davis82b19182017-11-10 09:30:25 -080084 static std::unique_ptr<DrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -070085 ::aos::EventLoop *event_loop, InputType type,
Austin Schuhbcce26a2018-03-26 23:41:24 -070086 const ::frc971::control_loops::drivetrain::DrivetrainConfig<double>
87 &dt_config);
Sabina Davis92d2efa2017-11-04 22:35:25 -070088
89 // Processes new joystick data and publishes drivetrain goal messages.
James Kuszmaul7077d342021-06-09 20:23:58 -070090 void HandleDrivetrain(const ::frc971::input::driver_station::Data &data);
Sabina Davis92d2efa2017-11-04 22:35:25 -070091
92 // Sets the scalar for the steering wheel for closed loop mode converting
93 // steering ratio to meters displacement on the two wheels.
94 void set_wheel_multiplier(double wheel_multiplier) {
95 wheel_multiplier_ = wheel_multiplier;
96 }
97
98 // Returns the current robot velocity in m/s.
99 double robot_velocity() const { return robot_velocity_; }
100
Austin Schuh0b00c862021-10-17 17:39:10 -0700101 // Returns the current drivetrain status.
102 const control_loops::drivetrain::Status *drivetrain_status() const {
103 return drivetrain_status_fetcher_.get();
104 }
105
Austin Schuha250b2d2019-05-27 16:14:02 -0700106 void set_vision_align_fn(
James Kuszmaul7077d342021-06-09 20:23:58 -0700107 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -0700108 vision_align_fn) {
109 vision_align_fn_ = vision_align_fn;
110 }
111
Sabina Davis92d2efa2017-11-04 22:35:25 -0700112 protected:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800113 const driver_station::JoystickAxis wheel_;
114 const driver_station::JoystickAxis throttle_;
Sabina Davis82b19182017-11-10 09:30:25 -0800115 const driver_station::ButtonLocation quick_turn_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700116 // Button for enabling control loop driving.
Sabina Davis82b19182017-11-10 09:30:25 -0800117 const driver_station::ButtonLocation turn1_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700118 const TurnButtonUse turn1_use_;
119 // But for enabling line following.
Sabina Davis82b19182017-11-10 09:30:25 -0800120 const driver_station::ButtonLocation turn2_;
James Kuszmaul8bad2412019-03-10 10:47:56 -0700121 const TurnButtonUse turn2_use_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700122
Austin Schuh48d3a962019-03-17 18:12:32 -0700123 bool last_is_control_loop_driving_ = false;
124
Sabina Davis92d2efa2017-11-04 22:35:25 -0700125 // Structure containing the (potentially adjusted) steering and throttle
126 // values from the joysticks.
127 struct WheelAndThrottle {
128 double wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800129 double wheel_velocity;
130 double wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700131 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800132 double throttle_velocity;
133 double throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -0800134 bool high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700135 };
136
137 private:
138 // Computes the steering and throttle from the provided driverstation data.
139 virtual WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700140 const ::frc971::input::driver_station::Data &data) = 0;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700141
Alex Perrycb7da4b2019-08-28 19:35:56 -0700142 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700143 drivetrain_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144 ::aos::Sender<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700145 drivetrain_goal_sender_;
146
Sabina Davis92d2efa2017-11-04 22:35:25 -0700147 double robot_velocity_ = 0.0;
148 // Goals to send to the drivetrain in closed loop mode.
149 double left_goal_ = 0.0;
150 double right_goal_ = 0.0;
151 // The scale for the joysticks for closed loop mode converting
152 // joysticks to meters displacement on the two wheels.
153 double wheel_multiplier_ = 0.5;
Austin Schuha250b2d2019-05-27 16:14:02 -0700154
James Kuszmaul7077d342021-06-09 20:23:58 -0700155 ::std::function<bool(const ::frc971::input::driver_station::Data &data)>
Austin Schuha250b2d2019-05-27 16:14:02 -0700156 vision_align_fn_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700157};
158
159// Implements DrivetrainInputReader for the original steering wheel.
160class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader {
161 public:
162 using DrivetrainInputReader::DrivetrainInputReader;
163
164 // Creates a DrivetrainInputReader with the corresponding joystick ports and
165 // axis for the big steering wheel and throttle stick.
Sabina Davis82b19182017-11-10 09:30:25 -0800166 static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700167 ::aos::EventLoop *event_loop, bool default_high_gear);
Sabina Davis82b19182017-11-10 09:30:25 -0800168
169 // Sets the default shifter position
170 void set_default_high_gear(bool default_high_gear) {
171 high_gear_ = default_high_gear;
172 default_high_gear_ = default_high_gear;
173 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700174
175 private:
176 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700177 const ::frc971::input::driver_station::Data &data) override;
Sabina Davis82b19182017-11-10 09:30:25 -0800178 bool high_gear_;
179 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700180};
181
182class PistolDrivetrainInputReader : public DrivetrainInputReader {
183 public:
184 using DrivetrainInputReader::DrivetrainInputReader;
185
186 // Creates a DrivetrainInputReader with the corresponding joystick ports and
187 // axis for the (cheap) pistol grip controller.
James Kuszmaul8bad2412019-03-10 10:47:56 -0700188 static std::unique_ptr<PistolDrivetrainInputReader> Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700189 ::aos::EventLoop *event_loop, bool default_high_gear,
Maxwell Henderson24f89f32023-03-25 15:55:46 -0700190 PistolTopButtonUse top_button_use,
191 PistolSecondButtonUse second_button_use,
192 PistolBottomButtonUse bottom_button_use);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700193
194 private:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800195 PistolDrivetrainInputReader(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700196 ::aos::EventLoop *event_loop, driver_station::JoystickAxis wheel_high,
Austin Schuh2b1fce02018-03-02 20:05:20 -0800197 driver_station::JoystickAxis wheel_low,
198 driver_station::JoystickAxis wheel_velocity_high,
199 driver_station::JoystickAxis wheel_velocity_low,
200 driver_station::JoystickAxis wheel_torque_high,
201 driver_station::JoystickAxis wheel_torque_low,
202 driver_station::JoystickAxis throttle_high,
203 driver_station::JoystickAxis throttle_low,
204 driver_station::JoystickAxis throttle_velocity_high,
205 driver_station::JoystickAxis throttle_velocity_low,
206 driver_station::JoystickAxis throttle_torque_high,
207 driver_station::JoystickAxis throttle_torque_low,
208 driver_station::ButtonLocation quick_turn,
209 driver_station::ButtonLocation shift_high,
210 driver_station::ButtonLocation shift_low,
211 driver_station::ButtonLocation turn1,
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700212 driver_station::ButtonLocation turn2,
213 driver_station::ButtonLocation slow_down)
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700214 : DrivetrainInputReader(event_loop, wheel_high, throttle_high, quick_turn,
215 turn1, TurnButtonUse::kLineFollow, turn2,
Austin Schuh48d3a962019-03-17 18:12:32 -0700216 TurnButtonUse::kControlLoopDriving),
Austin Schuh2b1fce02018-03-02 20:05:20 -0800217 wheel_low_(wheel_low),
218 wheel_velocity_high_(wheel_velocity_high),
219 wheel_velocity_low_(wheel_velocity_low),
220 wheel_torque_high_(wheel_torque_high),
221 wheel_torque_low_(wheel_torque_low),
222 throttle_low_(throttle_low),
223 throttle_velocity_high_(throttle_velocity_high),
224 throttle_velocity_low_(throttle_velocity_low),
225 throttle_torque_high_(throttle_torque_high),
226 throttle_torque_low_(throttle_torque_low),
227 shift_high_(shift_high),
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700228 shift_low_(shift_low),
229 slow_down_(slow_down) {}
Austin Schuh2b1fce02018-03-02 20:05:20 -0800230
Sabina Davis92d2efa2017-11-04 22:35:25 -0700231 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700232 const ::frc971::input::driver_station::Data &data) override;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800233
234 // Sets the default shifter position
235 void set_default_high_gear(bool default_high_gear) {
236 high_gear_ = default_high_gear;
237 default_high_gear_ = default_high_gear;
238 }
239
240 const driver_station::JoystickAxis wheel_low_;
241 const driver_station::JoystickAxis wheel_velocity_high_;
242 const driver_station::JoystickAxis wheel_velocity_low_;
243 const driver_station::JoystickAxis wheel_torque_high_;
244 const driver_station::JoystickAxis wheel_torque_low_;
245
246 const driver_station::JoystickAxis throttle_low_;
247 const driver_station::JoystickAxis throttle_velocity_high_;
248 const driver_station::JoystickAxis throttle_velocity_low_;
249 const driver_station::JoystickAxis throttle_torque_high_;
250 const driver_station::JoystickAxis throttle_torque_low_;
251
252 const driver_station::ButtonLocation shift_high_;
253 const driver_station::ButtonLocation shift_low_;
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700254 const driver_station::ButtonLocation slow_down_;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800255
256 bool high_gear_;
257 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700258};
259
260class XboxDrivetrainInputReader : public DrivetrainInputReader {
261 public:
262 using DrivetrainInputReader::DrivetrainInputReader;
263
264 // Creates a DrivetrainInputReader with the corresponding joystick ports and
265 // axis for the Xbox controller.
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700266 static std::unique_ptr<XboxDrivetrainInputReader> Make(
267 ::aos::EventLoop *event_loop);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700268
269 private:
270 WheelAndThrottle GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700271 const ::frc971::input::driver_station::Data &data) override;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700272};
273
274} // namespace input
James Kuszmaul7077d342021-06-09 20:23:58 -0700275} // namespace frc971
Sabina Davis92d2efa2017-11-04 22:35:25 -0700276
277#endif // AOS_INPUT_DRIVETRAIN_INPUT_H_