blob: 3951dfaecfa1214e92cf913a997c86c1dc68dc25 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include <unistd.h>
2
3#include <cmath>
4#include <cstdio>
5#include <cstring>
6
7#include "aos/actions/actions.h"
8#include "aos/init.h"
9#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"
13#include "frc971/control_loops/drivetrain/localizer_generated.h"
14#include "frc971/control_loops/profiled_subsystem_generated.h"
15#include "frc971/input/action_joystick_input.h"
16#include "frc971/input/driver_station_data.h"
17#include "frc971/input/drivetrain_input.h"
18#include "frc971/input/joystick_input.h"
19#include "frc971/zeroing/wrap.h"
20#include "y2023/constants.h"
21#include "y2023/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul4e171432023-02-26 13:39:37 -080022#include "y2023/control_loops/drivetrain/target_selector_hint_generated.h"
milind-udefab712023-02-20 22:22:02 -080023#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080024#include "y2023/control_loops/superstructure/superstructure_goal_generated.h"
25#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
26
27using frc971::CreateProfileParameters;
28using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
29using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
30using frc971::input::driver_station::ButtonLocation;
31using frc971::input::driver_station::ControlBit;
32using frc971::input::driver_station::JoystickAxis;
33using frc971::input::driver_station::POVLocation;
James Kuszmaul4e171432023-02-26 13:39:37 -080034using y2023::control_loops::drivetrain::GridSelectionHint;
James Kuszmaul055fe762023-03-03 21:14:01 -080035using y2023::control_loops::drivetrain::RowSelectionHint;
James Kuszmaul4e171432023-02-26 13:39:37 -080036using y2023::control_loops::drivetrain::SpotSelectionHint;
37using y2023::control_loops::drivetrain::TargetSelectorHint;
James Kuszmaul055fe762023-03-03 21:14:01 -080038using y2023::control_loops::superstructure::RollerGoal;
39using Side = frc971::control_loops::drivetrain::RobotSide;
Maxwell Hendersonad312342023-01-10 12:07:47 -080040
41namespace y2023 {
42namespace input {
43namespace joysticks {
44
milind-udefab712023-02-20 22:22:02 -080045// TODO(milind): add correct locations
Austin Schuh8e6f2872023-03-08 20:55:27 -080046const ButtonLocation kDriverSpit(2, 1);
Austin Schuh23a90022023-02-24 22:13:39 -080047const ButtonLocation kSpit(4, 13);
48
Austin Schuh9b3e41c2023-02-26 22:29:53 -080049const ButtonLocation kHighConeScoreLeft(4, 14);
50const ButtonLocation kHighConeScoreRight(3, 1);
51
52const ButtonLocation kMidConeScoreLeft(4, 15);
53const ButtonLocation kMidConeScoreRight(3, 2);
54
Austin Schuhe062be02023-03-04 21:12:07 -080055const ButtonLocation kLowConeScoreLeft(4, 16);
56const ButtonLocation kLowConeScoreRight(3, 3);
57
Austin Schuh9b3e41c2023-02-26 22:29:53 -080058const ButtonLocation kHighCube(4, 1);
59const ButtonLocation kMidCube(4, 2);
60const ButtonLocation kLowCube(4, 3);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080061
62const ButtonLocation kGroundPickupConeUp(4, 7);
Austin Schuhe062be02023-03-04 21:12:07 -080063const ButtonLocation kGroundPickupConeDownBase(4, 8);
milind-u71da5392023-02-26 12:45:00 -080064const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080065const ButtonLocation kHPConePickup(4, 6);
66
Austin Schuh9b3e41c2023-02-26 22:29:53 -080067const ButtonLocation kSuck(4, 11);
68const ButtonLocation kBack(4, 12);
milind-udefab712023-02-20 22:22:02 -080069
Austin Schuh6dc925b2023-02-24 16:23:32 -080070const ButtonLocation kWrist(4, 10);
Austin Schuh038b5452023-03-08 20:55:45 -080071const ButtonLocation kStayIn(3, 4);
Austin Schuh6dc925b2023-02-24 16:23:32 -080072
Maxwell Hendersonad312342023-01-10 12:07:47 -080073namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080074namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080075
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080076enum class GamePiece {
77 CONE_UP = 0,
78 CONE_DOWN = 1,
79 CUBE = 2,
80};
81
James Kuszmaul4ac45762023-03-04 19:10:55 -080082struct ButtonData {
83 ButtonLocation button;
84 std::optional<SpotSelectionHint> spot = std::nullopt;
85};
86
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080087struct ArmSetpoint {
88 uint32_t index;
89 double wrist_goal;
90 std::optional<double> score_wrist_goal = std::nullopt;
91 GamePiece game_piece;
James Kuszmaul4ac45762023-03-04 19:10:55 -080092 std::vector<ButtonData> buttons;
Austin Schuh9b3e41c2023-02-26 22:29:53 -080093 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -080094 std::optional<RowSelectionHint> row_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080095};
96
97const std::vector<ArmSetpoint> setpoints = {
98 {
99 .index = arm::GroundPickupBackConeUpIndex(),
100 .wrist_goal = 0.0,
101 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800102 .buttons = {{kGroundPickupConeUp}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800103 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800104 },
105 {
Austin Schuhe062be02023-03-04 21:12:07 -0800106 .index = arm::GroundPickupFrontConeUpIndex(),
107 .wrist_goal = 0.2,
108 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800109 .buttons = {{kGroundPickupConeUp}},
Austin Schuhe062be02023-03-04 21:12:07 -0800110 .side = Side::FRONT,
111 },
112 {
113 .index = arm::GroundPickupBackConeDownBaseIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800114 .wrist_goal = 0.0,
115 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800116 .buttons = {{kGroundPickupConeDownBase}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800117 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800118 },
119 {
Austin Schuhe062be02023-03-04 21:12:07 -0800120 .index = arm::GroundPickupFrontConeDownBaseIndex(),
121 .wrist_goal = 0.2,
122 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800123 .buttons = {{kGroundPickupConeDownBase}},
Austin Schuhe062be02023-03-04 21:12:07 -0800124 .side = Side::FRONT,
125 },
126 {
127 .index = arm::ScoreBackMidConeUpIndex(),
128 .wrist_goal = 0.0,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800129 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800130 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
131 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800132 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800133 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800134 },
135 {
Austin Schuhe062be02023-03-04 21:12:07 -0800136 .index = arm::ScoreBackLowConeUpIndex(),
137 .wrist_goal = 0.0,
138 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800139 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
140 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800141 .side = Side::BACK,
142 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800143 },
144 {
145 .index = arm::ScoreFrontLowConeUpIndex(),
146 .wrist_goal = 0.0,
147 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800148 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
149 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800150 .side = Side::FRONT,
151 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800152 },
153 {
154 .index = arm::ScoreBackMidConeDownBaseIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800155 .wrist_goal = 2.2,
156 .score_wrist_goal = 0.0,
157 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800158 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
159 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800160 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800161 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800162 },
163 {
Austin Schuhe062be02023-03-04 21:12:07 -0800164 .index = arm::ScoreBackLowConeDownBaseIndex(),
165 .wrist_goal = 0.0,
166 .score_wrist_goal = 0.0,
167 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800168 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
169 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800170 .side = Side::BACK,
171 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800172 },
173 {
174 .index = arm::ScoreFrontLowConeDownBaseIndex(),
175 .wrist_goal = 0.0,
176 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800177 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
178 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800179 .side = Side::FRONT,
180 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800181 },
182 {
183 .index = arm::ScoreFrontMidConeDownBaseIndex(),
184 .wrist_goal = 2.0,
185 .score_wrist_goal = 0.0,
186 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800187 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
188 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800189 .side = Side::FRONT,
190 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800191 },
192 {
193 .index = arm::ScoreFrontHighConeDownBaseIndex(),
194 .wrist_goal = 2.0,
195 .score_wrist_goal = 0.0,
196 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800197 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
198 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800199 .side = Side::FRONT,
200 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800201 },
202 {
203 .index = arm::HPPickupFrontConeUpIndex(),
204 .wrist_goal = 0.0,
205 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800206 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800207 .side = Side::FRONT,
208 },
209 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800210 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuhe062be02023-03-04 21:12:07 -0800211 .wrist_goal = 0.4,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800212 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800213 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800214 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800215 },
216 {
Austin Schuhe062be02023-03-04 21:12:07 -0800217 .index = arm::ScoreFrontHighConeUpIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800218 .wrist_goal = 0.05,
219 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800220 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
221 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800222 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800223 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800224 },
225 {
Austin Schuhe062be02023-03-04 21:12:07 -0800226 .index = arm::ScoreFrontMidConeUpIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800227 .wrist_goal = 0.05,
228 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800229 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
230 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800231 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800232 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800233 },
milind-u68842e12023-02-26 12:45:40 -0800234 {
235 .index = arm::GroundPickupBackCubeIndex(),
236 .wrist_goal = 0.6,
237 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800238 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800239 .side = Side::BACK,
240 },
241 {
242 .index = arm::ScoreFrontMidCubeIndex(),
243 .wrist_goal = 0.6,
244 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800245 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800246 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800247 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800248 },
249 {
250 .index = arm::ScoreBackMidCubeIndex(),
251 .wrist_goal = 0.6,
252 .score_wrist_goal = 0.0,
253 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800254 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800255 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800256 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800257 },
258 {
259 .index = arm::ScoreFrontLowCubeIndex(),
260 .wrist_goal = 0.6,
261 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800262 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800263 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800264 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800265 },
266 {
267 .index = arm::ScoreBackLowCubeIndex(),
268 .wrist_goal = 0.6,
269 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800270 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800271 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800272 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800273 },
274 {
275 .index = arm::ScoreFrontHighCubeIndex(),
276 .wrist_goal = 0.6,
277 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800278 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800279 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800280 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800281 },
282 {
283 .index = arm::ScoreBackHighCubeIndex(),
284 .wrist_goal = 0.6,
285 .score_wrist_goal = 0.0,
286 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800287 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800288 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800289 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800290 },
291 {
292 .index = arm::GroundPickupFrontCubeIndex(),
293 .wrist_goal = 0.6,
294 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800295 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800296 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800297 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800298};
299
Maxwell Hendersonad312342023-01-10 12:07:47 -0800300class Reader : public ::frc971::input::ActionJoystickInput {
301 public:
302 Reader(::aos::EventLoop *event_loop)
303 : ::frc971::input::ActionJoystickInput(
304 event_loop,
305 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
306 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
307 superstructure_goal_sender_(
308 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800309 target_selector_hint_sender_(
310 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800311 superstructure_status_fetcher_(
312 event_loop->MakeFetcher<superstructure::Status>(
313 "/superstructure")) {}
314
315 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
316
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800317 GamePiece current_game_piece_ = GamePiece::CONE_UP;
318
Maxwell Hendersonad312342023-01-10 12:07:47 -0800319 void HandleTeleop(
320 const ::frc971::input::driver_station::Data &data) override {
321 superstructure_status_fetcher_.Fetch();
322 if (!superstructure_status_fetcher_.get()) {
323 AOS_LOG(ERROR, "Got no superstructure status message.\n");
324 return;
325 }
326
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800327 if (!superstructure_status_fetcher_->has_wrist()) {
328 AOS_LOG(ERROR, "Got no superstructure status message.\n");
329 return;
330 }
milind-udefab712023-02-20 22:22:02 -0800331
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800332 double wrist_goal = 0.0;
333 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800334 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800335 std::optional<double> score_wrist_goal = std::nullopt;
336
337 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800338 roller_goal = RollerGoal::INTAKE_CONE_UP;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800339 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuhe062be02023-03-04 21:12:07 -0800340 } else if (data.IsPressed(kGroundPickupConeDownBase)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800341 roller_goal = RollerGoal::INTAKE_CONE_DOWN;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800342 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800343 } else if (data.IsPressed(kGroundPickupCube)) {
344 roller_goal = RollerGoal::INTAKE_CUBE;
345 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800346 }
347
milind-u68842e12023-02-26 12:45:40 -0800348 if (current_game_piece_ == GamePiece::CUBE) {
349 wrist_goal = 0.6;
350 }
351
James Kuszmaul4e171432023-02-26 13:39:37 -0800352 std::optional<RowSelectionHint> placing_row;
353 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800354
Austin Schuhe062be02023-03-04 21:12:07 -0800355 // Keep the setpoint if the button is still held. This lets us release the
356 // back button once a side has been selected.
357 if (current_setpoint_ != nullptr) {
358 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800359 for (const auto &button : current_setpoint_->buttons) {
360 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800361 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800362 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800363 }
364 }
365 if (!found) {
366 current_setpoint_ = nullptr;
367 }
368 }
369
370 // Ok, no active setpoint. Search for the right one.
371 if (current_setpoint_ == nullptr) {
372 const Side current_side =
373 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
374 // Search for the active setpoint.
375 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800376 for (const auto &button : setpoint.buttons) {
377 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800378 if (setpoint.game_piece == current_game_piece_ &&
379 setpoint.side == current_side) {
380 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800381 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800382 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800383 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800384 }
385 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800386 }
Austin Schuhe062be02023-03-04 21:12:07 -0800387
388 // And, pull the bits out of it.
389 if (current_setpoint_ != nullptr) {
Austin Schuh038b5452023-03-08 20:55:45 -0800390 if (!data.IsPressed(kStayIn)) {
391 wrist_goal = current_setpoint_->wrist_goal;
392 arm_goal_position_ = current_setpoint_->index;
393 score_wrist_goal = current_setpoint_->score_wrist_goal;
394 }
395
Austin Schuhe062be02023-03-04 21:12:07 -0800396 placing_row = current_setpoint_->row_hint;
Austin Schuhe062be02023-03-04 21:12:07 -0800397 }
398
James Kuszmaul4e171432023-02-26 13:39:37 -0800399 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800400
Austin Schuh23a90022023-02-24 22:13:39 -0800401 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800402 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh8e6f2872023-03-08 20:55:27 -0800403 } else if (data.IsPressed(kSpit) || data.IsPressed(kDriverSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800404 if (score_wrist_goal.has_value()) {
405 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800406
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800407 // If we are supposed to dunk it, wait until we are close enough to
408 // spit.
409 if (std::abs(score_wrist_goal.value() -
410 superstructure_status_fetcher_->wrist()->position()) <
411 0.1) {
412 roller_goal = RollerGoal::SPIT;
413 }
414 } else {
415 roller_goal = RollerGoal::SPIT;
416 }
milind-udefab712023-02-20 22:22:02 -0800417 }
418
Maxwell Hendersonad312342023-01-10 12:07:47 -0800419 {
420 auto builder = superstructure_goal_sender_.MakeBuilder();
421
Austin Schuh6dc925b2023-02-24 16:23:32 -0800422 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800423 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
424 *builder.fbb(), wrist_goal,
425 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800426
Maxwell Hendersonad312342023-01-10 12:07:47 -0800427 superstructure::Goal::Builder superstructure_goal_builder =
428 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800429 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800430 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800431 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800432 if (builder.Send(superstructure_goal_builder.Finish()) !=
433 aos::RawSender::Error::kOk) {
434 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
435 }
436 }
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800437 // TODO(james): Is there a more principled way to detect Human Player
438 // pickup? Probably don't bother fixing it until/unless we add more buttons
439 // that can select human player pickup.
440 if (data.IsPressed(kHPConePickup)) {
441 auto builder = target_selector_hint_sender_.MakeBuilder();
442 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
443 hint_builder.add_substation_pickup(true);
444 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
445 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
446 }
447 } else if (placing_row.has_value()) {
James Kuszmaul4e171432023-02-26 13:39:37 -0800448 auto builder = target_selector_hint_sender_.MakeBuilder();
449 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
450 hint_builder.add_row(placing_row.value());
451 hint_builder.add_spot(placing_spot.value());
James Kuszmaul055fe762023-03-03 21:14:01 -0800452 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul4e171432023-02-26 13:39:37 -0800453 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
454 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
455 }
456 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800457 }
458
459 private:
460 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800461 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800462
463 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800464
465 uint32_t arm_goal_position_;
Austin Schuhe062be02023-03-04 21:12:07 -0800466
467 const ArmSetpoint *current_setpoint_ = nullptr;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800468};
469
470} // namespace joysticks
471} // namespace input
472} // namespace y2023
473
474int main(int argc, char **argv) {
475 ::aos::InitGoogle(&argc, &argv);
476
477 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
478 aos::configuration::ReadConfig("aos_config.json");
479
480 ::aos::ShmEventLoop event_loop(&config.message());
481 ::y2023::input::joysticks::Reader reader(&event_loop);
482
483 event_loop.Run();
484
485 return 0;
486}