blob: d3191ccb1109e0a1cb4799c1753f92fb365403cf [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
Austin Schuhab15c4d2018-03-09 21:21:03 -080037const ButtonLocation kIntakeClosed(4, 1);
Neil Balchba9cbba2018-04-06 22:26:38 -070038const ButtonLocation kDuck(3, 11);
Austin Schuh83cdd8a2018-03-21 20:49:02 -070039const ButtonLocation kSmallBox(4, 4);
Austin Schuh47d74942018-03-04 01:15:59 -080040
Austin Schuhab15c4d2018-03-09 21:21:03 -080041const ButtonLocation kIntakeIn(3, 16);
42const ButtonLocation kIntakeOut(4, 3);
Neil Balch07fee582018-01-27 15:46:49 -080043
Austin Schuhab15c4d2018-03-09 21:21:03 -080044const ButtonLocation kArmFrontHighBox(4, 5);
45const ButtonLocation kArmFrontExtraHighBox(3, 8);
46const ButtonLocation kArmFrontMiddle2Box(4, 7);
47const ButtonLocation kArmFrontMiddle1Box(4, 9);
48const ButtonLocation kArmFrontLowBox(4, 11);
49const ButtonLocation kArmFrontSwitch(3, 14);
50
51const ButtonLocation kArmBackHighBox(4, 6);
52const ButtonLocation kArmBackExtraHighBox(3, 10);
53const ButtonLocation kArmBackMiddle2Box(4, 8);
54const ButtonLocation kArmBackMiddle1Box(4, 10);
55const ButtonLocation kArmBackLowBox(4, 12);
56const ButtonLocation kArmBackSwitch(3, 12);
57
Austin Schuh17e484e2018-03-11 01:11:36 -080058const ButtonLocation kArmAboveHang(3, 7);
59const ButtonLocation kArmBelowHang(3, 2);
60
Austin Schuh83cdd8a2018-03-21 20:49:02 -070061const ButtonLocation kWinch(3, 4);
Austin Schuh17e484e2018-03-11 01:11:36 -080062
Austin Schuhab15c4d2018-03-09 21:21:03 -080063const ButtonLocation kArmNeutral(3, 13);
64const ButtonLocation kArmUp(3, 9);
65
66const ButtonLocation kArmPickupBoxFromIntake(3, 6);
67
68const ButtonLocation kClawOpen(3, 1);
Neil Balchba9cbba2018-04-06 22:26:38 -070069const ButtonLocation kDriverClawOpen(2, 4);
Neil Balch07fee582018-01-27 15:46:49 -080070
Neil Balchacfca5b2018-01-28 14:04:08 -080071std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
72
73class Reader : public ::aos::input::JoystickInput {
74 public:
75 Reader() {
76 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080077 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080078 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
79 }
80
81 void RunIteration(const ::aos::input::driver_station::Data &data) override {
82 bool last_auto_running = auto_running_;
83 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
84 data.GetControlBit(ControlBit::kEnabled);
85 if (auto_running_ != last_auto_running) {
86 if (auto_running_) {
87 StartAuto();
88 } else {
89 StopAuto();
90 }
91 }
92
93 if (!auto_running_) {
94 HandleDrivetrain(data);
95 HandleTeleop(data);
96 }
97
98 // Process any pending actions.
99 action_queue_.Tick();
100 was_running_ = action_queue_.Running();
101 }
102
103 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
104 drivetrain_input_reader_->HandleDrivetrain(data);
Neil Balchacfca5b2018-01-28 14:04:08 -0800105 }
106
Austin Schuh1e2d4982018-03-21 20:34:33 -0700107 void SendColors(float red, float green, float blue) {
108 auto new_status_light = status_light.MakeMessage();
109 new_status_light->red = red;
110 new_status_light->green = green;
111 new_status_light->blue = blue;
112
113 if (!new_status_light.Send()) {
114 LOG(ERROR, "Failed to send lights.\n");
115 }
116 }
117
Neil Balchacfca5b2018-01-28 14:04:08 -0800118 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
119 if (!data.GetControlBit(ControlBit::kEnabled)) {
120 action_queue_.CancelAllActions();
121 LOG(DEBUG, "Canceling\n");
122 }
Neil Balch07fee582018-01-27 15:46:49 -0800123
Austin Schuhab15c4d2018-03-09 21:21:03 -0800124 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800125 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800126 if (!superstructure_queue.status.get() ||
127 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800128 LOG(ERROR, "Got no superstructure status packet.\n");
129 return;
130 }
131
Neil Balch07fee582018-01-27 15:46:49 -0800132 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
133
Sabina Davisfdd7a112018-02-04 16:16:23 -0800134 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
135 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800136
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700137 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800138 // Turn on the rollers.
Neil Balchba9cbba2018-04-06 22:26:38 -0700139 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800140 } else if (data.IsPressed(kIntakeOut)) {
141 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800142 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800143 } else {
144 // We don't want the rollers on.
145 new_superstructure_goal->intake.roller_voltage = 0.0;
146 }
147
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700148 if (superstructure_queue.position->box_back_beambreak_triggered) {
149 SendColors(0.0, 0.5, 0.0);
150 } else if (superstructure_queue.position->box_distance < 0.2) {
151 SendColors(0.0, 0.0, 0.5);
152 } else {
153 SendColors(0.0, 0.0, 0.0);
154 }
155
156 if (data.IsPressed(kSmallBox)) {
157 // Deploy the intake.
158 if (superstructure_queue.position->box_back_beambreak_triggered) {
159 intake_goal_ = 0.30;
160 } else {
161 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
162 superstructure_queue.position->box_distance < 0.15) {
163 intake_goal_ = 0.18;
164 } else {
165 intake_goal_ = -0.60;
166 }
167 }
168 } else if (data.IsPressed(kIntakeClosed)) {
169 // Deploy the intake.
170 if (superstructure_queue.position->box_back_beambreak_triggered) {
171 intake_goal_ = 0.30;
172 } else {
173 if (new_superstructure_goal->intake.roller_voltage > 0.1) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700174 if (superstructure_queue.position->box_distance < 0.10) {
175 new_superstructure_goal->intake.roller_voltage -= 3.0;
176 intake_goal_ = 0.22;
177 } else if (superstructure_queue.position->box_distance < 0.17) {
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700178 intake_goal_ = 0.13;
179 } else if (superstructure_queue.position->box_distance < 0.25) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700180 intake_goal_ = 0.05;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700181 } else {
Neil Balchba9cbba2018-04-06 22:26:38 -0700182 intake_goal_ = -0.10;
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700183 }
184 } else {
185 intake_goal_ = -0.60;
186 }
187 }
188 } else {
189 // Bring in the intake.
190 intake_goal_ = -3.3;
191 }
192
Neil Balchba9cbba2018-04-06 22:26:38 -0700193 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
194 intake_goal_ > 0.0) {
195 if (superstructure_queue.position->box_distance < 0.10) {
196 new_superstructure_goal->intake.roller_voltage -= 3.0;
197 }
198 new_superstructure_goal->intake.roller_voltage += 3.0;
199 }
200
Austin Schuhb874fd32018-03-05 00:27:10 -0800201 // If we are disabled, stay at the node closest to where we start. This
202 // should remove long motions when enabled.
Neil Balchba9cbba2018-04-06 22:26:38 -0700203 if (!data.GetControlBit(ControlBit::kEnabled) || never_disabled_) {
Austin Schuhb874fd32018-03-05 00:27:10 -0800204 arm_goal_position_ = superstructure_queue.status->arm.current_node;
Neil Balchba9cbba2018-04-06 22:26:38 -0700205 never_disabled_ = false;
Austin Schuhb874fd32018-03-05 00:27:10 -0800206 }
207
Austin Schuhab15c4d2018-03-09 21:21:03 -0800208 bool grab_box = false;
209 if (data.IsPressed(kArmPickupBoxFromIntake)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800210 grab_box = true;
Neil Balchba9cbba2018-04-06 22:26:38 -0700211 }
212 if (data.PosEdge(kArmPickupBoxFromIntake)) {
213 arm_goal_position_ = arm::NeutralIndex();
214 } else if (data.IsPressed(kDuck)) {
215 arm_goal_position_ = arm::DuckIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800216 } else if (data.IsPressed(kArmNeutral)) {
217 arm_goal_position_ = arm::NeutralIndex();
218 } else if (data.IsPressed(kArmUp)) {
219 arm_goal_position_ = arm::UpIndex();
220 } else if (data.IsPressed(kArmFrontSwitch)) {
221 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700222 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800223 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700224 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800225 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700226 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
227 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800228 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
229 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
230 } else if (data.IsPressed(kArmFrontLowBox)) {
231 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700232 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800233 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700234 } else if (data.IsPressed(kArmBackHighBox) ||
235 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800236 arm_goal_position_ = arm::BackMiddle2BoxIndex();
237 } else if (data.IsPressed(kArmBackMiddle1Box)) {
238 arm_goal_position_ = arm::BackMiddle1BoxIndex();
239 } else if (data.IsPressed(kArmBackLowBox)) {
240 arm_goal_position_ = arm::BackLowBoxIndex();
241 } else if (data.IsPressed(kArmBackSwitch)) {
242 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800243 } else if (data.IsPressed(kArmAboveHang)) {
244 if (data.IsPressed(kIntakeIn)) {
245 arm_goal_position_ = arm::SelfHangIndex();
246 } else if (data.IsPressed(kIntakeOut)) {
247 arm_goal_position_ = arm::PartnerHangIndex();
248 } else {
249 arm_goal_position_ = arm::AboveHangIndex();
250 }
251 } else if (data.IsPressed(kArmBelowHang)) {
252 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800253 }
254
Austin Schuh17e484e2018-03-11 01:11:36 -0800255 new_superstructure_goal->deploy_fork =
256 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
257
258 if (new_superstructure_goal->deploy_fork) {
259 intake_goal_ = -2.0;
260 }
261
262 if (data.IsPressed(kWinch)) {
Neil Balchba9cbba2018-04-06 22:26:38 -0700263 LOG(INFO, "Winching\n");
Austin Schuh17e484e2018-03-11 01:11:36 -0800264 new_superstructure_goal->voltage_winch = 12.0;
265 } else {
266 new_superstructure_goal->voltage_winch = 0.0;
267 }
268
269 new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang);
270
Austin Schuhcb091712018-02-21 20:01:55 -0800271 new_superstructure_goal->arm_goal_position = arm_goal_position_;
272
Neil Balchba9cbba2018-04-06 22:26:38 -0700273 if ((data.IsPressed(kClawOpen) && data.IsPressed(kDriverClawOpen)) ||
274 data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800275 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800276 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800277 new_superstructure_goal->open_claw = false;
278 }
279
Austin Schuhab15c4d2018-03-09 21:21:03 -0800280 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800281
282 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
283 if (!new_superstructure_goal.Send()) {
284 LOG(ERROR, "Sending superstructure goal failed.\n");
285 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800286 }
287
288 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800289 void StartAuto() {
290 LOG(INFO, "Starting auto mode\n");
291
292 ::frc971::autonomous::AutonomousActionParams params;
293 ::frc971::autonomous::auto_mode.FetchLatest();
294 if (::frc971::autonomous::auto_mode.get() != nullptr) {
Austin Schuhc231df42018-03-21 20:43:24 -0700295 params.mode = ::frc971::autonomous::auto_mode->mode << 2;
Austin Schuha3c148e2018-03-09 21:04:05 -0800296 } else {
297 LOG(WARNING, "no auto mode values\n");
298 params.mode = 0;
299 }
Austin Schuhc231df42018-03-21 20:43:24 -0700300 // TODO(austin): use the mode later if we care. We don't care right now.
301 params.mode = static_cast<int>(::aos::joystick_state->switch_left) |
302 (static_cast<int>(::aos::joystick_state->scale_left) << 1);
Austin Schuha3c148e2018-03-09 21:04:05 -0800303 action_queue_.EnqueueAction(
304 ::frc971::autonomous::MakeAutonomousAction(params));
305 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800306
307 void StopAuto() {
308 LOG(INFO, "Stopping auto mode\n");
309 action_queue_.CancelAllActions();
310 }
311
Neil Balch07fee582018-01-27 15:46:49 -0800312 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800313 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800314
Neil Balchacfca5b2018-01-28 14:04:08 -0800315 bool was_running_ = false;
316 bool auto_running_ = false;
Neil Balchba9cbba2018-04-06 22:26:38 -0700317 bool never_disabled_ = true;
Neil Balchacfca5b2018-01-28 14:04:08 -0800318
Austin Schuhcb091712018-02-21 20:01:55 -0800319 int arm_goal_position_ = 0;
320
Neil Balchacfca5b2018-01-28 14:04:08 -0800321 ::aos::common::actions::ActionQueue action_queue_;
322};
323
324} // namespace joysticks
325} // namespace input
326} // namespace y2018
327
328int main() {
329 ::aos::Init(-1);
330 ::y2018::input::joysticks::Reader reader;
331 reader.Run();
332 ::aos::Cleanup();
333}