blob: 2aa263eea0dd7b60c0727b34ae9e1ac2d1b3f95b [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include <unistd.h>
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
4#include <cstdio>
5#include <cstring>
6
Stephan Massaltd021f972020-01-05 20:41:23 -08007#include "aos/actions/actions.h"
8#include "aos/init.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08009#include "aos/logging/logging.h"
10#include "aos/network/team_number.h"
11#include "aos/util/log_interval.h"
12#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070013#include "frc971/control_loops/drivetrain/localizer_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070014#include "frc971/input/action_joystick_input.h"
15#include "frc971/input/driver_station_data.h"
16#include "frc971/input/drivetrain_input.h"
17#include "frc971/input/joystick_input.h"
Austin Schuh0b00c862021-10-17 17:39:10 -070018#include "frc971/zeroing/wrap.h"
Sabina Davisa8fed3d2020-02-22 21:44:57 -080019#include "y2020/constants.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080020#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080021#include "y2020/control_loops/superstructure/superstructure_goal_generated.h"
22#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080023#include "y2020/setpoint_generated.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080024
James Kuszmaul7077d342021-06-09 20:23:58 -070025using frc971::input::driver_station::ButtonLocation;
26using frc971::input::driver_station::ControlBit;
27using frc971::input::driver_station::JoystickAxis;
28using frc971::input::driver_station::POVLocation;
Stephan Massaltd021f972020-01-05 20:41:23 -080029
Austin Schuhf0a637c2020-02-25 23:44:12 -080030using frc971::CreateProfileParameters;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080031using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
32using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
33
Stephan Pleinesf63bde82024-01-13 15:59:33 -080034namespace y2020::input::joysticks {
Stephan Massaltd021f972020-01-05 20:41:23 -080035
36namespace superstructure = y2020::control_loops::superstructure;
37
Sabina Davisa8fed3d2020-02-22 21:44:57 -080038// TODO(sabina): fix button locations.
39
Austin Schuh3fdf2412021-10-16 13:56:46 -070040const ButtonLocation kAutoTrack(3, 3);
41const ButtonLocation kAutoNoHood(3, 5);
Ravago Jonese92ff112021-10-23 17:27:44 -070042const ButtonLocation kHood(3, 2);
Austin Schuh88dfe1e2021-10-29 23:07:54 -070043const ButtonLocation kShootSlow(4, 2);
Austin Schuh30e45ff2021-10-16 18:33:53 -070044const ButtonLocation kFixedTurret(3, 1);
Austin Schuh43a220f2020-02-26 22:02:34 -080045const ButtonLocation kFeed(4, 1);
Austin Schuh3fdf2412021-10-16 13:56:46 -070046const ButtonLocation kFeedDriver(1, 2);
Austin Schuhf0a637c2020-02-25 23:44:12 -080047const ButtonLocation kIntakeExtend(3, 9);
Austin Schuh30e45ff2021-10-16 18:33:53 -070048const ButtonLocation kIntakeExtendDriver(1, 4);
Austin Schuh0b00c862021-10-17 17:39:10 -070049const ButtonLocation kRedLocalizerReset(3, 13);
50const ButtonLocation kBlueLocalizerReset(3, 14);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080051const ButtonLocation kIntakeIn(4, 4);
Tyler Chatow1039e432020-02-28 21:37:50 -080052const ButtonLocation kSpit(4, 3);
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070053const ButtonLocation kLocalizerReset(3, 8);
Austin Schuhffae0962021-10-22 22:53:41 -070054const ButtonLocation kIntakeSlightlyOut(3, 7);
Tyler Chatow1039e432020-02-28 21:37:50 -080055
Ravago Jonese92ff112021-10-23 17:27:44 -070056const ButtonLocation kWinch(3, 4);
57const ButtonLocation kUnWinch(3, 6);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080058
James Kuszmaul7077d342021-06-09 20:23:58 -070059class Reader : public ::frc971::input::ActionJoystickInput {
Stephan Massaltd021f972020-01-05 20:41:23 -080060 public:
61 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070062 : ::frc971::input::ActionJoystickInput(
Stephan Massaltd021f972020-01-05 20:41:23 -080063 event_loop,
64 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -070065 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Stephan Massaltd021f972020-01-05 20:41:23 -080066 superstructure_goal_sender_(
67 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070068 localizer_control_sender_(
69 event_loop->MakeSender<
70 ::frc971::control_loops::drivetrain::LocalizerControl>(
71 "/drivetrain")),
Stephan Massaltd021f972020-01-05 20:41:23 -080072 superstructure_status_fetcher_(
Austin Schuhd58b2902020-03-01 19:28:04 -080073 event_loop->MakeFetcher<superstructure::Status>("/superstructure")),
74 setpoint_fetcher_(event_loop->MakeFetcher<y2020::joysticks::Setpoint>(
75 "/superstructure")) {}
Stephan Massaltd021f972020-01-05 20:41:23 -080076
Austin Schuh0b00c862021-10-17 17:39:10 -070077 void BlueResetLocalizer() {
78 auto builder = localizer_control_sender_.MakeBuilder();
79
80 frc971::control_loops::drivetrain::LocalizerControl::Builder
81 localizer_control_builder = builder.MakeBuilder<
82 frc971::control_loops::drivetrain::LocalizerControl>();
83 localizer_control_builder.add_x(7.4);
84 localizer_control_builder.add_y(-1.7);
85 localizer_control_builder.add_theta_uncertainty(10.0);
86 localizer_control_builder.add_theta(0.0);
87 localizer_control_builder.add_keep_current_theta(false);
milind1f1dca32021-07-03 13:50:07 -070088 if (builder.Send(localizer_control_builder.Finish()) !=
89 aos::RawSender::Error::kOk) {
Austin Schuh0b00c862021-10-17 17:39:10 -070090 AOS_LOG(ERROR, "Failed to reset blue localizer.\n");
91 }
92 }
93
94 void RedResetLocalizer() {
95 auto builder = localizer_control_sender_.MakeBuilder();
96
97 frc971::control_loops::drivetrain::LocalizerControl::Builder
98 localizer_control_builder = builder.MakeBuilder<
99 frc971::control_loops::drivetrain::LocalizerControl>();
100 localizer_control_builder.add_x(-7.4);
101 localizer_control_builder.add_y(1.7);
102 localizer_control_builder.add_theta_uncertainty(10.0);
103 localizer_control_builder.add_theta(M_PI);
104 localizer_control_builder.add_keep_current_theta(false);
milind1f1dca32021-07-03 13:50:07 -0700105 if (builder.Send(localizer_control_builder.Finish()) !=
106 aos::RawSender::Error::kOk) {
Austin Schuh0b00c862021-10-17 17:39:10 -0700107 AOS_LOG(ERROR, "Failed to reset red localizer.\n");
108 }
109 }
110
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700111 void ResetLocalizer() {
Austin Schuh0b00c862021-10-17 17:39:10 -0700112 const frc971::control_loops::drivetrain::Status *drivetrain_status =
113 this->drivetrain_status();
114 if (drivetrain_status == nullptr) {
115 return;
116 }
117 // Get the current position
118 // Snap to heading.
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700119 auto builder = localizer_control_sender_.MakeBuilder();
120
121 // Start roughly in front of the red-team goal, robot pointed away from
122 // goal.
123 frc971::control_loops::drivetrain::LocalizerControl::Builder
124 localizer_control_builder = builder.MakeBuilder<
125 frc971::control_loops::drivetrain::LocalizerControl>();
Austin Schuh0b00c862021-10-17 17:39:10 -0700126 localizer_control_builder.add_x(drivetrain_status->x());
127 localizer_control_builder.add_y(drivetrain_status->y());
128 const double new_theta =
129 frc971::zeroing::Wrap(drivetrain_status->theta(), 0, M_PI);
130 localizer_control_builder.add_theta(new_theta);
131 localizer_control_builder.add_theta_uncertainty(10.0);
milind1f1dca32021-07-03 13:50:07 -0700132 if (builder.Send(localizer_control_builder.Finish()) !=
133 aos::RawSender::Error::kOk) {
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700134 AOS_LOG(ERROR, "Failed to reset localizer.\n");
135 }
136 }
137
James Kuszmaul7077d342021-06-09 20:23:58 -0700138 void HandleTeleop(
139 const ::frc971::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -0800140 superstructure_status_fetcher_.Fetch();
141 if (!superstructure_status_fetcher_.get()) {
142 AOS_LOG(ERROR, "Got no superstructure status message.\n");
143 return;
144 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800145
Austin Schuhd58b2902020-03-01 19:28:04 -0800146 setpoint_fetcher_.Fetch();
147
Austin Schuhf0a637c2020-02-25 23:44:12 -0800148 double hood_pos = constants::Values::kHoodRange().middle();
149 double intake_pos = -0.89;
James Kuszmaule7e6f322021-09-25 17:24:00 -0700150 double turret_pos = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800151 float roller_speed = 0.0f;
Austin Schuh729d6842021-11-03 23:42:17 -0700152 bool roller_speed_compensation = false;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800153 double accelerator_speed = 0.0;
154 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800155 double climber_speed = 0.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700156 bool preload_intake = false;
Ravago Jonese92ff112021-10-23 17:27:44 -0700157 bool turret_tracking = false;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800158
Austin Schuh30e45ff2021-10-16 18:33:53 -0700159 const bool auto_track = data.IsPressed(kAutoTrack);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800160
Austin Schuhf0a637c2020-02-25 23:44:12 -0800161 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800162 hood_pos = 0.45;
163 } else {
164 if (setpoint_fetcher_.get()) {
165 hood_pos = setpoint_fetcher_->hood();
166 } else {
167 hood_pos = 0.58;
168 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800169 }
170
James Kuszmaule7e6f322021-09-25 17:24:00 -0700171 if (setpoint_fetcher_.get()) {
172 turret_pos = setpoint_fetcher_->turret();
173 } else {
174 turret_pos = 0.0;
175 }
176
Ravago Jonese92ff112021-10-23 17:27:44 -0700177 if (!data.IsPressed(kFixedTurret)) {
178 turret_tracking = true;
179 }
180
Austin Schuh30e45ff2021-10-16 18:33:53 -0700181 if (data.IsPressed(kAutoNoHood)) {
182 if (setpoint_fetcher_.get()) {
183 accelerator_speed = setpoint_fetcher_->accelerator();
184 finisher_speed = setpoint_fetcher_->finisher();
185 }
Austin Schuh88dfe1e2021-10-29 23:07:54 -0700186 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhab9247e2021-11-07 23:31:15 -0800187 accelerator_speed = 400.0;
Austin Schuh88dfe1e2021-10-29 23:07:54 -0700188 finisher_speed = 200.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800189 }
190
Austin Schuh30e45ff2021-10-16 18:33:53 -0700191 if (data.IsPressed(kIntakeExtend) || data.IsPressed(kIntakeExtendDriver)) {
Austin Schuh01d39b02021-11-06 20:51:55 -0700192 intake_pos = 1.24;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700193 roller_speed = 7.0f;
Austin Schuh729d6842021-11-03 23:42:17 -0700194 roller_speed_compensation = true;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700195 preload_intake = true;
Austin Schuh13e55522020-02-29 23:11:17 -0800196 }
197
198 if (superstructure_status_fetcher_.get() &&
Ravago Jonesa69ed2f2021-10-16 17:39:52 -0700199 superstructure_status_fetcher_->intake()->zeroed() &&
Austin Schuh13e55522020-02-29 23:11:17 -0800200 superstructure_status_fetcher_->intake()->position() > -0.5) {
201 roller_speed = std::max(roller_speed, 6.0f);
Austin Schuh729d6842021-11-03 23:42:17 -0700202 roller_speed_compensation = true;
Austin Schuh43a220f2020-02-26 22:02:34 -0800203 }
204
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800205 if (data.IsPressed(kIntakeIn)) {
Austin Schuh729d6842021-11-03 23:42:17 -0700206 roller_speed = 5.0f;
207 roller_speed_compensation = true;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700208 preload_intake = true;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800209 } else if (data.IsPressed(kSpit)) {
210 roller_speed = -6.0f;
Austin Schuhffae0962021-10-22 22:53:41 -0700211 } else if (data.IsPressed(kIntakeSlightlyOut)) {
212 intake_pos = -0.426585;
213 roller_speed = 6.0f;
214 preload_intake = true;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800215 }
216
Tyler Chatow1039e432020-02-28 21:37:50 -0800217 if (data.IsPressed(kWinch)) {
Austin Schuhc09b1572021-10-29 23:08:15 -0700218 ++winch_counter_;
219 } else {
220 winch_counter_ = 0;
221 }
222
223 if (winch_counter_ > 5 || (winch_counter_ > 0 && latched_climbing_)) {
Tyler Chatow1039e432020-02-28 21:37:50 -0800224 climber_speed = 12.0f;
Ravago Jonese92ff112021-10-23 17:27:44 -0700225 latched_climbing_ = true;
226 }
227
228 if (data.IsPressed(kUnWinch)) {
Austin Schuhc09b1572021-10-29 23:08:15 -0700229 ++unwinch_counter_;
230 } else {
231 unwinch_counter_ = 0;
232 }
233
234 if (unwinch_counter_ > 10 || (unwinch_counter_ > 0 && latched_climbing_)) {
Ravago Jonese92ff112021-10-23 17:27:44 -0700235 climber_speed = -12.0f;
236 latched_climbing_ = true;
237 }
238
239 if (data.IsPressed(kWinch) && data.IsPressed(kUnWinch)) {
240 latched_climbing_ = false;
Austin Schuhc09b1572021-10-29 23:08:15 -0700241 unwinch_counter_ = 0;
242 winch_counter_ = 0;
Ravago Jonese92ff112021-10-23 17:27:44 -0700243 }
244
245 if (latched_climbing_) {
246 turret_tracking = false;
247 turret_pos = -M_PI / 2.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800248 }
249
Austin Schuh0b00c862021-10-17 17:39:10 -0700250 if (data.PosEdge(kLocalizerReset)) {
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700251 ResetLocalizer();
252 }
253
Austin Schuh0b00c862021-10-17 17:39:10 -0700254 if (data.PosEdge(kRedLocalizerReset)) {
255 RedResetLocalizer();
256 }
257 if (data.PosEdge(kBlueLocalizerReset)) {
258 BlueResetLocalizer();
259 }
260
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800261 auto builder = superstructure_goal_sender_.MakeBuilder();
262
263 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
264 {
265 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
266 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800267 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800268 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800269
270 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
271 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800272 *builder.fbb(), intake_pos,
Austin Schuh8aacbef2021-10-16 23:15:14 -0700273 CreateProfileParameters(*builder.fbb(), 20.0, 70.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800274
275 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
276 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaule7e6f322021-09-25 17:24:00 -0700277 *builder.fbb(), turret_pos,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800278 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800279
280 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800281 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
282 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800283
284 superstructure::Goal::Builder superstructure_goal_builder =
285 builder.MakeBuilder<superstructure::Goal>();
286
287 superstructure_goal_builder.add_hood(hood_offset);
288 superstructure_goal_builder.add_intake(intake_offset);
289 superstructure_goal_builder.add_turret(turret_offset);
290 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700291 superstructure_goal_builder.add_roller_speed_compensation(
Austin Schuh729d6842021-11-03 23:42:17 -0700292 roller_speed_compensation ? 1.5f : 0.0f);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800293 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700294 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed) ||
295 data.IsPressed(kFeedDriver));
Austin Schuhb187ddb2021-11-13 16:16:16 -0800296 if (data.IsPressed(kSpit)) {
297 superstructure_goal_builder.add_feed_voltage_override(-7.0);
298 }
Tyler Chatow1039e432020-02-28 21:37:50 -0800299 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800300
Ravago Jonese92ff112021-10-23 17:27:44 -0700301 superstructure_goal_builder.add_turret_tracking(turret_tracking);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700302 superstructure_goal_builder.add_hood_tracking(
Austin Schuh30e45ff2021-10-16 18:33:53 -0700303 !data.IsPressed(kFixedTurret) && !data.IsPressed(kAutoNoHood));
304 superstructure_goal_builder.add_shooter_tracking(
305 auto_track ||
306 (!data.IsPressed(kFixedTurret) && !data.IsPressed(kAutoNoHood) &&
307 data.IsPressed(kFeedDriver)));
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700308 superstructure_goal_builder.add_intake_preloading(preload_intake);
James Kuszmaulccad78d2021-04-03 18:28:58 -0700309
milind1f1dca32021-07-03 13:50:07 -0700310 if (builder.Send(superstructure_goal_builder.Finish()) !=
311 aos::RawSender::Error::kOk) {
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800312 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
313 }
314 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800315 }
316
317 private:
318 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
319
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700320 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
321 localizer_control_sender_;
322
Stephan Massaltd021f972020-01-05 20:41:23 -0800323 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800324
325 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Austin Schuhc09b1572021-10-29 23:08:15 -0700326
327 bool latched_climbing_ = false;
328
329 size_t winch_counter_ = 0;
330 size_t unwinch_counter_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800331};
332
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800333} // namespace y2020::input::joysticks
Stephan Massaltd021f972020-01-05 20:41:23 -0800334
Austin Schuh094d09b2020-11-20 23:26:52 -0800335int main(int argc, char **argv) {
336 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800337
338 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800339 aos::configuration::ReadConfig("aos_config.json");
Stephan Massaltd021f972020-01-05 20:41:23 -0800340
341 ::aos::ShmEventLoop event_loop(&config.message());
342 ::y2020::input::joysticks::Reader reader(&event_loop);
343
344 event_loop.Run();
345
Austin Schuhae87e312020-08-01 16:15:01 -0700346 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800347}