blob: 232a0ce505825fabafc5f7ee1545faec7bcd39aa [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include <unistd.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07002
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
4#include <cstdio>
5#include <cstring>
6
Austin Schuhbfb04122019-05-22 21:16:51 -07007#include "aos/actions/actions.h"
John Park398c74a2018-10-20 21:17:39 -07008#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -070010#include "aos/time/time.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070011#include "aos/util/log_interval.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070013#include "frc971/input/action_joystick_input.h"
14#include "frc971/input/driver_station_data.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070015#include "y2014/actors/shoot_actor.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070016#include "y2014/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "y2014/control_loops/claw/claw_goal_generated.h"
18#include "y2014/control_loops/claw/claw_status_generated.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070019#include "y2014/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "y2014/control_loops/shooter/shooter_goal_generated.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070021
James Kuszmaul7077d342021-06-09 20:23:58 -070022using ::frc971::input::driver_station::ButtonLocation;
23using ::frc971::input::driver_station::ControlBit;
24using ::frc971::input::driver_station::JoystickAxis;
Brian Silverman17f503e2015-08-02 18:17:18 -070025
26#define OLD_DS 0
27
Stephan Pleinesf63bde82024-01-13 15:59:33 -080028namespace y2014::input::joysticks {
Brian Silverman17f503e2015-08-02 18:17:18 -070029
30const ButtonLocation kDriveControlLoopEnable1(1, 7),
James Kuszmaul7077d342021-06-09 20:23:58 -070031 kDriveControlLoopEnable2(1, 11);
Brian Silverman17f503e2015-08-02 18:17:18 -070032const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080033const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1);
Brian Silverman17f503e2015-08-02 18:17:18 -070034const ButtonLocation kQuickTurn(1, 5);
35
36const ButtonLocation kCatch(3, 10);
37
38#if OLD_DS
39const ButtonLocation kFire(3, 11);
40const ButtonLocation kUnload(1, 4);
41const ButtonLocation kReload(1, 2);
42
43const ButtonLocation kRollersOut(3, 12);
44const ButtonLocation kRollersIn(3, 7);
45
46const ButtonLocation kTuck(3, 9);
47const ButtonLocation kIntakePosition(3, 8);
48const ButtonLocation kIntakeOpenPosition(3, 10);
49const ButtonLocation kVerticalTuck(3, 1);
50const JoystickAxis kFlipRobot(3, 3);
51
52const ButtonLocation kLongShot(3, 5);
53const ButtonLocation kCloseShot(3, 2);
54const ButtonLocation kFenderShot(3, 3);
55const ButtonLocation kTrussShot(2, 11);
56const ButtonLocation kHumanPlayerShot(3, 2);
57#else
58const ButtonLocation kFire(3, 9);
59const ButtonLocation kUnload(1, 4);
60const ButtonLocation kReload(1, 2);
61
62const ButtonLocation kRollersOut(3, 8);
63const ButtonLocation kRollersIn(3, 3);
64
65const ButtonLocation kTuck(3, 4);
66const ButtonLocation kIntakePosition(3, 5);
67const ButtonLocation kIntakeOpenPosition(3, 11);
68const ButtonLocation kVerticalTuck(2, 6);
69const JoystickAxis kFlipRobot(3, 3);
70
71const ButtonLocation kLongShot(3, 7);
72const ButtonLocation kCloseShot(3, 6);
73const ButtonLocation kFenderShot(3, 2);
74const ButtonLocation kTrussShot(2, 11);
75const ButtonLocation kHumanPlayerShot(3, 1);
76#endif
77
78const ButtonLocation kUserLeft(2, 7);
79const ButtonLocation kUserRight(2, 10);
80
81const JoystickAxis kAdjustClawGoal(3, 2);
82const JoystickAxis kAdjustClawSeparation(3, 1);
83
84struct ClawGoal {
85 double angle;
86 double separation;
87};
88
89struct ShotGoal {
90 ClawGoal claw;
91 double shot_power;
92 double velocity_compensation;
93 double intake_power;
94};
95
96const double kIntakePower = 4.0;
97// In case we have to quickly adjust it.
98const double kGrabSeparation = 0;
99const double kShootSeparation = 0.11 + kGrabSeparation;
100
101const ClawGoal kTuckGoal = {-2.273474, -0.749484};
102const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation};
103const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation};
104const ClawGoal kIntakeOpenGoal = {-2.0, 1.1};
105
106// TODO(austin): Tune these by hand...
107const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
108const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
109const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
110
111// 34" between near edge of colored line and rear edge of bumper.
112// Only works running?
113const ShotGoal kLongShotGoal = {
114 {-1.08, kShootSeparation}, 145, 0.04, kIntakePower};
115// old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
116const ShotGoal kFlippedLongShotGoal = {
117 {0.96, kShootSeparation}, 145, 0.09, kIntakePower};
118// old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
119
120// 78" between near edge of colored line and rear edge of bumper.
121const ShotGoal kCloseShotGoal = {
122 {-0.95, kShootSeparation}, 105, 0.2, kIntakePower};
123// 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
124const ShotGoal kFlippedMediumShotGoal = {
125 {0.865, kShootSeparation}, 120, 0.2, kIntakePower};
126// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
127
128// Shot from the fender.
129const ShotGoal kFenderShotGoal = {
130 {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower};
131const ShotGoal kFlippedShortShotGoal = {
132 {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower};
133
134const ShotGoal kHumanShotGoal = {
135 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
136const ShotGoal kFlippedHumanShotGoal = {
137 {0.90, kShootSeparation}, 140, 0, kIntakePower};
138const ShotGoal kTrussShotGoal = {
139 {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower};
140const ShotGoal kFlippedTrussShotGoal = {
141 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
142
143const ShotGoal kFlippedDemoShotGoal = {
144 {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower};
145const ShotGoal kDemoShotGoal = {
146 {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower};
147
148const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
149const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
150
James Kuszmaul7077d342021-06-09 20:23:58 -0700151class Reader : public ::frc971::input::ActionJoystickInput {
Brian Silverman17f503e2015-08-02 18:17:18 -0700152 public:
Austin Schuh3e45c752019-02-02 12:19:11 -0800153 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -0700154 : ::frc971::input::ActionJoystickInput(
Austin Schuhbfb04122019-05-22 21:16:51 -0700155 event_loop, control_loops::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -0700156 ::frc971::input::DrivetrainInputReader::InputType::kSteeringWheel,
157 {}),
Austin Schuhb2461f42019-06-29 18:17:06 -0700158 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
160 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -0700161 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 event_loop->MakeSender<::y2014::control_loops::claw::Goal>(
163 "/claw")),
Austin Schuh493f7af2019-06-29 18:42:12 -0700164 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
166 "/shooter")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700167 drivetrain_status_fetcher_(
168 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
170 "/drivetrain")),
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 shot_power_(80.0),
172 goal_angle_(0.0),
173 separation_angle_(kGrabSeparation),
174 velocity_compensation_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -0700175 intake_power_(0.0),
176 shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700177
178 void SetGoal(ClawGoal goal) {
179 goal_angle_ = goal.angle;
180 separation_angle_ = goal.separation;
181 moving_for_shot_ = false;
182 velocity_compensation_ = 0.0;
183 intake_power_ = 0.0;
184 }
185
186 void SetGoal(ShotGoal goal) {
187 goal_angle_ = goal.claw.angle;
188 shot_separation_angle_ = goal.claw.separation;
189 separation_angle_ = kGrabSeparation;
190 moving_for_shot_ = true;
191 shot_power_ = goal.shot_power;
192 velocity_compensation_ = goal.velocity_compensation;
193 intake_power_ = goal.intake_power;
194 }
195
James Kuszmaul7077d342021-06-09 20:23:58 -0700196 void HandleTeleop(
197 const ::frc971::input::driver_station::Data &data) override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700198 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
199 intake_power_ = 0.0;
200 separation_angle_ = kGrabSeparation;
201 moving_for_shot_ = false;
202 }
203
204 static const double kAdjustClawGoalDeadband = 0.08;
205 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
206 if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
207 claw_goal_adjust = 0;
208 } else {
James Kuszmaul7077d342021-06-09 20:23:58 -0700209 claw_goal_adjust = (claw_goal_adjust - ((claw_goal_adjust < 0)
210 ? -kAdjustClawGoalDeadband
Brian Silverman17f503e2015-08-02 18:17:18 -0700211 : kAdjustClawGoalDeadband)) *
212 0.035;
213 }
214 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
215 if (OLD_DS ||
216 ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
217 claw_separation_adjust = 0;
218 } else {
219 claw_separation_adjust =
James Kuszmaul7077d342021-06-09 20:23:58 -0700220 (claw_separation_adjust - ((claw_separation_adjust < 0)
221 ? -kAdjustClawGoalDeadband
Brian Silverman17f503e2015-08-02 18:17:18 -0700222 : kAdjustClawGoalDeadband)) *
223 -0.035;
224 }
225
226#if OLD_DS
227 if (data.IsPressed(kFenderShot)) {
228#else
229 if (data.GetAxis(kFlipRobot) > 0.9) {
230#endif
231 claw_goal_adjust += claw_separation_adjust;
232 claw_goal_adjust *= -1;
233
234 if (data.IsPressed(kIntakeOpenPosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700235 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700236 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700237 SetGoal(kFlippedIntakeOpenGoal);
238 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700239 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700240 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700241 SetGoal(kFlippedIntakeGoal);
242 } else if (data.IsPressed(kVerticalTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700243 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700244 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700245 SetGoal(kVerticalTuckGoal);
246 } else if (data.IsPressed(kTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700247 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700248 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700249 SetGoal(kFlippedTuckGoal);
250 } else if (data.PosEdge(kLongShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700251 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700252 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700253 SetGoal(kFlippedLongShotGoal);
254 } else if (data.PosEdge(kCloseShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700255 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700256 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700257 SetGoal(kFlippedMediumShotGoal);
258 } else if (data.PosEdge(kFenderShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700259 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700260 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700261 SetGoal(kFlippedShortShotGoal);
262 } else if (data.PosEdge(kHumanPlayerShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700263 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700264 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700265 SetGoal(kFlippedHumanShotGoal);
266 } else if (data.PosEdge(kUserLeft)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700267 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700268 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700269 SetGoal(kFlipped254PassGoal);
270 } else if (data.PosEdge(kUserRight)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700271 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700272 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700273 SetGoal(kFlippedDemoShotGoal);
274 } else if (data.PosEdge(kTrussShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700275 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700276 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700277 SetGoal(kFlippedTrussShotGoal);
278 }
279 } else {
280 if (data.IsPressed(kIntakeOpenPosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700281 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700282 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700283 SetGoal(kIntakeOpenGoal);
284 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700285 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700286 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700287 SetGoal(kIntakeGoal);
288 } else if (data.IsPressed(kVerticalTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700289 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700290 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700291 SetGoal(kVerticalTuckGoal);
292 } else if (data.IsPressed(kTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700293 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700294 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700295 SetGoal(kTuckGoal);
296 } else if (data.PosEdge(kLongShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700297 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700298 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700299 SetGoal(kLongShotGoal);
300 } else if (data.PosEdge(kCloseShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700301 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700302 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700303 SetGoal(kCloseShotGoal);
304 } else if (data.PosEdge(kFenderShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700305 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700306 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700307 SetGoal(kFenderShotGoal);
308 } else if (data.PosEdge(kHumanPlayerShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700309 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700310 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700311 SetGoal(kHumanShotGoal);
312 } else if (data.PosEdge(kUserLeft)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700313 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700314 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700315 SetGoal(k254PassGoal);
316 } else if (data.PosEdge(kUserRight)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700317 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700318 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700319 SetGoal(kDemoShotGoal);
320 } else if (data.PosEdge(kTrussShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700321 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700322 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700323 SetGoal(kTrussShotGoal);
324 }
325 }
326
327 if (data.PosEdge(kFire)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 aos::common::actions::DoubleParamT param;
329 EnqueueAction(shoot_action_factory_.Make(param));
Brian Silverman17f503e2015-08-02 18:17:18 -0700330 } else if (data.NegEdge(kFire)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700331 CancelCurrentAction();
Brian Silverman17f503e2015-08-02 18:17:18 -0700332 }
333
Brian Silverman17f503e2015-08-02 18:17:18 -0700334 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700335 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700336 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700337 intake_power_ = 0.0;
338 velocity_compensation_ = 0.0;
339 }
340
341 // Send out the claw and shooter goals if no actions are running.
Austin Schuhbfb04122019-05-22 21:16:51 -0700342 if (!ActionRunning()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700343 goal_angle_ += claw_goal_adjust;
344 separation_angle_ += claw_separation_adjust;
345
346 // If the action just ended, turn the intake off and stop velocity
347 // compensating.
Austin Schuhbfb04122019-05-22 21:16:51 -0700348 if (was_running_action()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700349 intake_power_ = 0.0;
350 velocity_compensation_ = 0.0;
351 }
352
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700353 drivetrain_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700354 double goal_angle = goal_angle_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700355 if (drivetrain_status_fetcher_.get()) {
356 goal_angle +=
Alex Perrycb7da4b2019-08-28 19:35:56 -0700357 SpeedToAngleOffset(drivetrain_status_fetcher_->robot_speed());
Brian Silverman17f503e2015-08-02 18:17:18 -0700358 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700359 AOS_LOG_INTERVAL(no_drivetrain_status_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700360 }
361
362 if (moving_for_shot_) {
Austin Schuhb2461f42019-06-29 18:17:06 -0700363 claw_status_fetcher_.Fetch();
364 if (claw_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700365 if (::std::abs(claw_status_fetcher_->bottom() - goal_angle) < 0.2) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700366 moving_for_shot_ = false;
367 separation_angle_ = shot_separation_angle_;
368 }
369 }
370 }
371
372 double separation_angle = separation_angle_;
373
374 if (data.IsPressed(kCatch)) {
375 const double kCatchSeparation = 1.0;
376 goal_angle -= kCatchSeparation / 2.0;
377 separation_angle = kCatchSeparation;
378 }
379
380 bool intaking =
381 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
382 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb2461f42019-06-29 18:17:06 -0700383 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700384 auto builder = claw_goal_sender_.MakeBuilder();
385 control_loops::claw::Goal::Builder goal_builder =
386 builder.MakeBuilder<control_loops::claw::Goal>();
387 goal_builder.add_bottom_angle(goal_angle);
388 goal_builder.add_separation_angle(separation_angle);
389 goal_builder.add_intake(
Austin Schuhb2461f42019-06-29 18:17:06 -0700390 intaking ? 12.0
Alex Perrycb7da4b2019-08-28 19:35:56 -0700391 : (data.IsPressed(kRollersOut) ? -12.0 : intake_power_));
392 goal_builder.add_centering(intaking ? 12.0 : 0.0);
Austin Schuhb2461f42019-06-29 18:17:06 -0700393
Philipp Schrader790cb542023-07-05 21:06:52 -0700394 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700395 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -0700396 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700397 }
398
Austin Schuh493f7af2019-06-29 18:42:12 -0700399 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700400 auto builder = shooter_goal_sender_.MakeBuilder();
401 control_loops::shooter::Goal::Builder goal_builder =
402 builder.MakeBuilder<control_loops::shooter::Goal>();
403 goal_builder.add_shot_power(shot_power_);
404 goal_builder.add_shot_requested(data.IsPressed(kFire));
405 goal_builder.add_unload_requested(data.IsPressed(kUnload));
406 goal_builder.add_load_requested(data.IsPressed(kReload));
Philipp Schrader790cb542023-07-05 21:06:52 -0700407 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700408 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700409 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700410 }
411 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700412 }
413
414 double SpeedToAngleOffset(double speed) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500415 const ::y2014::constants::Values &values = ::y2014::constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700416 // scale speed to a [0.0-1.0] on something close to the max
417 // TODO(austin): Change the scale factor for different shots.
418 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
419 }
420
421 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422 ::aos::Fetcher<::y2014::control_loops::claw::Status> claw_status_fetcher_;
423 ::aos::Sender<::y2014::control_loops::claw::Goal> claw_goal_sender_;
424 ::aos::Sender<::y2014::control_loops::shooter::Goal> shooter_goal_sender_;
425 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700426 drivetrain_status_fetcher_;
Austin Schuhb2461f42019-06-29 18:17:06 -0700427
Brian Silverman17f503e2015-08-02 18:17:18 -0700428 double shot_power_;
429 double goal_angle_;
430 double separation_angle_, shot_separation_angle_;
431 double velocity_compensation_;
432 double intake_power_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700433 bool moving_for_shot_ = false;
434
Austin Schuh1bf8a212019-05-26 22:13:14 -0700435 actors::ShootActor::Factory shoot_action_factory_;
436
Brian Silverman17f503e2015-08-02 18:17:18 -0700437 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800438 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Brian Silverman17f503e2015-08-02 18:17:18 -0700439 "no drivetrain status");
440};
441
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800442} // namespace y2014::input::joysticks
Brian Silverman17f503e2015-08-02 18:17:18 -0700443
Austin Schuh094d09b2020-11-20 23:26:52 -0800444int main(int argc, char **argv) {
445 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700446
Alex Perrycb7da4b2019-08-28 19:35:56 -0700447 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800448 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449
450 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800451 ::y2014::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700452
453 event_loop.Run();
454
Austin Schuhae87e312020-08-01 16:15:01 -0700455 return 0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700456}