blob: 86b0e1018c17a3c5d52bfb7a4a470f875e51cc00 [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,
62 const ::frc971::control_loops::drivetrain::DrivetrainConfig &dt_config);
Sabina Davis92d2efa2017-11-04 22:35:25 -070063
64 // Processes new joystick data and publishes drivetrain goal messages.
65 void HandleDrivetrain(const ::aos::input::driver_station::Data &data);
66
67 // Sets the scalar for the steering wheel for closed loop mode converting
68 // steering ratio to meters displacement on the two wheels.
69 void set_wheel_multiplier(double wheel_multiplier) {
70 wheel_multiplier_ = wheel_multiplier;
71 }
72
73 // Returns the current robot velocity in m/s.
74 double robot_velocity() const { return robot_velocity_; }
75
76 protected:
Austin Schuh2b1fce02018-03-02 20:05:20 -080077 const driver_station::JoystickAxis wheel_;
78 const driver_station::JoystickAxis throttle_;
Sabina Davis82b19182017-11-10 09:30:25 -080079 const driver_station::ButtonLocation quick_turn_;
80 const driver_station::ButtonLocation turn1_;
81 const driver_station::ButtonLocation turn2_;
Sabina Davis92d2efa2017-11-04 22:35:25 -070082
83 // Structure containing the (potentially adjusted) steering and throttle
84 // values from the joysticks.
85 struct WheelAndThrottle {
86 double wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -080087 double wheel_velocity;
88 double wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -070089 double throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -080090 double throttle_velocity;
91 double throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -080092 bool high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -070093 };
94
95 private:
96 // Computes the steering and throttle from the provided driverstation data.
97 virtual WheelAndThrottle GetWheelAndThrottle(
98 const ::aos::input::driver_station::Data &data) = 0;
99
100 double robot_velocity_ = 0.0;
101 // Goals to send to the drivetrain in closed loop mode.
102 double left_goal_ = 0.0;
103 double right_goal_ = 0.0;
104 // The scale for the joysticks for closed loop mode converting
105 // joysticks to meters displacement on the two wheels.
106 double wheel_multiplier_ = 0.5;
107};
108
109// Implements DrivetrainInputReader for the original steering wheel.
110class SteeringWheelDrivetrainInputReader : public DrivetrainInputReader {
111 public:
112 using DrivetrainInputReader::DrivetrainInputReader;
113
114 // Creates a DrivetrainInputReader with the corresponding joystick ports and
115 // axis for the big steering wheel and throttle stick.
Sabina Davis82b19182017-11-10 09:30:25 -0800116 static std::unique_ptr<SteeringWheelDrivetrainInputReader> Make(
117 bool default_high_gear);
118
119 // Sets the default shifter position
120 void set_default_high_gear(bool default_high_gear) {
121 high_gear_ = default_high_gear;
122 default_high_gear_ = default_high_gear;
123 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700124
125 private:
126 WheelAndThrottle GetWheelAndThrottle(
127 const ::aos::input::driver_station::Data &data) override;
Sabina Davis82b19182017-11-10 09:30:25 -0800128 bool high_gear_;
129 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700130};
131
132class PistolDrivetrainInputReader : public DrivetrainInputReader {
133 public:
134 using DrivetrainInputReader::DrivetrainInputReader;
135
136 // Creates a DrivetrainInputReader with the corresponding joystick ports and
137 // axis for the (cheap) pistol grip controller.
Austin Schuh2b1fce02018-03-02 20:05:20 -0800138 static std::unique_ptr<PistolDrivetrainInputReader> Make(bool default_high_gear);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700139
140 private:
Austin Schuh2b1fce02018-03-02 20:05:20 -0800141 PistolDrivetrainInputReader(
142 driver_station::JoystickAxis wheel_high,
143 driver_station::JoystickAxis wheel_low,
144 driver_station::JoystickAxis wheel_velocity_high,
145 driver_station::JoystickAxis wheel_velocity_low,
146 driver_station::JoystickAxis wheel_torque_high,
147 driver_station::JoystickAxis wheel_torque_low,
148 driver_station::JoystickAxis throttle_high,
149 driver_station::JoystickAxis throttle_low,
150 driver_station::JoystickAxis throttle_velocity_high,
151 driver_station::JoystickAxis throttle_velocity_low,
152 driver_station::JoystickAxis throttle_torque_high,
153 driver_station::JoystickAxis throttle_torque_low,
154 driver_station::ButtonLocation quick_turn,
155 driver_station::ButtonLocation shift_high,
156 driver_station::ButtonLocation shift_low,
157 driver_station::ButtonLocation turn1,
158 driver_station::ButtonLocation turn2)
159 : DrivetrainInputReader(wheel_high, throttle_high, quick_turn, turn1,
160 turn2),
161 wheel_low_(wheel_low),
162 wheel_velocity_high_(wheel_velocity_high),
163 wheel_velocity_low_(wheel_velocity_low),
164 wheel_torque_high_(wheel_torque_high),
165 wheel_torque_low_(wheel_torque_low),
166 throttle_low_(throttle_low),
167 throttle_velocity_high_(throttle_velocity_high),
168 throttle_velocity_low_(throttle_velocity_low),
169 throttle_torque_high_(throttle_torque_high),
170 throttle_torque_low_(throttle_torque_low),
171 shift_high_(shift_high),
172 shift_low_(shift_low) {}
173
Sabina Davis92d2efa2017-11-04 22:35:25 -0700174 WheelAndThrottle GetWheelAndThrottle(
175 const ::aos::input::driver_station::Data &data) override;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800176
177 // Sets the default shifter position
178 void set_default_high_gear(bool default_high_gear) {
179 high_gear_ = default_high_gear;
180 default_high_gear_ = default_high_gear;
181 }
182
183 const driver_station::JoystickAxis wheel_low_;
184 const driver_station::JoystickAxis wheel_velocity_high_;
185 const driver_station::JoystickAxis wheel_velocity_low_;
186 const driver_station::JoystickAxis wheel_torque_high_;
187 const driver_station::JoystickAxis wheel_torque_low_;
188
189 const driver_station::JoystickAxis throttle_low_;
190 const driver_station::JoystickAxis throttle_velocity_high_;
191 const driver_station::JoystickAxis throttle_velocity_low_;
192 const driver_station::JoystickAxis throttle_torque_high_;
193 const driver_station::JoystickAxis throttle_torque_low_;
194
195 const driver_station::ButtonLocation shift_high_;
196 const driver_station::ButtonLocation shift_low_;
197
198 bool high_gear_;
199 bool default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700200};
201
202class XboxDrivetrainInputReader : public DrivetrainInputReader {
203 public:
204 using DrivetrainInputReader::DrivetrainInputReader;
205
206 // Creates a DrivetrainInputReader with the corresponding joystick ports and
207 // axis for the Xbox controller.
208 static std::unique_ptr<XboxDrivetrainInputReader> Make();
209
210 private:
211 WheelAndThrottle GetWheelAndThrottle(
212 const ::aos::input::driver_station::Data &data) override;
213};
214
215} // namespace input
216} // namespace aos
217
218#endif // AOS_INPUT_DRIVETRAIN_INPUT_H_