blob: 5cb148c90f2f9080cd80c9c0be29ec6b2fa8da2a [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 kIntake(4, 5);
41const ButtonLocation kScore(4, 4);
Austin Schuh23a90022023-02-24 22:13:39 -080042const ButtonLocation kSpit(4, 13);
43
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080044const ButtonLocation kMidBackTipConeScoreLeft(4, 15);
45const ButtonLocation kHighBackTipConeScoreLeft(4, 14);
46const ButtonLocation kMidBackTipConeScoreRight(3, 2);
47
48const ButtonLocation kGroundPickupConeUp(4, 7);
49const ButtonLocation kGroundPickupConeDown(4, 8);
milind-u71da5392023-02-26 12:45:00 -080050const ButtonLocation kGroundPickupCube(4, 10);
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080051const ButtonLocation kHPConePickup(4, 6);
52
Austin Schuh23a90022023-02-24 22:13:39 -080053const ButtonLocation kSuck(4, 12);
milind-udefab712023-02-20 22:22:02 -080054
Austin Schuh6dc925b2023-02-24 16:23:32 -080055const ButtonLocation kWrist(4, 10);
56
Maxwell Hendersonad312342023-01-10 12:07:47 -080057namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080058namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080059
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080060enum class GamePiece {
61 CONE_UP = 0,
62 CONE_DOWN = 1,
63 CUBE = 2,
64};
65
66struct ArmSetpoint {
67 uint32_t index;
68 double wrist_goal;
69 std::optional<double> score_wrist_goal = std::nullopt;
70 GamePiece game_piece;
71 ButtonLocation button;
72};
73
74const std::vector<ArmSetpoint> setpoints = {
75 {
76 .index = arm::GroundPickupBackConeUpIndex(),
77 .wrist_goal = 0.0,
78 .game_piece = GamePiece::CONE_UP,
79 .button = kGroundPickupConeUp,
80 },
81 {
82 .index = arm::GroundPickupBackConeDownIndex(),
83 .wrist_goal = 0.0,
84 .game_piece = GamePiece::CONE_DOWN,
85 .button = kGroundPickupConeDown,
86 },
87 {
88 .index = arm::ScoreBackMidConeUpPosIndex(),
89 .wrist_goal = 0.55,
90 .game_piece = GamePiece::CONE_UP,
91 .button = kMidBackTipConeScoreRight,
92 },
93 {
94 .index = arm::ScoreBackMidConeDownPosIndex(),
95 .wrist_goal = 2.2,
96 .score_wrist_goal = 0.0,
97 .game_piece = GamePiece::CONE_DOWN,
98 .button = kMidBackTipConeScoreRight,
99 },
100 {
101 .index = arm::HPPickupBackConeUpIndex(),
102 .wrist_goal = 0.2,
103 .game_piece = GamePiece::CONE_UP,
104 .button = kHPConePickup,
105 },
106 {
107 .index = arm::ScoreFrontHighConeUpPosIndex(),
108 .wrist_goal = 0.05,
109 .game_piece = GamePiece::CONE_UP,
110 .button = kHighBackTipConeScoreLeft,
111 },
112 {
113 .index = arm::ScoreFrontMidConeUpPosIndex(),
114 .wrist_goal = 0.05,
115 .game_piece = GamePiece::CONE_UP,
116 .button = kMidBackTipConeScoreLeft,
117 },
milind-u68842e12023-02-26 12:45:40 -0800118 {
119 .index = arm::GroundPickupBackCubeIndex(),
120 .wrist_goal = 0.6,
121 .game_piece = GamePiece::CUBE,
122 .button = kGroundPickupCube,
123 },
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800124};
125
Maxwell Hendersonad312342023-01-10 12:07:47 -0800126class Reader : public ::frc971::input::ActionJoystickInput {
127 public:
128 Reader(::aos::EventLoop *event_loop)
129 : ::frc971::input::ActionJoystickInput(
130 event_loop,
131 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
132 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
133 superstructure_goal_sender_(
134 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
135 superstructure_status_fetcher_(
136 event_loop->MakeFetcher<superstructure::Status>(
137 "/superstructure")) {}
138
139 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
140
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800141 GamePiece current_game_piece_ = GamePiece::CONE_UP;
142
Maxwell Hendersonad312342023-01-10 12:07:47 -0800143 void HandleTeleop(
144 const ::frc971::input::driver_station::Data &data) override {
145 superstructure_status_fetcher_.Fetch();
146 if (!superstructure_status_fetcher_.get()) {
147 AOS_LOG(ERROR, "Got no superstructure status message.\n");
148 return;
149 }
150
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800151 if (!superstructure_status_fetcher_->has_wrist()) {
152 AOS_LOG(ERROR, "Got no superstructure status message.\n");
153 return;
154 }
milind-udefab712023-02-20 22:22:02 -0800155
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800156 double wrist_goal = 0.0;
157 RollerGoal roller_goal = RollerGoal::IDLE;
158 arm_goal_position_ = arm::NeutralPosIndex();
159 std::optional<double> score_wrist_goal = std::nullopt;
160
161 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
milind-u71da5392023-02-26 12:45:00 -0800162 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800163 current_game_piece_ = GamePiece::CONE_UP;
164 } else if (data.IsPressed(kGroundPickupConeDown)) {
milind-u71da5392023-02-26 12:45:00 -0800165 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800166 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800167 } else if (data.IsPressed(kGroundPickupCube)) {
168 roller_goal = RollerGoal::INTAKE_CUBE;
169 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800170 }
171
milind-u68842e12023-02-26 12:45:40 -0800172 if (current_game_piece_ == GamePiece::CUBE) {
173 wrist_goal = 0.6;
174 }
175
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800176 // Search for the active setpoint.
177 for (const ArmSetpoint &setpoint : setpoints) {
178 if (data.IsPressed(setpoint.button)) {
179 if (setpoint.game_piece == current_game_piece_) {
180 wrist_goal = setpoint.wrist_goal;
181 arm_goal_position_ = setpoint.index;
182 score_wrist_goal = setpoint.score_wrist_goal;
183 break;
184 }
185 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800186 }
187
Austin Schuh23a90022023-02-24 22:13:39 -0800188 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800189 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh23a90022023-02-24 22:13:39 -0800190 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800191 if (score_wrist_goal.has_value()) {
192 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800193
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800194 // If we are supposed to dunk it, wait until we are close enough to
195 // spit.
196 if (std::abs(score_wrist_goal.value() -
197 superstructure_status_fetcher_->wrist()->position()) <
198 0.1) {
199 roller_goal = RollerGoal::SPIT;
200 }
201 } else {
202 roller_goal = RollerGoal::SPIT;
203 }
milind-udefab712023-02-20 22:22:02 -0800204 }
205
Maxwell Hendersonad312342023-01-10 12:07:47 -0800206 {
207 auto builder = superstructure_goal_sender_.MakeBuilder();
208
Austin Schuh6dc925b2023-02-24 16:23:32 -0800209 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800210 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
211 *builder.fbb(), wrist_goal,
212 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800213
Maxwell Hendersonad312342023-01-10 12:07:47 -0800214 superstructure::Goal::Builder superstructure_goal_builder =
215 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800216 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800217 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800218 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800219 if (builder.Send(superstructure_goal_builder.Finish()) !=
220 aos::RawSender::Error::kOk) {
221 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
222 }
223 }
224 }
225
226 private:
227 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
228
229 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800230
231 uint32_t arm_goal_position_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800232};
233
234} // namespace joysticks
235} // namespace input
236} // namespace y2023
237
238int main(int argc, char **argv) {
239 ::aos::InitGoogle(&argc, &argv);
240
241 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
242 aos::configuration::ReadConfig("aos_config.json");
243
244 ::aos::ShmEventLoop event_loop(&config.message());
245 ::y2023::input::joysticks::Reader reader(&event_loop);
246
247 event_loop.Run();
248
249 return 0;
250}