blob: 6e62c5b29f782c71ea9b34ad6615400e646b01ab [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080042namespace y2023::input::joysticks {
Maxwell Hendersonad312342023-01-10 12:07:47 -080043
Austin Schuh99dda682023-03-11 00:18:37 -080044constexpr double kConeWrist = 0.4;
45constexpr double kCubeWrist = 1.0;
46
milind-udefab712023-02-20 22:22:02 -080047// TODO(milind): add correct locations
Ravago Jones8c65c432023-03-25 17:35:39 -070048const ButtonLocation kDriverSpit(1, 1);
Ravago Joneseb70f6b2023-03-25 17:35:39 -070049const ButtonLocation kSpit(2, 5);
Austin Schuh23a90022023-02-24 22:13:39 -080050
Ravago Joneseb70f6b2023-03-25 17:35:39 -070051const ButtonLocation kHighConeScoreLeft(2, 6);
52const ButtonLocation kHighConeScoreRight(2, 9);
Austin Schuh9b3e41c2023-02-26 22:29:53 -080053
Ravago Joneseb70f6b2023-03-25 17:35:39 -070054const ButtonLocation kMidConeScoreLeft(2, 7);
55const ButtonLocation kMidConeScoreRight(2, 10);
Austin Schuh9b3e41c2023-02-26 22:29:53 -080056
Ravago Joneseb70f6b2023-03-25 17:35:39 -070057const ButtonLocation kLowConeScoreLeft(2, 8);
58const ButtonLocation kLowConeScoreRight(2, 11);
Austin Schuhe062be02023-03-04 21:12:07 -080059
Ravago Joneseb70f6b2023-03-25 17:35:39 -070060const ButtonLocation kHighCube(1, 6);
61const ButtonLocation kMidCube(1, 7);
62const ButtonLocation kLowCube(1, 8);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080063
Ravago Joneseb70f6b2023-03-25 17:35:39 -070064const ButtonLocation kGroundPickupConeUp(1, 12);
65const ButtonLocation kGroundPickupConeDown(1, 13);
66const ButtonLocation kGroundPickupCube(2, 2);
67const ButtonLocation kHPConePickup(1, 11);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080068
Ravago Joneseb70f6b2023-03-25 17:35:39 -070069const ButtonLocation kSuck(2, 3);
70const ButtonLocation kBack(2, 4);
milind-udefab712023-02-20 22:22:02 -080071
milind-u50c9a582023-05-03 20:05:22 -070072const ButtonLocation kStayIn(3, 2);
Austin Schuh6dc925b2023-02-24 16:23:32 -080073
Ravago Joneseb70f6b2023-03-25 17:35:39 -070074const ButtonLocation kConeDownTip(1, 9);
75const ButtonLocation kConeDownBase(1, 10);
Austin Schuh99dda682023-03-11 00:18:37 -080076
Maxwell Hendersonad312342023-01-10 12:07:47 -080077namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080078namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080079
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080080enum class GamePiece {
81 CONE_UP = 0,
82 CONE_DOWN = 1,
83 CUBE = 2,
Austin Schuh99dda682023-03-11 00:18:37 -080084 CONE_TIP = 4,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080085};
86
James Kuszmaul4ac45762023-03-04 19:10:55 -080087struct ButtonData {
88 ButtonLocation button;
89 std::optional<SpotSelectionHint> spot = std::nullopt;
90};
91
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080092struct ArmSetpoint {
93 uint32_t index;
Austin Schuh99dda682023-03-11 00:18:37 -080094 std::optional<uint32_t> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080095 double wrist_goal;
96 std::optional<double> score_wrist_goal = std::nullopt;
97 GamePiece game_piece;
James Kuszmaul4ac45762023-03-04 19:10:55 -080098 std::vector<ButtonData> buttons;
Austin Schuh9b3e41c2023-02-26 22:29:53 -080099 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -0800100 std::optional<RowSelectionHint> row_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800101};
102
103const std::vector<ArmSetpoint> setpoints = {
104 {
105 .index = arm::GroundPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800106 .wrist_goal = 0.7,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800107 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800108 .buttons = {{kGroundPickupConeUp}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800109 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800110 },
111 {
Austin Schuhe062be02023-03-04 21:12:07 -0800112 .index = arm::GroundPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800113 .wrist_goal = 0.6,
Austin Schuhe062be02023-03-04 21:12:07 -0800114 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800115 .buttons = {{kGroundPickupConeUp}},
Austin Schuhe062be02023-03-04 21:12:07 -0800116 .side = Side::FRONT,
117 },
118 {
119 .index = arm::GroundPickupBackConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800120 .wrist_goal = kConeWrist,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800121 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh99dda682023-03-11 00:18:37 -0800122 .buttons = {{kGroundPickupConeDown}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800123 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800124 },
125 {
Austin Schuhe062be02023-03-04 21:12:07 -0800126 .index = arm::GroundPickupFrontConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800127 .wrist_goal = 0.6,
Austin Schuhe062be02023-03-04 21:12:07 -0800128 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh99dda682023-03-11 00:18:37 -0800129 .buttons = {{kGroundPickupConeDown}},
Austin Schuhe062be02023-03-04 21:12:07 -0800130 .side = Side::FRONT,
131 },
132 {
Austin Schuh99dda682023-03-11 00:18:37 -0800133 .index = arm::ScoreBackLowConeDownTipIndex(),
134 .wrist_goal = 0.7,
135 .game_piece = GamePiece::CONE_TIP,
136 .buttons = {{kLowConeScoreRight, SpotSelectionHint::RIGHT},
137 {kLowCube, SpotSelectionHint::MIDDLE},
138 {kLowConeScoreLeft, SpotSelectionHint::LEFT}},
139 .side = Side::BACK,
140 .row_hint = RowSelectionHint::BOTTOM,
141 },
142 {
143 .index = arm::ScoreBackMidConeDownTipIndex(),
144 .place_index = arm::ScoreBackMidConeDownTipPlacedIndex(),
145 .wrist_goal = 0.8,
146 .score_wrist_goal = 2.0,
147 .game_piece = GamePiece::CONE_TIP,
148 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
149 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
150 .side = Side::BACK,
151 .row_hint = RowSelectionHint::MIDDLE,
152 },
153 {
154 .index = arm::ScoreFrontMidConeDownTipIndex(),
155 .place_index = arm::ScoreFrontMidConeDownTipPlacedIndex(),
Austin Schuhe062be02023-03-04 21:12:07 -0800156 .wrist_goal = 0.0,
Austin Schuh99dda682023-03-11 00:18:37 -0800157 .score_wrist_goal = 1.4,
158 .game_piece = GamePiece::CONE_TIP,
159 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
160 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
161 .side = Side::FRONT,
162 .row_hint = RowSelectionHint::MIDDLE,
163 },
164 {
165 .index = arm::ScoreFrontHighConeDownTipIndex(),
166 .place_index = arm::ScoreFrontHighConeDownTipPlacedIndex(),
167 .wrist_goal = 0.4,
168 .score_wrist_goal = 1.4,
169 .game_piece = GamePiece::CONE_TIP,
170 .buttons = {{kHighConeScoreRight, SpotSelectionHint::RIGHT},
171 {kHighConeScoreLeft, SpotSelectionHint::LEFT}},
172 .side = Side::FRONT,
173 .row_hint = RowSelectionHint::TOP,
174 },
175 {
176 .index = arm::ScoreFrontLowConeDownTipIndex(),
177 .wrist_goal = 2.8,
178 .game_piece = GamePiece::CONE_TIP,
179 .buttons = {{kLowConeScoreRight, SpotSelectionHint::RIGHT},
180 {kLowCube, SpotSelectionHint::MIDDLE},
181 {kLowConeScoreLeft, SpotSelectionHint::LEFT}},
182 .side = Side::FRONT,
183 .row_hint = RowSelectionHint::TOP,
184 },
185 {
186 .index = arm::ScoreBackMidConeUpIndex(),
Maxwell Henderson0cd91fb2023-09-22 22:25:21 -0700187 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800188 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800189 .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT},
190 {kMidConeScoreLeft, SpotSelectionHint::LEFT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800191 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800192 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800193 },
194 {
Austin Schuhe062be02023-03-04 21:12:07 -0800195 .index = arm::ScoreBackLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800196 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800197 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800198 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800199 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800200 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800201 .side = Side::BACK,
202 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800203 },
204 {
205 .index = arm::ScoreFrontLowConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800206 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800207 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800208 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800209 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800210 {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},
Austin Schuh6be75772023-03-11 15:12:33 -0800229 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800230 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800231 .side = Side::BACK,
232 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800233 },
234 {
235 .index = arm::ScoreFrontLowConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800236 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800237 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800238 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
Austin Schuh6be75772023-03-11 15:12:33 -0800239 {kLowCube, SpotSelectionHint::MIDDLE},
James Kuszmaul4ac45762023-03-04 19:10:55 -0800240 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800241 .side = Side::FRONT,
242 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuhe062be02023-03-04 21:12:07 -0800243 },
244 {
245 .index = arm::ScoreFrontMidConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800246 .wrist_goal = 2.6,
247 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800248 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800249 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
250 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800251 .side = Side::FRONT,
252 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuhe062be02023-03-04 21:12:07 -0800253 },
254 {
255 .index = arm::ScoreFrontHighConeDownBaseIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800256 .wrist_goal = 2.6,
257 .score_wrist_goal = 0.2,
Austin Schuhe062be02023-03-04 21:12:07 -0800258 .game_piece = GamePiece::CONE_DOWN,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800259 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
260 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuhe062be02023-03-04 21:12:07 -0800261 .side = Side::FRONT,
262 .row_hint = RowSelectionHint::TOP,
Austin Schuhe062be02023-03-04 21:12:07 -0800263 },
264 {
265 .index = arm::HPPickupFrontConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800266 .wrist_goal = kConeWrist,
Austin Schuhe062be02023-03-04 21:12:07 -0800267 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800268 .buttons = {{kHPConePickup}},
Austin Schuhe062be02023-03-04 21:12:07 -0800269 .side = Side::FRONT,
270 },
271 {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800272 .index = arm::HPPickupBackConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800273 .wrist_goal = 0.5,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800274 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800275 .buttons = {{kHPConePickup}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800276 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800277 },
278 {
Austin Schuhe062be02023-03-04 21:12:07 -0800279 .index = arm::ScoreFrontHighConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800280 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800281 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800282 .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT},
283 {kHighConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800284 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800285 .row_hint = RowSelectionHint::TOP,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800286 },
287 {
Austin Schuhe062be02023-03-04 21:12:07 -0800288 .index = arm::ScoreFrontMidConeUpIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800289 .wrist_goal = kConeWrist + 0.05,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800290 .game_piece = GamePiece::CONE_UP,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800291 .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT},
292 {kMidConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800293 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800294 .row_hint = RowSelectionHint::MIDDLE,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800295 },
milind-u68842e12023-02-26 12:45:40 -0800296 {
297 .index = arm::GroundPickupBackCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800298 .wrist_goal = kCubeWrist,
milind-u68842e12023-02-26 12:45:40 -0800299 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800300 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800301 .side = Side::BACK,
302 },
303 {
304 .index = arm::ScoreFrontMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800305 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800306 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800307 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800308 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800309 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800310 },
311 {
312 .index = arm::ScoreBackMidCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800313 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800314 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800315 .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800316 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800317 .row_hint = RowSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800318 },
319 {
320 .index = arm::ScoreFrontLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800321 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800322 .game_piece = GamePiece::CUBE,
Austin Schuh6be75772023-03-11 15:12:33 -0800323 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
324 {kLowCube, SpotSelectionHint::MIDDLE},
325 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800326 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800327 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800328 },
329 {
330 .index = arm::ScoreBackLowCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800331 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800332 .game_piece = GamePiece::CUBE,
Austin Schuh6be75772023-03-11 15:12:33 -0800333 .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT},
334 {kLowCube, SpotSelectionHint::MIDDLE},
335 {kLowConeScoreRight, SpotSelectionHint::RIGHT}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800336 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800337 .row_hint = RowSelectionHint::BOTTOM,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800338 },
339 {
340 .index = arm::ScoreFrontHighCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800341 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800342 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800343 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800344 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800345 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800346 },
347 {
348 .index = arm::ScoreBackHighCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800349 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800350 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800351 .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800352 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800353 .row_hint = RowSelectionHint::TOP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800354 },
355 {
356 .index = arm::GroundPickupFrontCubeIndex(),
Austin Schuh99dda682023-03-11 00:18:37 -0800357 .wrist_goal = kCubeWrist,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800358 .game_piece = GamePiece::CUBE,
James Kuszmaul4ac45762023-03-04 19:10:55 -0800359 .buttons = {{kGroundPickupCube}},
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800360 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800361 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800362};
363
Maxwell Hendersonad312342023-01-10 12:07:47 -0800364class Reader : public ::frc971::input::ActionJoystickInput {
365 public:
366 Reader(::aos::EventLoop *event_loop)
367 : ::frc971::input::ActionJoystickInput(
368 event_loop,
369 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
Ravago Jones8c65c432023-03-25 17:35:39 -0700370 ::frc971::input::DrivetrainInputReader::InputType::kPistol,
371 {.use_redundant_joysticks = true}),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800372 superstructure_goal_sender_(
373 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800374 target_selector_hint_sender_(
375 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800376 superstructure_status_fetcher_(
377 event_loop->MakeFetcher<superstructure::Status>(
378 "/superstructure")) {}
379
380 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
381
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800382 GamePiece current_game_piece_ = GamePiece::CONE_UP;
383
Austin Schuhf194af32023-03-22 20:31:18 -0700384 bool has_scored_ = false;
385
Maxwell Hendersonad312342023-01-10 12:07:47 -0800386 void HandleTeleop(
387 const ::frc971::input::driver_station::Data &data) override {
388 superstructure_status_fetcher_.Fetch();
389 if (!superstructure_status_fetcher_.get()) {
390 AOS_LOG(ERROR, "Got no superstructure status message.\n");
391 return;
392 }
393
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800394 if (!superstructure_status_fetcher_->has_wrist()) {
395 AOS_LOG(ERROR, "Got no superstructure status message.\n");
396 return;
397 }
milind-udefab712023-02-20 22:22:02 -0800398
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800399 double wrist_goal = 0.0;
400 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800401 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800402 std::optional<double> score_wrist_goal = std::nullopt;
Austin Schuh99dda682023-03-11 00:18:37 -0800403 std::optional<double> place_index = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800404
405 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800406 roller_goal = RollerGoal::INTAKE_CONE_UP;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800407 current_game_piece_ = GamePiece::CONE_UP;
Austin Schuh99dda682023-03-11 00:18:37 -0800408 } else if (data.IsPressed(kGroundPickupConeDown)) {
Maxwell Hendersonbf1bcec2023-03-05 18:00:20 -0800409 roller_goal = RollerGoal::INTAKE_CONE_DOWN;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800410 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800411 } else if (data.IsPressed(kGroundPickupCube)) {
412 roller_goal = RollerGoal::INTAKE_CUBE;
413 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800414 }
415
Austin Schuh99dda682023-03-11 00:18:37 -0800416 if (current_game_piece_ == GamePiece::CONE_DOWN ||
417 current_game_piece_ == GamePiece::CONE_TIP) {
418 if (data.IsPressed(kConeDownTip)) {
419 current_game_piece_ = GamePiece::CONE_TIP;
420 } else if (data.IsPressed(kConeDownBase)) {
421 current_game_piece_ = GamePiece::CONE_DOWN;
422 }
423 }
424
milind-u68842e12023-02-26 12:45:40 -0800425 if (current_game_piece_ == GamePiece::CUBE) {
Austin Schuh99dda682023-03-11 00:18:37 -0800426 wrist_goal = kCubeWrist;
milind-u68842e12023-02-26 12:45:40 -0800427 }
428
James Kuszmaul4e171432023-02-26 13:39:37 -0800429 std::optional<RowSelectionHint> placing_row;
430 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800431
Austin Schuhe062be02023-03-04 21:12:07 -0800432 // Keep the setpoint if the button is still held. This lets us release the
433 // back button once a side has been selected.
434 if (current_setpoint_ != nullptr) {
435 bool found = false;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800436 for (const auto &button : current_setpoint_->buttons) {
437 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800438 found = true;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800439 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800440 }
441 }
442 if (!found) {
443 current_setpoint_ = nullptr;
444 }
445 }
446
447 // Ok, no active setpoint. Search for the right one.
448 if (current_setpoint_ == nullptr) {
Austin Schuhf194af32023-03-22 20:31:18 -0700449 has_scored_ = false;
Austin Schuhe062be02023-03-04 21:12:07 -0800450 const Side current_side =
451 data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
452 // Search for the active setpoint.
453 for (const ArmSetpoint &setpoint : setpoints) {
James Kuszmaul4ac45762023-03-04 19:10:55 -0800454 for (const auto &button : setpoint.buttons) {
455 if (data.IsPressed(button.button)) {
Austin Schuhe062be02023-03-04 21:12:07 -0800456 if (setpoint.game_piece == current_game_piece_ &&
457 setpoint.side == current_side) {
458 current_setpoint_ = &setpoint;
James Kuszmaul4ac45762023-03-04 19:10:55 -0800459 placing_spot = button.spot;
Austin Schuhe062be02023-03-04 21:12:07 -0800460 }
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800461 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800462 }
463 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800464 }
Austin Schuhe062be02023-03-04 21:12:07 -0800465
466 // And, pull the bits out of it.
467 if (current_setpoint_ != nullptr) {
Austin Schuh038b5452023-03-08 20:55:45 -0800468 if (!data.IsPressed(kStayIn)) {
469 wrist_goal = current_setpoint_->wrist_goal;
470 arm_goal_position_ = current_setpoint_->index;
471 score_wrist_goal = current_setpoint_->score_wrist_goal;
Austin Schuh99dda682023-03-11 00:18:37 -0800472 place_index = current_setpoint_->place_index;
Austin Schuh038b5452023-03-08 20:55:45 -0800473 }
474
Austin Schuhe062be02023-03-04 21:12:07 -0800475 placing_row = current_setpoint_->row_hint;
Austin Schuhe062be02023-03-04 21:12:07 -0800476 }
477
James Kuszmaul4e171432023-02-26 13:39:37 -0800478 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800479
Austin Schuh23a90022023-02-24 22:13:39 -0800480 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800481 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh8e6f2872023-03-08 20:55:27 -0800482 } else if (data.IsPressed(kSpit) || data.IsPressed(kDriverSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800483 if (score_wrist_goal.has_value()) {
484 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800485
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800486 // If we are supposed to dunk it, wait until we are close enough to
487 // spit.
488 if (std::abs(score_wrist_goal.value() -
Austin Schuh99dda682023-03-11 00:18:37 -0800489 superstructure_status_fetcher_->wrist()->goal_position()) <
Austin Schuhf194af32023-03-22 20:31:18 -0700490 0.1 ||
491 has_scored_) {
Austin Schuh99dda682023-03-11 00:18:37 -0800492 if (place_index.has_value()) {
493 arm_goal_position_ = place_index.value();
Austin Schuhf194af32023-03-22 20:31:18 -0700494 if ((arm_goal_position_ ==
495 superstructure_status_fetcher_->arm()->current_node() &&
496 superstructure_status_fetcher_->arm()->path_distance_to_go() <
497 0.01) ||
498 has_scored_) {
499 has_scored_ = true;
Austin Schuh99dda682023-03-11 00:18:37 -0800500 roller_goal = RollerGoal::SPIT;
501 }
502 } else {
503 roller_goal = RollerGoal::SPIT;
504 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800505 }
506 } else {
507 roller_goal = RollerGoal::SPIT;
508 }
milind-udefab712023-02-20 22:22:02 -0800509 }
510
Maxwell Hendersonad312342023-01-10 12:07:47 -0800511 {
512 auto builder = superstructure_goal_sender_.MakeBuilder();
513
Austin Schuh6dc925b2023-02-24 16:23:32 -0800514 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800515 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
516 *builder.fbb(), wrist_goal,
517 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800518
Maxwell Hendersonad312342023-01-10 12:07:47 -0800519 superstructure::Goal::Builder superstructure_goal_builder =
520 builder.MakeBuilder<superstructure::Goal>();
Ravago Joneseb70f6b2023-03-25 17:35:39 -0700521 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800522 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800523 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800524 if (builder.Send(superstructure_goal_builder.Finish()) !=
525 aos::RawSender::Error::kOk) {
526 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
527 }
528 }
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800529 // TODO(james): Is there a more principled way to detect Human Player
530 // pickup? Probably don't bother fixing it until/unless we add more buttons
531 // that can select human player pickup.
532 if (data.IsPressed(kHPConePickup)) {
533 auto builder = target_selector_hint_sender_.MakeBuilder();
534 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
535 hint_builder.add_substation_pickup(true);
Austin Schuh6bdcc372024-06-27 14:49:11 -0700536 CHECK(current_setpoint_ != nullptr);
537 hint_builder.add_robot_side(current_setpoint_->side);
James Kuszmaul5a66f8a2023-03-11 14:28:30 -0800538 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
539 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
540 }
541 } else if (placing_row.has_value()) {
James Kuszmaul4e171432023-02-26 13:39:37 -0800542 auto builder = target_selector_hint_sender_.MakeBuilder();
543 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
544 hint_builder.add_row(placing_row.value());
545 hint_builder.add_spot(placing_spot.value());
Austin Schuh6bdcc372024-06-27 14:49:11 -0700546 CHECK(current_setpoint_ != nullptr);
547 hint_builder.add_robot_side(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
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800565} // namespace y2023::input::joysticks
Maxwell Hendersonad312342023-01-10 12:07:47 -0800566
567int main(int argc, char **argv) {
568 ::aos::InitGoogle(&argc, &argv);
569
570 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
571 aos::configuration::ReadConfig("aos_config.json");
572
573 ::aos::ShmEventLoop event_loop(&config.message());
574 ::y2023::input::joysticks::Reader reader(&event_loop);
575
576 event_loop.Run();
577
578 return 0;
579}