blob: a82804843ba1d78a03dd1cb7d3bdd5c5918b5746 [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);
Austin Schuh83cdd8a2018-03-21 20:49:02 -070038const ButtonLocation kSmallBox(4, 4);
Austin Schuh47d74942018-03-04 01:15:59 -080039
Austin Schuhab15c4d2018-03-09 21:21:03 -080040const ButtonLocation kIntakeIn(3, 16);
41const ButtonLocation kIntakeOut(4, 3);
Neil Balch07fee582018-01-27 15:46:49 -080042
Austin Schuhab15c4d2018-03-09 21:21:03 -080043const ButtonLocation kArmFrontHighBox(4, 5);
44const ButtonLocation kArmFrontExtraHighBox(3, 8);
45const ButtonLocation kArmFrontMiddle2Box(4, 7);
46const ButtonLocation kArmFrontMiddle1Box(4, 9);
47const ButtonLocation kArmFrontLowBox(4, 11);
48const ButtonLocation kArmFrontSwitch(3, 14);
49
50const ButtonLocation kArmBackHighBox(4, 6);
51const ButtonLocation kArmBackExtraHighBox(3, 10);
52const ButtonLocation kArmBackMiddle2Box(4, 8);
53const ButtonLocation kArmBackMiddle1Box(4, 10);
54const ButtonLocation kArmBackLowBox(4, 12);
55const ButtonLocation kArmBackSwitch(3, 12);
56
Austin Schuh17e484e2018-03-11 01:11:36 -080057const ButtonLocation kArmAboveHang(3, 7);
58const ButtonLocation kArmBelowHang(3, 2);
59
Austin Schuh83cdd8a2018-03-21 20:49:02 -070060const ButtonLocation kWinch(3, 4);
Austin Schuh17e484e2018-03-11 01:11:36 -080061
Austin Schuhab15c4d2018-03-09 21:21:03 -080062const ButtonLocation kArmNeutral(3, 13);
63const ButtonLocation kArmUp(3, 9);
64
65const ButtonLocation kArmPickupBoxFromIntake(3, 6);
66
67const ButtonLocation kClawOpen(3, 1);
Neil Balch07fee582018-01-27 15:46:49 -080068
Neil Balchacfca5b2018-01-28 14:04:08 -080069std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
70
71class Reader : public ::aos::input::JoystickInput {
72 public:
73 Reader() {
74 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080075 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080076 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
77 }
78
79 void RunIteration(const ::aos::input::driver_station::Data &data) override {
80 bool last_auto_running = auto_running_;
81 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
82 data.GetControlBit(ControlBit::kEnabled);
83 if (auto_running_ != last_auto_running) {
84 if (auto_running_) {
85 StartAuto();
86 } else {
87 StopAuto();
88 }
89 }
90
91 if (!auto_running_) {
92 HandleDrivetrain(data);
93 HandleTeleop(data);
94 }
95
96 // Process any pending actions.
97 action_queue_.Tick();
98 was_running_ = action_queue_.Running();
99 }
100
101 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
102 drivetrain_input_reader_->HandleDrivetrain(data);
Neil Balchacfca5b2018-01-28 14:04:08 -0800103 }
104
Austin Schuh1e2d4982018-03-21 20:34:33 -0700105 void SendColors(float red, float green, float blue) {
106 auto new_status_light = status_light.MakeMessage();
107 new_status_light->red = red;
108 new_status_light->green = green;
109 new_status_light->blue = blue;
110
111 if (!new_status_light.Send()) {
112 LOG(ERROR, "Failed to send lights.\n");
113 }
114 }
115
Neil Balchacfca5b2018-01-28 14:04:08 -0800116 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
117 if (!data.GetControlBit(ControlBit::kEnabled)) {
118 action_queue_.CancelAllActions();
119 LOG(DEBUG, "Canceling\n");
120 }
Neil Balch07fee582018-01-27 15:46:49 -0800121
Austin Schuhab15c4d2018-03-09 21:21:03 -0800122 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800123 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800124 if (!superstructure_queue.status.get() ||
125 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800126 LOG(ERROR, "Got no superstructure status packet.\n");
127 return;
128 }
129
Neil Balch07fee582018-01-27 15:46:49 -0800130 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
131
Sabina Davisfdd7a112018-02-04 16:16:23 -0800132 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
133 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800134
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700135 if (data.IsPressed(kIntakeIn)) {
Neil Balch07fee582018-01-27 15:46:49 -0800136 // Turn on the rollers.
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700137 new_superstructure_goal->intake.roller_voltage = 7.5;
Neil Balch07fee582018-01-27 15:46:49 -0800138 } else if (data.IsPressed(kIntakeOut)) {
139 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800140 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800141 } else {
142 // We don't want the rollers on.
143 new_superstructure_goal->intake.roller_voltage = 0.0;
144 }
145
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700146 if (superstructure_queue.position->box_back_beambreak_triggered) {
147 SendColors(0.0, 0.5, 0.0);
148 } else if (superstructure_queue.position->box_distance < 0.2) {
149 SendColors(0.0, 0.0, 0.5);
150 } else {
151 SendColors(0.0, 0.0, 0.0);
152 }
153
154 if (data.IsPressed(kSmallBox)) {
155 // Deploy the intake.
156 if (superstructure_queue.position->box_back_beambreak_triggered) {
157 intake_goal_ = 0.30;
158 } else {
159 if (new_superstructure_goal->intake.roller_voltage > 0.1 &&
160 superstructure_queue.position->box_distance < 0.15) {
161 intake_goal_ = 0.18;
162 } else {
163 intake_goal_ = -0.60;
164 }
165 }
166 } else if (data.IsPressed(kIntakeClosed)) {
167 // Deploy the intake.
168 if (superstructure_queue.position->box_back_beambreak_triggered) {
169 intake_goal_ = 0.30;
170 } else {
171 if (new_superstructure_goal->intake.roller_voltage > 0.1) {
172 if (superstructure_queue.position->box_distance < 0.15) {
173 intake_goal_ = 0.23;
174 } else if (superstructure_queue.position->box_distance < 0.20) {
175 intake_goal_ = 0.13;
176 } else if (superstructure_queue.position->box_distance < 0.25) {
177 intake_goal_ = -0.05;
178 } else if (superstructure_queue.position->box_distance < 0.28) {
179 intake_goal_ = -0.20;
180 } else {
181 intake_goal_ = -0.40;
182 }
183 } else {
184 intake_goal_ = -0.60;
185 }
186 }
187 } else {
188 // Bring in the intake.
189 intake_goal_ = -3.3;
190 }
191
Austin Schuhb874fd32018-03-05 00:27:10 -0800192 // If we are disabled, stay at the node closest to where we start. This
193 // should remove long motions when enabled.
194 if (!data.GetControlBit(ControlBit::kEnabled)) {
195 arm_goal_position_ = superstructure_queue.status->arm.current_node;
196 }
197
Austin Schuhab15c4d2018-03-09 21:21:03 -0800198 bool grab_box = false;
199 if (data.IsPressed(kArmPickupBoxFromIntake)) {
200 arm_goal_position_ = arm::NeutralIndex();
201 grab_box = true;
202 } else if (data.IsPressed(kArmNeutral)) {
203 arm_goal_position_ = arm::NeutralIndex();
204 } else if (data.IsPressed(kArmUp)) {
205 arm_goal_position_ = arm::UpIndex();
206 } else if (data.IsPressed(kArmFrontSwitch)) {
207 arm_goal_position_ = arm::FrontSwitchIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700208 } else if (data.IsPressed(kArmFrontExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800209 arm_goal_position_ = arm::FrontHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700210 } else if (data.IsPressed(kArmFrontHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800211 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700212 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
213 arm_goal_position_ = arm::FrontMiddle3BoxIndex();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800214 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
215 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
216 } else if (data.IsPressed(kArmFrontLowBox)) {
217 arm_goal_position_ = arm::FrontLowBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700218 } else if (data.IsPressed(kArmBackExtraHighBox)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800219 arm_goal_position_ = arm::BackHighBoxIndex();
Austin Schuh83cdd8a2018-03-21 20:49:02 -0700220 } else if (data.IsPressed(kArmBackHighBox) ||
221 data.IsPressed(kArmBackMiddle2Box)) {
Austin Schuhab15c4d2018-03-09 21:21:03 -0800222 arm_goal_position_ = arm::BackMiddle2BoxIndex();
223 } else if (data.IsPressed(kArmBackMiddle1Box)) {
224 arm_goal_position_ = arm::BackMiddle1BoxIndex();
225 } else if (data.IsPressed(kArmBackLowBox)) {
226 arm_goal_position_ = arm::BackLowBoxIndex();
227 } else if (data.IsPressed(kArmBackSwitch)) {
228 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800229 } else if (data.IsPressed(kArmAboveHang)) {
230 if (data.IsPressed(kIntakeIn)) {
231 arm_goal_position_ = arm::SelfHangIndex();
232 } else if (data.IsPressed(kIntakeOut)) {
233 arm_goal_position_ = arm::PartnerHangIndex();
234 } else {
235 arm_goal_position_ = arm::AboveHangIndex();
236 }
237 } else if (data.IsPressed(kArmBelowHang)) {
238 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800239 }
240
Austin Schuh17e484e2018-03-11 01:11:36 -0800241 new_superstructure_goal->deploy_fork =
242 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
243
244 if (new_superstructure_goal->deploy_fork) {
245 intake_goal_ = -2.0;
246 }
247
248 if (data.IsPressed(kWinch)) {
249 new_superstructure_goal->voltage_winch = 12.0;
250 } else {
251 new_superstructure_goal->voltage_winch = 0.0;
252 }
253
254 new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang);
255
Austin Schuhcb091712018-02-21 20:01:55 -0800256 new_superstructure_goal->arm_goal_position = arm_goal_position_;
257
Austin Schuhb874fd32018-03-05 00:27:10 -0800258 if (data.IsPressed(kClawOpen) || data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800259 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800260 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800261 new_superstructure_goal->open_claw = false;
262 }
263
Austin Schuhab15c4d2018-03-09 21:21:03 -0800264 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800265
266 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
267 if (!new_superstructure_goal.Send()) {
268 LOG(ERROR, "Sending superstructure goal failed.\n");
269 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800270 }
271
272 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800273 void StartAuto() {
274 LOG(INFO, "Starting auto mode\n");
275
276 ::frc971::autonomous::AutonomousActionParams params;
277 ::frc971::autonomous::auto_mode.FetchLatest();
278 if (::frc971::autonomous::auto_mode.get() != nullptr) {
Austin Schuhc231df42018-03-21 20:43:24 -0700279 params.mode = ::frc971::autonomous::auto_mode->mode << 2;
Austin Schuha3c148e2018-03-09 21:04:05 -0800280 } else {
281 LOG(WARNING, "no auto mode values\n");
282 params.mode = 0;
283 }
Austin Schuhc231df42018-03-21 20:43:24 -0700284 // TODO(austin): use the mode later if we care. We don't care right now.
285 params.mode = static_cast<int>(::aos::joystick_state->switch_left) |
286 (static_cast<int>(::aos::joystick_state->scale_left) << 1);
Austin Schuha3c148e2018-03-09 21:04:05 -0800287 action_queue_.EnqueueAction(
288 ::frc971::autonomous::MakeAutonomousAction(params));
289 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800290
291 void StopAuto() {
292 LOG(INFO, "Stopping auto mode\n");
293 action_queue_.CancelAllActions();
294 }
295
Neil Balch07fee582018-01-27 15:46:49 -0800296 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800297 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800298
Neil Balchacfca5b2018-01-28 14:04:08 -0800299 bool was_running_ = false;
300 bool auto_running_ = false;
301
Austin Schuhcb091712018-02-21 20:01:55 -0800302 int arm_goal_position_ = 0;
303
Neil Balchacfca5b2018-01-28 14:04:08 -0800304 ::aos::common::actions::ActionQueue action_queue_;
305};
306
307} // namespace joysticks
308} // namespace input
309} // namespace y2018
310
311int main() {
312 ::aos::Init(-1);
313 ::y2018::input::joysticks::Reader reader;
314 reader.Run();
315 ::aos::Cleanup();
316}