blob: cefb113cc05dec79af45bf4759c06dd576929478 [file] [log] [blame]
James Kuszmaul7077d342021-06-09 20:23:58 -07001#include "frc971/input/drivetrain_input.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -07002
3#include <math.h>
4#include <stdio.h>
5#include <string.h>
James Kuszmaul7077d342021-06-09 20:23:58 -07006
Sabina Davis92d2efa2017-11-04 22:35:25 -07007#include <cmath>
8
John Park33858a32018-09-28 23:05:48 -07009#include "aos/commonmath.h"
John Park33858a32018-09-28 23:05:48 -070010#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "frc971/control_loops/control_loops_generated.h"
12#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 -070016using ::frc971::input::driver_station::ButtonLocation;
17using ::frc971::input::driver_station::ControlBit;
18using ::frc971::input::driver_station::JoystickAxis;
19using ::frc971::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070020
Alex Perrycb7da4b2019-08-28 19:35:56 -070021namespace drivetrain = frc971::control_loops::drivetrain;
22
James Kuszmaul7077d342021-06-09 20:23:58 -070023namespace frc971 {
Sabina Davis92d2efa2017-11-04 22:35:25 -070024namespace input {
25
Sabina Davis82b19182017-11-10 09:30:25 -080026const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
27
Sabina Davis92d2efa2017-11-04 22:35:25 -070028void DrivetrainInputReader::HandleDrivetrain(
James Kuszmaul7077d342021-06-09 20:23:58 -070029 const ::frc971::input::driver_station::Data &data) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070030 const auto wheel_and_throttle = GetWheelAndThrottle(data);
31 const double wheel = wheel_and_throttle.wheel;
Austin Schuh2b1fce02018-03-02 20:05:20 -080032 const double wheel_velocity = wheel_and_throttle.wheel_velocity;
33 const double wheel_torque = wheel_and_throttle.wheel_torque;
Sabina Davis92d2efa2017-11-04 22:35:25 -070034 const double throttle = wheel_and_throttle.throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -080035 const double throttle_velocity = wheel_and_throttle.throttle_velocity;
36 const double throttle_torque = wheel_and_throttle.throttle_torque;
Sabina Davis82b19182017-11-10 09:30:25 -080037 const bool high_gear = wheel_and_throttle.high_gear;
Sabina Davis92d2efa2017-11-04 22:35:25 -070038
Austin Schuhbd0a40f2019-06-30 14:56:31 -070039 drivetrain_status_fetcher_.Fetch();
40 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 robot_velocity_ = drivetrain_status_fetcher_->robot_speed();
Sabina Davis92d2efa2017-11-04 22:35:25 -070042 }
43
Austin Schuha250b2d2019-05-27 16:14:02 -070044 // If we have a vision align function, and it is in control, don't run the
45 // normal driving code.
46 if (vision_align_fn_) {
47 if (vision_align_fn_(data)) {
48 return;
49 }
50 }
51
James Kuszmaul8bad2412019-03-10 10:47:56 -070052 bool is_control_loop_driving = false;
53 bool is_line_following = false;
54
55 if (data.IsPressed(turn1_)) {
56 switch (turn1_use_) {
57 case TurnButtonUse::kControlLoopDriving:
58 is_control_loop_driving = true;
59 break;
60 case TurnButtonUse::kLineFollow:
61 is_line_following = true;
62 break;
63 }
64 }
65
66 if (data.IsPressed(turn2_)) {
67 switch (turn2_use_) {
68 case TurnButtonUse::kControlLoopDriving:
69 is_control_loop_driving = true;
70 break;
71 case TurnButtonUse::kLineFollow:
72 is_line_following = true;
73 break;
74 }
75 }
76
Austin Schuhbd0a40f2019-06-30 14:56:31 -070077 if (drivetrain_status_fetcher_.get()) {
Austin Schuh48d3a962019-03-17 18:12:32 -070078 if (is_control_loop_driving && !last_is_control_loop_driving_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 left_goal_ = drivetrain_status_fetcher_->estimated_left_position() +
Austin Schuhf9202e82019-03-22 21:55:11 -070080 wheel * wheel_multiplier_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 right_goal_ = drivetrain_status_fetcher_->estimated_right_position() -
Austin Schuhf9202e82019-03-22 21:55:11 -070082 wheel * wheel_multiplier_;
Sabina Davis92d2efa2017-11-04 22:35:25 -070083 }
84 }
Austin Schuh48d3a962019-03-17 18:12:32 -070085
Sabina Davis92d2efa2017-11-04 22:35:25 -070086 const double current_left_goal =
87 left_goal_ - wheel * wheel_multiplier_ + throttle * 0.3;
88 const double current_right_goal =
89 right_goal_ + wheel * wheel_multiplier_ + throttle * 0.3;
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 auto builder = drivetrain_goal_sender_.MakeBuilder();
Sabina Davis92d2efa2017-11-04 22:35:25 -070091
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 frc971::ProfileParameters::Builder linear_builder =
93 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Davis92d2efa2017-11-04 22:35:25 -070094
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 linear_builder.add_max_velocity(3.0);
96 linear_builder.add_max_acceleration(20.0);
97
98 flatbuffers::Offset<frc971::ProfileParameters> linear_offset =
99 linear_builder.Finish();
100
101 auto goal_builder = builder.MakeBuilder<drivetrain::Goal>();
102 goal_builder.add_wheel(wheel);
103 goal_builder.add_wheel_velocity(wheel_velocity);
104 goal_builder.add_wheel_torque(wheel_torque);
105 goal_builder.add_throttle(throttle);
106 goal_builder.add_throttle_velocity(throttle_velocity);
107 goal_builder.add_throttle_torque(throttle_torque);
108 goal_builder.add_highgear(high_gear);
109 goal_builder.add_quickturn(data.IsPressed(quick_turn_));
110 goal_builder.add_controller_type(
James Kuszmaul7077d342021-06-09 20:23:58 -0700111 is_line_following ? drivetrain::ControllerType::LINE_FOLLOWER
112 : (is_control_loop_driving
113 ? drivetrain::ControllerType::MOTION_PROFILE
114 : drivetrain::ControllerType::POLYDRIVE));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 goal_builder.add_left_goal(current_left_goal);
116 goal_builder.add_right_goal(current_right_goal);
117 goal_builder.add_linear(linear_offset);
118
119 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700120 AOS_LOG(WARNING, "sending stick values failed\n");
Sabina Davis92d2efa2017-11-04 22:35:25 -0700121 }
Austin Schuh48d3a962019-03-17 18:12:32 -0700122
123 last_is_control_loop_driving_ = is_control_loop_driving;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700124}
125
126DrivetrainInputReader::WheelAndThrottle
127SteeringWheelDrivetrainInputReader::GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700128 const ::frc971::input::driver_station::Data &data) {
Austin Schuh2b1fce02018-03-02 20:05:20 -0800129 const double wheel = -data.GetAxis(wheel_);
130 const double throttle = -data.GetAxis(throttle_);
Sabina Davis82b19182017-11-10 09:30:25 -0800131
132 if (!data.GetControlBit(ControlBit::kEnabled)) {
133 high_gear_ = default_high_gear_;
134 }
135
136 if (data.PosEdge(kShiftLow)) {
137 high_gear_ = false;
138 }
139
140 if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) {
141 high_gear_ = true;
142 }
143
Austin Schuh2b1fce02018-03-02 20:05:20 -0800144 return DrivetrainInputReader::WheelAndThrottle{
145 wheel, 0.0, 0.0, throttle, 0.0, 0.0, high_gear_};
146}
147
James Kuszmaul7077d342021-06-09 20:23:58 -0700148double UnwrappedAxis(const ::frc971::input::driver_station::Data &data,
Austin Schuh2b1fce02018-03-02 20:05:20 -0800149 const JoystickAxis &high_bits,
150 const JoystickAxis &low_bits) {
151 const float high_bits_data = data.GetAxis(high_bits);
152 const float low_bits_data = data.GetAxis(low_bits);
153 const int16_t high_bits_data_int =
154 (high_bits_data < 0.0f ? high_bits_data * 128.0f
155 : high_bits_data * 127.0f);
156 const int16_t low_bits_data_int =
157 (low_bits_data < 0.0f ? low_bits_data * 128.0f : low_bits_data * 127.0f);
158
159 const uint16_t high_bits_data_uint =
160 ((static_cast<uint16_t>(high_bits_data_int) & 0xff) + 0x80) & 0xff;
161 const uint16_t low_bits_data_uint =
162 ((static_cast<uint16_t>(low_bits_data_int) & 0xff) + 0x80) & 0xff;
163
164 const uint16_t data_uint = (high_bits_data_uint << 8) | low_bits_data_uint;
165
166 const int32_t data_int = static_cast<int32_t>(data_uint) - 0x8000;
167
168 if (data_int < 0) {
169 return static_cast<double>(data_int) / static_cast<double>(0x8000);
170 } else {
171 return static_cast<double>(data_int) / static_cast<double>(0x7fff);
172 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700173}
174
175DrivetrainInputReader::WheelAndThrottle
176PistolDrivetrainInputReader::GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700177 const ::frc971::input::driver_station::Data &data) {
178 const double wheel = -UnwrappedAxis(data, wheel_, wheel_low_);
Austin Schuh2b1fce02018-03-02 20:05:20 -0800179 const double wheel_velocity =
180 -UnwrappedAxis(data, wheel_velocity_high_, wheel_velocity_low_) * 50.0;
181 const double wheel_torque =
182 -UnwrappedAxis(data, wheel_torque_high_, wheel_torque_low_) / 2.0;
183
James Kuszmaul7077d342021-06-09 20:23:58 -0700184 double throttle = UnwrappedAxis(data, throttle_, throttle_low_);
Austin Schuh2b1fce02018-03-02 20:05:20 -0800185 const double throttle_velocity =
James Kuszmaul7077d342021-06-09 20:23:58 -0700186 UnwrappedAxis(data, throttle_velocity_high_, throttle_velocity_low_) *
187 50.0;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800188 const double throttle_torque =
189 UnwrappedAxis(data, throttle_torque_high_, throttle_torque_low_) / 2.0;
190
Austin Schuh876b4f02018-03-10 19:16:59 -0800191 // TODO(austin): Deal with haptics here.
192 if (throttle < 0) {
193 throttle = ::std::max(-1.0, throttle / 0.7);
194 }
195
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700196 if (data.IsPressed(slow_down_)) {
197 throttle *= 0.5;
198 }
199
Austin Schuh2b1fce02018-03-02 20:05:20 -0800200 if (!data.GetControlBit(ControlBit::kEnabled)) {
201 high_gear_ = default_high_gear_;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700202 }
203
Austin Schuh2b1fce02018-03-02 20:05:20 -0800204 if (data.PosEdge(shift_low_)) {
205 high_gear_ = false;
Sabina Davis92d2efa2017-11-04 22:35:25 -0700206 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700207
Austin Schuh2b1fce02018-03-02 20:05:20 -0800208 if (data.PosEdge(shift_high_)) {
209 high_gear_ = true;
210 }
Sabina Davis92d2efa2017-11-04 22:35:25 -0700211
Austin Schuh2b1fce02018-03-02 20:05:20 -0800212 return DrivetrainInputReader::WheelAndThrottle{
James Kuszmaul7077d342021-06-09 20:23:58 -0700213 wheel, wheel_velocity, wheel_torque,
214 throttle, throttle_velocity, throttle_torque,
Austin Schuh2b1fce02018-03-02 20:05:20 -0800215 high_gear_};
Sabina Davis92d2efa2017-11-04 22:35:25 -0700216}
217
218DrivetrainInputReader::WheelAndThrottle
219XboxDrivetrainInputReader::GetWheelAndThrottle(
James Kuszmaul7077d342021-06-09 20:23:58 -0700220 const ::frc971::input::driver_station::Data &data) {
Sabina Davis92d2efa2017-11-04 22:35:25 -0700221 // xbox
222 constexpr double kWheelDeadband = 0.05;
223 constexpr double kThrottleDeadband = 0.05;
224 const double wheel =
Austin Schuh2b1fce02018-03-02 20:05:20 -0800225 aos::Deadband(-data.GetAxis(wheel_), kWheelDeadband, 1.0);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700226
227 const double unmodified_throttle =
Austin Schuh2b1fce02018-03-02 20:05:20 -0800228 aos::Deadband(-data.GetAxis(throttle_), kThrottleDeadband, 1.0);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700229
230 // Apply a sin function that's scaled to make it feel better.
231 constexpr double throttle_range = M_PI_2 * 0.9;
232
233 double throttle = ::std::sin(throttle_range * unmodified_throttle) /
234 ::std::sin(throttle_range);
235 throttle = ::std::sin(throttle_range * throttle) / ::std::sin(throttle_range);
236 throttle = 2.0 * unmodified_throttle - throttle;
Austin Schuh2b1fce02018-03-02 20:05:20 -0800237 return DrivetrainInputReader::WheelAndThrottle{wheel, 0.0, 0.0, throttle,
238 0.0, 0.0, true};
Sabina Davis92d2efa2017-11-04 22:35:25 -0700239}
240
241std::unique_ptr<SteeringWheelDrivetrainInputReader>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700242SteeringWheelDrivetrainInputReader::Make(::aos::EventLoop *event_loop,
243 bool default_high_gear) {
Sabina Davis92d2efa2017-11-04 22:35:25 -0700244 const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
245 const ButtonLocation kQuickTurn(1, 5);
246 const ButtonLocation kTurn1(1, 7);
247 const ButtonLocation kTurn2(1, 11);
248 std::unique_ptr<SteeringWheelDrivetrainInputReader> result(
James Kuszmaul8bad2412019-03-10 10:47:56 -0700249 new SteeringWheelDrivetrainInputReader(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700250 event_loop, kSteeringWheel, kDriveThrottle, kQuickTurn, kTurn1,
James Kuszmaul8bad2412019-03-10 10:47:56 -0700251 TurnButtonUse::kControlLoopDriving, kTurn2,
252 TurnButtonUse::kControlLoopDriving));
Sabina Davis82b19182017-11-10 09:30:25 -0800253 result.get()->set_default_high_gear(default_high_gear);
254
Sabina Davis92d2efa2017-11-04 22:35:25 -0700255 return result;
256}
257
Austin Schuh2b1fce02018-03-02 20:05:20 -0800258std::unique_ptr<PistolDrivetrainInputReader> PistolDrivetrainInputReader::Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700259 ::aos::EventLoop *event_loop, bool default_high_gear,
260 TopButtonUse top_button_use) {
Sabina Davis92d2efa2017-11-04 22:35:25 -0700261 // Pistol Grip controller
Austin Schuh2b1fce02018-03-02 20:05:20 -0800262 const JoystickAxis kTriggerHigh(1, 1), kTriggerLow(1, 4),
263 kTriggerVelocityHigh(1, 2), kTriggerVelocityLow(1, 5),
264 kTriggerTorqueHigh(1, 3), kTriggerTorqueLow(1, 6);
265
266 const JoystickAxis kWheelHigh(2, 1), kWheelLow(2, 4),
267 kWheelVelocityHigh(2, 2), kWheelVelocityLow(2, 5), kWheelTorqueHigh(2, 3),
268 kWheelTorqueLow(2, 6);
269
Austin Schuhe8a54c02018-03-05 00:25:58 -0800270 const ButtonLocation kQuickTurn(1, 3);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700271
James Kuszmaul8bad2412019-03-10 10:47:56 -0700272 const ButtonLocation TopButton(1, 1);
273 const ButtonLocation SecondButton(1, 2);
James Kuszmaulc4eb1b22019-04-13 15:48:34 -0700274 const ButtonLocation BottomButton(1, 4);
James Kuszmaul8bad2412019-03-10 10:47:56 -0700275 // Non-existant button for nops.
276 const ButtonLocation DummyButton(1, 10);
277
278 // TODO(james): Make a copy assignment operator for ButtonLocation so we don't
279 // have to shoehorn in these ternary operators.
Austin Schuh48d3a962019-03-17 18:12:32 -0700280 const ButtonLocation kTurn1 = (top_button_use == TopButtonUse::kLineFollow)
281 ? SecondButton
282 : DummyButton;
283 // Turn2 does closed loop driving.
284 const ButtonLocation kTurn2 =
James Kuszmaul8bad2412019-03-10 10:47:56 -0700285 (top_button_use == TopButtonUse::kLineFollow) ? TopButton : DummyButton;
Austin Schuh48d3a962019-03-17 18:12:32 -0700286
James Kuszmaul8bad2412019-03-10 10:47:56 -0700287 const ButtonLocation kShiftHigh =
288 (top_button_use == TopButtonUse::kShift) ? TopButton : DummyButton;
289 const ButtonLocation kShiftLow =
290 (top_button_use == TopButtonUse::kShift) ? SecondButton : DummyButton;
291
Sabina Davis92d2efa2017-11-04 22:35:25 -0700292 std::unique_ptr<PistolDrivetrainInputReader> result(
Austin Schuh2b1fce02018-03-02 20:05:20 -0800293 new PistolDrivetrainInputReader(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700294 event_loop, kWheelHigh, kWheelLow, kTriggerVelocityHigh,
295 kTriggerVelocityLow, kTriggerTorqueHigh, kTriggerTorqueLow,
296 kTriggerHigh, kTriggerLow, kWheelVelocityHigh, kWheelVelocityLow,
297 kWheelTorqueHigh, kWheelTorqueLow, kQuickTurn, kShiftHigh, kShiftLow,
298 kTurn1, kTurn2, BottomButton));
Austin Schuh2b1fce02018-03-02 20:05:20 -0800299
300 result->set_default_high_gear(default_high_gear);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700301 return result;
302}
303
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700304std::unique_ptr<XboxDrivetrainInputReader> XboxDrivetrainInputReader::Make(
305 ::aos::EventLoop *event_loop) {
Sabina Davis92d2efa2017-11-04 22:35:25 -0700306 // xbox
307 const JoystickAxis kSteeringWheel(1, 5), kDriveThrottle(1, 2);
308 const ButtonLocation kQuickTurn(1, 5);
309
310 // Nop
311 const ButtonLocation kTurn1(1, 1);
312 const ButtonLocation kTurn2(1, 2);
313
314 std::unique_ptr<XboxDrivetrainInputReader> result(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700315 new XboxDrivetrainInputReader(event_loop, kSteeringWheel, kDriveThrottle,
316 kQuickTurn, kTurn1,
317 TurnButtonUse::kControlLoopDriving, kTurn2,
James Kuszmaul8bad2412019-03-10 10:47:56 -0700318 TurnButtonUse::kControlLoopDriving));
Sabina Davis92d2efa2017-11-04 22:35:25 -0700319 return result;
320}
321::std::unique_ptr<DrivetrainInputReader> DrivetrainInputReader::Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700322 ::aos::EventLoop *event_loop, InputType type,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323 const drivetrain::DrivetrainConfig<double> &dt_config) {
Sabina Davis92d2efa2017-11-04 22:35:25 -0700324 std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader;
325
326 using InputType = DrivetrainInputReader::InputType;
Sabina Davis82b19182017-11-10 09:30:25 -0800327
Sabina Davis92d2efa2017-11-04 22:35:25 -0700328 switch (type) {
329 case InputType::kSteeringWheel:
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700330 drivetrain_input_reader = SteeringWheelDrivetrainInputReader::Make(
331 event_loop, dt_config.default_high_gear);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700332 break;
333 case InputType::kPistol:
James Kuszmaul8bad2412019-03-10 10:47:56 -0700334 drivetrain_input_reader = PistolDrivetrainInputReader::Make(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700335 event_loop, dt_config.default_high_gear,
James Kuszmaul8bad2412019-03-10 10:47:56 -0700336 dt_config.pistol_grip_shift_enables_line_follow
337 ? PistolDrivetrainInputReader::TopButtonUse::kLineFollow
338 : PistolDrivetrainInputReader::TopButtonUse::kShift);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700339 break;
340 case InputType::kXbox:
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700341 drivetrain_input_reader = XboxDrivetrainInputReader::Make(event_loop);
Sabina Davis92d2efa2017-11-04 22:35:25 -0700342 break;
343 }
344 return drivetrain_input_reader;
345}
346
347} // namespace input
James Kuszmaul7077d342021-06-09 20:23:58 -0700348} // namespace frc971