blob: fc789946caa7103710410c277abf9602b9f00f2f [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
Brian Silvermanb601d892015-12-20 18:24:38 -050028namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070029namespace input {
30namespace joysticks {
31
32const ButtonLocation kDriveControlLoopEnable1(1, 7),
James Kuszmaul7077d342021-06-09 20:23:58 -070033 kDriveControlLoopEnable2(1, 11);
Brian Silverman17f503e2015-08-02 18:17:18 -070034const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080035const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1);
Brian Silverman17f503e2015-08-02 18:17:18 -070036const ButtonLocation kQuickTurn(1, 5);
37
38const ButtonLocation kCatch(3, 10);
39
40#if OLD_DS
41const ButtonLocation kFire(3, 11);
42const ButtonLocation kUnload(1, 4);
43const ButtonLocation kReload(1, 2);
44
45const ButtonLocation kRollersOut(3, 12);
46const ButtonLocation kRollersIn(3, 7);
47
48const ButtonLocation kTuck(3, 9);
49const ButtonLocation kIntakePosition(3, 8);
50const ButtonLocation kIntakeOpenPosition(3, 10);
51const ButtonLocation kVerticalTuck(3, 1);
52const JoystickAxis kFlipRobot(3, 3);
53
54const ButtonLocation kLongShot(3, 5);
55const ButtonLocation kCloseShot(3, 2);
56const ButtonLocation kFenderShot(3, 3);
57const ButtonLocation kTrussShot(2, 11);
58const ButtonLocation kHumanPlayerShot(3, 2);
59#else
60const ButtonLocation kFire(3, 9);
61const ButtonLocation kUnload(1, 4);
62const ButtonLocation kReload(1, 2);
63
64const ButtonLocation kRollersOut(3, 8);
65const ButtonLocation kRollersIn(3, 3);
66
67const ButtonLocation kTuck(3, 4);
68const ButtonLocation kIntakePosition(3, 5);
69const ButtonLocation kIntakeOpenPosition(3, 11);
70const ButtonLocation kVerticalTuck(2, 6);
71const JoystickAxis kFlipRobot(3, 3);
72
73const ButtonLocation kLongShot(3, 7);
74const ButtonLocation kCloseShot(3, 6);
75const ButtonLocation kFenderShot(3, 2);
76const ButtonLocation kTrussShot(2, 11);
77const ButtonLocation kHumanPlayerShot(3, 1);
78#endif
79
80const ButtonLocation kUserLeft(2, 7);
81const ButtonLocation kUserRight(2, 10);
82
83const JoystickAxis kAdjustClawGoal(3, 2);
84const JoystickAxis kAdjustClawSeparation(3, 1);
85
86struct ClawGoal {
87 double angle;
88 double separation;
89};
90
91struct ShotGoal {
92 ClawGoal claw;
93 double shot_power;
94 double velocity_compensation;
95 double intake_power;
96};
97
98const double kIntakePower = 4.0;
99// In case we have to quickly adjust it.
100const double kGrabSeparation = 0;
101const double kShootSeparation = 0.11 + kGrabSeparation;
102
103const ClawGoal kTuckGoal = {-2.273474, -0.749484};
104const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation};
105const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation};
106const ClawGoal kIntakeOpenGoal = {-2.0, 1.1};
107
108// TODO(austin): Tune these by hand...
109const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
110const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
111const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
112
113// 34" between near edge of colored line and rear edge of bumper.
114// Only works running?
115const ShotGoal kLongShotGoal = {
116 {-1.08, kShootSeparation}, 145, 0.04, kIntakePower};
117// old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
118const ShotGoal kFlippedLongShotGoal = {
119 {0.96, kShootSeparation}, 145, 0.09, kIntakePower};
120// old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
121
122// 78" between near edge of colored line and rear edge of bumper.
123const ShotGoal kCloseShotGoal = {
124 {-0.95, kShootSeparation}, 105, 0.2, kIntakePower};
125// 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
126const ShotGoal kFlippedMediumShotGoal = {
127 {0.865, kShootSeparation}, 120, 0.2, kIntakePower};
128// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
129
130// Shot from the fender.
131const ShotGoal kFenderShotGoal = {
132 {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower};
133const ShotGoal kFlippedShortShotGoal = {
134 {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower};
135
136const ShotGoal kHumanShotGoal = {
137 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
138const ShotGoal kFlippedHumanShotGoal = {
139 {0.90, kShootSeparation}, 140, 0, kIntakePower};
140const ShotGoal kTrussShotGoal = {
141 {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower};
142const ShotGoal kFlippedTrussShotGoal = {
143 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
144
145const ShotGoal kFlippedDemoShotGoal = {
146 {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower};
147const ShotGoal kDemoShotGoal = {
148 {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower};
149
150const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
151const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
152
James Kuszmaul7077d342021-06-09 20:23:58 -0700153class Reader : public ::frc971::input::ActionJoystickInput {
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 public:
Austin Schuh3e45c752019-02-02 12:19:11 -0800155 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -0700156 : ::frc971::input::ActionJoystickInput(
Austin Schuhbfb04122019-05-22 21:16:51 -0700157 event_loop, control_loops::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -0700158 ::frc971::input::DrivetrainInputReader::InputType::kSteeringWheel,
159 {}),
Austin Schuhb2461f42019-06-29 18:17:06 -0700160 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
162 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -0700163 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 event_loop->MakeSender<::y2014::control_loops::claw::Goal>(
165 "/claw")),
Austin Schuh493f7af2019-06-29 18:42:12 -0700166 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
168 "/shooter")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700169 drivetrain_status_fetcher_(
170 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
172 "/drivetrain")),
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 shot_power_(80.0),
174 goal_angle_(0.0),
175 separation_angle_(kGrabSeparation),
176 velocity_compensation_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -0700177 intake_power_(0.0),
178 shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700179
180 void SetGoal(ClawGoal goal) {
181 goal_angle_ = goal.angle;
182 separation_angle_ = goal.separation;
183 moving_for_shot_ = false;
184 velocity_compensation_ = 0.0;
185 intake_power_ = 0.0;
186 }
187
188 void SetGoal(ShotGoal goal) {
189 goal_angle_ = goal.claw.angle;
190 shot_separation_angle_ = goal.claw.separation;
191 separation_angle_ = kGrabSeparation;
192 moving_for_shot_ = true;
193 shot_power_ = goal.shot_power;
194 velocity_compensation_ = goal.velocity_compensation;
195 intake_power_ = goal.intake_power;
196 }
197
James Kuszmaul7077d342021-06-09 20:23:58 -0700198 void HandleTeleop(
199 const ::frc971::input::driver_station::Data &data) override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700200 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
201 intake_power_ = 0.0;
202 separation_angle_ = kGrabSeparation;
203 moving_for_shot_ = false;
204 }
205
206 static const double kAdjustClawGoalDeadband = 0.08;
207 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
208 if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
209 claw_goal_adjust = 0;
210 } else {
James Kuszmaul7077d342021-06-09 20:23:58 -0700211 claw_goal_adjust = (claw_goal_adjust - ((claw_goal_adjust < 0)
212 ? -kAdjustClawGoalDeadband
Brian Silverman17f503e2015-08-02 18:17:18 -0700213 : kAdjustClawGoalDeadband)) *
214 0.035;
215 }
216 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
217 if (OLD_DS ||
218 ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
219 claw_separation_adjust = 0;
220 } else {
221 claw_separation_adjust =
James Kuszmaul7077d342021-06-09 20:23:58 -0700222 (claw_separation_adjust - ((claw_separation_adjust < 0)
223 ? -kAdjustClawGoalDeadband
Brian Silverman17f503e2015-08-02 18:17:18 -0700224 : kAdjustClawGoalDeadband)) *
225 -0.035;
226 }
227
228#if OLD_DS
229 if (data.IsPressed(kFenderShot)) {
230#else
231 if (data.GetAxis(kFlipRobot) > 0.9) {
232#endif
233 claw_goal_adjust += claw_separation_adjust;
234 claw_goal_adjust *= -1;
235
236 if (data.IsPressed(kIntakeOpenPosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700237 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700238 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700239 SetGoal(kFlippedIntakeOpenGoal);
240 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700241 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700242 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700243 SetGoal(kFlippedIntakeGoal);
244 } else if (data.IsPressed(kVerticalTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700245 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700246 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700247 SetGoal(kVerticalTuckGoal);
248 } else if (data.IsPressed(kTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700249 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700250 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700251 SetGoal(kFlippedTuckGoal);
252 } else if (data.PosEdge(kLongShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700253 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700254 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700255 SetGoal(kFlippedLongShotGoal);
256 } else if (data.PosEdge(kCloseShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700257 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700258 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700259 SetGoal(kFlippedMediumShotGoal);
260 } else if (data.PosEdge(kFenderShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700261 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700262 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700263 SetGoal(kFlippedShortShotGoal);
264 } else if (data.PosEdge(kHumanPlayerShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700265 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700266 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700267 SetGoal(kFlippedHumanShotGoal);
268 } else if (data.PosEdge(kUserLeft)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700269 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700270 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700271 SetGoal(kFlipped254PassGoal);
272 } else if (data.PosEdge(kUserRight)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700273 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700274 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700275 SetGoal(kFlippedDemoShotGoal);
276 } else if (data.PosEdge(kTrussShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700277 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700278 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700279 SetGoal(kFlippedTrussShotGoal);
280 }
281 } else {
282 if (data.IsPressed(kIntakeOpenPosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700283 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700284 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700285 SetGoal(kIntakeOpenGoal);
286 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700287 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700288 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700289 SetGoal(kIntakeGoal);
290 } else if (data.IsPressed(kVerticalTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700291 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700292 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700293 SetGoal(kVerticalTuckGoal);
294 } else if (data.IsPressed(kTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700295 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700296 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700297 SetGoal(kTuckGoal);
298 } else if (data.PosEdge(kLongShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700299 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700300 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700301 SetGoal(kLongShotGoal);
302 } else if (data.PosEdge(kCloseShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700303 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700304 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700305 SetGoal(kCloseShotGoal);
306 } else if (data.PosEdge(kFenderShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700307 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700308 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700309 SetGoal(kFenderShotGoal);
310 } else if (data.PosEdge(kHumanPlayerShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700311 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700312 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700313 SetGoal(kHumanShotGoal);
314 } else if (data.PosEdge(kUserLeft)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700315 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700316 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700317 SetGoal(k254PassGoal);
318 } else if (data.PosEdge(kUserRight)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700319 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700320 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700321 SetGoal(kDemoShotGoal);
322 } else if (data.PosEdge(kTrussShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700323 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700324 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700325 SetGoal(kTrussShotGoal);
326 }
327 }
328
329 if (data.PosEdge(kFire)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700330 aos::common::actions::DoubleParamT param;
331 EnqueueAction(shoot_action_factory_.Make(param));
Brian Silverman17f503e2015-08-02 18:17:18 -0700332 } else if (data.NegEdge(kFire)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700333 CancelCurrentAction();
Brian Silverman17f503e2015-08-02 18:17:18 -0700334 }
335
Brian Silverman17f503e2015-08-02 18:17:18 -0700336 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700337 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700338 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700339 intake_power_ = 0.0;
340 velocity_compensation_ = 0.0;
341 }
342
343 // Send out the claw and shooter goals if no actions are running.
Austin Schuhbfb04122019-05-22 21:16:51 -0700344 if (!ActionRunning()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700345 goal_angle_ += claw_goal_adjust;
346 separation_angle_ += claw_separation_adjust;
347
348 // If the action just ended, turn the intake off and stop velocity
349 // compensating.
Austin Schuhbfb04122019-05-22 21:16:51 -0700350 if (was_running_action()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700351 intake_power_ = 0.0;
352 velocity_compensation_ = 0.0;
353 }
354
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700355 drivetrain_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700356 double goal_angle = goal_angle_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700357 if (drivetrain_status_fetcher_.get()) {
358 goal_angle +=
Alex Perrycb7da4b2019-08-28 19:35:56 -0700359 SpeedToAngleOffset(drivetrain_status_fetcher_->robot_speed());
Brian Silverman17f503e2015-08-02 18:17:18 -0700360 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700361 AOS_LOG_INTERVAL(no_drivetrain_status_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700362 }
363
364 if (moving_for_shot_) {
Austin Schuhb2461f42019-06-29 18:17:06 -0700365 claw_status_fetcher_.Fetch();
366 if (claw_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700367 if (::std::abs(claw_status_fetcher_->bottom() - goal_angle) < 0.2) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700368 moving_for_shot_ = false;
369 separation_angle_ = shot_separation_angle_;
370 }
371 }
372 }
373
374 double separation_angle = separation_angle_;
375
376 if (data.IsPressed(kCatch)) {
377 const double kCatchSeparation = 1.0;
378 goal_angle -= kCatchSeparation / 2.0;
379 separation_angle = kCatchSeparation;
380 }
381
382 bool intaking =
383 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
384 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb2461f42019-06-29 18:17:06 -0700385 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700386 auto builder = claw_goal_sender_.MakeBuilder();
387 control_loops::claw::Goal::Builder goal_builder =
388 builder.MakeBuilder<control_loops::claw::Goal>();
389 goal_builder.add_bottom_angle(goal_angle);
390 goal_builder.add_separation_angle(separation_angle);
391 goal_builder.add_intake(
Austin Schuhb2461f42019-06-29 18:17:06 -0700392 intaking ? 12.0
Alex Perrycb7da4b2019-08-28 19:35:56 -0700393 : (data.IsPressed(kRollersOut) ? -12.0 : intake_power_));
394 goal_builder.add_centering(intaking ? 12.0 : 0.0);
Austin Schuhb2461f42019-06-29 18:17:06 -0700395
milind1f1dca32021-07-03 13:50:07 -0700396 if (builder.Send(goal_builder.Finish()) !=
397 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700398 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -0700399 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700400 }
401
Austin Schuh493f7af2019-06-29 18:42:12 -0700402 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700403 auto builder = shooter_goal_sender_.MakeBuilder();
404 control_loops::shooter::Goal::Builder goal_builder =
405 builder.MakeBuilder<control_loops::shooter::Goal>();
406 goal_builder.add_shot_power(shot_power_);
407 goal_builder.add_shot_requested(data.IsPressed(kFire));
408 goal_builder.add_unload_requested(data.IsPressed(kUnload));
409 goal_builder.add_load_requested(data.IsPressed(kReload));
milind1f1dca32021-07-03 13:50:07 -0700410 if (builder.Send(goal_builder.Finish()) !=
411 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700412 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700413 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700414 }
415 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700416 }
417
418 double SpeedToAngleOffset(double speed) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500419 const ::y2014::constants::Values &values = ::y2014::constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700420 // scale speed to a [0.0-1.0] on something close to the max
421 // TODO(austin): Change the scale factor for different shots.
422 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
423 }
424
425 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426 ::aos::Fetcher<::y2014::control_loops::claw::Status> claw_status_fetcher_;
427 ::aos::Sender<::y2014::control_loops::claw::Goal> claw_goal_sender_;
428 ::aos::Sender<::y2014::control_loops::shooter::Goal> shooter_goal_sender_;
429 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700430 drivetrain_status_fetcher_;
Austin Schuhb2461f42019-06-29 18:17:06 -0700431
Brian Silverman17f503e2015-08-02 18:17:18 -0700432 double shot_power_;
433 double goal_angle_;
434 double separation_angle_, shot_separation_angle_;
435 double velocity_compensation_;
436 double intake_power_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700437 bool moving_for_shot_ = false;
438
Austin Schuh1bf8a212019-05-26 22:13:14 -0700439 actors::ShootActor::Factory shoot_action_factory_;
440
Brian Silverman17f503e2015-08-02 18:17:18 -0700441 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800442 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Brian Silverman17f503e2015-08-02 18:17:18 -0700443 "no drivetrain status");
444};
445
446} // namespace joysticks
447} // namespace input
Brian Silvermanb601d892015-12-20 18:24:38 -0500448} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700449
Austin Schuh094d09b2020-11-20 23:26:52 -0800450int main(int argc, char **argv) {
451 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700452
Alex Perrycb7da4b2019-08-28 19:35:56 -0700453 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
454 aos::configuration::ReadConfig("config.json");
455
456 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800457 ::y2014::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700458
459 event_loop.Run();
460
Austin Schuhae87e312020-08-01 16:15:01 -0700461 return 0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700462}