blob: dcba253f044f4611f94a6b452e3aa2285adaad8c [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"
Ravago Jones8c65c432023-03-25 17:35:39 -070019#include "frc971/input/redundant_joystick_data.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080020#include "frc971/zeroing/wrap.h"
21#include "y2023/constants.h"
22#include "y2023/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul4e171432023-02-26 13:39:37 -080023#include "y2023/control_loops/drivetrain/target_selector_hint_generated.h"
milind-udefab712023-02-20 22:22:02 -080024#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080025#include "y2023/control_loops/superstructure/superstructure_goal_generated.h"
26#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
27
28using frc971::CreateProfileParameters;
29using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
30using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
31using frc971::input::driver_station::ButtonLocation;
32using frc971::input::driver_station::ControlBit;
33using frc971::input::driver_station::JoystickAxis;
34using frc971::input::driver_station::POVLocation;
James Kuszmaul4e171432023-02-26 13:39:37 -080035using y2023::control_loops::drivetrain::GridSelectionHint;
James Kuszmaul055fe762023-03-03 21:14:01 -080036using y2023::control_loops::drivetrain::RowSelectionHint;
James Kuszmaul4e171432023-02-26 13:39:37 -080037using y2023::control_loops::drivetrain::SpotSelectionHint;
38using y2023::control_loops::drivetrain::TargetSelectorHint;
James Kuszmaul055fe762023-03-03 21:14:01 -080039using y2023::control_loops::superstructure::RollerGoal;
40using Side = frc971::control_loops::drivetrain::RobotSide;
Maxwell Hendersonad312342023-01-10 12:07:47 -080041
42namespace y2023 {
43namespace input {
44namespace joysticks {
45
Austin Schuh99dda682023-03-11 00:18:37 -080046constexpr double kConeWrist = 0.4;
47constexpr double kCubeWrist = 1.0;
48
milind-udefab712023-02-20 22:22:02 -080049// TODO(milind): add correct locations
Ravago Jones8c65c432023-03-25 17:35:39 -070050const ButtonLocation kDriverSpit(1, 1);
Austin Schuh23a90022023-02-24 22:13:39 -080051const ButtonLocation kSpit(4, 13);
52
Austin Schuh9b3e41c2023-02-26 22:29:53 -080053const ButtonLocation kHighConeScoreLeft(4, 14);
54const ButtonLocation kHighConeScoreRight(3, 1);
55
56const ButtonLocation kMidConeScoreLeft(4, 15);
57const ButtonLocation kMidConeScoreRight(3, 2);
58
Austin Schuhe062be02023-03-04 21:12:07 -080059const ButtonLocation kLowConeScoreLeft(4, 16);
60const ButtonLocation kLowConeScoreRight(3, 3);
61
Austin Schuh9b3e41c2023-02-26 22:29:53 -080062const ButtonLocation kHighCube(4, 1);
63const ButtonLocation kMidCube(4, 2);
64const ButtonLocation kLowCube(4, 3);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080065
66const ButtonLocation kGroundPickupConeUp(4, 7);
Austin Schuh99dda682023-03-11 00:18:37 -080067const ButtonLocation kGroundPickupConeDown(4, 8);
milind-u71da5392023-02-26 12:45:00 -080068const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080069const ButtonLocation kHPConePickup(4, 6);
70
Austin Schuh9b3e41c2023-02-26 22:29:53 -080071const ButtonLocation kSuck(4, 11);
72const ButtonLocation kBack(4, 12);
milind-udefab712023-02-20 22:22:02 -080073
Austin Schuh6dc925b2023-02-24 16:23:32 -080074const ButtonLocation kWrist(4, 10);
Austin Schuh038b5452023-03-08 20:55:45 -080075const ButtonLocation kStayIn(3, 4);
Austin Schuh6dc925b2023-02-24 16:23:32 -080076
Austin Schuh99dda682023-03-11 00:18:37 -080077const ButtonLocation kConeDownTip(4, 4);
78const ButtonLocation kConeDownBase(4, 5);
79
Maxwell Hendersonad312342023-01-10 12:07:47 -080080namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080081namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080082
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080083enum class GamePiece {
84 CONE_UP = 0,
85 CONE_DOWN = 1,
86 CUBE = 2,
Austin Schuh99dda682023-03-11 00:18:37 -080087 CONE_TIP = 4,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080088};
89
James Kuszmaul4ac45762023-03-04 19:10:55 -080090struct ButtonData {
91 ButtonLocation button;
92 std::optional<SpotSelectionHint> spot = std::nullopt;
93};
94
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080095struct ArmSetpoint {
96 uint32_t index;
Austin Schuh99dda682023-03-11 00:18:37 -080097 std::optional<uint32_t> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080098 double wrist_goal;
99 std::optional<double> score_wrist_goal = std::nullopt;
100 GamePiece game_piece;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800101 std::vector<ButtonData> buttons;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800102 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -0800103 std::optional<RowSelectionHint> row_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800104};
105
106const std::vector<ArmSetpoint> setpoints = {
107 {
108 .index = arm::GroundPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800109 .wrist_goal = 0.7,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800110 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800111 .buttons = {{kGroundPickupConeUp}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800112 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800113 },
114 {
Austin Schuhe062be02023-03-04 21:12:07 -0800115 .index = arm::GroundPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800116 .wrist_goal = 0.6,
Austin Schuhe062be02023-03-04 21:12:07 -0800117 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800118 .buttons = {{kGroundPickupConeUp}},
Austin Schuhe062be02023-03-04 21:12:07 -0800119 .side = Side::FRONT,
120 },
121 {
122 .index = arm::GroundPickupBackConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800123 .wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800124 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh99dda682023-03-11 00:18:37 -0800125 .buttons = {{kGroundPickupConeDown}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800126 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800127 },
128 {
Austin Schuhe062be02023-03-04 21:12:07 -0800129 .index = arm::GroundPickupFrontConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800130 .wrist_goal = 0.6,
Austin Schuhe062be02023-03-04 21:12:07 -0800131 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh99dda682023-03-11 00:18:37 -0800132 .buttons = {{kGroundPickupConeDown}},
Austin Schuhe062be02023-03-04 21:12:07 -0800133 .side = Side::FRONT,
134 },
135 {
Austin Schuh99dda682023-03-11 00:18:37 -0800136 .index = arm::ScoreBackLowConeDownTipIndex(),
137 .wrist_goal = 0.7,
138 .game_piece = GamePiece::CONE_TIP,
139 .buttons = {{kLowConeScoreRight, SpotSelectionHint::RIGHT},
140 {kLowCube, SpotSelectionHint::MIDDLE},
141 {kLowConeScoreLeft, SpotSelectionHint::LEFT}},
142 .side = Side::BACK,
143 .row_hint = RowSelectionHint::BOTTOM,
144 },
145 {
146 .index = arm::ScoreBackMidConeDownTipIndex(),
147 .place_index = arm::ScoreBackMidConeDownTipPlacedIndex(),
148 .wrist_goal = 0.8,
149 .score_wrist_goal = 2.0,
150 .game_piece = GamePiece::CONE_TIP,
151 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
152 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
153 .side = Side::BACK,
154 .row_hint = RowSelectionHint::MIDDLE,
155 },
156 {
157 .index = arm::ScoreFrontMidConeDownTipIndex(),
158 .place_index = arm::ScoreFrontMidConeDownTipPlacedIndex(),
Austin Schuhe062be02023-03-04 21:12:07 -0800159 .wrist_goal = 0.0,
Austin Schuh99dda682023-03-11 00:18:37 -0800160 .score_wrist_goal = 1.4,
161 .game_piece = GamePiece::CONE_TIP,
162 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
163 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
164 .side = Side::FRONT,
165 .row_hint = RowSelectionHint::MIDDLE,
166 },
167 {
168 .index = arm::ScoreFrontHighConeDownTipIndex(),
169 .place_index = arm::ScoreFrontHighConeDownTipPlacedIndex(),
170 .wrist_goal = 0.4,
171 .score_wrist_goal = 1.4,
172 .game_piece = GamePiece::CONE_TIP,
173 .buttons = {{kHighConeScoreRight, SpotSelectionHint::RIGHT},
174 {kHighConeScoreLeft, SpotSelectionHint::LEFT}},
175 .side = Side::FRONT,
176 .row_hint = RowSelectionHint::TOP,
177 },
178 {
179 .index = arm::ScoreFrontLowConeDownTipIndex(),
180 .wrist_goal = 2.8,
181 .game_piece = GamePiece::CONE_TIP,
182 .buttons = {{kLowConeScoreRight, SpotSelectionHint::RIGHT},
183 {kLowCube, SpotSelectionHint::MIDDLE},
184 {kLowConeScoreLeft, SpotSelectionHint::LEFT}},
185 .side = Side::FRONT,
186 .row_hint = RowSelectionHint::TOP,
187 },
188 {
189 .index = arm::ScoreBackMidConeUpIndex(),
190 .wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800191 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800192 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
193 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800194 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800195 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800196 },
197 {
Austin Schuhe062be02023-03-04 21:12:07 -0800198 .index = arm::ScoreBackLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800199 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800200 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800201 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800202 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800203 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800204 .side = Side::BACK,
205 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800206 },
207 {
208 .index = arm::ScoreFrontLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800209 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800210 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800211 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800212 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800213 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800214 .side = Side::FRONT,
215 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800216 },
217 {
218 .index = arm::ScoreBackMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800219 .wrist_goal = 2.5,
220 .score_wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800221 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800222 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
223 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800224 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800225 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800226 },
227 {
Austin Schuhe062be02023-03-04 21:12:07 -0800228 .index = arm::ScoreBackLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800229 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800230 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800231 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800232 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800233 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800234 .side = Side::BACK,
235 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800236 },
237 {
238 .index = arm::ScoreFrontLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800239 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800240 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800241 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800242 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800243 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800244 .side = Side::FRONT,
245 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800246 },
247 {
248 .index = arm::ScoreFrontMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800249 .wrist_goal = 2.6,
250 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800251 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800252 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
253 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800254 .side = Side::FRONT,
255 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800256 },
257 {
258 .index = arm::ScoreFrontHighConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800259 .wrist_goal = 2.6,
260 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800261 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800262 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
263 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800264 .side = Side::FRONT,
265 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800266 },
267 {
268 .index = arm::HPPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800269 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800270 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800271 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800272 .side = Side::FRONT,
273 },
274 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800275 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800276 .wrist_goal = 0.5,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800277 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800278 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800279 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800280 },
281 {
Austin Schuhe062be02023-03-04 21:12:07 -0800282 .index = arm::ScoreFrontHighConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800283 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800284 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800285 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
286 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800287 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800288 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800289 },
290 {
Austin Schuhe062be02023-03-04 21:12:07 -0800291 .index = arm::ScoreFrontMidConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800292 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800293 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800294 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
295 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800296 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800297 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800298 },
milind-u68842e12023-02-26 12:45:40 -0800299 {
300 .index = arm::GroundPickupBackCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800301 .wrist_goal = kCubeWrist,
milind-u68842e12023-02-26 12:45:40 -0800302 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800303 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800304 .side = Side::BACK,
305 },
306 {
307 .index = arm::ScoreFrontMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800308 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800309 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800310 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800311 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800312 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800313 },
314 {
315 .index = arm::ScoreBackMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800316 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800317 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800318 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800319 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800320 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800321 },
322 {
323 .index = arm::ScoreFrontLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800324 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800325 .game_piece = GamePiece::CUBE,
Austin Schuh6be75772023-03-11 15:12:33 -0800326 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
327 {kLowCube, SpotSelectionHint::MIDDLE},
328 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800329 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800330 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800331 },
332 {
333 .index = arm::ScoreBackLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800334 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800335 .game_piece = GamePiece::CUBE,
Austin Schuh6be75772023-03-11 15:12:33 -0800336 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
337 {kLowCube, SpotSelectionHint::MIDDLE},
338 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800339 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800340 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800341 },
342 {
343 .index = arm::ScoreFrontHighCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800344 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800345 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800346 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800347 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800348 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800349 },
350 {
351 .index = arm::ScoreBackHighCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800352 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800353 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800354 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800355 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800356 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800357 },
358 {
359 .index = arm::GroundPickupFrontCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800360 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800361 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800362 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800363 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800364 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800365};
366
Maxwell Hendersonad312342023-01-10 12:07:47 -0800367class Reader : public ::frc971::input::ActionJoystickInput {
368 public:
369 Reader(::aos::EventLoop *event_loop)
370 : ::frc971::input::ActionJoystickInput(
371 event_loop,
372 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
Ravago Jones8c65c432023-03-25 17:35:39 -0700373 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
374 {.use_redundant_joysticks = true}),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800375 superstructure_goal_sender_(
376 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800377 target_selector_hint_sender_(
378 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800379 superstructure_status_fetcher_(
380 event_loop->MakeFetcher<superstructure::Status>(
381 "/superstructure")) {}
382
383 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
384
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800385 GamePiece current_game_piece_ = GamePiece::CONE_UP;
386
Austin Schuhf194af32023-03-22 20:31:18 -0700387 bool has_scored_ = false;
388
Maxwell Hendersonad312342023-01-10 12:07:47 -0800389 void HandleTeleop(
390 const ::frc971::input::driver_station::Data &data) override {
391 superstructure_status_fetcher_.Fetch();
392 if (!superstructure_status_fetcher_.get()) {
393 AOS_LOG(ERROR, "Got no superstructure status message.\n");
394 return;
395 }
396
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800397 if (!superstructure_status_fetcher_->has_wrist()) {
398 AOS_LOG(ERROR, "Got no superstructure status message.\n");
399 return;
400 }
milind-udefab712023-02-20 22:22:02 -0800401
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800402 double wrist_goal = 0.0;
403 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800404 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800405 std::optional<double> score_wrist_goal = std::nullopt;
Austin Schuh99dda682023-03-11 00:18:37 -0800406 std::optional<double> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800407
408 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800409 roller_goal = RollerGoal::INTAKE_CONE_UP;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800410 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuh99dda682023-03-11 00:18:37 -0800411 } else if (data.IsPressed(kGroundPickupConeDown)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800412 roller_goal = RollerGoal::INTAKE_CONE_DOWN;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800413 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800414 } else if (data.IsPressed(kGroundPickupCube)) {
415 roller_goal = RollerGoal::INTAKE_CUBE;
416 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800417 }
418
Austin Schuh99dda682023-03-11 00:18:37 -0800419 if (current_game_piece_ == GamePiece::CONE_DOWN ||
420 current_game_piece_ == GamePiece::CONE_TIP) {
421 if (data.IsPressed(kConeDownTip)) {
422 current_game_piece_ = GamePiece::CONE_TIP;
423 } else if (data.IsPressed(kConeDownBase)) {
424 current_game_piece_ = GamePiece::CONE_DOWN;
425 }
426 }
427
milind-u68842e12023-02-26 12:45:40 -0800428 if (current_game_piece_ == GamePiece::CUBE) {
Austin Schuh99dda682023-03-11 00:18:37 -0800429 wrist_goal = kCubeWrist;
milind-u68842e12023-02-26 12:45:40 -0800430 }
431
James Kuszmaul4e171432023-02-26 13:39:37 -0800432 std::optional<RowSelectionHint> placing_row;
433 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800434
Austin Schuhe062be02023-03-04 21:12:07 -0800435 // Keep the setpoint if the button is still held. This lets us release the
436 // back button once a side has been selected.
437 if (current_setpoint_ != nullptr) {
438 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800439 for (const auto &button : current_setpoint_->buttons) {
440 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800441 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800442 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800443 }
444 }
445 if (!found) {
446 current_setpoint_ = nullptr;
447 }
448 }
449
450 // Ok, no active setpoint. Search for the right one.
451 if (current_setpoint_ == nullptr) {
Austin Schuhf194af32023-03-22 20:31:18 -0700452 has_scored_ = false;
Austin Schuhe062be02023-03-04 21:12:07 -0800453 const Side current_side =
454 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
455 // Search for the active setpoint.
456 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800457 for (const auto &button : setpoint.buttons) {
458 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800459 if (setpoint.game_piece == current_game_piece_ &&
460 setpoint.side == current_side) {
461 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800462 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800463 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800464 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800465 }
466 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800467 }
Austin Schuhe062be02023-03-04 21:12:07 -0800468
469 // And, pull the bits out of it.
470 if (current_setpoint_ != nullptr) {
Austin Schuh038b5452023-03-08 20:55:45 -0800471 if (!data.IsPressed(kStayIn)) {
472 wrist_goal = current_setpoint_->wrist_goal;
473 arm_goal_position_ = current_setpoint_->index;
474 score_wrist_goal = current_setpoint_->score_wrist_goal;
Austin Schuh99dda682023-03-11 00:18:37 -0800475 place_index = current_setpoint_->place_index;
Austin Schuh038b5452023-03-08 20:55:45 -0800476 }
477
Austin Schuhe062be02023-03-04 21:12:07 -0800478 placing_row = current_setpoint_->row_hint;
Austin Schuhe062be02023-03-04 21:12:07 -0800479 }
480
James Kuszmaul4e171432023-02-26 13:39:37 -0800481 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800482
Austin Schuh23a90022023-02-24 22:13:39 -0800483 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800484 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh8e6f2872023-03-08 20:55:27 -0800485 } else if (data.IsPressed(kSpit) || data.IsPressed(kDriverSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800486 if (score_wrist_goal.has_value()) {
487 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800488
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800489 // If we are supposed to dunk it, wait until we are close enough to
490 // spit.
491 if (std::abs(score_wrist_goal.value() -
Austin Schuh99dda682023-03-11 00:18:37 -0800492 superstructure_status_fetcher_->wrist()->goal_position()) <
Austin Schuhf194af32023-03-22 20:31:18 -0700493 0.1 ||
494 has_scored_) {
Austin Schuh99dda682023-03-11 00:18:37 -0800495 if (place_index.has_value()) {
496 arm_goal_position_ = place_index.value();
Austin Schuhf194af32023-03-22 20:31:18 -0700497 if ((arm_goal_position_ ==
498 superstructure_status_fetcher_->arm()->current_node() &&
499 superstructure_status_fetcher_->arm()->path_distance_to_go() <
500 0.01) ||
501 has_scored_) {
502 has_scored_ = true;
Austin Schuh99dda682023-03-11 00:18:37 -0800503 roller_goal = RollerGoal::SPIT;
504 }
505 } else {
506 roller_goal = RollerGoal::SPIT;
507 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800508 }
509 } else {
510 roller_goal = RollerGoal::SPIT;
511 }
milind-udefab712023-02-20 22:22:02 -0800512 }
513
Maxwell Hendersonad312342023-01-10 12:07:47 -0800514 {
515 auto builder = superstructure_goal_sender_.MakeBuilder();
516
Austin Schuh6dc925b2023-02-24 16:23:32 -0800517 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800518 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
519 *builder.fbb(), wrist_goal,
520 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800521
Maxwell Hendersonad312342023-01-10 12:07:47 -0800522 superstructure::Goal::Builder superstructure_goal_builder =
523 builder.MakeBuilder<superstructure::Goal>();
Ravago Jones8c65c432023-03-25 17:35:39 -0700524 superstructure_goal_builder.add_arm_goal_position(arm::NeutralIndex());
Maxwell Henderson5938a832023-02-23 09:33:15 -0800525 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800526 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800527 if (builder.Send(superstructure_goal_builder.Finish()) !=
528 aos::RawSender::Error::kOk) {
529 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
530 }
531 }
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800532 // TODO(james): Is there a more principled way to detect Human Player
533 // pickup? Probably don't bother fixing it until/unless we add more buttons
534 // that can select human player pickup.
535 if (data.IsPressed(kHPConePickup)) {
536 auto builder = target_selector_hint_sender_.MakeBuilder();
537 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
538 hint_builder.add_substation_pickup(true);
James Kuszmaul7e167652023-03-11 17:52:46 -0800539 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800540 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
541 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
542 }
543 } else if (placing_row.has_value()) {
James Kuszmaul4e171432023-02-26 13:39:37 -0800544 auto builder = target_selector_hint_sender_.MakeBuilder();
545 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
546 hint_builder.add_row(placing_row.value());
547 hint_builder.add_spot(placing_spot.value());
James Kuszmaul055fe762023-03-03 21:14:01 -0800548 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul4e171432023-02-26 13:39:37 -0800549 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
550 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
551 }
552 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800553 }
554
555 private:
556 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800557 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800558
559 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800560
561 uint32_t arm_goal_position_;
Austin Schuhe062be02023-03-04 21:12:07 -0800562
563 const ArmSetpoint *current_setpoint_ = nullptr;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800564};
565
566} // namespace joysticks
567} // namespace input
568} // namespace y2023
569
570int main(int argc, char **argv) {
571 ::aos::InitGoogle(&argc, &argv);
572
573 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
574 aos::configuration::ReadConfig("aos_config.json");
575
576 ::aos::ShmEventLoop event_loop(&config.message());
577 ::y2023::input::joysticks::Reader reader(&event_loop);
578
579 event_loop.Run();
580
581 return 0;
582}