blob: a8f540fdf5ce015c1ec5dea54ad67f07395c6732 [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);
Ravago Joneseb70f6b2023-03-25 17:35:39 -070051const ButtonLocation kSpit(2, 5);
Austin Schuh23a90022023-02-24 22:13:39 -080052
Ravago Joneseb70f6b2023-03-25 17:35:39 -070053const ButtonLocation kHighConeScoreLeft(2, 6);
54const ButtonLocation kHighConeScoreRight(2, 9);
Austin Schuh9b3e41c2023-02-26 22:29:53 -080055
Ravago Joneseb70f6b2023-03-25 17:35:39 -070056const ButtonLocation kMidConeScoreLeft(2, 7);
57const ButtonLocation kMidConeScoreRight(2, 10);
Austin Schuh9b3e41c2023-02-26 22:29:53 -080058
Ravago Joneseb70f6b2023-03-25 17:35:39 -070059const ButtonLocation kLowConeScoreLeft(2, 8);
60const ButtonLocation kLowConeScoreRight(2, 11);
Austin Schuhe062be02023-03-04 21:12:07 -080061
Ravago Joneseb70f6b2023-03-25 17:35:39 -070062const ButtonLocation kHighCube(1, 6);
63const ButtonLocation kMidCube(1, 7);
64const ButtonLocation kLowCube(1, 8);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080065
Ravago Joneseb70f6b2023-03-25 17:35:39 -070066const ButtonLocation kGroundPickupConeUp(1, 12);
67const ButtonLocation kGroundPickupConeDown(1, 13);
68const ButtonLocation kGroundPickupCube(2, 2);
69const ButtonLocation kHPConePickup(1, 11);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080070
Ravago Joneseb70f6b2023-03-25 17:35:39 -070071const ButtonLocation kSuck(2, 3);
72const ButtonLocation kBack(2, 4);
milind-udefab712023-02-20 22:22:02 -080073
Ravago Joneseb70f6b2023-03-25 17:35:39 -070074const ButtonLocation kStayIn(2, 12);
Austin Schuh6dc925b2023-02-24 16:23:32 -080075
Ravago Joneseb70f6b2023-03-25 17:35:39 -070076const ButtonLocation kConeDownTip(1, 9);
77const ButtonLocation kConeDownBase(1, 10);
Austin Schuh99dda682023-03-11 00:18:37 -080078
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},
Austin Schuh6be75772023-03-11 15:12:33 -0800201 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800202 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800203 .side = Side::BACK,
204 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800205 },
206 {
207 .index = arm::ScoreFrontLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800208 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800209 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800210 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800211 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800212 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800213 .side = Side::FRONT,
214 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800215 },
216 {
217 .index = arm::ScoreBackMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800218 .wrist_goal = 2.5,
219 .score_wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800220 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800221 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
222 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800223 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800224 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800225 },
226 {
Austin Schuhe062be02023-03-04 21:12:07 -0800227 .index = arm::ScoreBackLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800228 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800229 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800230 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800231 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800232 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800233 .side = Side::BACK,
234 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800235 },
236 {
237 .index = arm::ScoreFrontLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800238 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800239 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800240 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800241 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800242 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800243 .side = Side::FRONT,
244 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800245 },
246 {
247 .index = arm::ScoreFrontMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800248 .wrist_goal = 2.6,
249 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800250 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800251 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
252 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800253 .side = Side::FRONT,
254 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800255 },
256 {
257 .index = arm::ScoreFrontHighConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800258 .wrist_goal = 2.6,
259 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800260 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800261 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
262 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800263 .side = Side::FRONT,
264 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800265 },
266 {
267 .index = arm::HPPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800268 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800269 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800270 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800271 .side = Side::FRONT,
272 },
273 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800274 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800275 .wrist_goal = 0.5,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800276 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800277 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800278 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800279 },
280 {
Austin Schuhe062be02023-03-04 21:12:07 -0800281 .index = arm::ScoreFrontHighConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800282 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800283 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800284 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
285 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800286 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800287 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800288 },
289 {
Austin Schuhe062be02023-03-04 21:12:07 -0800290 .index = arm::ScoreFrontMidConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800291 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800292 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800293 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
294 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800295 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800296 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800297 },
milind-u68842e12023-02-26 12:45:40 -0800298 {
299 .index = arm::GroundPickupBackCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800300 .wrist_goal = kCubeWrist,
milind-u68842e12023-02-26 12:45:40 -0800301 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800302 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800303 .side = Side::BACK,
304 },
305 {
306 .index = arm::ScoreFrontMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800307 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800308 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800309 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800310 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800311 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800312 },
313 {
314 .index = arm::ScoreBackMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800315 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800316 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800317 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800318 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800319 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800320 },
321 {
322 .index = arm::ScoreFrontLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800323 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800324 .game_piece = GamePiece::CUBE,
Austin Schuh6be75772023-03-11 15:12:33 -0800325 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
326 {kLowCube, SpotSelectionHint::MIDDLE},
327 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800328 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800329 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800330 },
331 {
332 .index = arm::ScoreBackLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800333 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800334 .game_piece = GamePiece::CUBE,
Austin Schuh6be75772023-03-11 15:12:33 -0800335 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
336 {kLowCube, SpotSelectionHint::MIDDLE},
337 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800338 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800339 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800340 },
341 {
342 .index = arm::ScoreFrontHighCubeIndex(),
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::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800347 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800348 },
349 {
350 .index = arm::ScoreBackHighCubeIndex(),
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 = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800354 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800355 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800356 },
357 {
358 .index = arm::GroundPickupFrontCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800359 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800360 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800361 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800362 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800363 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800364};
365
Maxwell Hendersonad312342023-01-10 12:07:47 -0800366class Reader : public ::frc971::input::ActionJoystickInput {
367 public:
368 Reader(::aos::EventLoop *event_loop)
369 : ::frc971::input::ActionJoystickInput(
370 event_loop,
371 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
Ravago Jones8c65c432023-03-25 17:35:39 -0700372 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
373 {.use_redundant_joysticks = true}),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800374 superstructure_goal_sender_(
375 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800376 target_selector_hint_sender_(
377 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800378 superstructure_status_fetcher_(
379 event_loop->MakeFetcher<superstructure::Status>(
380 "/superstructure")) {}
381
382 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
383
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800384 GamePiece current_game_piece_ = GamePiece::CONE_UP;
385
Austin Schuhf194af32023-03-22 20:31:18 -0700386 bool has_scored_ = false;
387
Maxwell Hendersonad312342023-01-10 12:07:47 -0800388 void HandleTeleop(
389 const ::frc971::input::driver_station::Data &data) override {
390 superstructure_status_fetcher_.Fetch();
391 if (!superstructure_status_fetcher_.get()) {
392 AOS_LOG(ERROR, "Got no superstructure status message.\n");
393 return;
394 }
395
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800396 if (!superstructure_status_fetcher_->has_wrist()) {
397 AOS_LOG(ERROR, "Got no superstructure status message.\n");
398 return;
399 }
milind-udefab712023-02-20 22:22:02 -0800400
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800401 double wrist_goal = 0.0;
402 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800403 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800404 std::optional<double> score_wrist_goal = std::nullopt;
Austin Schuh99dda682023-03-11 00:18:37 -0800405 std::optional<double> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800406
407 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800408 roller_goal = RollerGoal::INTAKE_CONE_UP;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800409 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuh99dda682023-03-11 00:18:37 -0800410 } else if (data.IsPressed(kGroundPickupConeDown)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800411 roller_goal = RollerGoal::INTAKE_CONE_DOWN;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800412 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800413 } else if (data.IsPressed(kGroundPickupCube)) {
414 roller_goal = RollerGoal::INTAKE_CUBE;
415 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800416 }
417
Austin Schuh99dda682023-03-11 00:18:37 -0800418 if (current_game_piece_ == GamePiece::CONE_DOWN ||
419 current_game_piece_ == GamePiece::CONE_TIP) {
420 if (data.IsPressed(kConeDownTip)) {
421 current_game_piece_ = GamePiece::CONE_TIP;
422 } else if (data.IsPressed(kConeDownBase)) {
423 current_game_piece_ = GamePiece::CONE_DOWN;
424 }
425 }
426
milind-u68842e12023-02-26 12:45:40 -0800427 if (current_game_piece_ == GamePiece::CUBE) {
Austin Schuh99dda682023-03-11 00:18:37 -0800428 wrist_goal = kCubeWrist;
milind-u68842e12023-02-26 12:45:40 -0800429 }
430
James Kuszmaul4e171432023-02-26 13:39:37 -0800431 std::optional<RowSelectionHint> placing_row;
432 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800433
Austin Schuhe062be02023-03-04 21:12:07 -0800434 // Keep the setpoint if the button is still held. This lets us release the
435 // back button once a side has been selected.
436 if (current_setpoint_ != nullptr) {
437 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800438 for (const auto &button : current_setpoint_->buttons) {
439 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800440 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800441 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800442 }
443 }
444 if (!found) {
445 current_setpoint_ = nullptr;
446 }
447 }
448
449 // Ok, no active setpoint. Search for the right one.
450 if (current_setpoint_ == nullptr) {
Austin Schuhf194af32023-03-22 20:31:18 -0700451 has_scored_ = false;
Austin Schuhe062be02023-03-04 21:12:07 -0800452 const Side current_side =
453 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
454 // Search for the active setpoint.
455 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800456 for (const auto &button : setpoint.buttons) {
457 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800458 if (setpoint.game_piece == current_game_piece_ &&
459 setpoint.side == current_side) {
460 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800461 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800462 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800463 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800464 }
465 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800466 }
Austin Schuhe062be02023-03-04 21:12:07 -0800467
468 // And, pull the bits out of it.
469 if (current_setpoint_ != nullptr) {
Austin Schuh038b5452023-03-08 20:55:45 -0800470 if (!data.IsPressed(kStayIn)) {
471 wrist_goal = current_setpoint_->wrist_goal;
472 arm_goal_position_ = current_setpoint_->index;
473 score_wrist_goal = current_setpoint_->score_wrist_goal;
Austin Schuh99dda682023-03-11 00:18:37 -0800474 place_index = current_setpoint_->place_index;
Austin Schuh038b5452023-03-08 20:55:45 -0800475 }
476
Austin Schuhe062be02023-03-04 21:12:07 -0800477 placing_row = current_setpoint_->row_hint;
Austin Schuhe062be02023-03-04 21:12:07 -0800478 }
479
James Kuszmaul4e171432023-02-26 13:39:37 -0800480 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800481
Austin Schuh23a90022023-02-24 22:13:39 -0800482 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800483 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh8e6f2872023-03-08 20:55:27 -0800484 } else if (data.IsPressed(kSpit) || data.IsPressed(kDriverSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800485 if (score_wrist_goal.has_value()) {
486 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800487
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800488 // If we are supposed to dunk it, wait until we are close enough to
489 // spit.
490 if (std::abs(score_wrist_goal.value() -
Austin Schuh99dda682023-03-11 00:18:37 -0800491 superstructure_status_fetcher_->wrist()->goal_position()) <
Austin Schuhf194af32023-03-22 20:31:18 -0700492 0.1 ||
493 has_scored_) {
Austin Schuh99dda682023-03-11 00:18:37 -0800494 if (place_index.has_value()) {
495 arm_goal_position_ = place_index.value();
Austin Schuhf194af32023-03-22 20:31:18 -0700496 if ((arm_goal_position_ ==
497 superstructure_status_fetcher_->arm()->current_node() &&
498 superstructure_status_fetcher_->arm()->path_distance_to_go() <
499 0.01) ||
500 has_scored_) {
501 has_scored_ = true;
Austin Schuh99dda682023-03-11 00:18:37 -0800502 roller_goal = RollerGoal::SPIT;
503 }
504 } else {
505 roller_goal = RollerGoal::SPIT;
506 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800507 }
508 } else {
509 roller_goal = RollerGoal::SPIT;
510 }
milind-udefab712023-02-20 22:22:02 -0800511 }
512
Maxwell Hendersonad312342023-01-10 12:07:47 -0800513 {
514 auto builder = superstructure_goal_sender_.MakeBuilder();
515
Austin Schuh6dc925b2023-02-24 16:23:32 -0800516 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800517 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
518 *builder.fbb(), wrist_goal,
519 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800520
Maxwell Hendersonad312342023-01-10 12:07:47 -0800521 superstructure::Goal::Builder superstructure_goal_builder =
522 builder.MakeBuilder<superstructure::Goal>();
Ravago Joneseb70f6b2023-03-25 17:35:39 -0700523 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800524 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800525 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800526 if (builder.Send(superstructure_goal_builder.Finish()) !=
527 aos::RawSender::Error::kOk) {
528 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
529 }
530 }
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800531 // TODO(james): Is there a more principled way to detect Human Player
532 // pickup? Probably don't bother fixing it until/unless we add more buttons
533 // that can select human player pickup.
534 if (data.IsPressed(kHPConePickup)) {
535 auto builder = target_selector_hint_sender_.MakeBuilder();
536 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
537 hint_builder.add_substation_pickup(true);
James Kuszmaul7e167652023-03-11 17:52:46 -0800538 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800539 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
540 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
541 }
542 } else if (placing_row.has_value()) {
James Kuszmaul4e171432023-02-26 13:39:37 -0800543 auto builder = target_selector_hint_sender_.MakeBuilder();
544 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
545 hint_builder.add_row(placing_row.value());
546 hint_builder.add_spot(placing_spot.value());
James Kuszmaul055fe762023-03-03 21:14:01 -0800547 hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side);
James Kuszmaul4e171432023-02-26 13:39:37 -0800548 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
549 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
550 }
551 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800552 }
553
554 private:
555 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800556 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800557
558 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800559
560 uint32_t arm_goal_position_;
Austin Schuhe062be02023-03-04 21:12:07 -0800561
562 const ArmSetpoint *current_setpoint_ = nullptr;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800563};
564
565} // namespace joysticks
566} // namespace input
567} // namespace y2023
568
569int main(int argc, char **argv) {
570 ::aos::InitGoogle(&argc, &argv);
571
572 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
573 aos::configuration::ReadConfig("aos_config.json");
574
575 ::aos::ShmEventLoop event_loop(&config.message());
576 ::y2023::input::joysticks::Reader reader(&event_loop);
577
578 event_loop.Run();
579
580 return 0;
581}