blob: f71aec3481dcb8c2800beed2de4c7370c017f739 [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
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include <google/protobuf/stubs/stringprintf.h>
9
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
Neil Balchacfca5b2018-01-28 14:04:08 -080041namespace y2018 {
42namespace input {
43namespace joysticks {
44
Austin Schuhab15c4d2018-03-09 21:21:03 -080045namespace arm = ::y2018::control_loops::superstructure::arm;
Austin Schuha8de4a62018-09-03 18:04:28 -070046using google::protobuf::StringPrintf;
Neil Balch07fee582018-01-27 15:46:49 -080047
Sabina Davis833ccf62018-04-06 20:52:31 -070048const ButtonLocation kIntakeClosed(3, 2);
49const ButtonLocation kDuck(3, 9);
50const ButtonLocation kSmallBox(3, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080051
Sabina Davis833ccf62018-04-06 20:52:31 -070052const ButtonLocation kIntakeIn(3, 4);
53const ButtonLocation kIntakeOut(3, 3);
Neil Balch07fee582018-01-27 15:46:49 -080054
Austin Schuha8de4a62018-09-03 18:04:28 -070055const ButtonLocation kArmBackHighBox(4, 11);
56const ButtonLocation kArmBackExtraHighBox(4, 1);
57const ButtonLocation kArmBackMiddle2Box(4, 9);
58const ButtonLocation kArmBackMiddle1Box(4, 7);
59const ButtonLocation kArmBackLowBox(4, 5);
60const ButtonLocation kArmBackSwitch(3, 7);
Austin Schuhab15c4d2018-03-09 21:21:03 -080061
Austin Schuha8de4a62018-09-03 18:04:28 -070062const ButtonLocation kArmFrontHighBox(4, 12);
63const ButtonLocation kArmFrontExtraHighBox(3, 14);
64const ButtonLocation kArmFrontMiddle2Box(4, 10);
65const ButtonLocation kArmFrontMiddle1Box(4, 8);
66const ButtonLocation kArmFrontLowBox(4, 6);
67const ButtonLocation kArmFrontSwitch(3, 10);
Austin Schuhab15c4d2018-03-09 21:21:03 -080068
Sabina Davis833ccf62018-04-06 20:52:31 -070069const ButtonLocation kArmAboveHang(3, 15);
70const ButtonLocation kArmBelowHang(3, 16);
Austin Schuh17e484e2018-03-11 01:11:36 -080071
Austin Schuhd76546a2018-07-08 16:05:14 -070072const ButtonLocation kEmergencyUp(3, 5);
73const ButtonLocation kEmergencyDown(3, 6);
74
Sabina Davis833ccf62018-04-06 20:52:31 -070075const ButtonLocation kWinch(4, 2);
Austin Schuh17e484e2018-03-11 01:11:36 -080076
Sabina Davis833ccf62018-04-06 20:52:31 -070077const ButtonLocation kArmNeutral(3, 8);
78const ButtonLocation kArmUp(3, 11);
Austin Schuhab15c4d2018-03-09 21:21:03 -080079
Sabina Davis833ccf62018-04-06 20:52:31 -070080const ButtonLocation kArmStepUp(3, 13);
81const ButtonLocation kArmStepDown(3, 12);
Austin Schuhab15c4d2018-03-09 21:21:03 -080082
Sabina Davis833ccf62018-04-06 20:52:31 -070083const ButtonLocation kArmPickupBoxFromIntake(4, 3);
84
85const ButtonLocation kClawOpen(4, 4);
Neil Balchba9cbba2018-04-06 22:26:38 -070086const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080087
James Kuszmaul7077d342021-06-09 20:23:58 -070088class Reader : public ::frc971::input::ActionJoystickInput {
Neil Balchacfca5b2018-01-28 14:04:08 -080089 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080090 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070091 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070092 event_loop,
93 ::y2018::control_loops::drivetrain::GetDrivetrainConfig(),
94 ::aos::network::GetTeamNumber() == 971
95 ? DrivetrainInputReader::InputType::kPistol
96 : DrivetrainInputReader::InputType::kSteeringWheel,
Austin Schuhd845c972019-06-29 21:20:05 -070097 {}),
98 superstructure_position_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -070099 event_loop
100 ->MakeFetcher<::y2018::control_loops::superstructure::Position>(
101 ".frc971.control_loops.superstructure_queue.position")),
Austin Schuhd845c972019-06-29 21:20:05 -0700102 superstructure_status_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -0700103 event_loop
104 ->MakeFetcher<::y2018::control_loops::superstructure::Status>(
105 ".frc971.control_loops.superstructure_queue.status")),
Austin Schuhd845c972019-06-29 21:20:05 -0700106 superstructure_goal_sender_(
107 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108 ->MakeSender<::y2018::control_loops::superstructure::Goal>(
Austin Schuhd845c972019-06-29 21:20:05 -0700109 ".frc971.control_loops.superstructure_queue.goal")) {
Austin Schuha8de4a62018-09-03 18:04:28 -0700110 const uint16_t team = ::aos::network::GetTeamNumber();
111
Austin Schuha8de4a62018-09-03 18:04:28 -0700112 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
113 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Neil Balchacfca5b2018-01-28 14:04:08 -0800114 }
115
James Kuszmaul7077d342021-06-09 20:23:58 -0700116 void HandleTeleop(
117 const ::frc971::input::driver_station::Data &data) override {
Austin Schuhd845c972019-06-29 21:20:05 -0700118 superstructure_position_fetcher_.Fetch();
119 superstructure_status_fetcher_.Fetch();
120 if (!superstructure_status_fetcher_.get() ||
121 !superstructure_position_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Neil Balch07fee582018-01-27 15:46:49 -0800123 return;
124 }
125
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 auto builder = superstructure_goal_sender_.MakeBuilder();
Neil Balch07fee582018-01-27 15:46:49 -0800127
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128 double roller_voltage = 0.0;
129 bool trajectory_override = false;
Neil Balch07fee582018-01-27 15:46:49 -0800130
Austin Schuh8d5fff42018-05-30 20:44:12 -0700131 if (data.PosEdge(kIntakeIn) || data.PosEdge(kSmallBox) ||
132 data.PosEdge(kIntakeClosed)) {
133 vision_control_.set_high_video(false);
134 }
135
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700136 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800137 // Turn on the rollers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800139 } else if (data.IsPressed(kIntakeOut)) {
140 // Turn off the rollers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800142 } else {
143 // We don't want the rollers on.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144 roller_voltage = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800145 }
146
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700147 if (data.IsPressed(kSmallBox)) {
148 // Deploy the intake.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149 if (superstructure_position_fetcher_->box_back_beambreak_triggered()) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700150 intake_goal_ = 0.30;
151 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 if (roller_voltage > 0.1 &&
153 superstructure_position_fetcher_->box_distance() < 0.15) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700154 intake_goal_ = 0.18;
155 } else {
156 intake_goal_ = -0.60;
157 }
158 }
159 } else if (data.IsPressed(kIntakeClosed)) {
160 // Deploy the intake.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 if (superstructure_position_fetcher_->box_back_beambreak_triggered()) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700162 intake_goal_ = 0.30;
163 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 if (roller_voltage > 0.1) {
165 if (superstructure_position_fetcher_->box_distance() < 0.10) {
166 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700167 intake_goal_ = 0.22;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168 } else if (superstructure_position_fetcher_->box_distance() < 0.17) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700169 intake_goal_ = 0.13;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170 } else if (superstructure_position_fetcher_->box_distance() < 0.25) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700171 intake_goal_ = 0.05;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700172 } else {
Neil Balchba9cbba2018-04-06 22:26:38 -0700173 intake_goal_ = -0.10;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700174 }
Austin Schuha250b2d2019-05-27 16:14:02 -0700175 if (robot_velocity() < -0.1 &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 superstructure_position_fetcher_->box_distance() > 0.15) {
Austin Schuhcf96d322018-04-07 15:52:31 -0700177 intake_goal_ += 0.10;
178 }
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700179 } else {
180 intake_goal_ = -0.60;
181 }
182 }
183 } else {
184 // Bring in the intake.
Austin Schuhcf96d322018-04-07 15:52:31 -0700185 intake_goal_ = -3.20;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700186 }
187
James Kuszmaul7077d342021-06-09 20:23:58 -0700188 if (roller_voltage > 0.1 && intake_goal_ > 0.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 if (superstructure_position_fetcher_->box_distance() < 0.10) {
190 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700191 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 roller_voltage += 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700193 }
194
Austin Schuhb874fd32018-03-05 00:27:10 -0800195 // If we are disabled, stay at the node closest to where we start. This
196 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700197 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700198 arm_goal_position_ =
199 superstructure_status_fetcher_->arm()->current_node();
Neil Balchba9cbba2018-04-06 22:26:38 -0700200 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800201 }
202
Austin Schuhab15c4d2018-03-09 21:21:03 -0800203 bool grab_box = false;
204 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800205 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700206 }
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700207 const bool near_goal =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208 superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhd845c972019-06-29 21:20:05 -0700209 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210 superstructure_status_fetcher_->arm()->path_distance_to_go() < 1e-3;
Austin Schuh822a1e52018-04-06 22:50:21 -0700211 if (data.IsPressed(kArmStepDown) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700212 uint32_t *front_point = ::std::find(
213 front_points_.begin(), front_points_.end(), arm_goal_position_);
214 uint32_t *back_point = ::std::find(
215 back_points_.begin(), back_points_.end(), arm_goal_position_);
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700216 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700217 ++front_point;
218 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700219 arm_goal_position_ = *front_point;
220 }
221 } else if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700222 ++back_point;
223 if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700224 arm_goal_position_ = *back_point;
225 }
226 }
Austin Schuh822a1e52018-04-06 22:50:21 -0700227 } else if (data.IsPressed(kArmStepUp) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700228 const uint32_t *front_point = ::std::find(
229 front_points_.begin(), front_points_.end(), arm_goal_position_);
230 const uint32_t *back_point = ::std::find(
231 back_points_.begin(), back_points_.end(), arm_goal_position_);
232 if (front_point != front_points_.end()) {
233 if (front_point != front_points_.begin()) {
234 --front_point;
235 arm_goal_position_ = *front_point;
236 }
237 } else if (back_point != back_points_.end()) {
238 if (back_point != back_points_.begin()) {
239 --back_point;
240 arm_goal_position_ = *back_point;
241 }
242 }
243 } else if (data.PosEdge(kArmPickupBoxFromIntake)) {
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::NeutralIndex();
246 } else if (data.IsPressed(kDuck)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700247 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700248 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800249 } else if (data.IsPressed(kArmNeutral)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700250 vision_control_.set_high_video(false);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800251 arm_goal_position_ = arm::NeutralIndex();
252 } else if (data.IsPressed(kArmUp)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700253 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800254 arm_goal_position_ = arm::UpIndex();
255 } else if (data.IsPressed(kArmFrontSwitch)) {
256 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700257 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
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::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700260 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700261 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800262 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700263 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700264 vision_control_.set_high_video(true);
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700265 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800266 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
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::FrontMiddle1BoxIndex();
269 } else if (data.IsPressed(kArmFrontLowBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700270 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800271 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700272 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800273 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700274 } else if (data.IsPressed(kArmBackHighBox) ||
275 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800276 arm_goal_position_ = arm::BackMiddle2BoxIndex();
277 } else if (data.IsPressed(kArmBackMiddle1Box)) {
278 arm_goal_position_ = arm::BackMiddle1BoxIndex();
279 } else if (data.IsPressed(kArmBackLowBox)) {
280 arm_goal_position_ = arm::BackLowBoxIndex();
Austin Schuhd845c972019-06-29 21:20:05 -0700281 } else if (data.IsPressed(kArmBackSwitch)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800282 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800283 } else if (data.IsPressed(kArmAboveHang)) {
284 if (data.IsPressed(kIntakeIn)) {
285 arm_goal_position_ = arm::SelfHangIndex();
286 } else if (data.IsPressed(kIntakeOut)) {
287 arm_goal_position_ = arm::PartnerHangIndex();
288 } else {
289 arm_goal_position_ = arm::AboveHangIndex();
290 }
291 } else if (data.IsPressed(kArmBelowHang)) {
292 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800293 }
294
Austin Schuhd76546a2018-07-08 16:05:14 -0700295 if (data.IsPressed(kEmergencyDown)) {
296 arm_goal_position_ = arm::NeutralIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700298 } else if (data.IsPressed(kEmergencyUp)) {
299 arm_goal_position_ = arm::UpIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700301 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 trajectory_override = false;
Austin Schuhd76546a2018-07-08 16:05:14 -0700303 }
304
Alex Perrycb7da4b2019-08-28 19:35:56 -0700305 const bool deploy_fork =
Austin Schuh17e484e2018-03-11 01:11:36 -0800306 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
307
Alex Perrycb7da4b2019-08-28 19:35:56 -0700308 if (deploy_fork) {
Austin Schuh17e484e2018-03-11 01:11:36 -0800309 intake_goal_ = -2.0;
310 }
311
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312 control_loops::superstructure::IntakeGoal::Builder intake_goal_builder =
313 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
314
315 intake_goal_builder.add_left_intake_angle(intake_goal_);
316 intake_goal_builder.add_right_intake_angle(intake_goal_);
317 intake_goal_builder.add_roller_voltage(roller_voltage);
318
319 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
320 intake_goal_offset = intake_goal_builder.Finish();
321
322 control_loops::superstructure::Goal::Builder superstructure_builder =
323 builder.MakeBuilder<control_loops::superstructure::Goal>();
324
325 superstructure_builder.add_intake(intake_goal_offset);
326 superstructure_builder.add_grab_box(grab_box);
327 superstructure_builder.add_trajectory_override(trajectory_override);
328 superstructure_builder.add_deploy_fork(deploy_fork);
329
Austin Schuh17e484e2018-03-11 01:11:36 -0800330 if (data.IsPressed(kWinch)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700331 AOS_LOG(INFO, "Winching\n");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700332 superstructure_builder.add_voltage_winch(12.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800333 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700334 superstructure_builder.add_voltage_winch(0.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800335 }
336
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 superstructure_builder.add_hook_release(data.IsPressed(kArmBelowHang));
Austin Schuh17e484e2018-03-11 01:11:36 -0800338
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 superstructure_builder.add_arm_goal_position(arm_goal_position_);
Austin Schuhd76546a2018-07-08 16:05:14 -0700340 if (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700341 superstructure_builder.add_open_threshold(0.35);
Austin Schuhd76546a2018-07-08 16:05:14 -0700342 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700343 superstructure_builder.add_open_threshold(0.0);
Austin Schuhd76546a2018-07-08 16:05:14 -0700344 }
Austin Schuhcb091712018-02-21 20:01:55 -0800345
Neil Balchba9cbba2018-04-06 22:26:38 -0700346 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
Austin Schuhd76546a2018-07-08 16:05:14 -0700347 data.PosEdge(kArmPickupBoxFromIntake) ||
348 (data.IsPressed(kClawOpen) &&
349 (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)))) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700350 superstructure_builder.add_open_claw(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800351 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700352 superstructure_builder.add_open_claw(false);
Neil Balch07fee582018-01-27 15:46:49 -0800353 }
354
milind1f1dca32021-07-03 13:50:07 -0700355 if (builder.Send(superstructure_builder.Finish()) !=
356 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700357 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Neil Balch07fee582018-01-27 15:46:49 -0800358 }
Austin Schuh8d5fff42018-05-30 20:44:12 -0700359
Austin Schuha8de4a62018-09-03 18:04:28 -0700360 video_tx_->Send(vision_control_);
Neil Balchacfca5b2018-01-28 14:04:08 -0800361 }
362
363 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700364 uint32_t GetAutonomousMode() override {
Austin Schuh3e45c752019-02-02 12:19:11 -0800365 // Low bit is switch, high bit is scale. 1 means left, 0 means right.
Austin Schuha250b2d2019-05-27 16:14:02 -0700366 return mode();
Neil Balchacfca5b2018-01-28 14:04:08 -0800367 }
368
Alex Perrycb7da4b2019-08-28 19:35:56 -0700369 ::aos::Fetcher<control_loops::superstructure::Position>
Austin Schuhd845c972019-06-29 21:20:05 -0700370 superstructure_position_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 ::aos::Fetcher<control_loops::superstructure::Status>
Austin Schuhd845c972019-06-29 21:20:05 -0700372 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700373 ::aos::Sender<control_loops::superstructure::Goal>
Austin Schuhd845c972019-06-29 21:20:05 -0700374 superstructure_goal_sender_;
375
Neil Balch07fee582018-01-27 15:46:49 -0800376 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800377 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800378
Neil Balchba9cbba2018-04-06 22:26:38 -0700379 bool never_disabled_ = true;
Neil Balchacfca5b2018-01-28 14:04:08 -0800380
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700381 uint32_t arm_goal_position_ = 0;
Austin Schuh8d5fff42018-05-30 20:44:12 -0700382 VisionControl vision_control_;
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700383
384 decltype(FrontPoints()) front_points_ = FrontPoints();
385 decltype(BackPoints()) back_points_ = BackPoints();
Austin Schuhcb091712018-02-21 20:01:55 -0800386
Austin Schuha8de4a62018-09-03 18:04:28 -0700387 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Neil Balchacfca5b2018-01-28 14:04:08 -0800388};
389
390} // namespace joysticks
391} // namespace input
392} // namespace y2018
393
Austin Schuh094d09b2020-11-20 23:26:52 -0800394int main(int argc, char **argv) {
395 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700396
Alex Perrycb7da4b2019-08-28 19:35:56 -0700397 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800398 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700399
400 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800401 ::y2018::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700402
403 event_loop.Run();
404
Austin Schuhae87e312020-08-01 16:15:01 -0700405 return 0;
Neil Balchacfca5b2018-01-28 14:04:08 -0800406}