blob: c8c53852ab2bd3bf47569e895d78e16e3312e05c [file] [log] [blame]
Neil Balchacfca5b2018-01-28 14:04:08 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Austin Schuh8d5fff42018-05-30 20:44:12 -07005#include <mutex>
Austin Schuha8de4a62018-09-03 18:04:28 -07006#include <google/protobuf/stubs/stringprintf.h>
Neil Balchacfca5b2018-01-28 14:04:08 -08007
John Park33858a32018-09-28 23:05:48 -07008#include "aos/actions/actions.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "aos/init.h"
10#include "aos/input/action_joystick_input.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/input/driver_station_data.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "aos/input/drivetrain_input.h"
John Park33858a32018-09-28 23:05:48 -070013#include "aos/logging/logging.h"
14#include "aos/network/team_number.h"
15#include "aos/stl_mutex/stl_mutex.h"
16#include "aos/time/time.h"
17#include "aos/util/log_interval.h"
Austin Schuh8d5fff42018-05-30 20:44:12 -070018#include "aos/vision/events/udp.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080019#include "frc971/autonomous/base_autonomous_actor.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080020#include "y2018/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhab15c4d2018-03-09 21:21:03 -080021#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "y2018/control_loops/superstructure/superstructure_goal_generated.h"
23#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
24#include "y2018/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuh8d5fff42018-05-30 20:44:12 -070025#include "y2018/vision.pb.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080026
Austin Schuh8d5fff42018-05-30 20:44:12 -070027using ::aos::monotonic_clock;
Austin Schuh8d5fff42018-05-30 20:44:12 -070028using ::aos::events::ProtoTXUdpSocket;
29using ::aos::events::RXUdpSocket;
Neil Balchacfca5b2018-01-28 14:04:08 -080030using ::aos::input::driver_station::ButtonLocation;
31using ::aos::input::driver_station::ControlBit;
32using ::aos::input::driver_station::JoystickAxis;
33using ::aos::input::driver_station::POVLocation;
34using ::aos::input::DrivetrainInputReader;
35
Austin Schuh60cdb3e2018-04-06 21:52:32 -070036using ::y2018::control_loops::superstructure::arm::FrontPoints;
37using ::y2018::control_loops::superstructure::arm::BackPoints;
38
Neil Balchacfca5b2018-01-28 14:04:08 -080039namespace y2018 {
40namespace input {
41namespace joysticks {
42
Austin Schuhab15c4d2018-03-09 21:21:03 -080043namespace arm = ::y2018::control_loops::superstructure::arm;
Austin Schuha8de4a62018-09-03 18:04:28 -070044using google::protobuf::StringPrintf;
Neil Balch07fee582018-01-27 15:46:49 -080045
Sabina Davis833ccf62018-04-06 20:52:31 -070046const ButtonLocation kIntakeClosed(3, 2);
47const ButtonLocation kDuck(3, 9);
48const ButtonLocation kSmallBox(3, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080049
Sabina Davis833ccf62018-04-06 20:52:31 -070050const ButtonLocation kIntakeIn(3, 4);
51const ButtonLocation kIntakeOut(3, 3);
Neil Balch07fee582018-01-27 15:46:49 -080052
Austin Schuha8de4a62018-09-03 18:04:28 -070053const ButtonLocation kArmBackHighBox(4, 11);
54const ButtonLocation kArmBackExtraHighBox(4, 1);
55const ButtonLocation kArmBackMiddle2Box(4, 9);
56const ButtonLocation kArmBackMiddle1Box(4, 7);
57const ButtonLocation kArmBackLowBox(4, 5);
58const ButtonLocation kArmBackSwitch(3, 7);
Austin Schuhab15c4d2018-03-09 21:21:03 -080059
Austin Schuha8de4a62018-09-03 18:04:28 -070060const ButtonLocation kArmFrontHighBox(4, 12);
61const ButtonLocation kArmFrontExtraHighBox(3, 14);
62const ButtonLocation kArmFrontMiddle2Box(4, 10);
63const ButtonLocation kArmFrontMiddle1Box(4, 8);
64const ButtonLocation kArmFrontLowBox(4, 6);
65const ButtonLocation kArmFrontSwitch(3, 10);
Austin Schuhab15c4d2018-03-09 21:21:03 -080066
Sabina Davis833ccf62018-04-06 20:52:31 -070067const ButtonLocation kArmAboveHang(3, 15);
68const ButtonLocation kArmBelowHang(3, 16);
Austin Schuh17e484e2018-03-11 01:11:36 -080069
Austin Schuhd76546a2018-07-08 16:05:14 -070070const ButtonLocation kEmergencyUp(3, 5);
71const ButtonLocation kEmergencyDown(3, 6);
72
Sabina Davis833ccf62018-04-06 20:52:31 -070073const ButtonLocation kWinch(4, 2);
Austin Schuh17e484e2018-03-11 01:11:36 -080074
Sabina Davis833ccf62018-04-06 20:52:31 -070075const ButtonLocation kArmNeutral(3, 8);
76const ButtonLocation kArmUp(3, 11);
Austin Schuhab15c4d2018-03-09 21:21:03 -080077
Sabina Davis833ccf62018-04-06 20:52:31 -070078const ButtonLocation kArmStepUp(3, 13);
79const ButtonLocation kArmStepDown(3, 12);
Austin Schuhab15c4d2018-03-09 21:21:03 -080080
Sabina Davis833ccf62018-04-06 20:52:31 -070081const ButtonLocation kArmPickupBoxFromIntake(4, 3);
82
83const ButtonLocation kClawOpen(4, 4);
Neil Balchba9cbba2018-04-06 22:26:38 -070084const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080085
Austin Schuha250b2d2019-05-27 16:14:02 -070086class Reader : public ::aos::input::ActionJoystickInput {
Neil Balchacfca5b2018-01-28 14:04:08 -080087 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080088 Reader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -070089 : ::aos::input::ActionJoystickInput(
90 event_loop,
91 ::y2018::control_loops::drivetrain::GetDrivetrainConfig(),
92 ::aos::network::GetTeamNumber() == 971
93 ? DrivetrainInputReader::InputType::kPistol
94 : DrivetrainInputReader::InputType::kSteeringWheel,
Austin Schuhd845c972019-06-29 21:20:05 -070095 {}),
96 superstructure_position_fetcher_(
97 event_loop->MakeFetcher<
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 ::y2018::control_loops::superstructure::Position>(
Austin Schuhd845c972019-06-29 21:20:05 -070099 ".frc971.control_loops.superstructure_queue.position")),
100 superstructure_status_fetcher_(
101 event_loop->MakeFetcher<
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 ::y2018::control_loops::superstructure::Status>(
Austin Schuhd845c972019-06-29 21:20:05 -0700103 ".frc971.control_loops.superstructure_queue.status")),
104 superstructure_goal_sender_(
105 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 ->MakeSender<::y2018::control_loops::superstructure::Goal>(
Austin Schuhd845c972019-06-29 21:20:05 -0700107 ".frc971.control_loops.superstructure_queue.goal")) {
Austin Schuha8de4a62018-09-03 18:04:28 -0700108 const uint16_t team = ::aos::network::GetTeamNumber();
109
Austin Schuha8de4a62018-09-03 18:04:28 -0700110 video_tx_.reset(new ProtoTXUdpSocket<VisionControl>(
111 StringPrintf("10.%d.%d.179", team / 100, team % 100), 5000));
Neil Balchacfca5b2018-01-28 14:04:08 -0800112 }
113
Austin Schuha250b2d2019-05-27 16:14:02 -0700114 void HandleTeleop(const ::aos::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
Alex Perrycb7da4b2019-08-28 19:35:56 -0700185 if (roller_voltage > 0.1 &&
Neil Balchba9cbba2018-04-06 22:26:38 -0700186 intake_goal_ > 0.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 if (superstructure_position_fetcher_->box_distance() < 0.10) {
188 roller_voltage -= 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700189 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190 roller_voltage += 3.0;
Neil Balchba9cbba2018-04-06 22:26:38 -0700191 }
192
Austin Schuhb874fd32018-03-05 00:27:10 -0800193 // If we are disabled, stay at the node closest to where we start. This
194 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700195 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700196 arm_goal_position_ =
197 superstructure_status_fetcher_->arm()->current_node();
Neil Balchba9cbba2018-04-06 22:26:38 -0700198 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800199 }
200
Austin Schuhab15c4d2018-03-09 21:21:03 -0800201 bool grab_box = false;
202 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800203 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700204 }
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700205 const bool near_goal =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206 superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhd845c972019-06-29 21:20:05 -0700207 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208 superstructure_status_fetcher_->arm()->path_distance_to_go() < 1e-3;
Austin Schuh822a1e52018-04-06 22:50:21 -0700209 if (data.IsPressed(kArmStepDown) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700210 uint32_t *front_point = ::std::find(
211 front_points_.begin(), front_points_.end(), arm_goal_position_);
212 uint32_t *back_point = ::std::find(
213 back_points_.begin(), back_points_.end(), arm_goal_position_);
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700214 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700215 ++front_point;
216 if (front_point != front_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700217 arm_goal_position_ = *front_point;
218 }
219 } else if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700220 ++back_point;
221 if (back_point != back_points_.end()) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700222 arm_goal_position_ = *back_point;
223 }
224 }
Austin Schuh822a1e52018-04-06 22:50:21 -0700225 } else if (data.IsPressed(kArmStepUp) && near_goal) {
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700226 const uint32_t *front_point = ::std::find(
227 front_points_.begin(), front_points_.end(), arm_goal_position_);
228 const uint32_t *back_point = ::std::find(
229 back_points_.begin(), back_points_.end(), arm_goal_position_);
230 if (front_point != front_points_.end()) {
231 if (front_point != front_points_.begin()) {
232 --front_point;
233 arm_goal_position_ = *front_point;
234 }
235 } else if (back_point != back_points_.end()) {
236 if (back_point != back_points_.begin()) {
237 --back_point;
238 arm_goal_position_ = *back_point;
239 }
240 }
241 } else if (data.PosEdge(kArmPickupBoxFromIntake)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700242 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700243 arm_goal_position_ = arm::NeutralIndex();
244 } else if (data.IsPressed(kDuck)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700245 vision_control_.set_high_video(false);
Neil Balchba9cbba2018-04-06 22:26:38 -0700246 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800247 } else if (data.IsPressed(kArmNeutral)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700248 vision_control_.set_high_video(false);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800249 arm_goal_position_ = arm::NeutralIndex();
250 } else if (data.IsPressed(kArmUp)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700251 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800252 arm_goal_position_ = arm::UpIndex();
253 } else if (data.IsPressed(kArmFrontSwitch)) {
254 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700255 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700256 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800257 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700258 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700259 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800260 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700261 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700262 vision_control_.set_high_video(true);
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700263 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800264 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700265 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800266 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
267 } else if (data.IsPressed(kArmFrontLowBox)) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700268 vision_control_.set_high_video(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800269 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700270 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800271 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700272 } else if (data.IsPressed(kArmBackHighBox) ||
273 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800274 arm_goal_position_ = arm::BackMiddle2BoxIndex();
275 } else if (data.IsPressed(kArmBackMiddle1Box)) {
276 arm_goal_position_ = arm::BackMiddle1BoxIndex();
277 } else if (data.IsPressed(kArmBackLowBox)) {
278 arm_goal_position_ = arm::BackLowBoxIndex();
Austin Schuhd845c972019-06-29 21:20:05 -0700279 } else if (data.IsPressed(kArmBackSwitch)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800280 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800281 } else if (data.IsPressed(kArmAboveHang)) {
282 if (data.IsPressed(kIntakeIn)) {
283 arm_goal_position_ = arm::SelfHangIndex();
284 } else if (data.IsPressed(kIntakeOut)) {
285 arm_goal_position_ = arm::PartnerHangIndex();
286 } else {
287 arm_goal_position_ = arm::AboveHangIndex();
288 }
289 } else if (data.IsPressed(kArmBelowHang)) {
290 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800291 }
292
Austin Schuhd76546a2018-07-08 16:05:14 -0700293 if (data.IsPressed(kEmergencyDown)) {
294 arm_goal_position_ = arm::NeutralIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700296 } else if (data.IsPressed(kEmergencyUp)) {
297 arm_goal_position_ = arm::UpIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 trajectory_override = true;
Austin Schuhd76546a2018-07-08 16:05:14 -0700299 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 trajectory_override = false;
Austin Schuhd76546a2018-07-08 16:05:14 -0700301 }
302
Alex Perrycb7da4b2019-08-28 19:35:56 -0700303 const bool deploy_fork =
Austin Schuh17e484e2018-03-11 01:11:36 -0800304 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
305
Alex Perrycb7da4b2019-08-28 19:35:56 -0700306 if (deploy_fork) {
Austin Schuh17e484e2018-03-11 01:11:36 -0800307 intake_goal_ = -2.0;
308 }
309
Alex Perrycb7da4b2019-08-28 19:35:56 -0700310 control_loops::superstructure::IntakeGoal::Builder intake_goal_builder =
311 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
312
313 intake_goal_builder.add_left_intake_angle(intake_goal_);
314 intake_goal_builder.add_right_intake_angle(intake_goal_);
315 intake_goal_builder.add_roller_voltage(roller_voltage);
316
317 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
318 intake_goal_offset = intake_goal_builder.Finish();
319
320 control_loops::superstructure::Goal::Builder superstructure_builder =
321 builder.MakeBuilder<control_loops::superstructure::Goal>();
322
323 superstructure_builder.add_intake(intake_goal_offset);
324 superstructure_builder.add_grab_box(grab_box);
325 superstructure_builder.add_trajectory_override(trajectory_override);
326 superstructure_builder.add_deploy_fork(deploy_fork);
327
Austin Schuh17e484e2018-03-11 01:11:36 -0800328 if (data.IsPressed(kWinch)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700329 AOS_LOG(INFO, "Winching\n");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700330 superstructure_builder.add_voltage_winch(12.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800331 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700332 superstructure_builder.add_voltage_winch(0.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800333 }
334
Alex Perrycb7da4b2019-08-28 19:35:56 -0700335 superstructure_builder.add_hook_release(data.IsPressed(kArmBelowHang));
Austin Schuh17e484e2018-03-11 01:11:36 -0800336
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 superstructure_builder.add_arm_goal_position(arm_goal_position_);
Austin Schuhd76546a2018-07-08 16:05:14 -0700338 if (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 superstructure_builder.add_open_threshold(0.35);
Austin Schuhd76546a2018-07-08 16:05:14 -0700340 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700341 superstructure_builder.add_open_threshold(0.0);
Austin Schuhd76546a2018-07-08 16:05:14 -0700342 }
Austin Schuhcb091712018-02-21 20:01:55 -0800343
Neil Balchba9cbba2018-04-06 22:26:38 -0700344 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
Austin Schuhd76546a2018-07-08 16:05:14 -0700345 data.PosEdge(kArmPickupBoxFromIntake) ||
346 (data.IsPressed(kClawOpen) &&
347 (data.IsPressed(kArmFrontSwitch) || data.IsPressed(kArmBackSwitch)))) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700348 superstructure_builder.add_open_claw(true);
Austin Schuhab15c4d2018-03-09 21:21:03 -0800349 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700350 superstructure_builder.add_open_claw(false);
Neil Balch07fee582018-01-27 15:46:49 -0800351 }
352
Alex Perrycb7da4b2019-08-28 19:35:56 -0700353 if (!builder.Send(superstructure_builder.Finish())) {
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
387} // namespace joysticks
388} // namespace input
389} // namespace y2018
390
391int main() {
James Kuszmaulad8a8082020-02-14 21:21:58 -0800392 ::aos::InitNRT();
Austin Schuh9fe68f72019-08-10 19:32:03 -0700393
Alex Perrycb7da4b2019-08-28 19:35:56 -0700394 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
395 aos::configuration::ReadConfig("config.json");
396
397 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800398 ::y2018::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700399
400 event_loop.Run();
401
Neil Balchacfca5b2018-01-28 14:04:08 -0800402 ::aos::Cleanup();
403}