blob: 489934b05b7a487d9d53b59bb1718d3c76f352eb [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"
milind-udefab712023-02-20 22:22:02 -080022#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080023#include "y2023/control_loops/superstructure/superstructure_goal_generated.h"
24#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
25
26using frc971::CreateProfileParameters;
27using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
28using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
29using frc971::input::driver_station::ButtonLocation;
30using frc971::input::driver_station::ControlBit;
31using frc971::input::driver_station::JoystickAxis;
32using frc971::input::driver_station::POVLocation;
Maxwell Henderson5938a832023-02-23 09:33:15 -080033using y2023::control_loops::superstructure::RollerGoal;
Maxwell Hendersonad312342023-01-10 12:07:47 -080034
35namespace y2023 {
36namespace input {
37namespace joysticks {
38
milind-udefab712023-02-20 22:22:02 -080039// TODO(milind): add correct locations
Austin Schuh6dc925b2023-02-24 16:23:32 -080040const ButtonLocation kScore(4, 4);
Austin Schuh23a90022023-02-24 22:13:39 -080041const ButtonLocation kSpit(4, 13);
42
Austin Schuh9b3e41c2023-02-26 22:29:53 -080043const ButtonLocation kHighConeScoreLeft(4, 14);
44const ButtonLocation kHighConeScoreRight(3, 1);
45
46const ButtonLocation kMidConeScoreLeft(4, 15);
47const ButtonLocation kMidConeScoreRight(3, 2);
48
49const ButtonLocation kHighCube(4, 1);
50const ButtonLocation kMidCube(4, 2);
51const ButtonLocation kLowCube(4, 3);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080052
53const ButtonLocation kGroundPickupConeUp(4, 7);
54const ButtonLocation kGroundPickupConeDown(4, 8);
milind-u71da5392023-02-26 12:45:00 -080055const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080056const ButtonLocation kHPConePickup(4, 6);
57
Austin Schuh9b3e41c2023-02-26 22:29:53 -080058const ButtonLocation kSuck(4, 11);
59const ButtonLocation kBack(4, 12);
milind-udefab712023-02-20 22:22:02 -080060
Austin Schuh6dc925b2023-02-24 16:23:32 -080061const ButtonLocation kWrist(4, 10);
62
Maxwell Hendersonad312342023-01-10 12:07:47 -080063namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080064namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080065
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080066enum class GamePiece {
67 CONE_UP = 0,
68 CONE_DOWN = 1,
69 CUBE = 2,
70};
71
Austin Schuh9b3e41c2023-02-26 22:29:53 -080072enum class Side {
73 FRONT = 0,
74 BACK = 1,
75};
76
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080077struct ArmSetpoint {
78 uint32_t index;
79 double wrist_goal;
80 std::optional<double> score_wrist_goal = std::nullopt;
81 GamePiece game_piece;
Austin Schuh9b3e41c2023-02-26 22:29:53 -080082 std::vector<ButtonLocation> buttons;
83 Side side;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080084};
85
86const std::vector<ArmSetpoint> setpoints = {
87 {
88 .index = arm::GroundPickupBackConeUpIndex(),
89 .wrist_goal = 0.0,
90 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -080091 .buttons = {kGroundPickupConeUp},
92 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080093 },
94 {
95 .index = arm::GroundPickupBackConeDownIndex(),
96 .wrist_goal = 0.0,
97 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh9b3e41c2023-02-26 22:29:53 -080098 .buttons = {kGroundPickupConeDown},
99 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800100 },
101 {
102 .index = arm::ScoreBackMidConeUpPosIndex(),
103 .wrist_goal = 0.55,
104 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800105 .buttons = {kMidConeScoreRight},
106 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800107 },
108 {
109 .index = arm::ScoreBackMidConeDownPosIndex(),
110 .wrist_goal = 2.2,
111 .score_wrist_goal = 0.0,
112 .game_piece = GamePiece::CONE_DOWN,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800113 .buttons = {kMidConeScoreRight},
114 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800115 },
116 {
117 .index = arm::HPPickupBackConeUpIndex(),
118 .wrist_goal = 0.2,
119 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800120 .buttons = {kHPConePickup},
121 .side = Side::BACK,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800122 },
123 {
124 .index = arm::ScoreFrontHighConeUpPosIndex(),
125 .wrist_goal = 0.05,
126 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800127 .buttons = {kHighConeScoreLeft, kHighConeScoreRight},
128 .side = Side::FRONT,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800129 },
130 {
131 .index = arm::ScoreFrontMidConeUpPosIndex(),
132 .wrist_goal = 0.05,
133 .game_piece = GamePiece::CONE_UP,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800134 .buttons = {kMidConeScoreLeft, kMidConeScoreRight},
135 .side = Side::FRONT,
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800136 },
milind-u68842e12023-02-26 12:45:40 -0800137 {
138 .index = arm::GroundPickupBackCubeIndex(),
139 .wrist_goal = 0.6,
140 .game_piece = GamePiece::CUBE,
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800141 .buttons = {kGroundPickupCube},
142 .side = Side::BACK,
143 },
144 {
145 .index = arm::ScoreFrontMidCubeIndex(),
146 .wrist_goal = 0.6,
147 .game_piece = GamePiece::CUBE,
148 .buttons = {kMidCube},
149 .side = Side::FRONT,
150 },
151 {
152 .index = arm::ScoreBackMidCubeIndex(),
153 .wrist_goal = 0.6,
154 .score_wrist_goal = 0.0,
155 .game_piece = GamePiece::CUBE,
156 .buttons = {kMidCube},
157 .side = Side::BACK,
158 },
159 {
160 .index = arm::ScoreFrontLowCubeIndex(),
161 .wrist_goal = 0.6,
162 .game_piece = GamePiece::CUBE,
163 .buttons = {kLowCube},
164 .side = Side::FRONT,
165 },
166 {
167 .index = arm::ScoreBackLowCubeIndex(),
168 .wrist_goal = 0.6,
169 .game_piece = GamePiece::CUBE,
170 .buttons = {kLowCube},
171 .side = Side::BACK,
172 },
173 {
174 .index = arm::ScoreFrontHighCubeIndex(),
175 .wrist_goal = 0.6,
176 .game_piece = GamePiece::CUBE,
177 .buttons = {kHighCube},
178 .side = Side::FRONT,
179 },
180 {
181 .index = arm::ScoreBackHighCubeIndex(),
182 .wrist_goal = 0.6,
183 .score_wrist_goal = 0.0,
184 .game_piece = GamePiece::CUBE,
185 .buttons = {kHighCube},
186 .side = Side::BACK,
187 },
188 {
189 .index = arm::GroundPickupFrontCubeIndex(),
190 .wrist_goal = 0.6,
191 .game_piece = GamePiece::CUBE,
192 .buttons = {kGroundPickupCube},
193 .side = Side::FRONT,
milind-u68842e12023-02-26 12:45:40 -0800194 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800195};
196
Maxwell Hendersonad312342023-01-10 12:07:47 -0800197class Reader : public ::frc971::input::ActionJoystickInput {
198 public:
199 Reader(::aos::EventLoop *event_loop)
200 : ::frc971::input::ActionJoystickInput(
201 event_loop,
202 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
203 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
204 superstructure_goal_sender_(
205 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
206 superstructure_status_fetcher_(
207 event_loop->MakeFetcher<superstructure::Status>(
208 "/superstructure")) {}
209
210 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
211
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800212 GamePiece current_game_piece_ = GamePiece::CONE_UP;
213
Maxwell Hendersonad312342023-01-10 12:07:47 -0800214 void HandleTeleop(
215 const ::frc971::input::driver_station::Data &data) override {
216 superstructure_status_fetcher_.Fetch();
217 if (!superstructure_status_fetcher_.get()) {
218 AOS_LOG(ERROR, "Got no superstructure status message.\n");
219 return;
220 }
221
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800222 if (!superstructure_status_fetcher_->has_wrist()) {
223 AOS_LOG(ERROR, "Got no superstructure status message.\n");
224 return;
225 }
milind-udefab712023-02-20 22:22:02 -0800226
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800227 double wrist_goal = 0.0;
228 RollerGoal roller_goal = RollerGoal::IDLE;
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800229 arm_goal_position_ = arm::NeutralIndex();
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800230 std::optional<double> score_wrist_goal = std::nullopt;
231
232 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
milind-u71da5392023-02-26 12:45:00 -0800233 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800234 current_game_piece_ = GamePiece::CONE_UP;
235 } else if (data.IsPressed(kGroundPickupConeDown)) {
milind-u71da5392023-02-26 12:45:00 -0800236 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800237 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800238 } else if (data.IsPressed(kGroundPickupCube)) {
239 roller_goal = RollerGoal::INTAKE_CUBE;
240 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800241 }
242
milind-u68842e12023-02-26 12:45:40 -0800243 if (current_game_piece_ == GamePiece::CUBE) {
244 wrist_goal = 0.6;
245 }
246
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800247 const Side current_side = data.IsPressed(kBack) ? Side::BACK : Side::FRONT;
248
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800249 // Search for the active setpoint.
250 for (const ArmSetpoint &setpoint : setpoints) {
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800251 for (const ButtonLocation &button : setpoint.buttons) {
252 if (data.IsPressed(button)) {
253 if (setpoint.game_piece == current_game_piece_ &&
254 setpoint.side == current_side) {
255 wrist_goal = setpoint.wrist_goal;
256 arm_goal_position_ = setpoint.index;
257 score_wrist_goal = setpoint.score_wrist_goal;
258 break;
259 }
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800260 }
261 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800262 }
263
Austin Schuh23a90022023-02-24 22:13:39 -0800264 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800265 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh23a90022023-02-24 22:13:39 -0800266 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800267 if (score_wrist_goal.has_value()) {
268 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800269
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800270 // If we are supposed to dunk it, wait until we are close enough to
271 // spit.
272 if (std::abs(score_wrist_goal.value() -
273 superstructure_status_fetcher_->wrist()->position()) <
274 0.1) {
275 roller_goal = RollerGoal::SPIT;
276 }
277 } else {
278 roller_goal = RollerGoal::SPIT;
279 }
milind-udefab712023-02-20 22:22:02 -0800280 }
281
Maxwell Hendersonad312342023-01-10 12:07:47 -0800282 {
283 auto builder = superstructure_goal_sender_.MakeBuilder();
284
Austin Schuh6dc925b2023-02-24 16:23:32 -0800285 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800286 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
287 *builder.fbb(), wrist_goal,
288 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800289
Maxwell Hendersonad312342023-01-10 12:07:47 -0800290 superstructure::Goal::Builder superstructure_goal_builder =
291 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800292 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800293 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800294 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800295 if (builder.Send(superstructure_goal_builder.Finish()) !=
296 aos::RawSender::Error::kOk) {
297 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
298 }
299 }
300 }
301
302 private:
303 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
304
305 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800306
307 uint32_t arm_goal_position_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800308};
309
310} // namespace joysticks
311} // namespace input
312} // namespace y2023
313
314int main(int argc, char **argv) {
315 ::aos::InitGoogle(&argc, &argv);
316
317 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
318 aos::configuration::ReadConfig("aos_config.json");
319
320 ::aos::ShmEventLoop event_loop(&config.message());
321 ::y2023::input::joysticks::Reader reader(&event_loop);
322
323 event_loop.Run();
324
325 return 0;
326}