blob: 135c987fb5e184c636c684492e8ffc2700aafac2 [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;
Maxwell Henderson5938a832023-02-23 09:33:15 -080034using y2023::control_loops::superstructure::RollerGoal;
James Kuszmaul4e171432023-02-26 13:39:37 -080035using y2023::control_loops::drivetrain::RowSelectionHint;
36using y2023::control_loops::drivetrain::GridSelectionHint;
37using y2023::control_loops::drivetrain::SpotSelectionHint;
38using y2023::control_loops::drivetrain::TargetSelectorHint;
Maxwell Hendersonad312342023-01-10 12:07:47 -080039
40namespace y2023 {
41namespace input {
42namespace joysticks {
43
milind-udefab712023-02-20 22:22:02 -080044// TODO(milind): add correct locations
Austin Schuh6dc925b2023-02-24 16:23:32 -080045const ButtonLocation kScore(4, 4);
Austin Schuh23a90022023-02-24 22:13:39 -080046const ButtonLocation kSpit(4, 13);
47
Austin Schuh9b3e41c2023-02-26 22:29:53 -080048const ButtonLocation kHighConeScoreLeft(4, 14);
49const ButtonLocation kHighConeScoreRight(3, 1);
50
51const ButtonLocation kMidConeScoreLeft(4, 15);
52const ButtonLocation kMidConeScoreRight(3, 2);
53
Austin Schuhe062be02023-03-04 21:12:07 -080054const ButtonLocation kLowConeScoreLeft(4, 16);
55const ButtonLocation kLowConeScoreRight(3, 3);
56
Austin Schuh9b3e41c2023-02-26 22:29:53 -080057const ButtonLocation kHighCube(4, 1);
58const ButtonLocation kMidCube(4, 2);
59const ButtonLocation kLowCube(4, 3);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080060
61const ButtonLocation kGroundPickupConeUp(4, 7);
Austin Schuhe062be02023-03-04 21:12:07 -080062const ButtonLocation kGroundPickupConeDownBase(4, 8);
milind-u71da5392023-02-26 12:45:00 -080063const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080064const ButtonLocation kHPConePickup(4, 6);
65
Austin Schuh9b3e41c2023-02-26 22:29:53 -080066const ButtonLocation kSuck(4, 11);
67const ButtonLocation kBack(4, 12);
milind-udefab712023-02-20 22:22:02 -080068
Austin Schuh6dc925b2023-02-24 16:23:32 -080069const ButtonLocation kWrist(4, 10);
70
Maxwell Hendersonad312342023-01-10 12:07:47 -080071namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080072namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080073
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080074enum class GamePiece {
75 CONE_UP = 0,
76 CONE_DOWN = 1,
77 CUBE = 2,
78};
79
Austin Schuh9b3e41c2023-02-26 22:29:53 -080080enum class Side {
81 FRONT = 0,
82 BACK = 1,
83};
84
James Kuszmaul4ac45762023-03-04 19:10:55 -080085struct ButtonData {
86 ButtonLocation button;
87 std::optional<SpotSelectionHint> spot = std::nullopt;
88};
89
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080090struct ArmSetpoint {
91 uint32_t index;
92 double wrist_goal;
93 std::optional<double> score_wrist_goal = std::nullopt;
94 GamePiece game_piece;
James Kuszmaul4ac45762023-03-04 19:10:55 -080095 std::vector<ButtonData> buttons;
Austin Schuh9b3e41c2023-02-26 22:29:53 -080096 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -080097 std::optional<RowSelectionHint> row_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080098};
99
100const std::vector<ArmSetpoint> setpoints = {
101 {
102 .index = arm::GroundPickupBackConeUpIndex(),
103 .wrist_goal = 0.0,
104 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800105 .buttons = {{kGroundPickupConeUp}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800106 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800107 },
108 {
Austin Schuhe062be02023-03-04 21:12:07 -0800109 .index = arm::GroundPickupFrontConeUpIndex(),
110 .wrist_goal = 0.2,
111 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800112 .buttons = {{kGroundPickupConeUp}},
Austin Schuhe062be02023-03-04 21:12:07 -0800113 .side = Side::FRONT,
114 },
115 {
116 .index = arm::GroundPickupBackConeDownBaseIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800117 .wrist_goal = 0.0,
118 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800119 .buttons = {{kGroundPickupConeDownBase}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800120 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800121 },
122 {
Austin Schuhe062be02023-03-04 21:12:07 -0800123 .index = arm::GroundPickupFrontConeDownBaseIndex(),
124 .wrist_goal = 0.2,
125 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800126 .buttons = {{kGroundPickupConeDownBase}},
Austin Schuhe062be02023-03-04 21:12:07 -0800127 .side = Side::FRONT,
128 },
129 {
130 .index = arm::ScoreBackMidConeUpIndex(),
131 .wrist_goal = 0.0,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800132 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800133 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
134 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800135 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800136 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800137 },
138 {
Austin Schuhe062be02023-03-04 21:12:07 -0800139 .index = arm::ScoreBackLowConeUpIndex(),
140 .wrist_goal = 0.0,
141 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800142 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
143 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800144 .side = Side::BACK,
145 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800146 },
147 {
148 .index = arm::ScoreFrontLowConeUpIndex(),
149 .wrist_goal = 0.0,
150 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800151 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
152 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800153 .side = Side::FRONT,
154 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800155 },
156 {
157 .index = arm::ScoreBackMidConeDownBaseIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800158 .wrist_goal = 2.2,
159 .score_wrist_goal = 0.0,
160 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800161 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
162 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800163 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800164 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800165 },
166 {
Austin Schuhe062be02023-03-04 21:12:07 -0800167 .index = arm::ScoreBackLowConeDownBaseIndex(),
168 .wrist_goal = 0.0,
169 .score_wrist_goal = 0.0,
170 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800171 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
172 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800173 .side = Side::BACK,
174 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800175 },
176 {
177 .index = arm::ScoreFrontLowConeDownBaseIndex(),
178 .wrist_goal = 0.0,
179 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800180 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
181 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800182 .side = Side::FRONT,
183 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800184 },
185 {
186 .index = arm::ScoreFrontMidConeDownBaseIndex(),
187 .wrist_goal = 2.0,
188 .score_wrist_goal = 0.0,
189 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800190 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
191 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800192 .side = Side::FRONT,
193 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800194 },
195 {
196 .index = arm::ScoreFrontHighConeDownBaseIndex(),
197 .wrist_goal = 2.0,
198 .score_wrist_goal = 0.0,
199 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800200 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
201 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800202 .side = Side::FRONT,
203 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800204 },
205 {
206 .index = arm::HPPickupFrontConeUpIndex(),
207 .wrist_goal = 0.0,
208 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800209 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800210 .side = Side::FRONT,
211 },
212 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800213 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuhe062be02023-03-04 21:12:07 -0800214 .wrist_goal = 0.4,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800215 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800216 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800217 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800218 },
219 {
Austin Schuhe062be02023-03-04 21:12:07 -0800220 .index = arm::ScoreFrontHighConeUpIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800221 .wrist_goal = 0.05,
222 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800223 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
224 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800225 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800226 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800227 },
228 {
Austin Schuhe062be02023-03-04 21:12:07 -0800229 .index = arm::ScoreFrontMidConeUpIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800230 .wrist_goal = 0.05,
231 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800232 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
233 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800234 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800235 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800236 },
milind-u68842e12023-02-26 12:45:40 -0800237 {
238 .index = arm::GroundPickupBackCubeIndex(),
239 .wrist_goal = 0.6,
240 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800241 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800242 .side = Side::BACK,
243 },
244 {
245 .index = arm::ScoreFrontMidCubeIndex(),
246 .wrist_goal = 0.6,
247 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800248 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800249 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800250 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800251 },
252 {
253 .index = arm::ScoreBackMidCubeIndex(),
254 .wrist_goal = 0.6,
255 .score_wrist_goal = 0.0,
256 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800257 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800258 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800259 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800260 },
261 {
262 .index = arm::ScoreFrontLowCubeIndex(),
263 .wrist_goal = 0.6,
264 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800265 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800266 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800267 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800268 },
269 {
270 .index = arm::ScoreBackLowCubeIndex(),
271 .wrist_goal = 0.6,
272 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800273 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800274 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800275 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800276 },
277 {
278 .index = arm::ScoreFrontHighCubeIndex(),
279 .wrist_goal = 0.6,
280 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800281 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800282 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800283 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800284 },
285 {
286 .index = arm::ScoreBackHighCubeIndex(),
287 .wrist_goal = 0.6,
288 .score_wrist_goal = 0.0,
289 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800290 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800291 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800292 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800293 },
294 {
295 .index = arm::GroundPickupFrontCubeIndex(),
296 .wrist_goal = 0.6,
297 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800298 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800299 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800300 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800301};
302
Maxwell Hendersonad312342023-01-10 12:07:47 -0800303class Reader : public ::frc971::input::ActionJoystickInput {
304 public:
305 Reader(::aos::EventLoop *event_loop)
306 : ::frc971::input::ActionJoystickInput(
307 event_loop,
308 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
309 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
310 superstructure_goal_sender_(
311 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800312 target_selector_hint_sender_(
313 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800314 superstructure_status_fetcher_(
315 event_loop->MakeFetcher<superstructure::Status>(
316 "/superstructure")) {}
317
318 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
319
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800320 GamePiece current_game_piece_ = GamePiece::CONE_UP;
321
Maxwell Hendersonad312342023-01-10 12:07:47 -0800322 void HandleTeleop(
323 const ::frc971::input::driver_station::Data &data) override {
324 superstructure_status_fetcher_.Fetch();
325 if (!superstructure_status_fetcher_.get()) {
326 AOS_LOG(ERROR, "Got no superstructure status message.\n");
327 return;
328 }
329
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800330 if (!superstructure_status_fetcher_->has_wrist()) {
331 AOS_LOG(ERROR, "Got no superstructure status message.\n");
332 return;
333 }
milind-udefab712023-02-20 22:22:02 -0800334
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800335 double wrist_goal = 0.0;
336 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800337 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800338 std::optional<double> score_wrist_goal = std::nullopt;
339
340 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
milind-u71da5392023-02-26 12:45:00 -0800341 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800342 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuhe062be02023-03-04 21:12:07 -0800343 } else if (data.IsPressed(kGroundPickupConeDownBase)) {
milind-u71da5392023-02-26 12:45:00 -0800344 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800345 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800346 } else if (data.IsPressed(kGroundPickupCube)) {
347 roller_goal = RollerGoal::INTAKE_CUBE;
348 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800349 }
350
milind-u68842e12023-02-26 12:45:40 -0800351 if (current_game_piece_ == GamePiece::CUBE) {
352 wrist_goal = 0.6;
353 }
354
James Kuszmaul4e171432023-02-26 13:39:37 -0800355 std::optional<RowSelectionHint> placing_row;
356 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800357
Austin Schuhe062be02023-03-04 21:12:07 -0800358 // Keep the setpoint if the button is still held. This lets us release the
359 // back button once a side has been selected.
360 if (current_setpoint_ != nullptr) {
361 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800362 for (const auto &button : current_setpoint_->buttons) {
363 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800364 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800365 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800366 }
367 }
368 if (!found) {
369 current_setpoint_ = nullptr;
370 }
371 }
372
373 // Ok, no active setpoint. Search for the right one.
374 if (current_setpoint_ == nullptr) {
375 const Side current_side =
376 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
377 // Search for the active setpoint.
378 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800379 for (const auto &button : setpoint.buttons) {
380 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800381 if (setpoint.game_piece == current_game_piece_ &&
382 setpoint.side == current_side) {
383 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800384 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800385 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800386 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800387 }
388 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800389 }
Austin Schuhe062be02023-03-04 21:12:07 -0800390
391 // And, pull the bits out of it.
392 if (current_setpoint_ != nullptr) {
393 wrist_goal = current_setpoint_->wrist_goal;
394 arm_goal_position_ = current_setpoint_->index;
395 score_wrist_goal = current_setpoint_->score_wrist_goal;
396 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 Schuh23a90022023-02-24 22:13:39 -0800403 } else if (data.IsPressed(kSpit)) {
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 Kuszmaul4e171432023-02-26 13:39:37 -0800437 if (placing_row.has_value()) {
438 auto builder = target_selector_hint_sender_.MakeBuilder();
439 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
440 hint_builder.add_row(placing_row.value());
441 hint_builder.add_spot(placing_spot.value());
442 // TODO: Add field to TargetSelector hint for forwards vs. backwards
443 // placement.
444 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
445 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
446 }
447 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800448 }
449
450 private:
451 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800452 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800453
454 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800455
456 uint32_t arm_goal_position_;
Austin Schuhe062be02023-03-04 21:12:07 -0800457
458 const ArmSetpoint *current_setpoint_ = nullptr;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800459};
460
461} // namespace joysticks
462} // namespace input
463} // namespace y2023
464
465int main(int argc, char **argv) {
466 ::aos::InitGoogle(&argc, &argv);
467
468 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
469 aos::configuration::ReadConfig("aos_config.json");
470
471 ::aos::ShmEventLoop event_loop(&config.message());
472 ::y2023::input::joysticks::Reader reader(&event_loop);
473
474 event_loop.Run();
475
476 return 0;
477}