blob: 351d1f58b02bddf075fc97684b67429820ba877a [file] [log] [blame]
Neil Balchacfca5b2018-01-28 14:04:08 -08001#include <unistd.h>
James Kuszmaul7077d342021-06-09 20:23:58 -07002
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
4#include <cstdio>
5#include <cstring>
Austin Schuh8d5fff42018-05-30 20:44:12 -07006#include <mutex>
Neil Balchacfca5b2018-01-28 14:04:08 -08007
Adam Snaider13d48d92023-08-03 12:20:15 -07008#include "absl/strings/str_format.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07009
John Park33858a32018-09-28 23:05:48 -070010#include "aos/actions/actions.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070012#include "aos/logging/logging.h"
13#include "aos/network/team_number.h"
14#include "aos/stl_mutex/stl_mutex.h"
15#include "aos/time/time.h"
16#include "aos/util/log_interval.h"
Austin Schuh8d5fff42018-05-30 20:44:12 -070017#include "aos/vision/events/udp.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080018#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070019#include "frc971/input/action_joystick_input.h"
20#include "frc971/input/driver_station_data.h"
21#include "frc971/input/drivetrain_input.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080022#include "y2018/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhab15c4d2018-03-09 21:21:03 -080023#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "y2018/control_loops/superstructure/superstructure_goal_generated.h"
25#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
26#include "y2018/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuh8d5fff42018-05-30 20:44:12 -070027#include "y2018/vision.pb.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080028
Austin Schuh8d5fff42018-05-30 20:44:12 -070029using ::aos::monotonic_clock;
Austin Schuh8d5fff42018-05-30 20:44:12 -070030using ::aos::events::ProtoTXUdpSocket;
31using ::aos::events::RXUdpSocket;
James Kuszmaul7077d342021-06-09 20:23:58 -070032using ::frc971::input::DrivetrainInputReader;
33using ::frc971::input::driver_station::ButtonLocation;
34using ::frc971::input::driver_station::ControlBit;
35using ::frc971::input::driver_station::JoystickAxis;
36using ::frc971::input::driver_station::POVLocation;
Neil Balchacfca5b2018-01-28 14:04:08 -080037
Austin Schuh60cdb3e2018-04-06 21:52:32 -070038using ::y2018::control_loops::superstructure::arm::BackPoints;
James Kuszmaul7077d342021-06-09 20:23:58 -070039using ::y2018::control_loops::superstructure::arm::FrontPoints;
Austin Schuh60cdb3e2018-04-06 21:52:32 -070040
Stephan Pleinesf63bde82024-01-13 15:59:33 -080041namespace y2018::input::joysticks {
Neil Balchacfca5b2018-01-28 14:04:08 -080042
Austin Schuhab15c4d2018-03-09 21:21:03 -080043namespace arm = ::y2018::control_loops::superstructure::arm;
Neil Balch07fee582018-01-27 15:46:49 -080044
Sabina Davis833ccf62018-04-06 20:52:31 -070045const ButtonLocation kIntakeClosed(3, 2);
46const ButtonLocation kDuck(3, 9);
47const ButtonLocation kSmallBox(3, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080048
Sabina Davis833ccf62018-04-06 20:52:31 -070049const ButtonLocation kIntakeIn(3, 4);
50const ButtonLocation kIntakeOut(3, 3);
Neil Balch07fee582018-01-27 15:46:49 -080051
Austin Schuha8de4a62018-09-03 18:04:28 -070052const ButtonLocation kArmBackHighBox(4, 11);
53const ButtonLocation kArmBackExtraHighBox(4, 1);
54const ButtonLocation kArmBackMiddle2Box(4, 9);
55const ButtonLocation kArmBackMiddle1Box(4, 7);
56const ButtonLocation kArmBackLowBox(4, 5);
57const ButtonLocation kArmBackSwitch(3, 7);
Austin Schuhab15c4d2018-03-09 21:21:03 -080058
Austin Schuha8de4a62018-09-03 18:04:28 -070059const ButtonLocation kArmFrontHighBox(4, 12);
60const ButtonLocation kArmFrontExtraHighBox(3, 14);
61const ButtonLocation kArmFrontMiddle2Box(4, 10);
62const ButtonLocation kArmFrontMiddle1Box(4, 8);
63const ButtonLocation kArmFrontLowBox(4, 6);
64const ButtonLocation kArmFrontSwitch(3, 10);
Austin Schuhab15c4d2018-03-09 21:21:03 -080065
Sabina Davis833ccf62018-04-06 20:52:31 -070066const ButtonLocation kArmAboveHang(3, 15);
67const ButtonLocation kArmBelowHang(3, 16);
Austin Schuh17e484e2018-03-11 01:11:36 -080068
Austin Schuhd76546a2018-07-08 16:05:14 -070069const ButtonLocation kEmergencyUp(3, 5);
70const ButtonLocation kEmergencyDown(3, 6);
71
Sabina Davis833ccf62018-04-06 20:52:31 -070072const ButtonLocation kWinch(4, 2);
Austin Schuh17e484e2018-03-11 01:11:36 -080073
Sabina Davis833ccf62018-04-06 20:52:31 -070074const ButtonLocation kArmNeutral(3, 8);
75const ButtonLocation kArmUp(3, 11);
Austin Schuhab15c4d2018-03-09 21:21:03 -080076
Sabina Davis833ccf62018-04-06 20:52:31 -070077const ButtonLocation kArmStepUp(3, 13);
78const ButtonLocation kArmStepDown(3, 12);
Austin Schuhab15c4d2018-03-09 21:21:03 -080079
Sabina Davis833ccf62018-04-06 20:52:31 -070080const ButtonLocation kArmPickupBoxFromIntake(4, 3);
81
82const ButtonLocation kClawOpen(4, 4);
Neil Balchba9cbba2018-04-06 22:26:38 -070083const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080084
James Kuszmaul7077d342021-06-09 20:23:58 -070085class Reader : public ::frc971::input::ActionJoystickInput {
Neil Balchacfca5b2018-01-28 14:04:08 -080086 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080087 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070088 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070089 event_loop,
90 ::y2018::control_loops::drivetrain::GetDrivetrainConfig(),
91 ::aos::network::GetTeamNumber() == 971
92 ? DrivetrainInputReader::InputType::kPistol
93 : DrivetrainInputReader::InputType::kSteeringWheel,
Austin Schuhd845c972019-06-29 21:20:05 -070094 {}),
95 superstructure_position_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -070096 event_loop
97 ->MakeFetcher<::y2018::control_loops::superstructure::Position>(
98 ".frc971.control_loops.superstructure_queue.position")),
Austin Schuhd845c972019-06-29 21:20:05 -070099 superstructure_status_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -0700100 event_loop
101 ->MakeFetcher<::y2018::control_loops::superstructure::Status>(
102 ".frc971.control_loops.superstructure_queue.status")),
Austin Schuhd845c972019-06-29 21:20:05 -0700103 superstructure_goal_sender_(
104 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 ->MakeSender<::y2018::control_loops::superstructure::Goal>(
Austin Schuhd845c972019-06-29 21:20:05 -0700106 ".frc971.control_loops.superstructure_queue.goal")) {
Austin Schuha8de4a62018-09-03 18:04:28 -0700107 const uint16_t team = ::aos::network::GetTeamNumber();
108
Austin Schuha8de4a62018-09-03 18:04:28 -0700109 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
Adam Snaider13d48d92023-08-03 12:20:15 -0700110 absl::StrFormat("10.%d.%d.179", team / 100, team % 100), 5000));
Neil Balchacfca5b2018-01-28 14:04:08 -0800111 }
112
James Kuszmaul7077d342021-06-09 20:23:58 -0700113 void HandleTeleop(
114 const ::frc971::input::driver_station::Data &data) override {
Austin Schuhd845c972019-06-29 21:20:05 -0700115 superstructure_position_fetcher_.Fetch();
116 superstructure_status_fetcher_.Fetch();
117 if (!superstructure_status_fetcher_.get() ||
118 !superstructure_position_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Neil Balch07fee582018-01-27 15:46:49 -0800120 return;
121 }
122
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 auto builder = superstructure_goal_sender_.MakeBuilder();
Neil Balch07fee582018-01-27 15:46:49 -0800124
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125 double roller_voltage = 0.0;
126 bool trajectory_override = false;
Neil Balch07fee582018-01-27 15:46:49 -0800127
Austin Schuh8d5fff42018-05-30 20:44:12 -0700128 if (data.PosEdge(kIntakeIn) || data.PosEdge(kSmallBox) ||
129 data.PosEdge(kIntakeClosed)) {
130 vision_control_.set_high_video(false);
131 }
132
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700133 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800134 // Turn on the rollers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700135 roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800136 } else if (data.IsPressed(kIntakeOut)) {
137 // Turn off the rollers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800139 } else {
140 // We don't want the rollers on.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 roller_voltage = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800142 }
143
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700144 if (data.IsPressed(kSmallBox)) {
145 // Deploy the intake.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146 if (superstructure_position_fetcher_->box_back_beambreak_triggered()) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700147 intake_goal_ = 0.30;
148 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149 if (roller_voltage > 0.1 &&
150 superstructure_position_fetcher_->box_distance() < 0.15) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700151 intake_goal_ = 0.18;
152 } else {
153 intake_goal_ = -0.60;
154 }
155 }
156 } else if (data.IsPressed(kIntakeClosed)) {
157 // Deploy the intake.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158 if (superstructure_position_fetcher_->box_back_beambreak_triggered()) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700159 intake_goal_ = 0.30;
160 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 if (roller_voltage > 0.1) {
162 if (superstructure_position_fetcher_->box_distance() < 0.10) {
163 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700164 intake_goal_ = 0.22;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165 } else if (superstructure_position_fetcher_->box_distance() < 0.17) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700166 intake_goal_ = 0.13;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167 } else if (superstructure_position_fetcher_->box_distance() < 0.25) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700168 intake_goal_ = 0.05;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700169 } else {
Neil Balchba9cbba2018-04-06 22:26:38 -0700170 intake_goal_ = -0.10;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700171 }
Austin Schuha250b2d2019-05-27 16:14:02 -0700172 if (robot_velocity() < -0.1 &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 superstructure_position_fetcher_->box_distance() > 0.15) {
Austin Schuhcf96d322018-04-07 15:52:31 -0700174 intake_goal_ += 0.10;
175 }
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700176 } else {
177 intake_goal_ = -0.60;
178 }
179 }
180 } else {
181 // Bring in the intake.
Austin Schuhcf96d322018-04-07 15:52:31 -0700182 intake_goal_ = -3.20;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700183 }
184
James Kuszmaul7077d342021-06-09 20:23:58 -0700185 if (roller_voltage > 0.1 && intake_goal_ > 0.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186 if (superstructure_position_fetcher_->box_distance() < 0.10) {
187 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700188 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 roller_voltage += 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700190 }
191
Austin Schuhb874fd32018-03-05 00:27:10 -0800192 // If we are disabled, stay at the node closest to where we start. This
193 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700194 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 arm_goal_position_ =
196 superstructure_status_fetcher_->arm()->current_node();
Neil Balchba9cbba2018-04-06 22:26:38 -0700197 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800198 }
199
Austin Schuhab15c4d2018-03-09 21:21:03 -0800200 bool grab_box = false;
201 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800202 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700203 }
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700204 const bool near_goal =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205 superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhd845c972019-06-29 21:20:05 -0700206 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 superstructure_status_fetcher_->arm()->path_distance_to_go() < 1e-3;
Austin Schuh822a1e52018-04-06 22:50:21 -0700208 if (data.IsPressed(kArmStepDown) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700209 uint32_t *front_point = ::std::find(
210 front_points_.begin(), front_points_.end(), arm_goal_position_);
211 uint32_t *back_point = ::std::find(
212 back_points_.begin(), back_points_.end(), arm_goal_position_);
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700213 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700214 ++front_point;
215 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700216 arm_goal_position_ = *front_point;
217 }
218 } else if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700219 ++back_point;
220 if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700221 arm_goal_position_ = *back_point;
222 }
223 }
Austin Schuh822a1e52018-04-06 22:50:21 -0700224 } else if (data.IsPressed(kArmStepUp) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700225 const uint32_t *front_point = ::std::find(
226 front_points_.begin(), front_points_.end(), arm_goal_position_);
227 const uint32_t *back_point = ::std::find(
228 back_points_.begin(), back_points_.end(), arm_goal_position_);
229 if (front_point != front_points_.end()) {
230 if (front_point != front_points_.begin()) {
231 --front_point;
232 arm_goal_position_ = *front_point;
233 }
234 } else if (back_point != back_points_.end()) {
235 if (back_point != back_points_.begin()) {
236 --back_point;
237 arm_goal_position_ = *back_point;
238 }
239 }
240 } else if (data.PosEdge(kArmPickupBoxFromIntake)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700241 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700242 arm_goal_position_ = arm::NeutralIndex();
243 } else if (data.IsPressed(kDuck)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700244 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700245 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800246 } else if (data.IsPressed(kArmNeutral)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700247 vision_control_.set_high_video(false);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800248 arm_goal_position_ = arm::NeutralIndex();
249 } else if (data.IsPressed(kArmUp)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700250 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800251 arm_goal_position_ = arm::UpIndex();
252 } else if (data.IsPressed(kArmFrontSwitch)) {
253 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700254 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700255 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800256 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700257 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700258 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800259 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700260 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700261 vision_control_.set_high_video(true);
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700262 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800263 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700264 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800265 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
266 } else if (data.IsPressed(kArmFrontLowBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700267 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800268 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700269 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800270 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700271 } else if (data.IsPressed(kArmBackHighBox) ||
272 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800273 arm_goal_position_ = arm::BackMiddle2BoxIndex();
274 } else if (data.IsPressed(kArmBackMiddle1Box)) {
275 arm_goal_position_ = arm::BackMiddle1BoxIndex();
276 } else if (data.IsPressed(kArmBackLowBox)) {
277 arm_goal_position_ = arm::BackLowBoxIndex();
Austin Schuhd845c972019-06-29 21:20:05 -0700278 } else if (data.IsPressed(kArmBackSwitch)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800279 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800280 } else if (data.IsPressed(kArmAboveHang)) {
281 if (data.IsPressed(kIntakeIn)) {
282 arm_goal_position_ = arm::SelfHangIndex();
283 } else if (data.IsPressed(kIntakeOut)) {
284 arm_goal_position_ = arm::PartnerHangIndex();
285 } else {
286 arm_goal_position_ = arm::AboveHangIndex();
287 }
288 } else if (data.IsPressed(kArmBelowHang)) {
289 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800290 }
291
Austin Schuhd76546a2018-07-08 16:05:14 -0700292 if (data.IsPressed(kEmergencyDown)) {
293 arm_goal_position_ = arm::NeutralIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700295 } else if (data.IsPressed(kEmergencyUp)) {
296 arm_goal_position_ = arm::UpIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700298 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 trajectory_override = false;
Austin Schuhd76546a2018-07-08 16:05:14 -0700300 }
301
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 const bool deploy_fork =
Austin Schuh17e484e2018-03-11 01:11:36 -0800303 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
304
Alex Perrycb7da4b2019-08-28 19:35:56 -0700305 if (deploy_fork) {
Austin Schuh17e484e2018-03-11 01:11:36 -0800306 intake_goal_ = -2.0;
307 }
308
Alex Perrycb7da4b2019-08-28 19:35:56 -0700309 control_loops::superstructure::IntakeGoal::Builder intake_goal_builder =
310 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
311
312 intake_goal_builder.add_left_intake_angle(intake_goal_);
313 intake_goal_builder.add_right_intake_angle(intake_goal_);
314 intake_goal_builder.add_roller_voltage(roller_voltage);
315
316 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
317 intake_goal_offset = intake_goal_builder.Finish();
318
319 control_loops::superstructure::Goal::Builder superstructure_builder =
320 builder.MakeBuilder<control_loops::superstructure::Goal>();
321
322 superstructure_builder.add_intake(intake_goal_offset);
323 superstructure_builder.add_grab_box(grab_box);
324 superstructure_builder.add_trajectory_override(trajectory_override);
325 superstructure_builder.add_deploy_fork(deploy_fork);
326
Austin Schuh17e484e2018-03-11 01:11:36 -0800327 if (data.IsPressed(kWinch)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700328 AOS_LOG(INFO, "Winching\n");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329 superstructure_builder.add_voltage_winch(12.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800330 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700331 superstructure_builder.add_voltage_winch(0.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800332 }
333
Alex Perrycb7da4b2019-08-28 19:35:56 -0700334 superstructure_builder.add_hook_release(data.IsPressed(kArmBelowHang));
Austin Schuh17e484e2018-03-11 01:11:36 -0800335
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 superstructure_builder.add_arm_goal_position(arm_goal_position_);
Austin Schuhd76546a2018-07-08 16:05:14 -0700337 if (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 superstructure_builder.add_open_threshold(0.35);
Austin Schuhd76546a2018-07-08 16:05:14 -0700339 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700340 superstructure_builder.add_open_threshold(0.0);
Austin Schuhd76546a2018-07-08 16:05:14 -0700341 }
Austin Schuhcb091712018-02-21 20:01:55 -0800342
Neil Balchba9cbba2018-04-06 22:26:38 -0700343 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
Austin Schuhd76546a2018-07-08 16:05:14 -0700344 data.PosEdge(kArmPickupBoxFromIntake) ||
345 (data.IsPressed(kClawOpen) &&
346 (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)))) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 superstructure_builder.add_open_claw(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800348 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700349 superstructure_builder.add_open_claw(false);
Neil Balch07fee582018-01-27 15:46:49 -0800350 }
351
milind1f1dca32021-07-03 13:50:07 -0700352 if (builder.Send(superstructure_builder.Finish()) !=
353 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700354 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Neil Balch07fee582018-01-27 15:46:49 -0800355 }
Austin Schuh8d5fff42018-05-30 20:44:12 -0700356
Austin Schuha8de4a62018-09-03 18:04:28 -0700357 video_tx_->Send(vision_control_);
Neil Balchacfca5b2018-01-28 14:04:08 -0800358 }
359
360 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700361 uint32_t GetAutonomousMode() override {
Austin Schuh3e45c752019-02-02 12:19:11 -0800362 // Low bit is switch, high bit is scale. 1 means left, 0 means right.
Austin Schuha250b2d2019-05-27 16:14:02 -0700363 return mode();
Neil Balchacfca5b2018-01-28 14:04:08 -0800364 }
365
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 ::aos::Fetcher<control_loops::superstructure::Position>
Austin Schuhd845c972019-06-29 21:20:05 -0700367 superstructure_position_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 ::aos::Fetcher<control_loops::superstructure::Status>
Austin Schuhd845c972019-06-29 21:20:05 -0700369 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370 ::aos::Sender<control_loops::superstructure::Goal>
Austin Schuhd845c972019-06-29 21:20:05 -0700371 superstructure_goal_sender_;
372
Neil Balch07fee582018-01-27 15:46:49 -0800373 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800374 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800375
Neil Balchba9cbba2018-04-06 22:26:38 -0700376 bool never_disabled_ = true;
Neil Balchacfca5b2018-01-28 14:04:08 -0800377
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700378 uint32_t arm_goal_position_ = 0;
Austin Schuh8d5fff42018-05-30 20:44:12 -0700379 VisionControl vision_control_;
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700380
381 decltype(FrontPoints()) front_points_ = FrontPoints();
382 decltype(BackPoints()) back_points_ = BackPoints();
Austin Schuhcb091712018-02-21 20:01:55 -0800383
Austin Schuha8de4a62018-09-03 18:04:28 -0700384 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Neil Balchacfca5b2018-01-28 14:04:08 -0800385};
386
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800387} // namespace y2018::input::joysticks
Neil Balchacfca5b2018-01-28 14:04:08 -0800388
Austin Schuh094d09b2020-11-20 23:26:52 -0800389int main(int argc, char **argv) {
390 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700391
Alex Perrycb7da4b2019-08-28 19:35:56 -0700392 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800393 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700394
395 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800396 ::y2018::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700397
398 event_loop.Run();
399
Austin Schuhae87e312020-08-01 16:15:01 -0700400 return 0;
Neil Balchacfca5b2018-01-28 14:04:08 -0800401}