blob: 58f7df1bb06bab979822801723d7f4d81105066d [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);
50const ButtonLocation kHPConePickup(4, 6);
51
Austin Schuh23a90022023-02-24 22:13:39 -080052const ButtonLocation kSuck(4, 12);
milind-udefab712023-02-20 22:22:02 -080053
Austin Schuh6dc925b2023-02-24 16:23:32 -080054const ButtonLocation kWrist(4, 10);
55
Maxwell Hendersonad312342023-01-10 12:07:47 -080056namespace superstructure = y2023::control_loops::superstructure;
milind-udefab712023-02-20 22:22:02 -080057namespace arm = superstructure::arm;
Maxwell Hendersonad312342023-01-10 12:07:47 -080058
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -080059enum class GamePiece {
60 CONE_UP = 0,
61 CONE_DOWN = 1,
62 CUBE = 2,
63};
64
65struct ArmSetpoint {
66 uint32_t index;
67 double wrist_goal;
68 std::optional<double> score_wrist_goal = std::nullopt;
69 GamePiece game_piece;
70 ButtonLocation button;
71};
72
73const std::vector<ArmSetpoint> setpoints = {
74 {
75 .index = arm::GroundPickupBackConeUpIndex(),
76 .wrist_goal = 0.0,
77 .game_piece = GamePiece::CONE_UP,
78 .button = kGroundPickupConeUp,
79 },
80 {
81 .index = arm::GroundPickupBackConeDownIndex(),
82 .wrist_goal = 0.0,
83 .game_piece = GamePiece::CONE_DOWN,
84 .button = kGroundPickupConeDown,
85 },
86 {
87 .index = arm::ScoreBackMidConeUpPosIndex(),
88 .wrist_goal = 0.55,
89 .game_piece = GamePiece::CONE_UP,
90 .button = kMidBackTipConeScoreRight,
91 },
92 {
93 .index = arm::ScoreBackMidConeDownPosIndex(),
94 .wrist_goal = 2.2,
95 .score_wrist_goal = 0.0,
96 .game_piece = GamePiece::CONE_DOWN,
97 .button = kMidBackTipConeScoreRight,
98 },
99 {
100 .index = arm::HPPickupBackConeUpIndex(),
101 .wrist_goal = 0.2,
102 .game_piece = GamePiece::CONE_UP,
103 .button = kHPConePickup,
104 },
105 {
106 .index = arm::ScoreFrontHighConeUpPosIndex(),
107 .wrist_goal = 0.05,
108 .game_piece = GamePiece::CONE_UP,
109 .button = kHighBackTipConeScoreLeft,
110 },
111 {
112 .index = arm::ScoreFrontMidConeUpPosIndex(),
113 .wrist_goal = 0.05,
114 .game_piece = GamePiece::CONE_UP,
115 .button = kMidBackTipConeScoreLeft,
116 },
117};
118
Maxwell Hendersonad312342023-01-10 12:07:47 -0800119class Reader : public ::frc971::input::ActionJoystickInput {
120 public:
121 Reader(::aos::EventLoop *event_loop)
122 : ::frc971::input::ActionJoystickInput(
123 event_loop,
124 ::y2023::control_loops::drivetrain::GetDrivetrainConfig(),
125 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
126 superstructure_goal_sender_(
127 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
128 superstructure_status_fetcher_(
129 event_loop->MakeFetcher<superstructure::Status>(
130 "/superstructure")) {}
131
132 void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); }
133
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800134 GamePiece current_game_piece_ = GamePiece::CONE_UP;
135
Maxwell Hendersonad312342023-01-10 12:07:47 -0800136 void HandleTeleop(
137 const ::frc971::input::driver_station::Data &data) override {
138 superstructure_status_fetcher_.Fetch();
139 if (!superstructure_status_fetcher_.get()) {
140 AOS_LOG(ERROR, "Got no superstructure status message.\n");
141 return;
142 }
143
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800144 if (!superstructure_status_fetcher_->has_wrist()) {
145 AOS_LOG(ERROR, "Got no superstructure status message.\n");
146 return;
147 }
milind-udefab712023-02-20 22:22:02 -0800148
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800149 double wrist_goal = 0.0;
150 RollerGoal roller_goal = RollerGoal::IDLE;
151 arm_goal_position_ = arm::NeutralPosIndex();
152 std::optional<double> score_wrist_goal = std::nullopt;
153
154 if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) {
155 roller_goal = RollerGoal::INTAKE;
156 current_game_piece_ = GamePiece::CONE_UP;
157 } else if (data.IsPressed(kGroundPickupConeDown)) {
158 roller_goal = RollerGoal::INTAKE;
159 current_game_piece_ = GamePiece::CONE_DOWN;
160 }
161
162 // Search for the active setpoint.
163 for (const ArmSetpoint &setpoint : setpoints) {
164 if (data.IsPressed(setpoint.button)) {
165 if (setpoint.game_piece == current_game_piece_) {
166 wrist_goal = setpoint.wrist_goal;
167 arm_goal_position_ = setpoint.index;
168 score_wrist_goal = setpoint.score_wrist_goal;
169 break;
170 }
171 }
Austin Schuh6dc925b2023-02-24 16:23:32 -0800172 }
173
Austin Schuh23a90022023-02-24 22:13:39 -0800174 if (data.IsPressed(kSuck)) {
175 roller_goal = RollerGoal::INTAKE;
176 } else if (data.IsPressed(kSpit)) {
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800177 if (score_wrist_goal.has_value()) {
178 wrist_goal = score_wrist_goal.value();
Austin Schuh23a90022023-02-24 22:13:39 -0800179
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800180 // If we are supposed to dunk it, wait until we are close enough to
181 // spit.
182 if (std::abs(score_wrist_goal.value() -
183 superstructure_status_fetcher_->wrist()->position()) <
184 0.1) {
185 roller_goal = RollerGoal::SPIT;
186 }
187 } else {
188 roller_goal = RollerGoal::SPIT;
189 }
milind-udefab712023-02-20 22:22:02 -0800190 }
191
Maxwell Hendersonad312342023-01-10 12:07:47 -0800192 {
193 auto builder = superstructure_goal_sender_.MakeBuilder();
194
Austin Schuh6dc925b2023-02-24 16:23:32 -0800195 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Maxwell Henderson1ac7aac2023-02-23 17:35:32 -0800196 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
197 *builder.fbb(), wrist_goal,
198 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
Austin Schuh6dc925b2023-02-24 16:23:32 -0800199
Maxwell Hendersonad312342023-01-10 12:07:47 -0800200 superstructure::Goal::Builder superstructure_goal_builder =
201 builder.MakeBuilder<superstructure::Goal>();
milind-udefab712023-02-20 22:22:02 -0800202 superstructure_goal_builder.add_arm_goal_position(arm_goal_position_);
Maxwell Henderson5938a832023-02-23 09:33:15 -0800203 superstructure_goal_builder.add_roller_goal(roller_goal);
Austin Schuh6dc925b2023-02-24 16:23:32 -0800204 superstructure_goal_builder.add_wrist(wrist_offset);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800205 if (builder.Send(superstructure_goal_builder.Finish()) !=
206 aos::RawSender::Error::kOk) {
207 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
208 }
209 }
210 }
211
212 private:
213 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
214
215 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
milind-udefab712023-02-20 22:22:02 -0800216
217 uint32_t arm_goal_position_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800218};
219
220} // namespace joysticks
221} // namespace input
222} // namespace y2023
223
224int main(int argc, char **argv) {
225 ::aos::InitGoogle(&argc, &argv);
226
227 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
228 aos::configuration::ReadConfig("aos_config.json");
229
230 ::aos::ShmEventLoop event_loop(&config.message());
231 ::y2023::input::joysticks::Reader reader(&event_loop);
232
233 event_loop.Run();
234
235 return 0;
236}