blob: f1237e1b1fc797e9dd1cb9baf249a307f43790e8 [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 Massaltd021f972020-01-05 20:41:23 -080034namespace y2020 {
35namespace input {
36namespace joysticks {
37
38namespace superstructure = y2020::control_loops::superstructure;
39
Sabina Davisa8fed3d2020-02-22 21:44:57 -080040// TODO(sabina): fix button locations.
41
Austin Schuh3fdf2412021-10-16 13:56:46 -070042const ButtonLocation kAutoTrack(3, 3);
43const ButtonLocation kAutoNoHood(3, 5);
Ravago Jonese92ff112021-10-23 17:27:44 -070044const ButtonLocation kHood(3, 2);
Austin Schuh88dfe1e2021-10-29 23:07:54 -070045const ButtonLocation kShootSlow(4, 2);
Austin Schuh30e45ff2021-10-16 18:33:53 -070046const ButtonLocation kFixedTurret(3, 1);
Austin Schuh43a220f2020-02-26 22:02:34 -080047const ButtonLocation kFeed(4, 1);
Austin Schuh3fdf2412021-10-16 13:56:46 -070048const ButtonLocation kFeedDriver(1, 2);
Austin Schuhf0a637c2020-02-25 23:44:12 -080049const ButtonLocation kIntakeExtend(3, 9);
Austin Schuh30e45ff2021-10-16 18:33:53 -070050const ButtonLocation kIntakeExtendDriver(1, 4);
Austin Schuh0b00c862021-10-17 17:39:10 -070051const ButtonLocation kRedLocalizerReset(3, 13);
52const ButtonLocation kBlueLocalizerReset(3, 14);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080053const ButtonLocation kIntakeIn(4, 4);
Tyler Chatow1039e432020-02-28 21:37:50 -080054const ButtonLocation kSpit(4, 3);
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070055const ButtonLocation kLocalizerReset(3, 8);
Austin Schuhffae0962021-10-22 22:53:41 -070056const ButtonLocation kIntakeSlightlyOut(3, 7);
Tyler Chatow1039e432020-02-28 21:37:50 -080057
Ravago Jonese92ff112021-10-23 17:27:44 -070058const ButtonLocation kWinch(3, 4);
59const ButtonLocation kUnWinch(3, 6);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080060
James Kuszmaul7077d342021-06-09 20:23:58 -070061class Reader : public ::frc971::input::ActionJoystickInput {
Stephan Massaltd021f972020-01-05 20:41:23 -080062 public:
63 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070064 : ::frc971::input::ActionJoystickInput(
Stephan Massaltd021f972020-01-05 20:41:23 -080065 event_loop,
66 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -070067 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Stephan Massaltd021f972020-01-05 20:41:23 -080068 superstructure_goal_sender_(
69 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070070 localizer_control_sender_(
71 event_loop->MakeSender<
72 ::frc971::control_loops::drivetrain::LocalizerControl>(
73 "/drivetrain")),
Stephan Massaltd021f972020-01-05 20:41:23 -080074 superstructure_status_fetcher_(
Austin Schuhd58b2902020-03-01 19:28:04 -080075 event_loop->MakeFetcher<superstructure::Status>("/superstructure")),
76 setpoint_fetcher_(event_loop->MakeFetcher<y2020::joysticks::Setpoint>(
77 "/superstructure")) {}
Stephan Massaltd021f972020-01-05 20:41:23 -080078
Austin Schuh0b00c862021-10-17 17:39:10 -070079 void BlueResetLocalizer() {
80 auto builder = localizer_control_sender_.MakeBuilder();
81
82 frc971::control_loops::drivetrain::LocalizerControl::Builder
83 localizer_control_builder = builder.MakeBuilder<
84 frc971::control_loops::drivetrain::LocalizerControl>();
85 localizer_control_builder.add_x(7.4);
86 localizer_control_builder.add_y(-1.7);
87 localizer_control_builder.add_theta_uncertainty(10.0);
88 localizer_control_builder.add_theta(0.0);
89 localizer_control_builder.add_keep_current_theta(false);
milind1f1dca32021-07-03 13:50:07 -070090 if (builder.Send(localizer_control_builder.Finish()) !=
91 aos::RawSender::Error::kOk) {
Austin Schuh0b00c862021-10-17 17:39:10 -070092 AOS_LOG(ERROR, "Failed to reset blue localizer.\n");
93 }
94 }
95
96 void RedResetLocalizer() {
97 auto builder = localizer_control_sender_.MakeBuilder();
98
99 frc971::control_loops::drivetrain::LocalizerControl::Builder
100 localizer_control_builder = builder.MakeBuilder<
101 frc971::control_loops::drivetrain::LocalizerControl>();
102 localizer_control_builder.add_x(-7.4);
103 localizer_control_builder.add_y(1.7);
104 localizer_control_builder.add_theta_uncertainty(10.0);
105 localizer_control_builder.add_theta(M_PI);
106 localizer_control_builder.add_keep_current_theta(false);
milind1f1dca32021-07-03 13:50:07 -0700107 if (builder.Send(localizer_control_builder.Finish()) !=
108 aos::RawSender::Error::kOk) {
Austin Schuh0b00c862021-10-17 17:39:10 -0700109 AOS_LOG(ERROR, "Failed to reset red localizer.\n");
110 }
111 }
112
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700113 void ResetLocalizer() {
Austin Schuh0b00c862021-10-17 17:39:10 -0700114 const frc971::control_loops::drivetrain::Status *drivetrain_status =
115 this->drivetrain_status();
116 if (drivetrain_status == nullptr) {
117 return;
118 }
119 // Get the current position
120 // Snap to heading.
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700121 auto builder = localizer_control_sender_.MakeBuilder();
122
123 // Start roughly in front of the red-team goal, robot pointed away from
124 // goal.
125 frc971::control_loops::drivetrain::LocalizerControl::Builder
126 localizer_control_builder = builder.MakeBuilder<
127 frc971::control_loops::drivetrain::LocalizerControl>();
Austin Schuh0b00c862021-10-17 17:39:10 -0700128 localizer_control_builder.add_x(drivetrain_status->x());
129 localizer_control_builder.add_y(drivetrain_status->y());
130 const double new_theta =
131 frc971::zeroing::Wrap(drivetrain_status->theta(), 0, M_PI);
132 localizer_control_builder.add_theta(new_theta);
133 localizer_control_builder.add_theta_uncertainty(10.0);
milind1f1dca32021-07-03 13:50:07 -0700134 if (builder.Send(localizer_control_builder.Finish()) !=
135 aos::RawSender::Error::kOk) {
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700136 AOS_LOG(ERROR, "Failed to reset localizer.\n");
137 }
138 }
139
James Kuszmaul7077d342021-06-09 20:23:58 -0700140 void HandleTeleop(
141 const ::frc971::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -0800142 superstructure_status_fetcher_.Fetch();
143 if (!superstructure_status_fetcher_.get()) {
144 AOS_LOG(ERROR, "Got no superstructure status message.\n");
145 return;
146 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800147
Austin Schuhd58b2902020-03-01 19:28:04 -0800148 setpoint_fetcher_.Fetch();
149
Austin Schuhf0a637c2020-02-25 23:44:12 -0800150 double hood_pos = constants::Values::kHoodRange().middle();
151 double intake_pos = -0.89;
James Kuszmaule7e6f322021-09-25 17:24:00 -0700152 double turret_pos = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800153 float roller_speed = 0.0f;
Austin Schuh729d6842021-11-03 23:42:17 -0700154 bool roller_speed_compensation = false;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800155 double accelerator_speed = 0.0;
156 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800157 double climber_speed = 0.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700158 bool preload_intake = false;
Ravago Jonese92ff112021-10-23 17:27:44 -0700159 bool turret_tracking = false;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800160
Austin Schuh30e45ff2021-10-16 18:33:53 -0700161 const bool auto_track = data.IsPressed(kAutoTrack);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800162
Austin Schuhf0a637c2020-02-25 23:44:12 -0800163 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800164 hood_pos = 0.45;
165 } else {
166 if (setpoint_fetcher_.get()) {
167 hood_pos = setpoint_fetcher_->hood();
168 } else {
169 hood_pos = 0.58;
170 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800171 }
172
James Kuszmaule7e6f322021-09-25 17:24:00 -0700173 if (setpoint_fetcher_.get()) {
174 turret_pos = setpoint_fetcher_->turret();
175 } else {
176 turret_pos = 0.0;
177 }
178
Ravago Jonese92ff112021-10-23 17:27:44 -0700179 if (!data.IsPressed(kFixedTurret)) {
180 turret_tracking = true;
181 }
182
Austin Schuh30e45ff2021-10-16 18:33:53 -0700183 if (data.IsPressed(kAutoNoHood)) {
184 if (setpoint_fetcher_.get()) {
185 accelerator_speed = setpoint_fetcher_->accelerator();
186 finisher_speed = setpoint_fetcher_->finisher();
187 }
Austin Schuh88dfe1e2021-10-29 23:07:54 -0700188 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhab9247e2021-11-07 23:31:15 -0800189 accelerator_speed = 400.0;
Austin Schuh88dfe1e2021-10-29 23:07:54 -0700190 finisher_speed = 200.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800191 }
192
Austin Schuh30e45ff2021-10-16 18:33:53 -0700193 if (data.IsPressed(kIntakeExtend) || data.IsPressed(kIntakeExtendDriver)) {
Austin Schuh01d39b02021-11-06 20:51:55 -0700194 intake_pos = 1.24;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700195 roller_speed = 7.0f;
Austin Schuh729d6842021-11-03 23:42:17 -0700196 roller_speed_compensation = true;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700197 preload_intake = true;
Austin Schuh13e55522020-02-29 23:11:17 -0800198 }
199
200 if (superstructure_status_fetcher_.get() &&
Ravago Jonesa69ed2f2021-10-16 17:39:52 -0700201 superstructure_status_fetcher_->intake()->zeroed() &&
Austin Schuh13e55522020-02-29 23:11:17 -0800202 superstructure_status_fetcher_->intake()->position() > -0.5) {
203 roller_speed = std::max(roller_speed, 6.0f);
Austin Schuh729d6842021-11-03 23:42:17 -0700204 roller_speed_compensation = true;
Austin Schuh43a220f2020-02-26 22:02:34 -0800205 }
206
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800207 if (data.IsPressed(kIntakeIn)) {
Austin Schuh729d6842021-11-03 23:42:17 -0700208 roller_speed = 5.0f;
209 roller_speed_compensation = true;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700210 preload_intake = true;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800211 } else if (data.IsPressed(kSpit)) {
212 roller_speed = -6.0f;
Austin Schuhffae0962021-10-22 22:53:41 -0700213 } else if (data.IsPressed(kIntakeSlightlyOut)) {
214 intake_pos = -0.426585;
215 roller_speed = 6.0f;
216 preload_intake = true;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800217 }
218
Tyler Chatow1039e432020-02-28 21:37:50 -0800219 if (data.IsPressed(kWinch)) {
Austin Schuhc09b1572021-10-29 23:08:15 -0700220 ++winch_counter_;
221 } else {
222 winch_counter_ = 0;
223 }
224
225 if (winch_counter_ > 5 || (winch_counter_ > 0 && latched_climbing_)) {
Tyler Chatow1039e432020-02-28 21:37:50 -0800226 climber_speed = 12.0f;
Ravago Jonese92ff112021-10-23 17:27:44 -0700227 latched_climbing_ = true;
228 }
229
230 if (data.IsPressed(kUnWinch)) {
Austin Schuhc09b1572021-10-29 23:08:15 -0700231 ++unwinch_counter_;
232 } else {
233 unwinch_counter_ = 0;
234 }
235
236 if (unwinch_counter_ > 10 || (unwinch_counter_ > 0 && latched_climbing_)) {
Ravago Jonese92ff112021-10-23 17:27:44 -0700237 climber_speed = -12.0f;
238 latched_climbing_ = true;
239 }
240
241 if (data.IsPressed(kWinch) && data.IsPressed(kUnWinch)) {
242 latched_climbing_ = false;
Austin Schuhc09b1572021-10-29 23:08:15 -0700243 unwinch_counter_ = 0;
244 winch_counter_ = 0;
Ravago Jonese92ff112021-10-23 17:27:44 -0700245 }
246
247 if (latched_climbing_) {
248 turret_tracking = false;
249 turret_pos = -M_PI / 2.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800250 }
251
Austin Schuh0b00c862021-10-17 17:39:10 -0700252 if (data.PosEdge(kLocalizerReset)) {
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700253 ResetLocalizer();
254 }
255
Austin Schuh0b00c862021-10-17 17:39:10 -0700256 if (data.PosEdge(kRedLocalizerReset)) {
257 RedResetLocalizer();
258 }
259 if (data.PosEdge(kBlueLocalizerReset)) {
260 BlueResetLocalizer();
261 }
262
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800263 auto builder = superstructure_goal_sender_.MakeBuilder();
264
265 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
266 {
267 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
268 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800269 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800270 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800271
272 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
273 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800274 *builder.fbb(), intake_pos,
Austin Schuh8aacbef2021-10-16 23:15:14 -0700275 CreateProfileParameters(*builder.fbb(), 20.0, 70.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800276
277 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
278 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaule7e6f322021-09-25 17:24:00 -0700279 *builder.fbb(), turret_pos,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800280 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800281
282 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800283 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
284 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800285
286 superstructure::Goal::Builder superstructure_goal_builder =
287 builder.MakeBuilder<superstructure::Goal>();
288
289 superstructure_goal_builder.add_hood(hood_offset);
290 superstructure_goal_builder.add_intake(intake_offset);
291 superstructure_goal_builder.add_turret(turret_offset);
292 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700293 superstructure_goal_builder.add_roller_speed_compensation(
Austin Schuh729d6842021-11-03 23:42:17 -0700294 roller_speed_compensation ? 1.5f : 0.0f);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800295 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700296 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed) ||
297 data.IsPressed(kFeedDriver));
Tyler Chatow1039e432020-02-28 21:37:50 -0800298 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800299
Ravago Jonese92ff112021-10-23 17:27:44 -0700300 superstructure_goal_builder.add_turret_tracking(turret_tracking);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700301 superstructure_goal_builder.add_hood_tracking(
Austin Schuh30e45ff2021-10-16 18:33:53 -0700302 !data.IsPressed(kFixedTurret) && !data.IsPressed(kAutoNoHood));
303 superstructure_goal_builder.add_shooter_tracking(
304 auto_track ||
305 (!data.IsPressed(kFixedTurret) && !data.IsPressed(kAutoNoHood) &&
306 data.IsPressed(kFeedDriver)));
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700307 superstructure_goal_builder.add_intake_preloading(preload_intake);
James Kuszmaulccad78d2021-04-03 18:28:58 -0700308
milind1f1dca32021-07-03 13:50:07 -0700309 if (builder.Send(superstructure_goal_builder.Finish()) !=
310 aos::RawSender::Error::kOk) {
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800311 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
312 }
313 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800314 }
315
316 private:
317 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
318
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700319 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
320 localizer_control_sender_;
321
Stephan Massaltd021f972020-01-05 20:41:23 -0800322 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800323
324 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Austin Schuhc09b1572021-10-29 23:08:15 -0700325
326 bool latched_climbing_ = false;
327
328 size_t winch_counter_ = 0;
329 size_t unwinch_counter_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800330};
331
332} // namespace joysticks
333} // namespace input
334} // namespace y2020
335
Austin Schuh094d09b2020-11-20 23:26:52 -0800336int main(int argc, char **argv) {
337 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800338
339 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
340 aos::configuration::ReadConfig("config.json");
341
342 ::aos::ShmEventLoop event_loop(&config.message());
343 ::y2020::input::joysticks::Reader reader(&event_loop);
344
345 event_loop.Run();
346
Austin Schuhae87e312020-08-01 16:15:01 -0700347 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800348}