blob: a7fed8f5c70843b51d7545e4bee288e81d174090 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include <unistd.h>
2
3#include <cmath>
4#include <cstdio>
5#include <cstring>
6
7#include "aos/actions/actions.h"
8#include "aos/init.h"
9#include "aos/logging/logging.h"
10#include "aos/network/team_number.h"
11#include "aos/util/log_interval.h"
12#include "frc971/autonomous/base_autonomous_actor.h"
13#include "frc971/control_loops/drivetrain/localizer_generated.h"
14#include "frc971/control_loops/profiled_subsystem_generated.h"
15#include "frc971/input/action_joystick_input.h"
16#include "frc971/input/driver_station_data.h"
17#include "frc971/input/drivetrain_input.h"
18#include "frc971/input/joystick_input.h"
19#include "frc971/zeroing/wrap.h"
20#include "y2023/constants.h"
21#include "y2023/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul4e171432023-02-26 13:39:37 -080022#include "y2023/control_loops/drivetrain/target_selector_hint_generated.h"
milind-udefab712023-02-20 22:22:02 -080023#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080024#include "y2023/control_loops/superstructure/superstructure_goal_generated.h"
25#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
26
27using frc971::CreateProfileParameters;
28using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
29using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
30using frc971::input::driver_station::ButtonLocation;
31using frc971::input::driver_station::ControlBit;
32using frc971::input::driver_station::JoystickAxis;
33using frc971::input::driver_station::POVLocation;
Maxwell Henderson5938a832023-02-23 09:33:15 -080034using y2023::control_loops::superstructure::RollerGoal;
James Kuszmaul4e171432023-02-26 13:39:37 -080035using y2023::control_loops::drivetrain::RowSelectionHint;
36using y2023::control_loops::drivetrain::GridSelectionHint;
37using y2023::control_loops::drivetrain::SpotSelectionHint;
38using y2023::control_loops::drivetrain::TargetSelectorHint;
Maxwell Hendersonad312342023-01-10 12:07:47 -080039
40namespace y2023 {
41namespace input {
42namespace joysticks {
43
milind-udefab712023-02-20 22:22:02 -080044// TODO(milind): add correct locations
Austin Schuh6dc925b2023-02-24 16:23:32 -080045const ButtonLocation kScore(4, 4);
Austin Schuh23a90022023-02-24 22:13:39 -080046const ButtonLocation kSpit(4, 13);
47
Austin Schuh9b3e41c2023-02-26 22:29:53 -080048const ButtonLocation kHighConeScoreLeft(4, 14);
49const ButtonLocation kHighConeScoreRight(3, 1);
50
51const ButtonLocation kMidConeScoreLeft(4, 15);
52const ButtonLocation kMidConeScoreRight(3, 2);
53
54const ButtonLocation kHighCube(4, 1);
55const ButtonLocation kMidCube(4, 2);
56const ButtonLocation kLowCube(4, 3);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080057
58const ButtonLocation kGroundPickupConeUp(4, 7);
59const ButtonLocation kGroundPickupConeDown(4, 8);
milind-u71da5392023-02-26 12:45:00 -080060const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080061const ButtonLocation kHPConePickup(4, 6);
62
Austin Schuh9b3e41c2023-02-26 22:29:53 -080063const ButtonLocation kSuck(4, 11);
64const ButtonLocation kBack(4, 12);
milind-udefab712023-02-20 22:22:02 -080065
Austin Schuh6dc925b2023-02-24 16:23:32 -080066const ButtonLocation kWrist(4, 10);
67
Maxwell Hendersonad312342023-01-10 12:07:47 -080068namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080069namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080070
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080071enum class GamePiece {
72 CONE_UP = 0,
73 CONE_DOWN = 1,
74 CUBE = 2,
75};
76
Austin Schuh9b3e41c2023-02-26 22:29:53 -080077enum class Side {
78 FRONT = 0,
79 BACK = 1,
80};
81
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080082struct ArmSetpoint {
83 uint32_t index;
84 double wrist_goal;
85 std::optional<double> score_wrist_goal = std::nullopt;
86 GamePiece game_piece;
Austin Schuh9b3e41c2023-02-26 22:29:53 -080087 std::vector<ButtonLocation> buttons;
88 Side side;
James Kuszmaul4e171432023-02-26 13:39:37 -080089 std::optional<RowSelectionHint> row_hint = std::nullopt;
90 std::optional<SpotSelectionHint> spot_hint = std::nullopt;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080091};
92
93const std::vector<ArmSetpoint> setpoints = {
94 {
95 .index = arm::GroundPickupBackConeUpIndex(),
96 .wrist_goal = 0.0,
97 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -080098 .buttons = {kGroundPickupConeUp},
99 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800100 },
101 {
102 .index = arm::GroundPickupBackConeDownIndex(),
103 .wrist_goal = 0.0,
104 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800105 .buttons = {kGroundPickupConeDown},
106 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800107 },
108 {
109 .index = arm::ScoreBackMidConeUpPosIndex(),
110 .wrist_goal = 0.55,
111 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800112 .buttons = {kMidConeScoreRight},
113 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800114 .row_hint = RowSelectionHint::MIDDLE,
115 .spot_hint = SpotSelectionHint::RIGHT,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800116 },
117 {
118 .index = arm::ScoreBackMidConeDownPosIndex(),
119 .wrist_goal = 2.2,
120 .score_wrist_goal = 0.0,
121 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800122 .buttons = {kMidConeScoreRight},
123 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800124 .row_hint = RowSelectionHint::MIDDLE,
125 .spot_hint = SpotSelectionHint::RIGHT,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800126 },
127 {
128 .index = arm::HPPickupBackConeUpIndex(),
129 .wrist_goal = 0.2,
130 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800131 .buttons = {kHPConePickup},
132 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800133 },
134 {
135 .index = arm::ScoreFrontHighConeUpPosIndex(),
136 .wrist_goal = 0.05,
137 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800138 .buttons = {kHighConeScoreLeft, kHighConeScoreRight},
139 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800140 .row_hint = RowSelectionHint::TOP,
141 .spot_hint = SpotSelectionHint::LEFT,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800142 },
143 {
144 .index = arm::ScoreFrontMidConeUpPosIndex(),
145 .wrist_goal = 0.05,
146 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800147 .buttons = {kMidConeScoreLeft, kMidConeScoreRight},
148 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800149 .row_hint = RowSelectionHint::MIDDLE,
150 .spot_hint = SpotSelectionHint::LEFT,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800151 },
milind-u68842e12023-02-26 12:45:40 -0800152 {
153 .index = arm::GroundPickupBackCubeIndex(),
154 .wrist_goal = 0.6,
155 .game_piece = GamePiece::CUBE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800156 .buttons = {kGroundPickupCube},
157 .side = Side::BACK,
158 },
159 {
160 .index = arm::ScoreFrontMidCubeIndex(),
161 .wrist_goal = 0.6,
162 .game_piece = GamePiece::CUBE,
163 .buttons = {kMidCube},
164 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800165 .row_hint = RowSelectionHint::MIDDLE,
166 .spot_hint = SpotSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800167 },
168 {
169 .index = arm::ScoreBackMidCubeIndex(),
170 .wrist_goal = 0.6,
171 .score_wrist_goal = 0.0,
172 .game_piece = GamePiece::CUBE,
173 .buttons = {kMidCube},
174 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800175 .row_hint = RowSelectionHint::MIDDLE,
176 .spot_hint = SpotSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800177 },
178 {
179 .index = arm::ScoreFrontLowCubeIndex(),
180 .wrist_goal = 0.6,
181 .game_piece = GamePiece::CUBE,
182 .buttons = {kLowCube},
183 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800184 .row_hint = RowSelectionHint::BOTTOM,
185 .spot_hint = SpotSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800186 },
187 {
188 .index = arm::ScoreBackLowCubeIndex(),
189 .wrist_goal = 0.6,
190 .game_piece = GamePiece::CUBE,
191 .buttons = {kLowCube},
192 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800193 .row_hint = RowSelectionHint::BOTTOM,
194 .spot_hint = SpotSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800195 },
196 {
197 .index = arm::ScoreFrontHighCubeIndex(),
198 .wrist_goal = 0.6,
199 .game_piece = GamePiece::CUBE,
200 .buttons = {kHighCube},
201 .side = Side::FRONT,
James Kuszmaul4e171432023-02-26 13:39:37 -0800202 .row_hint = RowSelectionHint::TOP,
203 .spot_hint = SpotSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800204 },
205 {
206 .index = arm::ScoreBackHighCubeIndex(),
207 .wrist_goal = 0.6,
208 .score_wrist_goal = 0.0,
209 .game_piece = GamePiece::CUBE,
210 .buttons = {kHighCube},
211 .side = Side::BACK,
James Kuszmaul4e171432023-02-26 13:39:37 -0800212 .row_hint = RowSelectionHint::TOP,
213 .spot_hint = SpotSelectionHint::MIDDLE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800214 },
215 {
216 .index = arm::GroundPickupFrontCubeIndex(),
217 .wrist_goal = 0.6,
218 .game_piece = GamePiece::CUBE,
219 .buttons = {kGroundPickupCube},
220 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800221 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800222};
223
Maxwell Hendersonad312342023-01-10 12:07:47 -0800224class Reader : public ::frc971::input::ActionJoystickInput {
225 public:
226 Reader(::aos::EventLoop *event_loop)
227 : ::frc971::input::ActionJoystickInput(
228 event_loop,
229 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
230 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
231 superstructure_goal_sender_(
232 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul4e171432023-02-26 13:39:37 -0800233 target_selector_hint_sender_(
234 event_loop->MakeSender<TargetSelectorHint>("/drivetrain")),
Maxwell Hendersonad312342023-01-10 12:07:47 -0800235 superstructure_status_fetcher_(
236 event_loop->MakeFetcher<superstructure::Status>(
237 "/superstructure")) {}
238
239 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
240
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800241 GamePiece current_game_piece_ = GamePiece::CONE_UP;
242
Maxwell Hendersonad312342023-01-10 12:07:47 -0800243 void HandleTeleop(
244 const ::frc971::input::driver_station::Data &data) override {
245 superstructure_status_fetcher_.Fetch();
246 if (!superstructure_status_fetcher_.get()) {
247 AOS_LOG(ERROR, "Got no superstructure status message.\n");
248 return;
249 }
250
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800251 if (!superstructure_status_fetcher_->has_wrist()) {
252 AOS_LOG(ERROR, "Got no superstructure status message.\n");
253 return;
254 }
milind-udefab712023-02-20 22:22:02 -0800255
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800256 double wrist_goal = 0.0;
257 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800258 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800259 std::optional<double> score_wrist_goal = std::nullopt;
260
261 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
milind-u71da5392023-02-26 12:45:00 -0800262 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800263 current_game_piece_ = GamePiece::CONE_UP;
264 } else if (data.IsPressed(kGroundPickupConeDown)) {
milind-u71da5392023-02-26 12:45:00 -0800265 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800266 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800267 } else if (data.IsPressed(kGroundPickupCube)) {
268 roller_goal = RollerGoal::INTAKE_CUBE;
269 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800270 }
271
milind-u68842e12023-02-26 12:45:40 -0800272 if (current_game_piece_ == GamePiece::CUBE) {
273 wrist_goal = 0.6;
274 }
275
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800276 const Side current_side = data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
James Kuszmaul4e171432023-02-26 13:39:37 -0800277 std::optional<RowSelectionHint> placing_row;
278 std::optional<SpotSelectionHint> placing_spot;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800279
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800280 // Search for the active setpoint.
281 for (const ArmSetpoint &setpoint : setpoints) {
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800282 for (const ButtonLocation &button : setpoint.buttons) {
283 if (data.IsPressed(button)) {
284 if (setpoint.game_piece == current_game_piece_ &&
285 setpoint.side == current_side) {
286 wrist_goal = setpoint.wrist_goal;
287 arm_goal_position_ = setpoint.index;
288 score_wrist_goal = setpoint.score_wrist_goal;
James Kuszmaul4e171432023-02-26 13:39:37 -0800289 placing_row = setpoint.row_hint;
290 placing_spot = setpoint.spot_hint;
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800291 break;
292 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800293 }
294 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800295 }
James Kuszmaul4e171432023-02-26 13:39:37 -0800296 CHECK_EQ(placing_row.has_value(), placing_spot.has_value());
Austin Schuh6dc925b2023-02-24 16:23:32 -0800297
Austin Schuh23a90022023-02-24 22:13:39 -0800298 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800299 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh23a90022023-02-24 22:13:39 -0800300 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800301 if (score_wrist_goal.has_value()) {
302 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800303
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800304 // If we are supposed to dunk it, wait until we are close enough to
305 // spit.
306 if (std::abs(score_wrist_goal.value() -
307 superstructure_status_fetcher_->wrist()->position()) <
308 0.1) {
309 roller_goal = RollerGoal::SPIT;
310 }
311 } else {
312 roller_goal = RollerGoal::SPIT;
313 }
milind-udefab712023-02-20 22:22:02 -0800314 }
315
Maxwell Hendersonad312342023-01-10 12:07:47 -0800316 {
317 auto builder = superstructure_goal_sender_.MakeBuilder();
318
Austin Schuh6dc925b2023-02-24 16:23:32 -0800319 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800320 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
321 *builder.fbb(), wrist_goal,
322 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800323
Maxwell Hendersonad312342023-01-10 12:07:47 -0800324 superstructure::Goal::Builder superstructure_goal_builder =
325 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800326 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800327 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800328 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800329 if (builder.Send(superstructure_goal_builder.Finish()) !=
330 aos::RawSender::Error::kOk) {
331 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
332 }
333 }
James Kuszmaul4e171432023-02-26 13:39:37 -0800334 if (placing_row.has_value()) {
335 auto builder = target_selector_hint_sender_.MakeBuilder();
336 auto hint_builder = builder.MakeBuilder<TargetSelectorHint>();
337 hint_builder.add_row(placing_row.value());
338 hint_builder.add_spot(placing_spot.value());
339 // TODO: Add field to TargetSelector hint for forwards vs. backwards
340 // placement.
341 if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) {
342 AOS_LOG(ERROR, "Sending target selector hint failed.\n");
343 }
344 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800345 }
346
347 private:
348 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
James Kuszmaul4e171432023-02-26 13:39:37 -0800349 ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800350
351 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800352
353 uint32_t arm_goal_position_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800354};
355
356} // namespace joysticks
357} // namespace input
358} // namespace y2023
359
360int main(int argc, char **argv) {
361 ::aos::InitGoogle(&argc, &argv);
362
363 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
364 aos::configuration::ReadConfig("aos_config.json");
365
366 ::aos::ShmEventLoop event_loop(&config.message());
367 ::y2023::input::joysticks::Reader reader(&event_loop);
368
369 event_loop.Run();
370
371 return 0;
372}