blob: b235f649f235a3413eb2dc1904df82235371cdea [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#include <unistd.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
4#include <cstdio>
5#include <cstring>
6
Austin Schuha250b2d2019-05-27 16:14:02 -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 Schuha250b2d2019-05-27 16:14:02 -070011#include "aos/util/log_interval.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "frc971/autonomous/auto_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
14#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070015#include "frc971/input/action_joystick_input.h"
16#include "frc971/input/driver_station_data.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
James Kuszmaul7077d342021-06-09 20:23:58 -070029using ::frc971::input::driver_station::ButtonLocation;
30using ::frc971::input::driver_station::ControlBit;
31using ::frc971::input::driver_station::JoystickAxis;
32using ::frc971::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
James Kuszmaul7077d342021-06-09 20:23:58 -070069class Reader : public ::frc971::input::ActionJoystickInput {
Comran Morshed9a9948c2016-01-16 15:58:04 +000070 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080071 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070072 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070073 event_loop, control_loops::drivetrain::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -070074 ::frc971::input::DrivetrainInputReader::InputType::kSteeringWheel,
75 {}),
Austin Schuh1bf8a212019-05-26 22:13:14 -070076 vision_status_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -070077 event_loop->MakeFetcher<::y2016::vision::VisionStatus>("/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)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700106 set_vision_align_fn(
107 [this](const ::frc971::input::driver_station::Data &data) {
108 return VisionAlign(data);
109 });
Austin Schuha250b2d2019-05-27 16:14:02 -0700110 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000111
Austin Schuha250b2d2019-05-27 16:14:02 -0700112 // Returns true when we are vision aligning
James Kuszmaul7077d342021-06-09 20:23:58 -0700113 bool VisionAlign(const ::frc971::input::driver_station::Data &data) {
Austin Schuhadd6d792016-03-19 01:20:01 -0700114 vision_valid_ = false;
115
Austin Schuh1bf8a212019-05-26 22:13:14 -0700116 vision_status_fetcher_.Fetch();
Austin Schuhadd6d792016-03-19 01:20:01 -0700117
Austin Schuh1bf8a212019-05-26 22:13:14 -0700118 if (vision_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700119 vision_valid_ = (vision_status_fetcher_->left_image_valid() &&
120 vision_status_fetcher_->right_image_valid());
121 last_angle_ = vision_status_fetcher_->horizontal_angle();
Austin Schuhadd6d792016-03-19 01:20:01 -0700122 }
123
Austin Schuh974cf652016-04-20 20:18:13 -0700124 if (data.IsPressed(kVisionAlign)) {
125 if (vision_valid_ && !vision_action_running_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 actors::vision_align_action::VisionAlignActionParamsT params;
Austin Schuha250b2d2019-05-27 16:14:02 -0700127 EnqueueAction(vision_align_action_factory_.Make(params));
Austin Schuh974cf652016-04-20 20:18:13 -0700128 vision_action_running_ = true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700129 AOS_LOG(INFO, "Starting vision align\n");
Austin Schuh974cf652016-04-20 20:18:13 -0700130 } else {
131 if (!vision_valid_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700132 AOS_LOG(INFO, "Vision align but not valid\n");
Austin Schuh974cf652016-04-20 20:18:13 -0700133 }
134 }
Austin Schuh18799112016-03-16 22:09:54 -0700135 }
136
137 if (data.NegEdge(kVisionAlign)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700138 CancelAllActions();
Austin Schuh18799112016-03-16 22:09:54 -0700139 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700140 if (!data.IsPressed(kVisionAlign)) {
141 vision_action_running_ = false;
142 }
Austin Schuh18799112016-03-16 22:09:54 -0700143
Austin Schuha250b2d2019-05-27 16:14:02 -0700144 return vision_action_running_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000145 }
146
James Kuszmaul7077d342021-06-09 20:23:58 -0700147 void HandleTeleop(const ::frc971::input::driver_station::Data &data) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700148 if (!data.GetControlBit(ControlBit::kEnabled)) {
149 // If we are not enabled, reset the waiting for zero bit.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700150 AOS_LOG(DEBUG, "Waiting for zero.\n");
Austin Schuha250b2d2019-05-27 16:14:02 -0700151 waiting_for_zero_ = true;
152 }
153
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700154 float voltage_climber = 0.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700155 // Default the intake to up.
156 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
157
158 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000159 if (!data.GetControlBit(ControlBit::kEnabled)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700160 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700161 AOS_LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000162 }
163
Austin Schuh9481d0d2019-06-29 21:56:17 -0700164 superstructure_status_fetcher_.Fetch();
165 if (!superstructure_status_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700166 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000167 }
168
Austin Schuh9481d0d2019-06-29 21:56:17 -0700169 if (superstructure_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170 superstructure_status_fetcher_->zeroed()) {
Comran Morshed200dd4b2016-02-16 17:54:58 +0000171 if (waiting_for_zero_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700172 AOS_LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000173 waiting_for_zero_ = false;
174 }
175 } else {
176 waiting_for_zero_ = true;
177 }
178
Austin Schuhadd6d792016-03-19 01:20:01 -0700179 double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference;
180 bool use_slow_profile = false;
181 if (vision_action_running_) {
182 use_slow_profile = true;
183 if (vision_valid_) {
184 intake_when_shooting -= 0.5;
185 }
186 }
187
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700188 if (data.IsPressed(kFrontLong)) {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800189 // Forwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700190 shoulder_goal_ = M_PI / 2.0 + 0.1;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700191 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700192 drivetrain_status_fetcher_.Fetch();
193 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 wrist_goal_ += drivetrain_status_fetcher_->ground_angle();
Austin Schuhb0099642016-04-03 21:37:27 -0700195 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800196 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700197 intake_goal_ = intake_when_shooting;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800198 } else if (data.IsPressed(kBackLong)) {
199 // Backwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700200 shoulder_goal_ = M_PI / 2.0 - 0.4;
Austin Schuhe153c5a2016-04-20 20:18:42 -0700201 wrist_goal_ = -0.62 - 0.02;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700202 drivetrain_status_fetcher_.Fetch();
203 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 wrist_goal_ += drivetrain_status_fetcher_->ground_angle();
Austin Schuhb0099642016-04-03 21:37:27 -0700205 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800206 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700207 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700208 } else if (data.IsPressed(kBackFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700209 // Backwards shot no IMU
210 shoulder_goal_ = M_PI / 2.0 - 0.4;
211 wrist_goal_ = -0.62 - 0.02;
212 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700213 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700214 } else if (data.IsPressed(kFrontFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700215 // Forwards shot no IMU
Austin Schuh6a871ff2016-05-01 12:31:23 -0700216 shoulder_goal_ = M_PI / 2.0 + 0.1;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700217 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700218 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700219 intake_goal_ = intake_when_shooting;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700220 } else if (data.IsPressed(kExpand) || data.IsPressed(kWinch)) {
221 // Set the goals to the hanging position so when the actor finishes, we
222 // will still be at the right spot.
223 shoulder_goal_ = 1.2;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700224 wrist_goal_ = 1.0;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700225 intake_goal_ = 0.0;
226 if (data.PosEdge(kExpand)) {
227 is_expanding_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700228 actors::superstructure_action::SuperstructureActionParamsT params;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700229 params.partial_angle = 0.3;
230 params.delay_time = 0.7;
231 params.full_angle = shoulder_goal_;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700232 params.shooter_angle = wrist_goal_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700233 EnqueueAction(superstructure_action_factory_.Make(params));
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700234 }
235 if (data.IsPressed(kWinch)) {
236 voltage_climber = 12.0;
237 }
Austin Schuhde802e92016-02-27 14:49:03 -0800238 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800239 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800240 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800241 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000242 }
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700243 if (data.NegEdge(kExpand) || voltage_climber > 1.0) {
244 is_expanding_ = false;
Austin Schuha250b2d2019-05-27 16:14:02 -0700245 CancelAllActions();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700246 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000247
Comran Morshedaa0573c2016-03-05 19:05:54 +0000248 bool ball_detected = false;
Austin Schuh4b652c92019-05-27 13:22:27 -0700249 ball_detector_fetcher_.Fetch();
250 if (ball_detector_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700251 ball_detected = ball_detector_fetcher_->voltage() > 2.5;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000252 }
253 if (data.PosEdge(kIntakeIn)) {
254 saw_ball_when_started_intaking_ = ball_detected;
255 }
256
Austin Schuh45d07f62016-03-13 15:33:31 -0700257 if (data.IsPressed(kIntakeIn)) {
258 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
259 if (ball_detected) {
260 force_lights_on = true;
261 }
262 } else {
263 is_intaking_ = false;
264 }
265
Austin Schuhd52df772016-03-19 15:38:41 -0700266 fire_ = false;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800267 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhd52df772016-03-19 15:38:41 -0700268 if (data.IsPressed(kVisionAlign)) {
269 // Make sure that we are lined up.
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700270 drivetrain_status_fetcher_.Fetch();
271 drivetrain_goal_fetcher_.Fetch();
272 if (drivetrain_status_fetcher_.get() &&
273 drivetrain_goal_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700274 const double left_goal = drivetrain_goal_fetcher_->left_goal();
275 const double right_goal = drivetrain_goal_fetcher_->right_goal();
Austin Schuhd52df772016-03-19 15:38:41 -0700276 const double left_current =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 drivetrain_status_fetcher_->estimated_left_position();
Austin Schuhd52df772016-03-19 15:38:41 -0700278 const double right_current =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279 drivetrain_status_fetcher_->estimated_right_position();
Austin Schuhd52df772016-03-19 15:38:41 -0700280 const double left_velocity =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700281 drivetrain_status_fetcher_->estimated_left_velocity();
Austin Schuhd52df772016-03-19 15:38:41 -0700282 const double right_velocity =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 drivetrain_status_fetcher_->estimated_right_velocity();
Austin Schuhd52df772016-03-19 15:38:41 -0700284 if (vision_action_running_ && ::std::abs(last_angle_) < 0.02 &&
285 ::std::abs((left_goal - right_goal) -
286 (left_current - right_current)) /
Austin Schuha250b2d2019-05-27 16:14:02 -0700287 dt_config().robot_radius / 2.0 <
Austin Schuhd52df772016-03-19 15:38:41 -0700288 0.02 &&
289 ::std::abs(left_velocity - right_velocity) < 0.01) {
290 ++ready_to_fire_;
291 } else {
292 ready_to_fire_ = 0;
293 }
Austin Schuh3d79cc02016-03-20 21:08:53 -0700294 if (ready_to_fire_ > 9) {
Austin Schuhd52df772016-03-19 15:38:41 -0700295 fire_ = true;
296 }
297 }
298 } else {
299 fire_ = true;
300 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000301 }
302
Austin Schuh4ea06c12016-03-12 17:54:31 -0800303 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800304
Austin Schuhd52df772016-03-19 15:38:41 -0700305 if (is_intaking_ || is_outtaking_) {
306 recently_intaking_accumulator_ = 20;
307 }
308
309 if (data.IsPressed(kIntakeDown)) {
310 if (recently_intaking_accumulator_) {
311 intake_goal_ = 0.1;
312 } else {
313 intake_goal_ = -0.05;
314 }
315 }
316
317 if (recently_intaking_accumulator_ > 0) {
318 --recently_intaking_accumulator_;
319 }
320
Austin Schuh843412b2016-03-20 16:48:46 -0700321 if (data.IsPressed(kPortcullis)) {
322 traverse_unlatched_ = true;
323 traverse_down_ = true;
324 } else if (data.IsPressed(kChevalDeFrise)) {
325 traverse_unlatched_ = false;
326 traverse_down_ = true;
327 } else {
328 traverse_unlatched_ = true;
329 traverse_down_ = false;
330 }
331
Comran Morshed200dd4b2016-02-16 17:54:58 +0000332 if (!waiting_for_zero_) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700333 if (!is_expanding_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700334 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuh3f0b1192016-03-12 13:03:56 -0800335
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 ::y2016::control_loops::superstructure::Goal::Builder
337 superstructure_builder = builder.MakeBuilder<
338 ::y2016::control_loops::superstructure::Goal>();
339 superstructure_builder.add_angle_intake(intake_goal_);
340 superstructure_builder.add_angle_shoulder(shoulder_goal_);
341 superstructure_builder.add_angle_wrist(wrist_goal_);
342
343 superstructure_builder.add_max_angular_velocity_intake(7.0);
344 superstructure_builder.add_max_angular_velocity_shoulder(4.0);
345 superstructure_builder.add_max_angular_velocity_wrist(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700346 if (use_slow_profile) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 superstructure_builder.add_max_angular_acceleration_intake(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700348 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700349 superstructure_builder.add_max_angular_acceleration_intake(40.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700350 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351 superstructure_builder.add_max_angular_acceleration_shoulder(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700352 if (shoulder_goal_ > 1.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700353 superstructure_builder.add_max_angular_acceleration_wrist(45.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700354 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700355 superstructure_builder.add_max_angular_acceleration_wrist(25.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700356 }
Austin Schuh3f0b1192016-03-12 13:03:56 -0800357
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700358 // Granny mode
359 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -0700360 superstructure_builder.add_max_angular_velocity_intake(0.2);
361 superstructure_builder.add_max_angular_velocity_shoulder(0.2);
362 superstructure_builder.add_max_angular_velocity_wrist(0.2);
363 superstructure_builder.add_max_angular_acceleration_intake(1.0);
364 superstructure_builder.add_max_angular_acceleration_shoulder(1.0);
365 superstructure_builder.add_max_angular_acceleration_wrist(1.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700366 */
367 if (is_intaking_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 superstructure_builder.add_voltage_top_rollers(12.0);
369 superstructure_builder.add_voltage_bottom_rollers(12.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700370 } else if (is_outtaking_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 superstructure_builder.add_voltage_top_rollers(-12.0);
372 superstructure_builder.add_voltage_bottom_rollers(-7.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700373 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700374 superstructure_builder.add_voltage_top_rollers(0.0);
375 superstructure_builder.add_voltage_bottom_rollers(0.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700376 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000377
Alex Perrycb7da4b2019-08-28 19:35:56 -0700378 superstructure_builder.add_traverse_unlatched(traverse_unlatched_);
379 superstructure_builder.add_unfold_climber(false);
380 superstructure_builder.add_voltage_climber(voltage_climber);
381 superstructure_builder.add_traverse_down(traverse_down_);
382 superstructure_builder.add_force_intake(true);
Austin Schuh843412b2016-03-20 16:48:46 -0700383
milind1f1dca32021-07-03 13:50:07 -0700384 if (builder.Send(superstructure_builder.Finish()) !=
385 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700386 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700387 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700388 AOS_LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n",
389 intake_goal_, shoulder_goal_, wrist_goal_);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700390 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700391 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000392
Alex Perrycb7da4b2019-08-28 19:35:56 -0700393 {
394 auto builder = shooter_goal_sender_.MakeBuilder();
395 y2016::control_loops::shooter::Goal::Builder shooter_builder =
396 builder.MakeBuilder<y2016::control_loops::shooter::Goal>();
397 shooter_builder.add_angular_velocity(shooter_velocity_);
398 shooter_builder.add_clamp_open(is_intaking_ || is_outtaking_);
399 shooter_builder.add_push_to_shooter(fire_);
400 shooter_builder.add_force_lights_on(force_lights_on);
401 shooter_builder.add_shooting_forwards(wrist_goal_ > 0);
Austin Schuhae023fb2019-06-29 17:11:45 -0700402
milind1f1dca32021-07-03 13:50:07 -0700403 if (builder.Send(shooter_builder.Finish()) !=
404 aos::RawSender::Error::kOk) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700405 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
406 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000407 }
408 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000409 }
410
Comran Morshed9a9948c2016-01-16 15:58:04 +0000411 private:
Austin Schuh1bf8a212019-05-26 22:13:14 -0700412 ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700413 ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
James Kuszmaul7077d342021-06-09 20:23:58 -0700414 ::aos::Sender<::y2016::control_loops::shooter::Goal> shooter_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700415 ::aos::Fetcher<::y2016::control_loops::superstructure::Status>
Austin Schuh9481d0d2019-06-29 21:56:17 -0700416 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 ::aos::Sender<::y2016::control_loops::superstructure::Goal>
Austin Schuh9481d0d2019-06-29 21:56:17 -0700418 superstructure_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419 ::aos::Fetcher<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700420 drivetrain_goal_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700421 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700422 drivetrain_status_fetcher_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700423
Comran Morshed200dd4b2016-02-16 17:54:58 +0000424 // Whatever these are set to are our default goals to send out after zeroing.
425 double intake_goal_;
426 double shoulder_goal_;
427 double wrist_goal_;
Austin Schuhde802e92016-02-27 14:49:03 -0800428 double shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000429
Austin Schuh843412b2016-03-20 16:48:46 -0700430 bool traverse_unlatched_ = false;
431 bool traverse_down_ = false;
432
Comran Morshed200dd4b2016-02-16 17:54:58 +0000433 // If we're waiting for the subsystems to zero.
434 bool waiting_for_zero_ = true;
435
Comran Morshedaa0573c2016-03-05 19:05:54 +0000436 // If true, the ball was present when the intaking button was pressed.
437 bool saw_ball_when_started_intaking_ = false;
438
Austin Schuhde802e92016-02-27 14:49:03 -0800439 bool is_intaking_ = false;
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800440 bool is_outtaking_ = false;
Austin Schuhde802e92016-02-27 14:49:03 -0800441 bool fire_ = false;
442
Austin Schuhadd6d792016-03-19 01:20:01 -0700443 bool vision_action_running_ = false;
444 bool vision_valid_ = false;
445
Austin Schuhd52df772016-03-19 15:38:41 -0700446 int recently_intaking_accumulator_ = 0;
447 double last_angle_ = 100;
448
449 int ready_to_fire_ = 0;
450
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700451 bool is_expanding_ = false;
452
Austin Schuh1bf8a212019-05-26 22:13:14 -0700453 actors::VisionAlignActor::Factory vision_align_action_factory_;
454 actors::SuperstructureActor::Factory superstructure_action_factory_;
455
Comran Morshed9a9948c2016-01-16 15:58:04 +0000456 ::aos::util::SimpleLogInterval no_drivetrain_status_ =
Austin Schuh61bdc602016-12-04 19:10:10 -0800457 ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000458 "no drivetrain status");
459};
460
461} // namespace joysticks
462} // namespace input
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000463} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000464
Austin Schuh094d09b2020-11-20 23:26:52 -0800465int main(int argc, char **argv) {
466 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700467
Alex Perrycb7da4b2019-08-28 19:35:56 -0700468 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
469 aos::configuration::ReadConfig("config.json");
470
471 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800472 ::y2016::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700473
474 event_loop.Run();
475
Austin Schuhae87e312020-08-01 16:15:01 -0700476 return 0;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000477}