blob: 68fe6af2670a996c33e1c871c09bcace725a1d07 [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 Schuh6dc925b2023-02-24 16:23:32 -080046const ButtonLocation kScore(4, 4);
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);
71
Maxwell Hendersonad312342023-01-10 12:07:47 -080072namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080073namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080074
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080075enum class GamePiece {
76 CONE_UP = 0,
77 CONE_DOWN = 1,
78 CUBE = 2,
79};
80
James Kuszmaul4ac45762023-03-04 19:10:55 -080081struct ButtonData {
82 ButtonLocation button;
83 std::optional<SpotSelectionHint> spot = std::nullopt;
84};
85
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080086struct ArmSetpoint {
87 uint32_t index;
88 double wrist_goal;
89 std::optional<double> score_wrist_goal = std::nullopt;
90 GamePiece game_piece;
James Kuszmaul4ac45762023-03-04 19:10:55 -080091 std::vector<ButtonData> buttons;
Austin Schuh9b3e41c2023-02-26 22:29:53 -080092 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -080093 std::optional<RowSelectionHint> row_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080094};
95
96const std::vector<ArmSetpoint> setpoints = {
97 {
98 .index = arm::GroundPickupBackConeUpIndex(),
99 .wrist_goal = 0.0,
100 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800101 .buttons = {{kGroundPickupConeUp}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800102 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800103 },
104 {
Austin Schuhe062be02023-03-04 21:12:07 -0800105 .index = arm::GroundPickupFrontConeUpIndex(),
106 .wrist_goal = 0.2,
107 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800108 .buttons = {{kGroundPickupConeUp}},
Austin Schuhe062be02023-03-04 21:12:07 -0800109 .side = Side::FRONT,
110 },
111 {
112 .index = arm::GroundPickupBackConeDownBaseIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800113 .wrist_goal = 0.0,
114 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800115 .buttons = {{kGroundPickupConeDownBase}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800116 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800117 },
118 {
Austin Schuhe062be02023-03-04 21:12:07 -0800119 .index = arm::GroundPickupFrontConeDownBaseIndex(),
120 .wrist_goal = 0.2,
121 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800122 .buttons = {{kGroundPickupConeDownBase}},
Austin Schuhe062be02023-03-04 21:12:07 -0800123 .side = Side::FRONT,
124 },
125 {
126 .index = arm::ScoreBackMidConeUpIndex(),
127 .wrist_goal = 0.0,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800128 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800129 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
130 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800131 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800132 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800133 },
134 {
Austin Schuhe062be02023-03-04 21:12:07 -0800135 .index = arm::ScoreBackLowConeUpIndex(),
136 .wrist_goal = 0.0,
137 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800138 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
139 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800140 .side = Side::BACK,
141 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800142 },
143 {
144 .index = arm::ScoreFrontLowConeUpIndex(),
145 .wrist_goal = 0.0,
146 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800147 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
148 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800149 .side = Side::FRONT,
150 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800151 },
152 {
153 .index = arm::ScoreBackMidConeDownBaseIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800154 .wrist_goal = 2.2,
155 .score_wrist_goal = 0.0,
156 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800157 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
158 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800159 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800160 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800161 },
162 {
Austin Schuhe062be02023-03-04 21:12:07 -0800163 .index = arm::ScoreBackLowConeDownBaseIndex(),
164 .wrist_goal = 0.0,
165 .score_wrist_goal = 0.0,
166 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800167 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
168 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800169 .side = Side::BACK,
170 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800171 },
172 {
173 .index = arm::ScoreFrontLowConeDownBaseIndex(),
174 .wrist_goal = 0.0,
175 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800176 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
177 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800178 .side = Side::FRONT,
179 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800180 },
181 {
182 .index = arm::ScoreFrontMidConeDownBaseIndex(),
183 .wrist_goal = 2.0,
184 .score_wrist_goal = 0.0,
185 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800186 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
187 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800188 .side = Side::FRONT,
189 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800190 },
191 {
192 .index = arm::ScoreFrontHighConeDownBaseIndex(),
193 .wrist_goal = 2.0,
194 .score_wrist_goal = 0.0,
195 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800196 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
197 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800198 .side = Side::FRONT,
199 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800200 },
201 {
202 .index = arm::HPPickupFrontConeUpIndex(),
203 .wrist_goal = 0.0,
204 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800205 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800206 .side = Side::FRONT,
207 },
208 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800209 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuhe062be02023-03-04 21:12:07 -0800210 .wrist_goal = 0.4,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800211 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800212 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800213 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800214 },
215 {
Austin Schuhe062be02023-03-04 21:12:07 -0800216 .index = arm::ScoreFrontHighConeUpIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800217 .wrist_goal = 0.05,
218 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800219 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
220 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800221 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800222 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800223 },
224 {
Austin Schuhe062be02023-03-04 21:12:07 -0800225 .index = arm::ScoreFrontMidConeUpIndex(),
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800226 .wrist_goal = 0.05,
227 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800228 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
229 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800230 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800231 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800232 },
milind-u68842e12023-02-26 12:45:40 -0800233 {
234 .index = arm::GroundPickupBackCubeIndex(),
235 .wrist_goal = 0.6,
236 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800237 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800238 .side = Side::BACK,
239 },
240 {
241 .index = arm::ScoreFrontMidCubeIndex(),
242 .wrist_goal = 0.6,
243 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800244 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800245 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800246 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800247 },
248 {
249 .index = arm::ScoreBackMidCubeIndex(),
250 .wrist_goal = 0.6,
251 .score_wrist_goal = 0.0,
252 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800253 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800254 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800255 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800256 },
257 {
258 .index = arm::ScoreFrontLowCubeIndex(),
259 .wrist_goal = 0.6,
260 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800261 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800262 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800263 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800264 },
265 {
266 .index = arm::ScoreBackLowCubeIndex(),
267 .wrist_goal = 0.6,
268 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800269 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800270 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800271 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800272 },
273 {
274 .index = arm::ScoreFrontHighCubeIndex(),
275 .wrist_goal = 0.6,
276 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800277 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800278 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800279 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800280 },
281 {
282 .index = arm::ScoreBackHighCubeIndex(),
283 .wrist_goal = 0.6,
284 .score_wrist_goal = 0.0,
285 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800286 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800287 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800288 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800289 },
290 {
291 .index = arm::GroundPickupFrontCubeIndex(),
292 .wrist_goal = 0.6,
293 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800294 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800295 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800296 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800297};
298
Maxwell Hendersonad312342023-01-10 12:07:47 -0800299class Reader : public ::frc971::input::ActionJoystickInput {
300 public:
301 Reader(::aos::EventLoop *event_loop)
302 : ::frc971::input::ActionJoystickInput(
303 event_loop,
304 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
305 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
306 superstructure_goal_sender_(
307 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800308 target_selector_hint_sender_(
309 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800310 superstructure_status_fetcher_(
311 event_loop->MakeFetcher<superstructure::Status>(
312 "/superstructure")) {}
313
314 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
315
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800316 GamePiece current_game_piece_ = GamePiece::CONE_UP;
317
Maxwell Hendersonad312342023-01-10 12:07:47 -0800318 void HandleTeleop(
319 const ::frc971::input::driver_station::Data &data) override {
320 superstructure_status_fetcher_.Fetch();
321 if (!superstructure_status_fetcher_.get()) {
322 AOS_LOG(ERROR, "Got no superstructure status message.\n");
323 return;
324 }
325
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800326 if (!superstructure_status_fetcher_->has_wrist()) {
327 AOS_LOG(ERROR, "Got no superstructure status message.\n");
328 return;
329 }
milind-udefab712023-02-20 22:22:02 -0800330
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800331 double wrist_goal = 0.0;
332 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800333 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800334 std::optional<double> score_wrist_goal = std::nullopt;
335
336 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800337 roller_goal = RollerGoal::INTAKE_CONE_UP;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800338 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuhe062be02023-03-04 21:12:07 -0800339 } else if (data.IsPressed(kGroundPickupConeDownBase)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800340 roller_goal = RollerGoal::INTAKE_CONE_DOWN;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800341 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800342 } else if (data.IsPressed(kGroundPickupCube)) {
343 roller_goal = RollerGoal::INTAKE_CUBE;
344 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800345 }
346
milind-u68842e12023-02-26 12:45:40 -0800347 if (current_game_piece_ == GamePiece::CUBE) {
348 wrist_goal = 0.6;
349 }
350
James Kuszmaul4e171432023-02-26 13:39:37 -0800351 std::optional<RowSelectionHint> placing_row;
352 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800353
Austin Schuhe062be02023-03-04 21:12:07 -0800354 // Keep the setpoint if the button is still held. This lets us release the
355 // back button once a side has been selected.
356 if (current_setpoint_ != nullptr) {
357 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800358 for (const auto &button : current_setpoint_->buttons) {
359 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800360 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800361 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800362 }
363 }
364 if (!found) {
365 current_setpoint_ = nullptr;
366 }
367 }
368
369 // Ok, no active setpoint. Search for the right one.
370 if (current_setpoint_ == nullptr) {
371 const Side current_side =
372 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
373 // Search for the active setpoint.
374 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800375 for (const auto &button : setpoint.buttons) {
376 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800377 if (setpoint.game_piece == current_game_piece_ &&
378 setpoint.side == current_side) {
379 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800380 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800381 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800382 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800383 }
384 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800385 }
Austin Schuhe062be02023-03-04 21:12:07 -0800386
387 // And, pull the bits out of it.
388 if (current_setpoint_ != nullptr) {
389 wrist_goal = current_setpoint_->wrist_goal;
390 arm_goal_position_ = current_setpoint_->index;
391 score_wrist_goal = current_setpoint_->score_wrist_goal;
392 placing_row = current_setpoint_->row_hint;
Austin Schuhe062be02023-03-04 21:12:07 -0800393 }
394
James Kuszmaul4e171432023-02-26 13:39:37 -0800395 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800396
Austin Schuh23a90022023-02-24 22:13:39 -0800397 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800398 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh23a90022023-02-24 22:13:39 -0800399 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800400 if (score_wrist_goal.has_value()) {
401 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800402
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800403 // If we are supposed to dunk it, wait until we are close enough to
404 // spit.
405 if (std::abs(score_wrist_goal.value() -
406 superstructure_status_fetcher_->wrist()->position()) <
407 0.1) {
408 roller_goal = RollerGoal::SPIT;
409 }
410 } else {
411 roller_goal = RollerGoal::SPIT;
412 }
milind-udefab712023-02-20 22:22:02 -0800413 }
414
Maxwell Hendersonad312342023-01-10 12:07:47 -0800415 {
416 auto builder = superstructure_goal_sender_.MakeBuilder();
417
Austin Schuh6dc925b2023-02-24 16:23:32 -0800418 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800419 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
420 *builder.fbb(), wrist_goal,
421 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800422
Maxwell Hendersonad312342023-01-10 12:07:47 -0800423 superstructure::Goal::Builder superstructure_goal_builder =
424 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800425 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800426 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800427 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800428 if (builder.Send(superstructure_goal_builder.Finish()) !=
429 aos::RawSender::Error::kOk) {
430 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
431 }
432 }
James Kuszmaul4e171432023-02-26 13:39:37 -0800433 if (placing_row.has_value()) {
434 auto builder = target_selector_hint_sender_.MakeBuilder();
435 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
436 hint_builder.add_row(placing_row.value());
437 hint_builder.add_spot(placing_spot.value());
James Kuszmaul055fe762023-03-03 21:14:01 -0800438 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul4e171432023-02-26 13:39:37 -0800439 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
440 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
441 }
442 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800443 }
444
445 private:
446 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800447 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800448
449 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800450
451 uint32_t arm_goal_position_;
Austin Schuhe062be02023-03-04 21:12:07 -0800452
453 const ArmSetpoint *current_setpoint_ = nullptr;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800454};
455
456} // namespace joysticks
457} // namespace input
458} // namespace y2023
459
460int main(int argc, char **argv) {
461 ::aos::InitGoogle(&argc, &argv);
462
463 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
464 aos::configuration::ReadConfig("aos_config.json");
465
466 ::aos::ShmEventLoop event_loop(&config.message());
467 ::y2023::input::joysticks::Reader reader(&event_loop);
468
469 event_loop.Run();
470
471 return 0;
472}