blob: fbb91a5ac3d4ee106126060063357ccd30bf0fd0 [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
Austin Schuha250b2d2019-05-27 16:14:02 -07006#include "aos/actions/actions.h"
John Park398c74a2018-10-20 21:17:39 -07007#include "aos/init.h"
Austin Schuha250b2d2019-05-27 16:14:02 -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 Schuha250b2d2019-05-27 16:14:02 -070012#include "aos/util/log_interval.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000013
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "frc971/autonomous/auto_generated.h"
15#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
16#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080017#include "y2016/actors/autonomous_actor.h"
18#include "y2016/actors/superstructure_actor.h"
19#include "y2016/actors/vision_align_actor.h"
20#include "y2016/constants.h"
21#include "y2016/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "y2016/control_loops/shooter/shooter_goal_generated.h"
Austin Schuh45d07f62016-03-13 15:33:31 -070023#include "y2016/control_loops/superstructure/superstructure.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "y2016/control_loops/superstructure/superstructure_goal_generated.h"
25#include "y2016/control_loops/superstructure/superstructure_status_generated.h"
26#include "y2016/queues/ball_detector_generated.h"
27#include "y2016/vision/vision_generated.h"
Comran Morshed200dd4b2016-02-16 17:54:58 +000028
Comran Morshed9a9948c2016-01-16 15:58:04 +000029using ::aos::input::driver_station::ButtonLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000030using ::aos::input::driver_station::ControlBit;
Austin Schuh4ea06c12016-03-12 17:54:31 -080031using ::aos::input::driver_station::JoystickAxis;
32using ::aos::input::driver_station::POVLocation;
Comran Morshed9a9948c2016-01-16 15:58:04 +000033
Comran Morshed6c6a0a92016-01-17 12:45:16 +000034namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000035namespace input {
36namespace joysticks {
37
Austin Schuh45d07f62016-03-13 15:33:31 -070038namespace {
39
40constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
41 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
42
43} // namespace
44
Comran Morshed9a9948c2016-01-16 15:58:04 +000045const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080046const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000047const ButtonLocation kQuickTurn(1, 5);
48
Austin Schuh781cdcc2016-03-12 13:03:12 -080049const ButtonLocation kTurn1(1, 7);
50const ButtonLocation kTurn2(1, 11);
51
Comran Morshed200dd4b2016-02-16 17:54:58 +000052// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080053const ButtonLocation kIntakeDown(3, 11);
54const POVLocation kFrontLong(3, 180);
55const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070056const POVLocation kBackFender(3, 90);
57const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080058const ButtonLocation kIntakeIn(3, 12);
Austin Schuh4ea06c12016-03-12 17:54:31 -080059const ButtonLocation kFire(3, 3);
Austin Schuh4ea06c12016-03-12 17:54:31 -080060const ButtonLocation kIntakeOut(3, 9);
Austin Schuh843412b2016-03-20 16:48:46 -070061const ButtonLocation kPortcullis(3, 7);
62const ButtonLocation kChevalDeFrise(3, 8);
Comran Morshed200dd4b2016-02-16 17:54:58 +000063
Austin Schuhadd6d792016-03-19 01:20:01 -070064const ButtonLocation kVisionAlign(3, 4);
Austin Schuh18799112016-03-16 22:09:54 -070065
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070066const ButtonLocation kExpand(3, 6);
67const ButtonLocation kWinch(3, 5);
68
Austin Schuha250b2d2019-05-27 16:14:02 -070069class Reader : public ::aos::input::ActionJoystickInput {
Comran Morshed9a9948c2016-01-16 15:58:04 +000070 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080071 Reader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -070072 : ::aos::input::ActionJoystickInput(
73 event_loop, control_loops::drivetrain::GetDrivetrainConfig(),
74 ::aos::input::DrivetrainInputReader::InputType::kSteeringWheel, {}),
Austin Schuh1bf8a212019-05-26 22:13:14 -070075 vision_status_fetcher_(
76 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
Tyler Chatow15ce3bd2020-01-19 21:49:47 -080077 "/vision")),
Austin Schuh4b652c92019-05-27 13:22:27 -070078 ball_detector_fetcher_(
79 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 "/superstructure")),
Austin Schuhae023fb2019-06-29 17:11:45 -070081 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 event_loop->MakeSender<::y2016::control_loops::shooter::Goal>(
83 "/shooter")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070084 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 event_loop
86 ->MakeFetcher<::y2016::control_loops::superstructure::Status>(
87 "/superstructure")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070088 superstructure_goal_sender_(
89 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 ->MakeSender<::y2016::control_loops::superstructure::Goal>(
91 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070092 drivetrain_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Goal>(
94 "/drivetrain")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070095 drivetrain_status_fetcher_(
96 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070097 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
98 "/drivetrain")),
Comran Morshed200dd4b2016-02-16 17:54:58 +000099 intake_goal_(0.0),
100 shoulder_goal_(M_PI / 2.0),
Austin Schuhd52df772016-03-19 15:38:41 -0700101 wrist_goal_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -0700102 vision_align_action_factory_(
103 actors::VisionAlignActor::MakeFactory(event_loop)),
104 superstructure_action_factory_(
Austin Schuha250b2d2019-05-27 16:14:02 -0700105 actors::SuperstructureActor::MakeFactory(event_loop)) {
106 set_vision_align_fn([this](const ::aos::input::driver_station::Data &data) {
107 return VisionAlign(data);
108 });
109 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000110
Austin Schuha250b2d2019-05-27 16:14:02 -0700111 // Returns true when we are vision aligning
112 bool VisionAlign(const ::aos::input::driver_station::Data &data) {
Austin Schuhadd6d792016-03-19 01:20:01 -0700113 vision_valid_ = false;
114
Austin Schuh1bf8a212019-05-26 22:13:14 -0700115 vision_status_fetcher_.Fetch();
Austin Schuhadd6d792016-03-19 01:20:01 -0700116
Austin Schuh1bf8a212019-05-26 22:13:14 -0700117 if (vision_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118 vision_valid_ = (vision_status_fetcher_->left_image_valid() &&
119 vision_status_fetcher_->right_image_valid());
120 last_angle_ = vision_status_fetcher_->horizontal_angle();
Austin Schuhadd6d792016-03-19 01:20:01 -0700121 }
122
Austin Schuh974cf652016-04-20 20:18:13 -0700123 if (data.IsPressed(kVisionAlign)) {
124 if (vision_valid_ && !vision_action_running_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125 actors::vision_align_action::VisionAlignActionParamsT params;
Austin Schuha250b2d2019-05-27 16:14:02 -0700126 EnqueueAction(vision_align_action_factory_.Make(params));
Austin Schuh974cf652016-04-20 20:18:13 -0700127 vision_action_running_ = true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700128 AOS_LOG(INFO, "Starting vision align\n");
Austin Schuh974cf652016-04-20 20:18:13 -0700129 } else {
130 if (!vision_valid_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700131 AOS_LOG(INFO, "Vision align but not valid\n");
Austin Schuh974cf652016-04-20 20:18:13 -0700132 }
133 }
Austin Schuh18799112016-03-16 22:09:54 -0700134 }
135
136 if (data.NegEdge(kVisionAlign)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700137 CancelAllActions();
Austin Schuh18799112016-03-16 22:09:54 -0700138 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700139 if (!data.IsPressed(kVisionAlign)) {
140 vision_action_running_ = false;
141 }
Austin Schuh18799112016-03-16 22:09:54 -0700142
Austin Schuha250b2d2019-05-27 16:14:02 -0700143 return vision_action_running_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000144 }
145
Comran Morshed9a9948c2016-01-16 15:58:04 +0000146 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700147 if (!data.GetControlBit(ControlBit::kEnabled)) {
148 // If we are not enabled, reset the waiting for zero bit.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700149 AOS_LOG(DEBUG, "Waiting for zero.\n");
Austin Schuha250b2d2019-05-27 16:14:02 -0700150 waiting_for_zero_ = true;
151 }
152
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700153 float voltage_climber = 0.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700154 // Default the intake to up.
155 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
156
157 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000158 if (!data.GetControlBit(ControlBit::kEnabled)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700159 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700160 AOS_LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000161 }
162
Austin Schuh9481d0d2019-06-29 21:56:17 -0700163 superstructure_status_fetcher_.Fetch();
164 if (!superstructure_status_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700165 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000166 }
167
Austin Schuh9481d0d2019-06-29 21:56:17 -0700168 if (superstructure_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 superstructure_status_fetcher_->zeroed()) {
Comran Morshed200dd4b2016-02-16 17:54:58 +0000170 if (waiting_for_zero_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700171 AOS_LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000172 waiting_for_zero_ = false;
173 }
174 } else {
175 waiting_for_zero_ = true;
176 }
177
Austin Schuhadd6d792016-03-19 01:20:01 -0700178 double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference;
179 bool use_slow_profile = false;
180 if (vision_action_running_) {
181 use_slow_profile = true;
182 if (vision_valid_) {
183 intake_when_shooting -= 0.5;
184 }
185 }
186
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700187 if (data.IsPressed(kFrontLong)) {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800188 // Forwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700189 shoulder_goal_ = M_PI / 2.0 + 0.1;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700190 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700191 drivetrain_status_fetcher_.Fetch();
192 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 wrist_goal_ += drivetrain_status_fetcher_->ground_angle();
Austin Schuhb0099642016-04-03 21:37:27 -0700194 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800195 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700196 intake_goal_ = intake_when_shooting;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800197 } else if (data.IsPressed(kBackLong)) {
198 // Backwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700199 shoulder_goal_ = M_PI / 2.0 - 0.4;
Austin Schuhe153c5a2016-04-20 20:18:42 -0700200 wrist_goal_ = -0.62 - 0.02;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700201 drivetrain_status_fetcher_.Fetch();
202 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700203 wrist_goal_ += drivetrain_status_fetcher_->ground_angle();
Austin Schuhb0099642016-04-03 21:37:27 -0700204 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800205 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700206 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700207 } else if (data.IsPressed(kBackFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700208 // Backwards shot no IMU
209 shoulder_goal_ = M_PI / 2.0 - 0.4;
210 wrist_goal_ = -0.62 - 0.02;
211 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700212 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700213 } else if (data.IsPressed(kFrontFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700214 // Forwards shot no IMU
Austin Schuh6a871ff2016-05-01 12:31:23 -0700215 shoulder_goal_ = M_PI / 2.0 + 0.1;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700216 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700217 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700218 intake_goal_ = intake_when_shooting;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700219 } else if (data.IsPressed(kExpand) || data.IsPressed(kWinch)) {
220 // Set the goals to the hanging position so when the actor finishes, we
221 // will still be at the right spot.
222 shoulder_goal_ = 1.2;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700223 wrist_goal_ = 1.0;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700224 intake_goal_ = 0.0;
225 if (data.PosEdge(kExpand)) {
226 is_expanding_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700227 actors::superstructure_action::SuperstructureActionParamsT params;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700228 params.partial_angle = 0.3;
229 params.delay_time = 0.7;
230 params.full_angle = shoulder_goal_;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700231 params.shooter_angle = wrist_goal_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700232 EnqueueAction(superstructure_action_factory_.Make(params));
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700233 }
234 if (data.IsPressed(kWinch)) {
235 voltage_climber = 12.0;
236 }
Austin Schuhde802e92016-02-27 14:49:03 -0800237 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800238 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800239 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800240 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000241 }
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700242 if (data.NegEdge(kExpand) || voltage_climber > 1.0) {
243 is_expanding_ = false;
Austin Schuha250b2d2019-05-27 16:14:02 -0700244 CancelAllActions();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700245 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000246
Comran Morshedaa0573c2016-03-05 19:05:54 +0000247 bool ball_detected = false;
Austin Schuh4b652c92019-05-27 13:22:27 -0700248 ball_detector_fetcher_.Fetch();
249 if (ball_detector_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700250 ball_detected = ball_detector_fetcher_->voltage() > 2.5;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000251 }
252 if (data.PosEdge(kIntakeIn)) {
253 saw_ball_when_started_intaking_ = ball_detected;
254 }
255
Austin Schuh45d07f62016-03-13 15:33:31 -0700256 if (data.IsPressed(kIntakeIn)) {
257 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
258 if (ball_detected) {
259 force_lights_on = true;
260 }
261 } else {
262 is_intaking_ = false;
263 }
264
Austin Schuhd52df772016-03-19 15:38:41 -0700265 fire_ = false;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800266 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhd52df772016-03-19 15:38:41 -0700267 if (data.IsPressed(kVisionAlign)) {
268 // Make sure that we are lined up.
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700269 drivetrain_status_fetcher_.Fetch();
270 drivetrain_goal_fetcher_.Fetch();
271 if (drivetrain_status_fetcher_.get() &&
272 drivetrain_goal_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 const double left_goal = drivetrain_goal_fetcher_->left_goal();
274 const double right_goal = drivetrain_goal_fetcher_->right_goal();
Austin Schuhd52df772016-03-19 15:38:41 -0700275 const double left_current =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700276 drivetrain_status_fetcher_->estimated_left_position();
Austin Schuhd52df772016-03-19 15:38:41 -0700277 const double right_current =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700278 drivetrain_status_fetcher_->estimated_right_position();
Austin Schuhd52df772016-03-19 15:38:41 -0700279 const double left_velocity =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700280 drivetrain_status_fetcher_->estimated_left_velocity();
Austin Schuhd52df772016-03-19 15:38:41 -0700281 const double right_velocity =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282 drivetrain_status_fetcher_->estimated_right_velocity();
Austin Schuhd52df772016-03-19 15:38:41 -0700283 if (vision_action_running_ && ::std::abs(last_angle_) < 0.02 &&
284 ::std::abs((left_goal - right_goal) -
285 (left_current - right_current)) /
Austin Schuha250b2d2019-05-27 16:14:02 -0700286 dt_config().robot_radius / 2.0 <
Austin Schuhd52df772016-03-19 15:38:41 -0700287 0.02 &&
288 ::std::abs(left_velocity - right_velocity) < 0.01) {
289 ++ready_to_fire_;
290 } else {
291 ready_to_fire_ = 0;
292 }
Austin Schuh3d79cc02016-03-20 21:08:53 -0700293 if (ready_to_fire_ > 9) {
Austin Schuhd52df772016-03-19 15:38:41 -0700294 fire_ = true;
295 }
296 }
297 } else {
298 fire_ = true;
299 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000300 }
301
Austin Schuh4ea06c12016-03-12 17:54:31 -0800302 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800303
Austin Schuhd52df772016-03-19 15:38:41 -0700304 if (is_intaking_ || is_outtaking_) {
305 recently_intaking_accumulator_ = 20;
306 }
307
308 if (data.IsPressed(kIntakeDown)) {
309 if (recently_intaking_accumulator_) {
310 intake_goal_ = 0.1;
311 } else {
312 intake_goal_ = -0.05;
313 }
314 }
315
316 if (recently_intaking_accumulator_ > 0) {
317 --recently_intaking_accumulator_;
318 }
319
Austin Schuh843412b2016-03-20 16:48:46 -0700320 if (data.IsPressed(kPortcullis)) {
321 traverse_unlatched_ = true;
322 traverse_down_ = true;
323 } else if (data.IsPressed(kChevalDeFrise)) {
324 traverse_unlatched_ = false;
325 traverse_down_ = true;
326 } else {
327 traverse_unlatched_ = true;
328 traverse_down_ = false;
329 }
330
Comran Morshed200dd4b2016-02-16 17:54:58 +0000331 if (!waiting_for_zero_) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700332 if (!is_expanding_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuh3f0b1192016-03-12 13:03:56 -0800334
Alex Perrycb7da4b2019-08-28 19:35:56 -0700335 ::y2016::control_loops::superstructure::Goal::Builder
336 superstructure_builder = builder.MakeBuilder<
337 ::y2016::control_loops::superstructure::Goal>();
338 superstructure_builder.add_angle_intake(intake_goal_);
339 superstructure_builder.add_angle_shoulder(shoulder_goal_);
340 superstructure_builder.add_angle_wrist(wrist_goal_);
341
342 superstructure_builder.add_max_angular_velocity_intake(7.0);
343 superstructure_builder.add_max_angular_velocity_shoulder(4.0);
344 superstructure_builder.add_max_angular_velocity_wrist(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700345 if (use_slow_profile) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346 superstructure_builder.add_max_angular_acceleration_intake(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700347 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700348 superstructure_builder.add_max_angular_acceleration_intake(40.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700349 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700350 superstructure_builder.add_max_angular_acceleration_shoulder(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700351 if (shoulder_goal_ > 1.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700352 superstructure_builder.add_max_angular_acceleration_wrist(45.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700353 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700354 superstructure_builder.add_max_angular_acceleration_wrist(25.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700355 }
Austin Schuh3f0b1192016-03-12 13:03:56 -0800356
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700357 // Granny mode
358 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -0700359 superstructure_builder.add_max_angular_velocity_intake(0.2);
360 superstructure_builder.add_max_angular_velocity_shoulder(0.2);
361 superstructure_builder.add_max_angular_velocity_wrist(0.2);
362 superstructure_builder.add_max_angular_acceleration_intake(1.0);
363 superstructure_builder.add_max_angular_acceleration_shoulder(1.0);
364 superstructure_builder.add_max_angular_acceleration_wrist(1.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700365 */
366 if (is_intaking_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700367 superstructure_builder.add_voltage_top_rollers(12.0);
368 superstructure_builder.add_voltage_bottom_rollers(12.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700369 } else if (is_outtaking_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370 superstructure_builder.add_voltage_top_rollers(-12.0);
371 superstructure_builder.add_voltage_bottom_rollers(-7.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700372 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700373 superstructure_builder.add_voltage_top_rollers(0.0);
374 superstructure_builder.add_voltage_bottom_rollers(0.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700375 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000376
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377 superstructure_builder.add_traverse_unlatched(traverse_unlatched_);
378 superstructure_builder.add_unfold_climber(false);
379 superstructure_builder.add_voltage_climber(voltage_climber);
380 superstructure_builder.add_traverse_down(traverse_down_);
381 superstructure_builder.add_force_intake(true);
Austin Schuh843412b2016-03-20 16:48:46 -0700382
Alex Perrycb7da4b2019-08-28 19:35:56 -0700383 if (!builder.Send(superstructure_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700384 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700385 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700386 AOS_LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
387 intake_goal_, shoulder_goal_, wrist_goal_);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700388 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700389 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000390
Alex Perrycb7da4b2019-08-28 19:35:56 -0700391 {
392 auto builder = shooter_goal_sender_.MakeBuilder();
393 y2016::control_loops::shooter::Goal::Builder shooter_builder =
394 builder.MakeBuilder<y2016::control_loops::shooter::Goal>();
395 shooter_builder.add_angular_velocity(shooter_velocity_);
396 shooter_builder.add_clamp_open(is_intaking_ || is_outtaking_);
397 shooter_builder.add_push_to_shooter(fire_);
398 shooter_builder.add_force_lights_on(force_lights_on);
399 shooter_builder.add_shooting_forwards(wrist_goal_ > 0);
Austin Schuhae023fb2019-06-29 17:11:45 -0700400
Alex Perrycb7da4b2019-08-28 19:35:56 -0700401 if (!builder.Send(shooter_builder.Finish())) {
402 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
403 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000404 }
405 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000406 }
407
Comran Morshed9a9948c2016-01-16 15:58:04 +0000408 private:
Austin Schuh1bf8a212019-05-26 22:13:14 -0700409 ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700410 ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700411 ::aos::Sender<::y2016::control_loops::shooter::Goal>
Austin Schuhae023fb2019-06-29 17:11:45 -0700412 shooter_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700413 ::aos::Fetcher<::y2016::control_loops::superstructure::Status>
Austin Schuh9481d0d2019-06-29 21:56:17 -0700414 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700415 ::aos::Sender<::y2016::control_loops::superstructure::Goal>
Austin Schuh9481d0d2019-06-29 21:56:17 -0700416 superstructure_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 ::aos::Fetcher<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700418 drivetrain_goal_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700420 drivetrain_status_fetcher_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700421
Comran Morshed200dd4b2016-02-16 17:54:58 +0000422 // Whatever these are set to are our default goals to send out after zeroing.
423 double intake_goal_;
424 double shoulder_goal_;
425 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800426 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000427
Austin Schuh843412b2016-03-20 16:48:46 -0700428 bool traverse_unlatched_ = false;
429 bool traverse_down_ = false;
430
Comran Morshed200dd4b2016-02-16 17:54:58 +0000431 // If we're waiting for the subsystems to zero.
432 bool waiting_for_zero_ = true;
433
Comran Morshedaa0573c2016-03-05 19:05:54 +0000434 // If true, the ball was present when the intaking button was pressed.
435 bool saw_ball_when_started_intaking_ = false;
436
Austin Schuhde802e92016-02-27 14:49:03 -0800437 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800438 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800439 bool fire_ = false;
440
Austin Schuhadd6d792016-03-19 01:20:01 -0700441 bool vision_action_running_ = false;
442 bool vision_valid_ = false;
443
Austin Schuhd52df772016-03-19 15:38:41 -0700444 int recently_intaking_accumulator_ = 0;
445 double last_angle_ = 100;
446
447 int ready_to_fire_ = 0;
448
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700449 bool is_expanding_ = false;
450
Austin Schuh1bf8a212019-05-26 22:13:14 -0700451 actors::VisionAlignActor::Factory vision_align_action_factory_;
452 actors::SuperstructureActor::Factory superstructure_action_factory_;
453
Comran Morshed9a9948c2016-01-16 15:58:04 +0000454 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800455 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000456 "no drivetrain status");
457};
458
459} // namespace joysticks
460} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000461} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000462
463int main() {
James Kuszmaulad8a8082020-02-14 21:21:58 -0800464 ::aos::InitNRT();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700465
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
467 aos::configuration::ReadConfig("config.json");
468
469 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800470 ::y2016::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700471
472 event_loop.Run();
473
Comran Morshed9a9948c2016-01-16 15:58:04 +0000474 ::aos::Cleanup();
475}