blob: 9efd84899cf03a79af2e7c283663a0dbdf09845f [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
Austin Schuh99dda682023-03-11 00:18:37 -080045constexpr double kConeWrist = 0.4;
46constexpr double kCubeWrist = 1.0;
47
milind-udefab712023-02-20 22:22:02 -080048// TODO(milind): add correct locations
Austin Schuh8e6f2872023-03-08 20:55:27 -080049const ButtonLocation kDriverSpit(2, 1);
Austin Schuh23a90022023-02-24 22:13:39 -080050const ButtonLocation kSpit(4, 13);
51
Austin Schuh9b3e41c2023-02-26 22:29:53 -080052const ButtonLocation kHighConeScoreLeft(4, 14);
53const ButtonLocation kHighConeScoreRight(3, 1);
54
55const ButtonLocation kMidConeScoreLeft(4, 15);
56const ButtonLocation kMidConeScoreRight(3, 2);
57
Austin Schuhe062be02023-03-04 21:12:07 -080058const ButtonLocation kLowConeScoreLeft(4, 16);
59const ButtonLocation kLowConeScoreRight(3, 3);
60
Austin Schuh9b3e41c2023-02-26 22:29:53 -080061const ButtonLocation kHighCube(4, 1);
62const ButtonLocation kMidCube(4, 2);
63const ButtonLocation kLowCube(4, 3);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080064
65const ButtonLocation kGroundPickupConeUp(4, 7);
Austin Schuh99dda682023-03-11 00:18:37 -080066const ButtonLocation kGroundPickupConeDown(4, 8);
milind-u71da5392023-02-26 12:45:00 -080067const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080068const ButtonLocation kHPConePickup(4, 6);
69
Austin Schuh9b3e41c2023-02-26 22:29:53 -080070const ButtonLocation kSuck(4, 11);
71const ButtonLocation kBack(4, 12);
milind-udefab712023-02-20 22:22:02 -080072
Austin Schuh6dc925b2023-02-24 16:23:32 -080073const ButtonLocation kWrist(4, 10);
Austin Schuh038b5452023-03-08 20:55:45 -080074const ButtonLocation kStayIn(3, 4);
Austin Schuh6dc925b2023-02-24 16:23:32 -080075
Austin Schuh99dda682023-03-11 00:18:37 -080076const ButtonLocation kConeDownTip(4, 4);
77const ButtonLocation kConeDownBase(4, 5);
78
Maxwell Hendersonad312342023-01-10 12:07:47 -080079namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080080namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080081
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080082enum class GamePiece {
83 CONE_UP = 0,
84 CONE_DOWN = 1,
85 CUBE = 2,
Austin Schuh99dda682023-03-11 00:18:37 -080086 CONE_TIP = 4,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080087};
88
James Kuszmaul4ac45762023-03-04 19:10:55 -080089struct ButtonData {
90 ButtonLocation button;
91 std::optional<SpotSelectionHint> spot = std::nullopt;
92};
93
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080094struct ArmSetpoint {
95 uint32_t index;
Austin Schuh99dda682023-03-11 00:18:37 -080096 std::optional<uint32_t> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080097 double wrist_goal;
98 std::optional<double> score_wrist_goal = std::nullopt;
99 GamePiece game_piece;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800100 std::vector<ButtonData> buttons;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800101 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -0800102 std::optional<RowSelectionHint> row_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800103};
104
105const std::vector<ArmSetpoint> setpoints = {
106 {
107 .index = arm::GroundPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800108 .wrist_goal = 0.7,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800109 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800110 .buttons = {{kGroundPickupConeUp}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800111 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800112 },
113 {
Austin Schuhe062be02023-03-04 21:12:07 -0800114 .index = arm::GroundPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800115 .wrist_goal = 0.6,
Austin Schuhe062be02023-03-04 21:12:07 -0800116 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800117 .buttons = {{kGroundPickupConeUp}},
Austin Schuhe062be02023-03-04 21:12:07 -0800118 .side = Side::FRONT,
119 },
120 {
121 .index = arm::GroundPickupBackConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800122 .wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800123 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh99dda682023-03-11 00:18:37 -0800124 .buttons = {{kGroundPickupConeDown}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800125 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800126 },
127 {
Austin Schuhe062be02023-03-04 21:12:07 -0800128 .index = arm::GroundPickupFrontConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800129 .wrist_goal = 0.6,
Austin Schuhe062be02023-03-04 21:12:07 -0800130 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh99dda682023-03-11 00:18:37 -0800131 .buttons = {{kGroundPickupConeDown}},
Austin Schuhe062be02023-03-04 21:12:07 -0800132 .side = Side::FRONT,
133 },
134 {
Austin Schuh99dda682023-03-11 00:18:37 -0800135 .index = arm::ScoreBackLowConeDownTipIndex(),
136 .wrist_goal = 0.7,
137 .game_piece = GamePiece::CONE_TIP,
138 .buttons = {{kLowConeScoreRight, SpotSelectionHint::RIGHT},
139 {kLowCube, SpotSelectionHint::MIDDLE},
140 {kLowConeScoreLeft, SpotSelectionHint::LEFT}},
141 .side = Side::BACK,
142 .row_hint = RowSelectionHint::BOTTOM,
143 },
144 {
145 .index = arm::ScoreBackMidConeDownTipIndex(),
146 .place_index = arm::ScoreBackMidConeDownTipPlacedIndex(),
147 .wrist_goal = 0.8,
148 .score_wrist_goal = 2.0,
149 .game_piece = GamePiece::CONE_TIP,
150 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
151 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
152 .side = Side::BACK,
153 .row_hint = RowSelectionHint::MIDDLE,
154 },
155 {
156 .index = arm::ScoreFrontMidConeDownTipIndex(),
157 .place_index = arm::ScoreFrontMidConeDownTipPlacedIndex(),
Austin Schuhe062be02023-03-04 21:12:07 -0800158 .wrist_goal = 0.0,
Austin Schuh99dda682023-03-11 00:18:37 -0800159 .score_wrist_goal = 1.4,
160 .game_piece = GamePiece::CONE_TIP,
161 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
162 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
163 .side = Side::FRONT,
164 .row_hint = RowSelectionHint::MIDDLE,
165 },
166 {
167 .index = arm::ScoreFrontHighConeDownTipIndex(),
168 .place_index = arm::ScoreFrontHighConeDownTipPlacedIndex(),
169 .wrist_goal = 0.4,
170 .score_wrist_goal = 1.4,
171 .game_piece = GamePiece::CONE_TIP,
172 .buttons = {{kHighConeScoreRight, SpotSelectionHint::RIGHT},
173 {kHighConeScoreLeft, SpotSelectionHint::LEFT}},
174 .side = Side::FRONT,
175 .row_hint = RowSelectionHint::TOP,
176 },
177 {
178 .index = arm::ScoreFrontLowConeDownTipIndex(),
179 .wrist_goal = 2.8,
180 .game_piece = GamePiece::CONE_TIP,
181 .buttons = {{kLowConeScoreRight, SpotSelectionHint::RIGHT},
182 {kLowCube, SpotSelectionHint::MIDDLE},
183 {kLowConeScoreLeft, SpotSelectionHint::LEFT}},
184 .side = Side::FRONT,
185 .row_hint = RowSelectionHint::TOP,
186 },
187 {
188 .index = arm::ScoreBackMidConeUpIndex(),
189 .wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800190 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800191 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
192 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800193 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800194 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800195 },
196 {
Austin Schuhe062be02023-03-04 21:12:07 -0800197 .index = arm::ScoreBackLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800198 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800199 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800200 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
201 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800202 .side = Side::BACK,
203 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800204 },
205 {
206 .index = arm::ScoreFrontLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800207 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800208 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800209 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
210 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800211 .side = Side::FRONT,
212 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800213 },
214 {
215 .index = arm::ScoreBackMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800216 .wrist_goal = 2.5,
217 .score_wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800218 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800219 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
220 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800221 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800222 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800223 },
224 {
Austin Schuhe062be02023-03-04 21:12:07 -0800225 .index = arm::ScoreBackLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800226 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800227 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800228 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
229 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800230 .side = Side::BACK,
231 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800232 },
233 {
234 .index = arm::ScoreFrontLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800235 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800236 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800237 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
238 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800239 .side = Side::FRONT,
240 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800241 },
242 {
243 .index = arm::ScoreFrontMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800244 .wrist_goal = 2.6,
245 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800246 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800247 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
248 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800249 .side = Side::FRONT,
250 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800251 },
252 {
253 .index = arm::ScoreFrontHighConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800254 .wrist_goal = 2.6,
255 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800256 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800257 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
258 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800259 .side = Side::FRONT,
260 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800261 },
262 {
263 .index = arm::HPPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800264 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800265 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800266 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800267 .side = Side::FRONT,
268 },
269 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800270 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800271 .wrist_goal = 0.5,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800272 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800273 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800274 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800275 },
276 {
Austin Schuhe062be02023-03-04 21:12:07 -0800277 .index = arm::ScoreFrontHighConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800278 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800279 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800280 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
281 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800282 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800283 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800284 },
285 {
Austin Schuhe062be02023-03-04 21:12:07 -0800286 .index = arm::ScoreFrontMidConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800287 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800288 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800289 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
290 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800291 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800292 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800293 },
milind-u68842e12023-02-26 12:45:40 -0800294 {
295 .index = arm::GroundPickupBackCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800296 .wrist_goal = kCubeWrist,
milind-u68842e12023-02-26 12:45:40 -0800297 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800298 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800299 .side = Side::BACK,
300 },
301 {
302 .index = arm::ScoreFrontMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800303 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800304 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800305 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800306 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800307 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800308 },
309 {
310 .index = arm::ScoreBackMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800311 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800312 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800313 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800314 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800315 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800316 },
317 {
318 .index = arm::ScoreFrontLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800319 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800320 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800321 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800322 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800323 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800324 },
325 {
326 .index = arm::ScoreBackLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800327 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800328 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800329 .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800330 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800331 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800332 },
333 {
334 .index = arm::ScoreFrontHighCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800335 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800336 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800337 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800338 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800339 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800340 },
341 {
342 .index = arm::ScoreBackHighCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800343 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800344 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800345 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800346 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800347 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800348 },
349 {
350 .index = arm::GroundPickupFrontCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800351 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800352 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800353 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800354 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800355 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800356};
357
Maxwell Hendersonad312342023-01-10 12:07:47 -0800358class Reader : public ::frc971::input::ActionJoystickInput {
359 public:
360 Reader(::aos::EventLoop *event_loop)
361 : ::frc971::input::ActionJoystickInput(
362 event_loop,
363 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
364 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
365 superstructure_goal_sender_(
366 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800367 target_selector_hint_sender_(
368 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800369 superstructure_status_fetcher_(
370 event_loop->MakeFetcher<superstructure::Status>(
371 "/superstructure")) {}
372
373 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
374
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800375 GamePiece current_game_piece_ = GamePiece::CONE_UP;
376
Maxwell Hendersonad312342023-01-10 12:07:47 -0800377 void HandleTeleop(
378 const ::frc971::input::driver_station::Data &data) override {
379 superstructure_status_fetcher_.Fetch();
380 if (!superstructure_status_fetcher_.get()) {
381 AOS_LOG(ERROR, "Got no superstructure status message.\n");
382 return;
383 }
384
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800385 if (!superstructure_status_fetcher_->has_wrist()) {
386 AOS_LOG(ERROR, "Got no superstructure status message.\n");
387 return;
388 }
milind-udefab712023-02-20 22:22:02 -0800389
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800390 double wrist_goal = 0.0;
391 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800392 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800393 std::optional<double> score_wrist_goal = std::nullopt;
Austin Schuh99dda682023-03-11 00:18:37 -0800394 std::optional<double> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800395
396 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800397 roller_goal = RollerGoal::INTAKE_CONE_UP;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800398 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuh99dda682023-03-11 00:18:37 -0800399 } else if (data.IsPressed(kGroundPickupConeDown)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800400 roller_goal = RollerGoal::INTAKE_CONE_DOWN;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800401 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800402 } else if (data.IsPressed(kGroundPickupCube)) {
403 roller_goal = RollerGoal::INTAKE_CUBE;
404 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800405 }
406
Austin Schuh99dda682023-03-11 00:18:37 -0800407 if (current_game_piece_ == GamePiece::CONE_DOWN ||
408 current_game_piece_ == GamePiece::CONE_TIP) {
409 if (data.IsPressed(kConeDownTip)) {
410 current_game_piece_ = GamePiece::CONE_TIP;
411 } else if (data.IsPressed(kConeDownBase)) {
412 current_game_piece_ = GamePiece::CONE_DOWN;
413 }
414 }
415
milind-u68842e12023-02-26 12:45:40 -0800416 if (current_game_piece_ == GamePiece::CUBE) {
Austin Schuh99dda682023-03-11 00:18:37 -0800417 wrist_goal = kCubeWrist;
milind-u68842e12023-02-26 12:45:40 -0800418 }
419
James Kuszmaul4e171432023-02-26 13:39:37 -0800420 std::optional<RowSelectionHint> placing_row;
421 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800422
Austin Schuhe062be02023-03-04 21:12:07 -0800423 // Keep the setpoint if the button is still held. This lets us release the
424 // back button once a side has been selected.
425 if (current_setpoint_ != nullptr) {
426 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800427 for (const auto &button : current_setpoint_->buttons) {
428 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800429 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800430 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800431 }
432 }
433 if (!found) {
434 current_setpoint_ = nullptr;
435 }
436 }
437
438 // Ok, no active setpoint. Search for the right one.
439 if (current_setpoint_ == nullptr) {
440 const Side current_side =
441 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
442 // Search for the active setpoint.
443 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800444 for (const auto &button : setpoint.buttons) {
445 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800446 if (setpoint.game_piece == current_game_piece_ &&
447 setpoint.side == current_side) {
448 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800449 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800450 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800451 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800452 }
453 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800454 }
Austin Schuhe062be02023-03-04 21:12:07 -0800455
456 // And, pull the bits out of it.
457 if (current_setpoint_ != nullptr) {
Austin Schuh038b5452023-03-08 20:55:45 -0800458 if (!data.IsPressed(kStayIn)) {
459 wrist_goal = current_setpoint_->wrist_goal;
460 arm_goal_position_ = current_setpoint_->index;
461 score_wrist_goal = current_setpoint_->score_wrist_goal;
Austin Schuh99dda682023-03-11 00:18:37 -0800462 place_index = current_setpoint_->place_index;
Austin Schuh038b5452023-03-08 20:55:45 -0800463 }
464
Austin Schuhe062be02023-03-04 21:12:07 -0800465 placing_row = current_setpoint_->row_hint;
Austin Schuhe062be02023-03-04 21:12:07 -0800466 }
467
James Kuszmaul4e171432023-02-26 13:39:37 -0800468 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800469
Austin Schuh23a90022023-02-24 22:13:39 -0800470 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800471 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh8e6f2872023-03-08 20:55:27 -0800472 } else if (data.IsPressed(kSpit) || data.IsPressed(kDriverSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800473 if (score_wrist_goal.has_value()) {
474 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800475
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800476 // If we are supposed to dunk it, wait until we are close enough to
477 // spit.
478 if (std::abs(score_wrist_goal.value() -
Austin Schuh99dda682023-03-11 00:18:37 -0800479 superstructure_status_fetcher_->wrist()->goal_position()) <
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800480 0.1) {
Austin Schuh99dda682023-03-11 00:18:37 -0800481 if (place_index.has_value()) {
482 arm_goal_position_ = place_index.value();
483 if (arm_goal_position_ ==
484 superstructure_status_fetcher_->arm()->current_node() &&
485 superstructure_status_fetcher_->arm()->path_distance_to_go() <
486 0.01) {
487 roller_goal = RollerGoal::SPIT;
488 }
489 } else {
490 roller_goal = RollerGoal::SPIT;
491 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800492 }
493 } else {
494 roller_goal = RollerGoal::SPIT;
495 }
milind-udefab712023-02-20 22:22:02 -0800496 }
497
Maxwell Hendersonad312342023-01-10 12:07:47 -0800498 {
499 auto builder = superstructure_goal_sender_.MakeBuilder();
500
Austin Schuh6dc925b2023-02-24 16:23:32 -0800501 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800502 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
503 *builder.fbb(), wrist_goal,
504 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800505
Maxwell Hendersonad312342023-01-10 12:07:47 -0800506 superstructure::Goal::Builder superstructure_goal_builder =
507 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800508 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800509 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800510 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800511 if (builder.Send(superstructure_goal_builder.Finish()) !=
512 aos::RawSender::Error::kOk) {
513 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
514 }
515 }
James Kuszmaul4e171432023-02-26 13:39:37 -0800516 if (placing_row.has_value()) {
517 auto builder = target_selector_hint_sender_.MakeBuilder();
518 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
519 hint_builder.add_row(placing_row.value());
520 hint_builder.add_spot(placing_spot.value());
James Kuszmaul055fe762023-03-03 21:14:01 -0800521 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul4e171432023-02-26 13:39:37 -0800522 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
523 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
524 }
525 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800526 }
527
528 private:
529 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800530 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800531
532 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800533
534 uint32_t arm_goal_position_;
Austin Schuhe062be02023-03-04 21:12:07 -0800535
536 const ArmSetpoint *current_setpoint_ = nullptr;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800537};
538
539} // namespace joysticks
540} // namespace input
541} // namespace y2023
542
543int main(int argc, char **argv) {
544 ::aos::InitGoogle(&argc, &argv);
545
546 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
547 aos::configuration::ReadConfig("aos_config.json");
548
549 ::aos::ShmEventLoop event_loop(&config.message());
550 ::y2023::input::joysticks::Reader reader(&event_loop);
551
552 event_loop.Run();
553
554 return 0;
555}