blob: ace4552c1cb037056066ce4612e1578173832b7a [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>
5
6#include "aos/common/actions/actions.h"
7#include "aos/common/input/driver_station_data.h"
8#include "aos/common/logging/logging.h"
9#include "aos/common/time.h"
10#include "aos/common/util/log_interval.h"
11#include "aos/input/drivetrain_input.h"
12#include "aos/input/joystick_input.h"
13#include "aos/linux_code/init.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080014#include "frc971/autonomous/auto.q.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080016#include "frc971/control_loops/drivetrain/drivetrain.q.h"
17#include "y2018/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhab15c4d2018-03-09 21:21:03 -080018#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Neil Balch07fee582018-01-27 15:46:49 -080019#include "y2018/control_loops/superstructure/superstructure.q.h"
Austin Schuh1e2d4982018-03-21 20:34:33 -070020#include "y2018/status_light.q.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080021
22using ::frc971::control_loops::drivetrain_queue;
Neil Balch07fee582018-01-27 15:46:49 -080023using ::y2018::control_loops::superstructure_queue;
Neil Balchacfca5b2018-01-28 14:04:08 -080024
25using ::aos::input::driver_station::ButtonLocation;
26using ::aos::input::driver_station::ControlBit;
27using ::aos::input::driver_station::JoystickAxis;
28using ::aos::input::driver_station::POVLocation;
29using ::aos::input::DrivetrainInputReader;
30
31namespace y2018 {
32namespace input {
33namespace joysticks {
34
Austin Schuhab15c4d2018-03-09 21:21:03 -080035namespace arm = ::y2018::control_loops::superstructure::arm;
Neil Balch07fee582018-01-27 15:46:49 -080036
Sabina Davis833ccf62018-04-06 20:52:31 -070037const ButtonLocation kIntakeClosed(3, 2);
38const ButtonLocation kDuck(3, 9);
39const ButtonLocation kSmallBox(3, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080040
Sabina Davis833ccf62018-04-06 20:52:31 -070041const ButtonLocation kIntakeIn(3, 4);
42const ButtonLocation kIntakeOut(3, 3);
Neil Balch07fee582018-01-27 15:46:49 -080043
Sabina Davis833ccf62018-04-06 20:52:31 -070044const ButtonLocation kArmFrontHighBox(4, 11);
45const ButtonLocation kArmFrontExtraHighBox(4, 1);
46const ButtonLocation kArmFrontMiddle2Box(4, 9);
47const ButtonLocation kArmFrontMiddle1Box(4, 7);
48const ButtonLocation kArmFrontLowBox(4, 5);
49const ButtonLocation kArmFrontSwitch(3, 7);
Austin Schuhab15c4d2018-03-09 21:21:03 -080050
Sabina Davis833ccf62018-04-06 20:52:31 -070051const ButtonLocation kArmBackHighBox(4, 12);
52const ButtonLocation kArmBackExtraHighBox(3, 14);
53const ButtonLocation kArmBackMiddle2Box(4, 10);
54const ButtonLocation kArmBackMiddle1Box(4, 8);
55const ButtonLocation kArmBackLowBox(4, 6);
56const ButtonLocation kArmBackSwitch(3, 10);
Austin Schuhab15c4d2018-03-09 21:21:03 -080057
Sabina Davis833ccf62018-04-06 20:52:31 -070058const ButtonLocation kArmAboveHang(3, 15);
59const ButtonLocation kArmBelowHang(3, 16);
Austin Schuh17e484e2018-03-11 01:11:36 -080060
Sabina Davis833ccf62018-04-06 20:52:31 -070061const ButtonLocation kWinch(4, 2);
Austin Schuh17e484e2018-03-11 01:11:36 -080062
Sabina Davis833ccf62018-04-06 20:52:31 -070063const ButtonLocation kArmNeutral(3, 8);
64const ButtonLocation kArmUp(3, 11);
Austin Schuhab15c4d2018-03-09 21:21:03 -080065
Sabina Davis833ccf62018-04-06 20:52:31 -070066const ButtonLocation kArmStepUp(3, 13);
67const ButtonLocation kArmStepDown(3, 12);
Austin Schuhab15c4d2018-03-09 21:21:03 -080068
Sabina Davis833ccf62018-04-06 20:52:31 -070069const ButtonLocation kArmPickupBoxFromIntake(4, 3);
70
71const ButtonLocation kClawOpen(4, 4);
Neil Balchba9cbba2018-04-06 22:26:38 -070072const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080073
Neil Balchacfca5b2018-01-28 14:04:08 -080074std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
75
76class Reader : public ::aos::input::JoystickInput {
77 public:
78 Reader() {
79 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080080 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080081 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
82 }
83
84 void RunIteration(const ::aos::input::driver_station::Data &data) override {
85 bool last_auto_running = auto_running_;
86 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
87 data.GetControlBit(ControlBit::kEnabled);
88 if (auto_running_ != last_auto_running) {
89 if (auto_running_) {
90 StartAuto();
91 } else {
92 StopAuto();
93 }
94 }
95
96 if (!auto_running_) {
97 HandleDrivetrain(data);
98 HandleTeleop(data);
99 }
100
101 // Process any pending actions.
102 action_queue_.Tick();
103 was_running_ = action_queue_.Running();
104 }
105
106 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
107 drivetrain_input_reader_->HandleDrivetrain(data);
Neil Balchacfca5b2018-01-28 14:04:08 -0800108 }
109
Austin Schuh1e2d4982018-03-21 20:34:33 -0700110 void SendColors(float red, float green, float blue) {
111 auto new_status_light = status_light.MakeMessage();
112 new_status_light->red = red;
113 new_status_light->green = green;
114 new_status_light->blue = blue;
115
116 if (!new_status_light.Send()) {
117 LOG(ERROR, "Failed to send lights.\n");
118 }
119 }
120
Neil Balchacfca5b2018-01-28 14:04:08 -0800121 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
122 if (!data.GetControlBit(ControlBit::kEnabled)) {
123 action_queue_.CancelAllActions();
124 LOG(DEBUG, "Canceling\n");
125 }
Neil Balch07fee582018-01-27 15:46:49 -0800126
Austin Schuhab15c4d2018-03-09 21:21:03 -0800127 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800128 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800129 if (!superstructure_queue.status.get() ||
130 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800131 LOG(ERROR, "Got no superstructure status packet.\n");
132 return;
133 }
134
Neil Balch07fee582018-01-27 15:46:49 -0800135 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
136
Sabina Davisfdd7a112018-02-04 16:16:23 -0800137 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
138 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800139
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700140 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800141 // Turn on the rollers.
Neil Balchba9cbba2018-04-06 22:26:38 -0700142 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800143 } else if (data.IsPressed(kIntakeOut)) {
144 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800145 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800146 } else {
147 // We don't want the rollers on.
148 new_superstructure_goal->intake.roller_voltage = 0.0;
149 }
150
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700151 if (superstructure_queue.position->box_back_beambreak_triggered) {
152 SendColors(0.0, 0.5, 0.0);
153 } else if (superstructure_queue.position->box_distance < 0.2) {
154 SendColors(0.0, 0.0, 0.5);
155 } else {
156 SendColors(0.0, 0.0, 0.0);
157 }
158
159 if (data.IsPressed(kSmallBox)) {
160 // Deploy the intake.
161 if (superstructure_queue.position->box_back_beambreak_triggered) {
162 intake_goal_ = 0.30;
163 } else {
164 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
165 superstructure_queue.position->box_distance < 0.15) {
166 intake_goal_ = 0.18;
167 } else {
168 intake_goal_ = -0.60;
169 }
170 }
171 } else if (data.IsPressed(kIntakeClosed)) {
172 // Deploy the intake.
173 if (superstructure_queue.position->box_back_beambreak_triggered) {
174 intake_goal_ = 0.30;
175 } else {
176 if (new_superstructure_goal->intake.roller_voltage > 0.1) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700177 if (superstructure_queue.position->box_distance < 0.10) {
178 new_superstructure_goal->intake.roller_voltage -= 3.0;
179 intake_goal_ = 0.22;
180 } else if (superstructure_queue.position->box_distance < 0.17) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700181 intake_goal_ = 0.13;
182 } else if (superstructure_queue.position->box_distance < 0.25) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700183 intake_goal_ = 0.05;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700184 } else {
Neil Balchba9cbba2018-04-06 22:26:38 -0700185 intake_goal_ = -0.10;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700186 }
187 } else {
188 intake_goal_ = -0.60;
189 }
190 }
191 } else {
192 // Bring in the intake.
193 intake_goal_ = -3.3;
194 }
195
Neil Balchba9cbba2018-04-06 22:26:38 -0700196 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
197 intake_goal_ > 0.0) {
198 if (superstructure_queue.position->box_distance < 0.10) {
199 new_superstructure_goal->intake.roller_voltage -= 3.0;
200 }
201 new_superstructure_goal->intake.roller_voltage += 3.0;
202 }
203
Austin Schuhb874fd32018-03-05 00:27:10 -0800204 // If we are disabled, stay at the node closest to where we start. This
205 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700206 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Austin Schuhb874fd32018-03-05 00:27:10 -0800207 arm_goal_position_ = superstructure_queue.status->arm.current_node;
Neil Balchba9cbba2018-04-06 22:26:38 -0700208 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800209 }
210
Austin Schuhab15c4d2018-03-09 21:21:03 -0800211 bool grab_box = false;
212 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800213 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700214 }
215 if (data.PosEdge(kArmPickupBoxFromIntake)) {
216 arm_goal_position_ = arm::NeutralIndex();
217 } else if (data.IsPressed(kDuck)) {
218 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800219 } else if (data.IsPressed(kArmNeutral)) {
220 arm_goal_position_ = arm::NeutralIndex();
221 } else if (data.IsPressed(kArmUp)) {
222 arm_goal_position_ = arm::UpIndex();
223 } else if (data.IsPressed(kArmFrontSwitch)) {
224 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700225 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800226 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700227 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800228 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700229 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
230 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800231 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
232 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
233 } else if (data.IsPressed(kArmFrontLowBox)) {
234 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700235 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800236 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700237 } else if (data.IsPressed(kArmBackHighBox) ||
238 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800239 arm_goal_position_ = arm::BackMiddle2BoxIndex();
240 } else if (data.IsPressed(kArmBackMiddle1Box)) {
241 arm_goal_position_ = arm::BackMiddle1BoxIndex();
242 } else if (data.IsPressed(kArmBackLowBox)) {
243 arm_goal_position_ = arm::BackLowBoxIndex();
244 } else if (data.IsPressed(kArmBackSwitch)) {
245 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800246 } else if (data.IsPressed(kArmAboveHang)) {
247 if (data.IsPressed(kIntakeIn)) {
248 arm_goal_position_ = arm::SelfHangIndex();
249 } else if (data.IsPressed(kIntakeOut)) {
250 arm_goal_position_ = arm::PartnerHangIndex();
251 } else {
252 arm_goal_position_ = arm::AboveHangIndex();
253 }
254 } else if (data.IsPressed(kArmBelowHang)) {
255 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800256 }
257
Austin Schuh17e484e2018-03-11 01:11:36 -0800258 new_superstructure_goal->deploy_fork =
259 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
260
261 if (new_superstructure_goal->deploy_fork) {
262 intake_goal_ = -2.0;
263 }
264
265 if (data.IsPressed(kWinch)) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700266 LOG(INFO, "Winching\n");
Austin Schuh17e484e2018-03-11 01:11:36 -0800267 new_superstructure_goal->voltage_winch = 12.0;
268 } else {
269 new_superstructure_goal->voltage_winch = 0.0;
270 }
271
272 new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang);
273
Austin Schuhcb091712018-02-21 20:01:55 -0800274 new_superstructure_goal->arm_goal_position = arm_goal_position_;
275
Neil Balchba9cbba2018-04-06 22:26:38 -0700276 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
277 data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800278 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800279 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800280 new_superstructure_goal->open_claw = false;
281 }
282
Austin Schuhab15c4d2018-03-09 21:21:03 -0800283 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800284
285 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
286 if (!new_superstructure_goal.Send()) {
287 LOG(ERROR, "Sending superstructure goal failed.\n");
288 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800289 }
290
291 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800292 void StartAuto() {
293 LOG(INFO, "Starting auto mode\n");
294
295 ::frc971::autonomous::AutonomousActionParams params;
296 ::frc971::autonomous::auto_mode.FetchLatest();
297 if (::frc971::autonomous::auto_mode.get() != nullptr) {
Austin Schuhc231df42018-03-21 20:43:24 -0700298 params.mode = ::frc971::autonomous::auto_mode->mode << 2;
Austin Schuha3c148e2018-03-09 21:04:05 -0800299 } else {
300 LOG(WARNING, "no auto mode values\n");
301 params.mode = 0;
302 }
Austin Schuhc231df42018-03-21 20:43:24 -0700303 // TODO(austin): use the mode later if we care. We don't care right now.
304 params.mode = static_cast<int>(::aos::joystick_state->switch_left) |
305 (static_cast<int>(::aos::joystick_state->scale_left) << 1);
Austin Schuha3c148e2018-03-09 21:04:05 -0800306 action_queue_.EnqueueAction(
307 ::frc971::autonomous::MakeAutonomousAction(params));
308 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800309
310 void StopAuto() {
311 LOG(INFO, "Stopping auto mode\n");
312 action_queue_.CancelAllActions();
313 }
314
Neil Balch07fee582018-01-27 15:46:49 -0800315 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800316 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800317
Neil Balchacfca5b2018-01-28 14:04:08 -0800318 bool was_running_ = false;
319 bool auto_running_ = false;
Neil Balchba9cbba2018-04-06 22:26:38 -0700320 bool never_disabled_ = true;
Neil Balchacfca5b2018-01-28 14:04:08 -0800321
Austin Schuhcb091712018-02-21 20:01:55 -0800322 int arm_goal_position_ = 0;
323
Neil Balchacfca5b2018-01-28 14:04:08 -0800324 ::aos::common::actions::ActionQueue action_queue_;
325};
326
327} // namespace joysticks
328} // namespace input
329} // namespace y2018
330
331int main() {
332 ::aos::Init(-1);
333 ::y2018::input::joysticks::Reader reader;
334 reader.Run();
335 ::aos::Cleanup();
336}