blob: 92f79d8419e41b8a7d889c5d80663f66fe070d09 [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 },
118};
119
Maxwell Hendersonad312342023-01-10 12:07:47 -0800120class Reader : public ::frc971::input::ActionJoystickInput {
121 public:
122 Reader(::aos::EventLoop *event_loop)
123 : ::frc971::input::ActionJoystickInput(
124 event_loop,
125 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
126 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
127 superstructure_goal_sender_(
128 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
129 superstructure_status_fetcher_(
130 event_loop->MakeFetcher<superstructure::Status>(
131 "/superstructure")) {}
132
133 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
134
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800135 GamePiece current_game_piece_ = GamePiece::CONE_UP;
136
Maxwell Hendersonad312342023-01-10 12:07:47 -0800137 void HandleTeleop(
138 const ::frc971::input::driver_station::Data &data) override {
139 superstructure_status_fetcher_.Fetch();
140 if (!superstructure_status_fetcher_.get()) {
141 AOS_LOG(ERROR, "Got no superstructure status message.\n");
142 return;
143 }
144
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800145 if (!superstructure_status_fetcher_->has_wrist()) {
146 AOS_LOG(ERROR, "Got no superstructure status message.\n");
147 return;
148 }
milind-udefab712023-02-20 22:22:02 -0800149
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800150 double wrist_goal = 0.0;
151 RollerGoal roller_goal = RollerGoal::IDLE;
152 arm_goal_position_ = arm::NeutralPosIndex();
153 std::optional<double> score_wrist_goal = std::nullopt;
154
155 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
milind-u71da5392023-02-26 12:45:00 -0800156 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800157 current_game_piece_ = GamePiece::CONE_UP;
158 } else if (data.IsPressed(kGroundPickupConeDown)) {
milind-u71da5392023-02-26 12:45:00 -0800159 roller_goal = RollerGoal::INTAKE_CONE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800160 current_game_piece_ = GamePiece::CONE_DOWN;
milind-u71da5392023-02-26 12:45:00 -0800161 } else if (data.IsPressed(kGroundPickupCube)) {
162 roller_goal = RollerGoal::INTAKE_CUBE;
163 current_game_piece_ = GamePiece::CUBE;
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800164 }
165
166 // Search for the active setpoint.
167 for (const ArmSetpoint &setpoint : setpoints) {
168 if (data.IsPressed(setpoint.button)) {
169 if (setpoint.game_piece == current_game_piece_) {
170 wrist_goal = setpoint.wrist_goal;
171 arm_goal_position_ = setpoint.index;
172 score_wrist_goal = setpoint.score_wrist_goal;
173 break;
174 }
175 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800176 }
177
Austin Schuh23a90022023-02-24 22:13:39 -0800178 if (data.IsPressed(kSuck)) {
milind-u71da5392023-02-26 12:45:00 -0800179 roller_goal = RollerGoal::INTAKE_LAST;
Austin Schuh23a90022023-02-24 22:13:39 -0800180 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800181 if (score_wrist_goal.has_value()) {
182 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800183
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800184 // If we are supposed to dunk it, wait until we are close enough to
185 // spit.
186 if (std::abs(score_wrist_goal.value() -
187 superstructure_status_fetcher_->wrist()->position()) <
188 0.1) {
189 roller_goal = RollerGoal::SPIT;
190 }
191 } else {
192 roller_goal = RollerGoal::SPIT;
193 }
milind-udefab712023-02-20 22:22:02 -0800194 }
195
Maxwell Hendersonad312342023-01-10 12:07:47 -0800196 {
197 auto builder = superstructure_goal_sender_.MakeBuilder();
198
Austin Schuh6dc925b2023-02-24 16:23:32 -0800199 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800200 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
201 *builder.fbb(), wrist_goal,
202 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800203
Maxwell Hendersonad312342023-01-10 12:07:47 -0800204 superstructure::Goal::Builder superstructure_goal_builder =
205 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800206 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800207 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800208 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800209 if (builder.Send(superstructure_goal_builder.Finish()) !=
210 aos::RawSender::Error::kOk) {
211 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
212 }
213 }
214 }
215
216 private:
217 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
218
219 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800220
221 uint32_t arm_goal_position_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800222};
223
224} // namespace joysticks
225} // namespace input
226} // namespace y2023
227
228int main(int argc, char **argv) {
229 ::aos::InitGoogle(&argc, &argv);
230
231 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
232 aos::configuration::ReadConfig("aos_config.json");
233
234 ::aos::ShmEventLoop event_loop(&config.message());
235 ::y2023::input::joysticks::Reader reader(&event_loop);
236
237 event_loop.Run();
238
239 return 0;
240}