blob: 6f94bfefdb2b852a2718fbcd77f9d965483fa140 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
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"
Austin Schuhbfb04122019-05-22 21:16:51 -07008#include "aos/input/action_joystick_input.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/input/driver_station_data.h"
10#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/time/time.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070012#include "aos/util/log_interval.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070013
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "frc971/control_loops/drivetrain/drivetrain_status_generated.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
Brian Silverman17f503e2015-08-02 18:17:18 -070022using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::ControlBit;
25
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),
33 kDriveControlLoopEnable2(1, 11);
34const 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
Austin Schuhbfb04122019-05-22 21:16:51 -0700153class Reader : public ::aos::input::ActionJoystickInput {
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 public:
Austin Schuh3e45c752019-02-02 12:19:11 -0800155 Reader(::aos::EventLoop *event_loop)
Austin Schuhbfb04122019-05-22 21:16:51 -0700156 : ::aos::input::ActionJoystickInput(
157 event_loop, control_loops::GetDrivetrainConfig(),
158 ::aos::input::DrivetrainInputReader::InputType::kSteeringWheel, {}),
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
Austin Schuhbfb04122019-05-22 21:16:51 -0700197 void HandleTeleop(const ::aos::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 {
209 claw_goal_adjust = (claw_goal_adjust -
210 ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband
211 : 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 =
220 (claw_separation_adjust -
221 ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband
222 : 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
Alex Perrycb7da4b2019-08-28 19:35:56 -0700394 if (!builder.Send(goal_builder.Finish())) {
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));
407 if (!builder.Send(goal_builder.Finish())) {
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
442} // namespace joysticks
443} // namespace input
Brian Silvermanb601d892015-12-20 18:24:38 -0500444} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700445
446int main() {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700447 ::aos::InitNRT(true);
448
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
450 aos::configuration::ReadConfig("config.json");
451
452 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800453 ::y2014::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700454
455 event_loop.Run();
456
Brian Silverman17f503e2015-08-02 18:17:18 -0700457 ::aos::Cleanup();
458}