blob: be7be1feaf95dd5162f0681425b7029226418a89 [file] [log] [blame]
James Kuszmaul7077d342021-06-09 20:23:58 -07001#include <google/protobuf/stubs/stringprintf.h>
Neil Balchacfca5b2018-01-28 14:04:08 -08002#include <unistd.h>
James Kuszmaul7077d342021-06-09 20:23:58 -07003
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cmath>
5#include <cstdio>
6#include <cstring>
Austin Schuh8d5fff42018-05-30 20:44:12 -07007#include <mutex>
Neil Balchacfca5b2018-01-28 14:04:08 -08008
John Park33858a32018-09-28 23:05:48 -07009#include "aos/actions/actions.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/logging/logging.h"
12#include "aos/network/team_number.h"
13#include "aos/stl_mutex/stl_mutex.h"
14#include "aos/time/time.h"
15#include "aos/util/log_interval.h"
Austin Schuh8d5fff42018-05-30 20:44:12 -070016#include "aos/vision/events/udp.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080017#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070018#include "frc971/input/action_joystick_input.h"
19#include "frc971/input/driver_station_data.h"
20#include "frc971/input/drivetrain_input.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080021#include "y2018/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhab15c4d2018-03-09 21:21:03 -080022#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070023#include "y2018/control_loops/superstructure/superstructure_goal_generated.h"
24#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
25#include "y2018/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuh8d5fff42018-05-30 20:44:12 -070026#include "y2018/vision.pb.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080027
Austin Schuh8d5fff42018-05-30 20:44:12 -070028using ::aos::monotonic_clock;
Austin Schuh8d5fff42018-05-30 20:44:12 -070029using ::aos::events::ProtoTXUdpSocket;
30using ::aos::events::RXUdpSocket;
James Kuszmaul7077d342021-06-09 20:23:58 -070031using ::frc971::input::DrivetrainInputReader;
32using ::frc971::input::driver_station::ButtonLocation;
33using ::frc971::input::driver_station::ControlBit;
34using ::frc971::input::driver_station::JoystickAxis;
35using ::frc971::input::driver_station::POVLocation;
Neil Balchacfca5b2018-01-28 14:04:08 -080036
Austin Schuh60cdb3e2018-04-06 21:52:32 -070037using ::y2018::control_loops::superstructure::arm::BackPoints;
James Kuszmaul7077d342021-06-09 20:23:58 -070038using ::y2018::control_loops::superstructure::arm::FrontPoints;
Austin Schuh60cdb3e2018-04-06 21:52:32 -070039
Neil Balchacfca5b2018-01-28 14:04:08 -080040namespace y2018 {
41namespace input {
42namespace joysticks {
43
Austin Schuhab15c4d2018-03-09 21:21:03 -080044namespace arm = ::y2018::control_loops::superstructure::arm;
Austin Schuha8de4a62018-09-03 18:04:28 -070045using google::protobuf::StringPrintf;
Neil Balch07fee582018-01-27 15:46:49 -080046
Sabina Davis833ccf62018-04-06 20:52:31 -070047const ButtonLocation kIntakeClosed(3, 2);
48const ButtonLocation kDuck(3, 9);
49const ButtonLocation kSmallBox(3, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080050
Sabina Davis833ccf62018-04-06 20:52:31 -070051const ButtonLocation kIntakeIn(3, 4);
52const ButtonLocation kIntakeOut(3, 3);
Neil Balch07fee582018-01-27 15:46:49 -080053
Austin Schuha8de4a62018-09-03 18:04:28 -070054const ButtonLocation kArmBackHighBox(4, 11);
55const ButtonLocation kArmBackExtraHighBox(4, 1);
56const ButtonLocation kArmBackMiddle2Box(4, 9);
57const ButtonLocation kArmBackMiddle1Box(4, 7);
58const ButtonLocation kArmBackLowBox(4, 5);
59const ButtonLocation kArmBackSwitch(3, 7);
Austin Schuhab15c4d2018-03-09 21:21:03 -080060
Austin Schuha8de4a62018-09-03 18:04:28 -070061const ButtonLocation kArmFrontHighBox(4, 12);
62const ButtonLocation kArmFrontExtraHighBox(3, 14);
63const ButtonLocation kArmFrontMiddle2Box(4, 10);
64const ButtonLocation kArmFrontMiddle1Box(4, 8);
65const ButtonLocation kArmFrontLowBox(4, 6);
66const ButtonLocation kArmFrontSwitch(3, 10);
Austin Schuhab15c4d2018-03-09 21:21:03 -080067
Sabina Davis833ccf62018-04-06 20:52:31 -070068const ButtonLocation kArmAboveHang(3, 15);
69const ButtonLocation kArmBelowHang(3, 16);
Austin Schuh17e484e2018-03-11 01:11:36 -080070
Austin Schuhd76546a2018-07-08 16:05:14 -070071const ButtonLocation kEmergencyUp(3, 5);
72const ButtonLocation kEmergencyDown(3, 6);
73
Sabina Davis833ccf62018-04-06 20:52:31 -070074const ButtonLocation kWinch(4, 2);
Austin Schuh17e484e2018-03-11 01:11:36 -080075
Sabina Davis833ccf62018-04-06 20:52:31 -070076const ButtonLocation kArmNeutral(3, 8);
77const ButtonLocation kArmUp(3, 11);
Austin Schuhab15c4d2018-03-09 21:21:03 -080078
Sabina Davis833ccf62018-04-06 20:52:31 -070079const ButtonLocation kArmStepUp(3, 13);
80const ButtonLocation kArmStepDown(3, 12);
Austin Schuhab15c4d2018-03-09 21:21:03 -080081
Sabina Davis833ccf62018-04-06 20:52:31 -070082const ButtonLocation kArmPickupBoxFromIntake(4, 3);
83
84const ButtonLocation kClawOpen(4, 4);
Neil Balchba9cbba2018-04-06 22:26:38 -070085const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080086
James Kuszmaul7077d342021-06-09 20:23:58 -070087class Reader : public ::frc971::input::ActionJoystickInput {
Neil Balchacfca5b2018-01-28 14:04:08 -080088 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080089 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070090 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070091 event_loop,
92 ::y2018::control_loops::drivetrain::GetDrivetrainConfig(),
93 ::aos::network::GetTeamNumber() == 971
94 ? DrivetrainInputReader::InputType::kPistol
95 : DrivetrainInputReader::InputType::kSteeringWheel,
Austin Schuhd845c972019-06-29 21:20:05 -070096 {}),
97 superstructure_position_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -070098 event_loop
99 ->MakeFetcher<::y2018::control_loops::superstructure::Position>(
100 ".frc971.control_loops.superstructure_queue.position")),
Austin Schuhd845c972019-06-29 21:20:05 -0700101 superstructure_status_fetcher_(
James Kuszmaul7077d342021-06-09 20:23:58 -0700102 event_loop
103 ->MakeFetcher<::y2018::control_loops::superstructure::Status>(
104 ".frc971.control_loops.superstructure_queue.status")),
Austin Schuhd845c972019-06-29 21:20:05 -0700105 superstructure_goal_sender_(
106 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107 ->MakeSender<::y2018::control_loops::superstructure::Goal>(
Austin Schuhd845c972019-06-29 21:20:05 -0700108 ".frc971.control_loops.superstructure_queue.goal")) {
Austin Schuha8de4a62018-09-03 18:04:28 -0700109 const uint16_t team = ::aos::network::GetTeamNumber();
110
Austin Schuha8de4a62018-09-03 18:04:28 -0700111 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
112 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Neil Balchacfca5b2018-01-28 14:04:08 -0800113 }
114
James Kuszmaul7077d342021-06-09 20:23:58 -0700115 void HandleTeleop(
116 const ::frc971::input::driver_station::Data &data) override {
Austin Schuhd845c972019-06-29 21:20:05 -0700117 superstructure_position_fetcher_.Fetch();
118 superstructure_status_fetcher_.Fetch();
119 if (!superstructure_status_fetcher_.get() ||
120 !superstructure_position_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700121 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Neil Balch07fee582018-01-27 15:46:49 -0800122 return;
123 }
124
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125 auto builder = superstructure_goal_sender_.MakeBuilder();
Neil Balch07fee582018-01-27 15:46:49 -0800126
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127 double roller_voltage = 0.0;
128 bool trajectory_override = false;
Neil Balch07fee582018-01-27 15:46:49 -0800129
Austin Schuh8d5fff42018-05-30 20:44:12 -0700130 if (data.PosEdge(kIntakeIn) || data.PosEdge(kSmallBox) ||
131 data.PosEdge(kIntakeClosed)) {
132 vision_control_.set_high_video(false);
133 }
134
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700135 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800136 // Turn on the rollers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800138 } else if (data.IsPressed(kIntakeOut)) {
139 // Turn off the rollers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800141 } else {
142 // We don't want the rollers on.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 roller_voltage = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800144 }
145
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700146 if (data.IsPressed(kSmallBox)) {
147 // Deploy the intake.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 if (superstructure_position_fetcher_->box_back_beambreak_triggered()) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700149 intake_goal_ = 0.30;
150 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 if (roller_voltage > 0.1 &&
152 superstructure_position_fetcher_->box_distance() < 0.15) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700153 intake_goal_ = 0.18;
154 } else {
155 intake_goal_ = -0.60;
156 }
157 }
158 } else if (data.IsPressed(kIntakeClosed)) {
159 // Deploy the intake.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 if (superstructure_position_fetcher_->box_back_beambreak_triggered()) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700161 intake_goal_ = 0.30;
162 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 if (roller_voltage > 0.1) {
164 if (superstructure_position_fetcher_->box_distance() < 0.10) {
165 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700166 intake_goal_ = 0.22;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167 } else if (superstructure_position_fetcher_->box_distance() < 0.17) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700168 intake_goal_ = 0.13;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 } else if (superstructure_position_fetcher_->box_distance() < 0.25) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700170 intake_goal_ = 0.05;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700171 } else {
Neil Balchba9cbba2018-04-06 22:26:38 -0700172 intake_goal_ = -0.10;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700173 }
Austin Schuha250b2d2019-05-27 16:14:02 -0700174 if (robot_velocity() < -0.1 &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 superstructure_position_fetcher_->box_distance() > 0.15) {
Austin Schuhcf96d322018-04-07 15:52:31 -0700176 intake_goal_ += 0.10;
177 }
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700178 } else {
179 intake_goal_ = -0.60;
180 }
181 }
182 } else {
183 // Bring in the intake.
Austin Schuhcf96d322018-04-07 15:52:31 -0700184 intake_goal_ = -3.20;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700185 }
186
James Kuszmaul7077d342021-06-09 20:23:58 -0700187 if (roller_voltage > 0.1 && intake_goal_ > 0.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 if (superstructure_position_fetcher_->box_distance() < 0.10) {
189 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700190 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700191 roller_voltage += 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700192 }
193
Austin Schuhb874fd32018-03-05 00:27:10 -0800194 // If we are disabled, stay at the node closest to where we start. This
195 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700196 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 arm_goal_position_ =
198 superstructure_status_fetcher_->arm()->current_node();
Neil Balchba9cbba2018-04-06 22:26:38 -0700199 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800200 }
201
Austin Schuhab15c4d2018-03-09 21:21:03 -0800202 bool grab_box = false;
203 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800204 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700205 }
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700206 const bool near_goal =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhd845c972019-06-29 21:20:05 -0700208 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209 superstructure_status_fetcher_->arm()->path_distance_to_go() < 1e-3;
Austin Schuh822a1e52018-04-06 22:50:21 -0700210 if (data.IsPressed(kArmStepDown) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700211 uint32_t *front_point = ::std::find(
212 front_points_.begin(), front_points_.end(), arm_goal_position_);
213 uint32_t *back_point = ::std::find(
214 back_points_.begin(), back_points_.end(), arm_goal_position_);
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700215 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700216 ++front_point;
217 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700218 arm_goal_position_ = *front_point;
219 }
220 } else if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700221 ++back_point;
222 if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700223 arm_goal_position_ = *back_point;
224 }
225 }
Austin Schuh822a1e52018-04-06 22:50:21 -0700226 } else if (data.IsPressed(kArmStepUp) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700227 const uint32_t *front_point = ::std::find(
228 front_points_.begin(), front_points_.end(), arm_goal_position_);
229 const uint32_t *back_point = ::std::find(
230 back_points_.begin(), back_points_.end(), arm_goal_position_);
231 if (front_point != front_points_.end()) {
232 if (front_point != front_points_.begin()) {
233 --front_point;
234 arm_goal_position_ = *front_point;
235 }
236 } else if (back_point != back_points_.end()) {
237 if (back_point != back_points_.begin()) {
238 --back_point;
239 arm_goal_position_ = *back_point;
240 }
241 }
242 } else if (data.PosEdge(kArmPickupBoxFromIntake)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700243 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700244 arm_goal_position_ = arm::NeutralIndex();
245 } else if (data.IsPressed(kDuck)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700246 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700247 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800248 } else if (data.IsPressed(kArmNeutral)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700249 vision_control_.set_high_video(false);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800250 arm_goal_position_ = arm::NeutralIndex();
251 } else if (data.IsPressed(kArmUp)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700252 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800253 arm_goal_position_ = arm::UpIndex();
254 } else if (data.IsPressed(kArmFrontSwitch)) {
255 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700256 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700257 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800258 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700259 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700260 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800261 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700262 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700263 vision_control_.set_high_video(true);
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700264 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800265 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700266 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800267 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
268 } else if (data.IsPressed(kArmFrontLowBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700269 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800270 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700271 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800272 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700273 } else if (data.IsPressed(kArmBackHighBox) ||
274 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800275 arm_goal_position_ = arm::BackMiddle2BoxIndex();
276 } else if (data.IsPressed(kArmBackMiddle1Box)) {
277 arm_goal_position_ = arm::BackMiddle1BoxIndex();
278 } else if (data.IsPressed(kArmBackLowBox)) {
279 arm_goal_position_ = arm::BackLowBoxIndex();
Austin Schuhd845c972019-06-29 21:20:05 -0700280 } else if (data.IsPressed(kArmBackSwitch)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800281 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800282 } else if (data.IsPressed(kArmAboveHang)) {
283 if (data.IsPressed(kIntakeIn)) {
284 arm_goal_position_ = arm::SelfHangIndex();
285 } else if (data.IsPressed(kIntakeOut)) {
286 arm_goal_position_ = arm::PartnerHangIndex();
287 } else {
288 arm_goal_position_ = arm::AboveHangIndex();
289 }
290 } else if (data.IsPressed(kArmBelowHang)) {
291 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800292 }
293
Austin Schuhd76546a2018-07-08 16:05:14 -0700294 if (data.IsPressed(kEmergencyDown)) {
295 arm_goal_position_ = arm::NeutralIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700296 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700297 } else if (data.IsPressed(kEmergencyUp)) {
298 arm_goal_position_ = arm::UpIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700300 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 trajectory_override = false;
Austin Schuhd76546a2018-07-08 16:05:14 -0700302 }
303
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 const bool deploy_fork =
Austin Schuh17e484e2018-03-11 01:11:36 -0800305 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
306
Alex Perrycb7da4b2019-08-28 19:35:56 -0700307 if (deploy_fork) {
Austin Schuh17e484e2018-03-11 01:11:36 -0800308 intake_goal_ = -2.0;
309 }
310
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 control_loops::superstructure::IntakeGoal::Builder intake_goal_builder =
312 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
313
314 intake_goal_builder.add_left_intake_angle(intake_goal_);
315 intake_goal_builder.add_right_intake_angle(intake_goal_);
316 intake_goal_builder.add_roller_voltage(roller_voltage);
317
318 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
319 intake_goal_offset = intake_goal_builder.Finish();
320
321 control_loops::superstructure::Goal::Builder superstructure_builder =
322 builder.MakeBuilder<control_loops::superstructure::Goal>();
323
324 superstructure_builder.add_intake(intake_goal_offset);
325 superstructure_builder.add_grab_box(grab_box);
326 superstructure_builder.add_trajectory_override(trajectory_override);
327 superstructure_builder.add_deploy_fork(deploy_fork);
328
Austin Schuh17e484e2018-03-11 01:11:36 -0800329 if (data.IsPressed(kWinch)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700330 AOS_LOG(INFO, "Winching\n");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700331 superstructure_builder.add_voltage_winch(12.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800332 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333 superstructure_builder.add_voltage_winch(0.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800334 }
335
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 superstructure_builder.add_hook_release(data.IsPressed(kArmBelowHang));
Austin Schuh17e484e2018-03-11 01:11:36 -0800337
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 superstructure_builder.add_arm_goal_position(arm_goal_position_);
Austin Schuhd76546a2018-07-08 16:05:14 -0700339 if (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700340 superstructure_builder.add_open_threshold(0.35);
Austin Schuhd76546a2018-07-08 16:05:14 -0700341 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700342 superstructure_builder.add_open_threshold(0.0);
Austin Schuhd76546a2018-07-08 16:05:14 -0700343 }
Austin Schuhcb091712018-02-21 20:01:55 -0800344
Neil Balchba9cbba2018-04-06 22:26:38 -0700345 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
Austin Schuhd76546a2018-07-08 16:05:14 -0700346 data.PosEdge(kArmPickupBoxFromIntake) ||
347 (data.IsPressed(kClawOpen) &&
348 (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)))) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700349 superstructure_builder.add_open_claw(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800350 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351 superstructure_builder.add_open_claw(false);
Neil Balch07fee582018-01-27 15:46:49 -0800352 }
353
milind1f1dca32021-07-03 13:50:07 -0700354 if (builder.Send(superstructure_builder.Finish()) !=
355 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700356 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Neil Balch07fee582018-01-27 15:46:49 -0800357 }
Austin Schuh8d5fff42018-05-30 20:44:12 -0700358
Austin Schuha8de4a62018-09-03 18:04:28 -0700359 video_tx_->Send(vision_control_);
Neil Balchacfca5b2018-01-28 14:04:08 -0800360 }
361
362 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700363 uint32_t GetAutonomousMode() override {
Austin Schuh3e45c752019-02-02 12:19:11 -0800364 // Low bit is switch, high bit is scale. 1 means left, 0 means right.
Austin Schuha250b2d2019-05-27 16:14:02 -0700365 return mode();
Neil Balchacfca5b2018-01-28 14:04:08 -0800366 }
367
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 ::aos::Fetcher<control_loops::superstructure::Position>
Austin Schuhd845c972019-06-29 21:20:05 -0700369 superstructure_position_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370 ::aos::Fetcher<control_loops::superstructure::Status>
Austin Schuhd845c972019-06-29 21:20:05 -0700371 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700372 ::aos::Sender<control_loops::superstructure::Goal>
Austin Schuhd845c972019-06-29 21:20:05 -0700373 superstructure_goal_sender_;
374
Neil Balch07fee582018-01-27 15:46:49 -0800375 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800376 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800377
Neil Balchba9cbba2018-04-06 22:26:38 -0700378 bool never_disabled_ = true;
Neil Balchacfca5b2018-01-28 14:04:08 -0800379
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700380 uint32_t arm_goal_position_ = 0;
Austin Schuh8d5fff42018-05-30 20:44:12 -0700381 VisionControl vision_control_;
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700382
383 decltype(FrontPoints()) front_points_ = FrontPoints();
384 decltype(BackPoints()) back_points_ = BackPoints();
Austin Schuhcb091712018-02-21 20:01:55 -0800385
Austin Schuha8de4a62018-09-03 18:04:28 -0700386 ::std::unique_ptr<ProtoTXUdpSocket<VisionControl>> video_tx_;
Neil Balchacfca5b2018-01-28 14:04:08 -0800387};
388
389} // namespace joysticks
390} // namespace input
391} // namespace y2018
392
Austin Schuh094d09b2020-11-20 23:26:52 -0800393int main(int argc, char **argv) {
394 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700395
Alex Perrycb7da4b2019-08-28 19:35:56 -0700396 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800397 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700398
399 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800400 ::y2018::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700401
402 event_loop.Run();
403
Austin Schuhae87e312020-08-01 16:15:01 -0700404 return 0;
Neil Balchacfca5b2018-01-28 14:04:08 -0800405}