blob: d2a7ed93e16175de09583a22ecad3cf808986112 [file] [log] [blame]
James Kuszmaul7077d342021-06-09 20:23:58 -07001#include <math.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07005
Austin Schuhbfb04122019-05-22 21:16:51 -07006#include "aos/actions/actions.h"
John Park398c74a2018-10-20 21:17:39 -07007#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07008#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/time/time.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070010#include "aos/util/log_interval.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070012#include "frc971/input/action_joystick_input.h"
13#include "frc971/input/driver_station_data.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070014#include "y2014/actors/shoot_actor.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070015#include "y2014/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "y2014/control_loops/claw/claw_goal_generated.h"
17#include "y2014/control_loops/claw/claw_status_generated.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070018#include "y2014/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "y2014/control_loops/shooter/shooter_goal_generated.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070020
James Kuszmaul7077d342021-06-09 20:23:58 -070021using ::frc971::input::driver_station::ButtonLocation;
22using ::frc971::input::driver_station::ControlBit;
23using ::frc971::input::driver_station::JoystickAxis;
Brian Silverman17f503e2015-08-02 18:17:18 -070024
25#define OLD_DS 0
26
Brian Silvermanb601d892015-12-20 18:24:38 -050027namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070028namespace input {
29namespace joysticks {
30
31const ButtonLocation kDriveControlLoopEnable1(1, 7),
James Kuszmaul7077d342021-06-09 20:23:58 -070032 kDriveControlLoopEnable2(1, 11);
Brian Silverman17f503e2015-08-02 18:17:18 -070033const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080034const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1);
Brian Silverman17f503e2015-08-02 18:17:18 -070035const ButtonLocation kQuickTurn(1, 5);
36
37const ButtonLocation kCatch(3, 10);
38
39#if OLD_DS
40const ButtonLocation kFire(3, 11);
41const ButtonLocation kUnload(1, 4);
42const ButtonLocation kReload(1, 2);
43
44const ButtonLocation kRollersOut(3, 12);
45const ButtonLocation kRollersIn(3, 7);
46
47const ButtonLocation kTuck(3, 9);
48const ButtonLocation kIntakePosition(3, 8);
49const ButtonLocation kIntakeOpenPosition(3, 10);
50const ButtonLocation kVerticalTuck(3, 1);
51const JoystickAxis kFlipRobot(3, 3);
52
53const ButtonLocation kLongShot(3, 5);
54const ButtonLocation kCloseShot(3, 2);
55const ButtonLocation kFenderShot(3, 3);
56const ButtonLocation kTrussShot(2, 11);
57const ButtonLocation kHumanPlayerShot(3, 2);
58#else
59const ButtonLocation kFire(3, 9);
60const ButtonLocation kUnload(1, 4);
61const ButtonLocation kReload(1, 2);
62
63const ButtonLocation kRollersOut(3, 8);
64const ButtonLocation kRollersIn(3, 3);
65
66const ButtonLocation kTuck(3, 4);
67const ButtonLocation kIntakePosition(3, 5);
68const ButtonLocation kIntakeOpenPosition(3, 11);
69const ButtonLocation kVerticalTuck(2, 6);
70const JoystickAxis kFlipRobot(3, 3);
71
72const ButtonLocation kLongShot(3, 7);
73const ButtonLocation kCloseShot(3, 6);
74const ButtonLocation kFenderShot(3, 2);
75const ButtonLocation kTrussShot(2, 11);
76const ButtonLocation kHumanPlayerShot(3, 1);
77#endif
78
79const ButtonLocation kUserLeft(2, 7);
80const ButtonLocation kUserRight(2, 10);
81
82const JoystickAxis kAdjustClawGoal(3, 2);
83const JoystickAxis kAdjustClawSeparation(3, 1);
84
85struct ClawGoal {
86 double angle;
87 double separation;
88};
89
90struct ShotGoal {
91 ClawGoal claw;
92 double shot_power;
93 double velocity_compensation;
94 double intake_power;
95};
96
97const double kIntakePower = 4.0;
98// In case we have to quickly adjust it.
99const double kGrabSeparation = 0;
100const double kShootSeparation = 0.11 + kGrabSeparation;
101
102const ClawGoal kTuckGoal = {-2.273474, -0.749484};
103const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation};
104const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation};
105const ClawGoal kIntakeOpenGoal = {-2.0, 1.1};
106
107// TODO(austin): Tune these by hand...
108const ClawGoal kFlippedTuckGoal = {2.733474, -0.75};
109const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation};
110const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0};
111
112// 34" between near edge of colored line and rear edge of bumper.
113// Only works running?
114const ShotGoal kLongShotGoal = {
115 {-1.08, kShootSeparation}, 145, 0.04, kIntakePower};
116// old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower};
117const ShotGoal kFlippedLongShotGoal = {
118 {0.96, kShootSeparation}, 145, 0.09, kIntakePower};
119// old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower};
120
121// 78" between near edge of colored line and rear edge of bumper.
122const ShotGoal kCloseShotGoal = {
123 {-0.95, kShootSeparation}, 105, 0.2, kIntakePower};
124// 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower};
125const ShotGoal kFlippedMediumShotGoal = {
126 {0.865, kShootSeparation}, 120, 0.2, kIntakePower};
127// 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower};
128
129// Shot from the fender.
130const ShotGoal kFenderShotGoal = {
131 {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower};
132const ShotGoal kFlippedShortShotGoal = {
133 {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower};
134
135const ShotGoal kHumanShotGoal = {
136 {-0.90, kShootSeparation}, 140, 0.04, kIntakePower};
137const ShotGoal kFlippedHumanShotGoal = {
138 {0.90, kShootSeparation}, 140, 0, kIntakePower};
139const ShotGoal kTrussShotGoal = {
140 {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower};
141const ShotGoal kFlippedTrussShotGoal = {
142 {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower};
143
144const ShotGoal kFlippedDemoShotGoal = {
145 {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower};
146const ShotGoal kDemoShotGoal = {
147 {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower};
148
149const ClawGoal k254PassGoal = {-1.95, kGrabSeparation};
150const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation};
151
James Kuszmaul7077d342021-06-09 20:23:58 -0700152class Reader : public ::frc971::input::ActionJoystickInput {
Brian Silverman17f503e2015-08-02 18:17:18 -0700153 public:
Austin Schuh3e45c752019-02-02 12:19:11 -0800154 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -0700155 : ::frc971::input::ActionJoystickInput(
Austin Schuhbfb04122019-05-22 21:16:51 -0700156 event_loop, control_loops::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -0700157 ::frc971::input::DrivetrainInputReader::InputType::kSteeringWheel,
158 {}),
Austin Schuhb2461f42019-06-29 18:17:06 -0700159 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
161 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -0700162 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 event_loop->MakeSender<::y2014::control_loops::claw::Goal>(
164 "/claw")),
Austin Schuh493f7af2019-06-29 18:42:12 -0700165 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
167 "/shooter")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700168 drivetrain_status_fetcher_(
169 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
171 "/drivetrain")),
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 shot_power_(80.0),
173 goal_angle_(0.0),
174 separation_angle_(kGrabSeparation),
175 velocity_compensation_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -0700176 intake_power_(0.0),
177 shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700178
179 void SetGoal(ClawGoal goal) {
180 goal_angle_ = goal.angle;
181 separation_angle_ = goal.separation;
182 moving_for_shot_ = false;
183 velocity_compensation_ = 0.0;
184 intake_power_ = 0.0;
185 }
186
187 void SetGoal(ShotGoal goal) {
188 goal_angle_ = goal.claw.angle;
189 shot_separation_angle_ = goal.claw.separation;
190 separation_angle_ = kGrabSeparation;
191 moving_for_shot_ = true;
192 shot_power_ = goal.shot_power;
193 velocity_compensation_ = goal.velocity_compensation;
194 intake_power_ = goal.intake_power;
195 }
196
James Kuszmaul7077d342021-06-09 20:23:58 -0700197 void HandleTeleop(
198 const ::frc971::input::driver_station::Data &data) override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700199 if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) {
200 intake_power_ = 0.0;
201 separation_angle_ = kGrabSeparation;
202 moving_for_shot_ = false;
203 }
204
205 static const double kAdjustClawGoalDeadband = 0.08;
206 double claw_goal_adjust = data.GetAxis(kAdjustClawGoal);
207 if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) {
208 claw_goal_adjust = 0;
209 } else {
James Kuszmaul7077d342021-06-09 20:23:58 -0700210 claw_goal_adjust = (claw_goal_adjust - ((claw_goal_adjust < 0)
211 ? -kAdjustClawGoalDeadband
Brian Silverman17f503e2015-08-02 18:17:18 -0700212 : kAdjustClawGoalDeadband)) *
213 0.035;
214 }
215 double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation);
216 if (OLD_DS ||
217 ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) {
218 claw_separation_adjust = 0;
219 } else {
220 claw_separation_adjust =
James Kuszmaul7077d342021-06-09 20:23:58 -0700221 (claw_separation_adjust - ((claw_separation_adjust < 0)
222 ? -kAdjustClawGoalDeadband
Brian Silverman17f503e2015-08-02 18:17:18 -0700223 : kAdjustClawGoalDeadband)) *
224 -0.035;
225 }
226
227#if OLD_DS
228 if (data.IsPressed(kFenderShot)) {
229#else
230 if (data.GetAxis(kFlipRobot) > 0.9) {
231#endif
232 claw_goal_adjust += claw_separation_adjust;
233 claw_goal_adjust *= -1;
234
235 if (data.IsPressed(kIntakeOpenPosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700236 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700237 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 SetGoal(kFlippedIntakeOpenGoal);
239 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700240 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700241 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700242 SetGoal(kFlippedIntakeGoal);
243 } else if (data.IsPressed(kVerticalTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700244 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700245 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700246 SetGoal(kVerticalTuckGoal);
247 } else if (data.IsPressed(kTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700248 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700249 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700250 SetGoal(kFlippedTuckGoal);
251 } else if (data.PosEdge(kLongShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700252 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700253 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700254 SetGoal(kFlippedLongShotGoal);
255 } else if (data.PosEdge(kCloseShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700256 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700257 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700258 SetGoal(kFlippedMediumShotGoal);
259 } else if (data.PosEdge(kFenderShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700260 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700261 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700262 SetGoal(kFlippedShortShotGoal);
263 } else if (data.PosEdge(kHumanPlayerShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700264 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700265 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700266 SetGoal(kFlippedHumanShotGoal);
267 } else if (data.PosEdge(kUserLeft)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700268 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700269 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700270 SetGoal(kFlipped254PassGoal);
271 } else if (data.PosEdge(kUserRight)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700272 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700273 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700274 SetGoal(kFlippedDemoShotGoal);
275 } else if (data.PosEdge(kTrussShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700276 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700277 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700278 SetGoal(kFlippedTrussShotGoal);
279 }
280 } else {
281 if (data.IsPressed(kIntakeOpenPosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700282 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700283 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700284 SetGoal(kIntakeOpenGoal);
285 } else if (data.IsPressed(kIntakePosition)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700286 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700287 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700288 SetGoal(kIntakeGoal);
289 } else if (data.IsPressed(kVerticalTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700290 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700291 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700292 SetGoal(kVerticalTuckGoal);
293 } else if (data.IsPressed(kTuck)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700294 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700295 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700296 SetGoal(kTuckGoal);
297 } else if (data.PosEdge(kLongShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700298 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700299 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700300 SetGoal(kLongShotGoal);
301 } else if (data.PosEdge(kCloseShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700302 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700303 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700304 SetGoal(kCloseShotGoal);
305 } else if (data.PosEdge(kFenderShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700306 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700307 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700308 SetGoal(kFenderShotGoal);
309 } else if (data.PosEdge(kHumanPlayerShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700310 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700311 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700312 SetGoal(kHumanShotGoal);
313 } else if (data.PosEdge(kUserLeft)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700314 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700315 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700316 SetGoal(k254PassGoal);
317 } else if (data.PosEdge(kUserRight)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700318 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700319 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700320 SetGoal(kDemoShotGoal);
321 } else if (data.PosEdge(kTrussShot)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700322 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700323 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700324 SetGoal(kTrussShotGoal);
325 }
326 }
327
328 if (data.PosEdge(kFire)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329 aos::common::actions::DoubleParamT param;
330 EnqueueAction(shoot_action_factory_.Make(param));
Brian Silverman17f503e2015-08-02 18:17:18 -0700331 } else if (data.NegEdge(kFire)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700332 CancelCurrentAction();
Brian Silverman17f503e2015-08-02 18:17:18 -0700333 }
334
Brian Silverman17f503e2015-08-02 18:17:18 -0700335 if (data.IsPressed(kUnload) || data.IsPressed(kReload)) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700336 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700337 AOS_LOG(DEBUG, "Canceling\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700338 intake_power_ = 0.0;
339 velocity_compensation_ = 0.0;
340 }
341
342 // Send out the claw and shooter goals if no actions are running.
Austin Schuhbfb04122019-05-22 21:16:51 -0700343 if (!ActionRunning()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700344 goal_angle_ += claw_goal_adjust;
345 separation_angle_ += claw_separation_adjust;
346
347 // If the action just ended, turn the intake off and stop velocity
348 // compensating.
Austin Schuhbfb04122019-05-22 21:16:51 -0700349 if (was_running_action()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700350 intake_power_ = 0.0;
351 velocity_compensation_ = 0.0;
352 }
353
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700354 drivetrain_status_fetcher_.Fetch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700355 double goal_angle = goal_angle_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700356 if (drivetrain_status_fetcher_.get()) {
357 goal_angle +=
Alex Perrycb7da4b2019-08-28 19:35:56 -0700358 SpeedToAngleOffset(drivetrain_status_fetcher_->robot_speed());
Brian Silverman17f503e2015-08-02 18:17:18 -0700359 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700360 AOS_LOG_INTERVAL(no_drivetrain_status_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700361 }
362
363 if (moving_for_shot_) {
Austin Schuhb2461f42019-06-29 18:17:06 -0700364 claw_status_fetcher_.Fetch();
365 if (claw_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 if (::std::abs(claw_status_fetcher_->bottom() - goal_angle) < 0.2) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700367 moving_for_shot_ = false;
368 separation_angle_ = shot_separation_angle_;
369 }
370 }
371 }
372
373 double separation_angle = separation_angle_;
374
375 if (data.IsPressed(kCatch)) {
376 const double kCatchSeparation = 1.0;
377 goal_angle -= kCatchSeparation / 2.0;
378 separation_angle = kCatchSeparation;
379 }
380
381 bool intaking =
382 data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) ||
383 data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch);
Austin Schuhb2461f42019-06-29 18:17:06 -0700384 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700385 auto builder = claw_goal_sender_.MakeBuilder();
386 control_loops::claw::Goal::Builder goal_builder =
387 builder.MakeBuilder<control_loops::claw::Goal>();
388 goal_builder.add_bottom_angle(goal_angle);
389 goal_builder.add_separation_angle(separation_angle);
390 goal_builder.add_intake(
Austin Schuhb2461f42019-06-29 18:17:06 -0700391 intaking ? 12.0
Alex Perrycb7da4b2019-08-28 19:35:56 -0700392 : (data.IsPressed(kRollersOut) ? -12.0 : intake_power_));
393 goal_builder.add_centering(intaking ? 12.0 : 0.0);
Austin Schuhb2461f42019-06-29 18:17:06 -0700394
Alex Perrycb7da4b2019-08-28 19:35:56 -0700395 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700396 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhb2461f42019-06-29 18:17:06 -0700397 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700398 }
399
Austin Schuh493f7af2019-06-29 18:42:12 -0700400 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700401 auto builder = shooter_goal_sender_.MakeBuilder();
402 control_loops::shooter::Goal::Builder goal_builder =
403 builder.MakeBuilder<control_loops::shooter::Goal>();
404 goal_builder.add_shot_power(shot_power_);
405 goal_builder.add_shot_requested(data.IsPressed(kFire));
406 goal_builder.add_unload_requested(data.IsPressed(kUnload));
407 goal_builder.add_load_requested(data.IsPressed(kReload));
408 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700409 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuh493f7af2019-06-29 18:42:12 -0700410 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700411 }
412 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700413 }
414
415 double SpeedToAngleOffset(double speed) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500416 const ::y2014::constants::Values &values = ::y2014::constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700417 // scale speed to a [0.0-1.0] on something close to the max
418 // TODO(austin): Change the scale factor for different shots.
419 return (speed / values.drivetrain_max_speed) * velocity_compensation_;
420 }
421
422 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700423 ::aos::Fetcher<::y2014::control_loops::claw::Status> claw_status_fetcher_;
424 ::aos::Sender<::y2014::control_loops::claw::Goal> claw_goal_sender_;
425 ::aos::Sender<::y2014::control_loops::shooter::Goal> shooter_goal_sender_;
426 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700427 drivetrain_status_fetcher_;
Austin Schuhb2461f42019-06-29 18:17:06 -0700428
Brian Silverman17f503e2015-08-02 18:17:18 -0700429 double shot_power_;
430 double goal_angle_;
431 double separation_angle_, shot_separation_angle_;
432 double velocity_compensation_;
433 double intake_power_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700434 bool moving_for_shot_ = false;
435
Austin Schuh1bf8a212019-05-26 22:13:14 -0700436 actors::ShootActor::Factory shoot_action_factory_;
437
Brian Silverman17f503e2015-08-02 18:17:18 -0700438 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800439 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Brian Silverman17f503e2015-08-02 18:17:18 -0700440 "no drivetrain status");
441};
442
443} // namespace joysticks
444} // namespace input
Brian Silvermanb601d892015-12-20 18:24:38 -0500445} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700446
Austin Schuh094d09b2020-11-20 23:26:52 -0800447int main(int argc, char **argv) {
448 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700449
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
451 aos::configuration::ReadConfig("config.json");
452
453 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800454 ::y2014::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700455
456 event_loop.Run();
457
Austin Schuhae87e312020-08-01 16:15:01 -0700458 return 0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700459}