blob: 2eb2ed14b60b15c0154bc6d3722d5acae4ea1e6f [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080034namespace y2016::input::joysticks {
Comran Morshed9a9948c2016-01-16 15:58:04 +000035
Austin Schuh45d07f62016-03-13 15:33:31 -070036namespace {
37
38constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops::
39 superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
40
41} // namespace
42
Comran Morshed9a9948c2016-01-16 15:58:04 +000043const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
Campbell Crowley5b27f022016-02-20 16:55:35 -080044const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1);
Comran Morshed9a9948c2016-01-16 15:58:04 +000045const ButtonLocation kQuickTurn(1, 5);
46
Austin Schuh781cdcc2016-03-12 13:03:12 -080047const ButtonLocation kTurn1(1, 7);
48const ButtonLocation kTurn2(1, 11);
49
Comran Morshed200dd4b2016-02-16 17:54:58 +000050// Buttons on the lexan driver station to get things running on bring-up day.
Austin Schuh4ea06c12016-03-12 17:54:31 -080051const ButtonLocation kIntakeDown(3, 11);
52const POVLocation kFrontLong(3, 180);
53const POVLocation kBackLong(3, 0);
Austin Schuh45d07f62016-03-13 15:33:31 -070054const POVLocation kBackFender(3, 90);
55const POVLocation kFrontFender(3, 270);
Austin Schuh4ea06c12016-03-12 17:54:31 -080056const ButtonLocation kIntakeIn(3, 12);
Austin Schuh4ea06c12016-03-12 17:54:31 -080057const ButtonLocation kFire(3, 3);
Austin Schuh4ea06c12016-03-12 17:54:31 -080058const ButtonLocation kIntakeOut(3, 9);
Austin Schuh843412b2016-03-20 16:48:46 -070059const ButtonLocation kPortcullis(3, 7);
60const ButtonLocation kChevalDeFrise(3, 8);
Comran Morshed200dd4b2016-02-16 17:54:58 +000061
Austin Schuhadd6d792016-03-19 01:20:01 -070062const ButtonLocation kVisionAlign(3, 4);
Austin Schuh18799112016-03-16 22:09:54 -070063
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070064const ButtonLocation kExpand(3, 6);
65const ButtonLocation kWinch(3, 5);
66
James Kuszmaul7077d342021-06-09 20:23:58 -070067class Reader : public ::frc971::input::ActionJoystickInput {
Comran Morshed9a9948c2016-01-16 15:58:04 +000068 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080069 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070070 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070071 event_loop, control_loops::drivetrain::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -070072 ::frc971::input::DrivetrainInputReader::InputType::kSteeringWheel,
73 {}),
Austin Schuh1bf8a212019-05-26 22:13:14 -070074 vision_status_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -070075 event_loop->MakeFetcher<::y2016::vision::VisionStatus>("/vision")),
Austin Schuh4b652c92019-05-27 13:22:27 -070076 ball_detector_fetcher_(
77 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070078 "/superstructure")),
Austin Schuhae023fb2019-06-29 17:11:45 -070079 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 event_loop->MakeSender<::y2016::control_loops::shooter::Goal>(
81 "/shooter")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070082 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 event_loop
84 ->MakeFetcher<::y2016::control_loops::superstructure::Status>(
85 "/superstructure")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070086 superstructure_goal_sender_(
87 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070088 ->MakeSender<::y2016::control_loops::superstructure::Goal>(
89 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070090 drivetrain_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 event_loop->MakeFetcher<::frc971::control_loops::drivetrain::Goal>(
92 "/drivetrain")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -070093 drivetrain_status_fetcher_(
94 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
96 "/drivetrain")),
Comran Morshed200dd4b2016-02-16 17:54:58 +000097 intake_goal_(0.0),
98 shoulder_goal_(M_PI / 2.0),
Austin Schuhd52df772016-03-19 15:38:41 -070099 wrist_goal_(0.0),
Austin Schuh1bf8a212019-05-26 22:13:14 -0700100 vision_align_action_factory_(
101 actors::VisionAlignActor::MakeFactory(event_loop)),
102 superstructure_action_factory_(
Austin Schuha250b2d2019-05-27 16:14:02 -0700103 actors::SuperstructureActor::MakeFactory(event_loop)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700104 set_vision_align_fn(
105 [this](const ::frc971::input::driver_station::Data &data) {
106 return VisionAlign(data);
107 });
Austin Schuha250b2d2019-05-27 16:14:02 -0700108 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000109
Austin Schuha250b2d2019-05-27 16:14:02 -0700110 // Returns true when we are vision aligning
James Kuszmaul7077d342021-06-09 20:23:58 -0700111 bool VisionAlign(const ::frc971::input::driver_station::Data &data) {
Austin Schuhadd6d792016-03-19 01:20:01 -0700112 vision_valid_ = false;
113
Austin Schuh1bf8a212019-05-26 22:13:14 -0700114 vision_status_fetcher_.Fetch();
Austin Schuhadd6d792016-03-19 01:20:01 -0700115
Austin Schuh1bf8a212019-05-26 22:13:14 -0700116 if (vision_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117 vision_valid_ = (vision_status_fetcher_->left_image_valid() &&
118 vision_status_fetcher_->right_image_valid());
119 last_angle_ = vision_status_fetcher_->horizontal_angle();
Austin Schuhadd6d792016-03-19 01:20:01 -0700120 }
121
Austin Schuh974cf652016-04-20 20:18:13 -0700122 if (data.IsPressed(kVisionAlign)) {
123 if (vision_valid_ && !vision_action_running_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700124 actors::vision_align_action::VisionAlignActionParamsT params;
Austin Schuha250b2d2019-05-27 16:14:02 -0700125 EnqueueAction(vision_align_action_factory_.Make(params));
Austin Schuh974cf652016-04-20 20:18:13 -0700126 vision_action_running_ = true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700127 AOS_LOG(INFO, "Starting vision align\n");
Austin Schuh974cf652016-04-20 20:18:13 -0700128 } else {
129 if (!vision_valid_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700130 AOS_LOG(INFO, "Vision align but not valid\n");
Austin Schuh974cf652016-04-20 20:18:13 -0700131 }
132 }
Austin Schuh18799112016-03-16 22:09:54 -0700133 }
134
135 if (data.NegEdge(kVisionAlign)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700136 CancelAllActions();
Austin Schuh18799112016-03-16 22:09:54 -0700137 }
Austin Schuhadd6d792016-03-19 01:20:01 -0700138 if (!data.IsPressed(kVisionAlign)) {
139 vision_action_running_ = false;
140 }
Austin Schuh18799112016-03-16 22:09:54 -0700141
Austin Schuha250b2d2019-05-27 16:14:02 -0700142 return vision_action_running_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000143 }
144
James Kuszmaul7077d342021-06-09 20:23:58 -0700145 void HandleTeleop(const ::frc971::input::driver_station::Data &data) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700146 if (!data.GetControlBit(ControlBit::kEnabled)) {
147 // If we are not enabled, reset the waiting for zero bit.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700148 AOS_LOG(DEBUG, "Waiting for zero.\n");
Austin Schuha250b2d2019-05-27 16:14:02 -0700149 waiting_for_zero_ = true;
150 }
151
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700152 float voltage_climber = 0.0;
Austin Schuh45d07f62016-03-13 15:33:31 -0700153 // Default the intake to up.
154 intake_goal_ = constants::Values::kIntakeRange.upper - 0.04;
155
156 bool force_lights_on = false;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000157 if (!data.GetControlBit(ControlBit::kEnabled)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700158 CancelAllActions();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700159 AOS_LOG(DEBUG, "Canceling\n");
Comran Morshed9a9948c2016-01-16 15:58:04 +0000160 }
161
Austin Schuh9481d0d2019-06-29 21:56:17 -0700162 superstructure_status_fetcher_.Fetch();
163 if (!superstructure_status_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700164 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000165 }
166
Austin Schuh9481d0d2019-06-29 21:56:17 -0700167 if (superstructure_status_fetcher_.get() &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168 superstructure_status_fetcher_->zeroed()) {
Comran Morshed200dd4b2016-02-16 17:54:58 +0000169 if (waiting_for_zero_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700170 AOS_LOG(DEBUG, "Zeroed! Starting teleop mode.\n");
Comran Morshed200dd4b2016-02-16 17:54:58 +0000171 waiting_for_zero_ = false;
172 }
173 } else {
174 waiting_for_zero_ = true;
175 }
176
Austin Schuhadd6d792016-03-19 01:20:01 -0700177 double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference;
178 bool use_slow_profile = false;
179 if (vision_action_running_) {
180 use_slow_profile = true;
181 if (vision_valid_) {
182 intake_when_shooting -= 0.5;
183 }
184 }
185
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700186 if (data.IsPressed(kFrontLong)) {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800187 // Forwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700188 shoulder_goal_ = M_PI / 2.0 + 0.1;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700189 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700190 drivetrain_status_fetcher_.Fetch();
191 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 wrist_goal_ += drivetrain_status_fetcher_->ground_angle();
Austin Schuhb0099642016-04-03 21:37:27 -0700193 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800194 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700195 intake_goal_ = intake_when_shooting;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800196 } else if (data.IsPressed(kBackLong)) {
197 // Backwards shot
Austin Schuh3d79cc02016-03-20 21:08:53 -0700198 shoulder_goal_ = M_PI / 2.0 - 0.4;
Austin Schuhe153c5a2016-04-20 20:18:42 -0700199 wrist_goal_ = -0.62 - 0.02;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700200 drivetrain_status_fetcher_.Fetch();
201 if (drivetrain_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 wrist_goal_ += drivetrain_status_fetcher_->ground_angle();
Austin Schuhb0099642016-04-03 21:37:27 -0700203 }
Austin Schuh4ea06c12016-03-12 17:54:31 -0800204 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700205 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700206 } else if (data.IsPressed(kBackFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700207 // Backwards shot no IMU
208 shoulder_goal_ = M_PI / 2.0 - 0.4;
209 wrist_goal_ = -0.62 - 0.02;
210 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700211 intake_goal_ = intake_when_shooting;
Austin Schuh45d07f62016-03-13 15:33:31 -0700212 } else if (data.IsPressed(kFrontFender)) {
Adam Snaidera3271fe2016-10-26 21:03:38 -0700213 // Forwards shot no IMU
Austin Schuh6a871ff2016-05-01 12:31:23 -0700214 shoulder_goal_ = M_PI / 2.0 + 0.1;
Adam Snaidera3271fe2016-10-26 21:03:38 -0700215 wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005;
Austin Schuh6a871ff2016-05-01 12:31:23 -0700216 shooter_velocity_ = 640.0;
Austin Schuhadd6d792016-03-19 01:20:01 -0700217 intake_goal_ = intake_when_shooting;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700218 } else if (data.IsPressed(kExpand) || data.IsPressed(kWinch)) {
219 // Set the goals to the hanging position so when the actor finishes, we
220 // will still be at the right spot.
221 shoulder_goal_ = 1.2;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700222 wrist_goal_ = 1.0;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700223 intake_goal_ = 0.0;
224 if (data.PosEdge(kExpand)) {
225 is_expanding_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700226 actors::superstructure_action::SuperstructureActionParamsT params;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700227 params.partial_angle = 0.3;
228 params.delay_time = 0.7;
229 params.full_angle = shoulder_goal_;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700230 params.shooter_angle = wrist_goal_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700231 EnqueueAction(superstructure_action_factory_.Make(params));
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700232 }
233 if (data.IsPressed(kWinch)) {
234 voltage_climber = 12.0;
235 }
Austin Schuhde802e92016-02-27 14:49:03 -0800236 } else {
Austin Schuh4ea06c12016-03-12 17:54:31 -0800237 wrist_goal_ = 0.0;
Austin Schuh3f0b1192016-03-12 13:03:56 -0800238 shoulder_goal_ = -0.010;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800239 shooter_velocity_ = 0.0;
Comran Morshed200dd4b2016-02-16 17:54:58 +0000240 }
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700241 if (data.NegEdge(kExpand) || voltage_climber > 1.0) {
242 is_expanding_ = false;
Austin Schuha250b2d2019-05-27 16:14:02 -0700243 CancelAllActions();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700244 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000245
Comran Morshedaa0573c2016-03-05 19:05:54 +0000246 bool ball_detected = false;
Austin Schuh4b652c92019-05-27 13:22:27 -0700247 ball_detector_fetcher_.Fetch();
248 if (ball_detector_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 ball_detected = ball_detector_fetcher_->voltage() > 2.5;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000250 }
251 if (data.PosEdge(kIntakeIn)) {
252 saw_ball_when_started_intaking_ = ball_detected;
253 }
254
Austin Schuh45d07f62016-03-13 15:33:31 -0700255 if (data.IsPressed(kIntakeIn)) {
256 is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_);
257 if (ball_detected) {
258 force_lights_on = true;
259 }
260 } else {
261 is_intaking_ = false;
262 }
263
Austin Schuhd52df772016-03-19 15:38:41 -0700264 fire_ = false;
Austin Schuh4ea06c12016-03-12 17:54:31 -0800265 if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) {
Austin Schuhd52df772016-03-19 15:38:41 -0700266 if (data.IsPressed(kVisionAlign)) {
267 // Make sure that we are lined up.
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700268 drivetrain_status_fetcher_.Fetch();
269 drivetrain_goal_fetcher_.Fetch();
270 if (drivetrain_status_fetcher_.get() &&
271 drivetrain_goal_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700272 const double left_goal = drivetrain_goal_fetcher_->left_goal();
273 const double right_goal = drivetrain_goal_fetcher_->right_goal();
Austin Schuhd52df772016-03-19 15:38:41 -0700274 const double left_current =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700275 drivetrain_status_fetcher_->estimated_left_position();
Austin Schuhd52df772016-03-19 15:38:41 -0700276 const double right_current =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 drivetrain_status_fetcher_->estimated_right_position();
Austin Schuhd52df772016-03-19 15:38:41 -0700278 const double left_velocity =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279 drivetrain_status_fetcher_->estimated_left_velocity();
Austin Schuhd52df772016-03-19 15:38:41 -0700280 const double right_velocity =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700281 drivetrain_status_fetcher_->estimated_right_velocity();
Austin Schuhd52df772016-03-19 15:38:41 -0700282 if (vision_action_running_ && ::std::abs(last_angle_) < 0.02 &&
283 ::std::abs((left_goal - right_goal) -
284 (left_current - right_current)) /
Austin Schuha250b2d2019-05-27 16:14:02 -0700285 dt_config().robot_radius / 2.0 <
Austin Schuhd52df772016-03-19 15:38:41 -0700286 0.02 &&
287 ::std::abs(left_velocity - right_velocity) < 0.01) {
288 ++ready_to_fire_;
289 } else {
290 ready_to_fire_ = 0;
291 }
Austin Schuh3d79cc02016-03-20 21:08:53 -0700292 if (ready_to_fire_ > 9) {
Austin Schuhd52df772016-03-19 15:38:41 -0700293 fire_ = true;
294 }
295 }
296 } else {
297 fire_ = true;
298 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000299 }
300
Austin Schuh4ea06c12016-03-12 17:54:31 -0800301 is_outtaking_ = data.IsPressed(kIntakeOut);
Austin Schuh5a6db4d2016-02-28 22:02:42 -0800302
Austin Schuhd52df772016-03-19 15:38:41 -0700303 if (is_intaking_ || is_outtaking_) {
304 recently_intaking_accumulator_ = 20;
305 }
306
307 if (data.IsPressed(kIntakeDown)) {
308 if (recently_intaking_accumulator_) {
309 intake_goal_ = 0.1;
310 } else {
311 intake_goal_ = -0.05;
312 }
313 }
314
315 if (recently_intaking_accumulator_ > 0) {
316 --recently_intaking_accumulator_;
317 }
318
Austin Schuh843412b2016-03-20 16:48:46 -0700319 if (data.IsPressed(kPortcullis)) {
320 traverse_unlatched_ = true;
321 traverse_down_ = true;
322 } else if (data.IsPressed(kChevalDeFrise)) {
323 traverse_unlatched_ = false;
324 traverse_down_ = true;
325 } else {
326 traverse_unlatched_ = true;
327 traverse_down_ = false;
328 }
329
Comran Morshed200dd4b2016-02-16 17:54:58 +0000330 if (!waiting_for_zero_) {
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700331 if (!is_expanding_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700332 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuh3f0b1192016-03-12 13:03:56 -0800333
Alex Perrycb7da4b2019-08-28 19:35:56 -0700334 ::y2016::control_loops::superstructure::Goal::Builder
335 superstructure_builder = builder.MakeBuilder<
336 ::y2016::control_loops::superstructure::Goal>();
337 superstructure_builder.add_angle_intake(intake_goal_);
338 superstructure_builder.add_angle_shoulder(shoulder_goal_);
339 superstructure_builder.add_angle_wrist(wrist_goal_);
340
341 superstructure_builder.add_max_angular_velocity_intake(7.0);
342 superstructure_builder.add_max_angular_velocity_shoulder(4.0);
343 superstructure_builder.add_max_angular_velocity_wrist(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700344 if (use_slow_profile) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 superstructure_builder.add_max_angular_acceleration_intake(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700346 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 superstructure_builder.add_max_angular_acceleration_intake(40.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700348 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700349 superstructure_builder.add_max_angular_acceleration_shoulder(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700350 if (shoulder_goal_ > 1.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351 superstructure_builder.add_max_angular_acceleration_wrist(45.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700352 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700353 superstructure_builder.add_max_angular_acceleration_wrist(25.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700354 }
Austin Schuh3f0b1192016-03-12 13:03:56 -0800355
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700356 // Granny mode
357 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -0700358 superstructure_builder.add_max_angular_velocity_intake(0.2);
359 superstructure_builder.add_max_angular_velocity_shoulder(0.2);
360 superstructure_builder.add_max_angular_velocity_wrist(0.2);
361 superstructure_builder.add_max_angular_acceleration_intake(1.0);
362 superstructure_builder.add_max_angular_acceleration_shoulder(1.0);
363 superstructure_builder.add_max_angular_acceleration_wrist(1.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700364 */
365 if (is_intaking_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 superstructure_builder.add_voltage_top_rollers(12.0);
367 superstructure_builder.add_voltage_bottom_rollers(12.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700368 } else if (is_outtaking_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700369 superstructure_builder.add_voltage_top_rollers(-12.0);
370 superstructure_builder.add_voltage_bottom_rollers(-7.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700371 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700372 superstructure_builder.add_voltage_top_rollers(0.0);
373 superstructure_builder.add_voltage_bottom_rollers(0.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700374 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000375
Alex Perrycb7da4b2019-08-28 19:35:56 -0700376 superstructure_builder.add_traverse_unlatched(traverse_unlatched_);
377 superstructure_builder.add_unfold_climber(false);
378 superstructure_builder.add_voltage_climber(voltage_climber);
379 superstructure_builder.add_traverse_down(traverse_down_);
380 superstructure_builder.add_force_intake(true);
Austin Schuh843412b2016-03-20 16:48:46 -0700381
milind1f1dca32021-07-03 13:50:07 -0700382 if (builder.Send(superstructure_builder.Finish()) !=
383 aos::RawSender::Error::kOk) {
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
milind1f1dca32021-07-03 13:50:07 -0700401 if (builder.Send(shooter_builder.Finish()) !=
402 aos::RawSender::Error::kOk) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700403 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
404 }
Comran Morshed200dd4b2016-02-16 17:54:58 +0000405 }
406 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000407 }
408
Comran Morshed9a9948c2016-01-16 15:58:04 +0000409 private:
Austin Schuh1bf8a212019-05-26 22:13:14 -0700410 ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700411 ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
James Kuszmaul7077d342021-06-09 20:23:58 -0700412 ::aos::Sender<::y2016::control_loops::shooter::Goal> 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
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800459} // namespace y2016::input::joysticks
Comran Morshed9a9948c2016-01-16 15:58:04 +0000460
Austin Schuh094d09b2020-11-20 23:26:52 -0800461int main(int argc, char **argv) {
462 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700463
Alex Perrycb7da4b2019-08-28 19:35:56 -0700464 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800465 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466
467 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800468 ::y2016::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700469
470 event_loop.Run();
471
Austin Schuhae87e312020-08-01 16:15:01 -0700472 return 0;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000473}