blob: a5eaf14c04206ebd34d7fb66074bf6f7a20e8023 [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"
Neil Balchacfca5b2018-01-28 14:04:08 -080020
21using ::frc971::control_loops::drivetrain_queue;
Neil Balch07fee582018-01-27 15:46:49 -080022using ::y2018::control_loops::superstructure_queue;
Neil Balchacfca5b2018-01-28 14:04:08 -080023
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::ControlBit;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::POVLocation;
28using ::aos::input::DrivetrainInputReader;
29
30namespace y2018 {
31namespace input {
32namespace joysticks {
33
Austin Schuhab15c4d2018-03-09 21:21:03 -080034namespace arm = ::y2018::control_loops::superstructure::arm;
Neil Balch07fee582018-01-27 15:46:49 -080035
Austin Schuhab15c4d2018-03-09 21:21:03 -080036const ButtonLocation kIntakeClosed(4, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080037
Austin Schuhab15c4d2018-03-09 21:21:03 -080038const ButtonLocation kIntakeIn(3, 16);
39const ButtonLocation kIntakeOut(4, 3);
Neil Balch07fee582018-01-27 15:46:49 -080040
Austin Schuhab15c4d2018-03-09 21:21:03 -080041const ButtonLocation kArmFrontHighBox(4, 5);
42const ButtonLocation kArmFrontExtraHighBox(3, 8);
43const ButtonLocation kArmFrontMiddle2Box(4, 7);
44const ButtonLocation kArmFrontMiddle1Box(4, 9);
45const ButtonLocation kArmFrontLowBox(4, 11);
46const ButtonLocation kArmFrontSwitch(3, 14);
47
48const ButtonLocation kArmBackHighBox(4, 6);
49const ButtonLocation kArmBackExtraHighBox(3, 10);
50const ButtonLocation kArmBackMiddle2Box(4, 8);
51const ButtonLocation kArmBackMiddle1Box(4, 10);
52const ButtonLocation kArmBackLowBox(4, 12);
53const ButtonLocation kArmBackSwitch(3, 12);
54
Austin Schuh17e484e2018-03-11 01:11:36 -080055const ButtonLocation kArmAboveHang(3, 7);
56const ButtonLocation kArmBelowHang(3, 2);
57
58const ButtonLocation kWinch(3, 5);
59
Austin Schuhab15c4d2018-03-09 21:21:03 -080060const ButtonLocation kArmNeutral(3, 13);
61const ButtonLocation kArmUp(3, 9);
62
63const ButtonLocation kArmPickupBoxFromIntake(3, 6);
64
65const ButtonLocation kClawOpen(3, 1);
Neil Balch07fee582018-01-27 15:46:49 -080066
67const ButtonLocation kForkDeploy(3, 11);
68const ButtonLocation kForkStow(3, 10);
69
Neil Balchacfca5b2018-01-28 14:04:08 -080070std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
71
72class Reader : public ::aos::input::JoystickInput {
73 public:
74 Reader() {
75 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080076 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080077 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
78 }
79
80 void RunIteration(const ::aos::input::driver_station::Data &data) override {
81 bool last_auto_running = auto_running_;
82 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
83 data.GetControlBit(ControlBit::kEnabled);
84 if (auto_running_ != last_auto_running) {
85 if (auto_running_) {
86 StartAuto();
87 } else {
88 StopAuto();
89 }
90 }
91
92 if (!auto_running_) {
93 HandleDrivetrain(data);
94 HandleTeleop(data);
95 }
96
97 // Process any pending actions.
98 action_queue_.Tick();
99 was_running_ = action_queue_.Running();
100 }
101
102 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
103 drivetrain_input_reader_->HandleDrivetrain(data);
Neil Balchacfca5b2018-01-28 14:04:08 -0800104 }
105
106 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
107 if (!data.GetControlBit(ControlBit::kEnabled)) {
108 action_queue_.CancelAllActions();
109 LOG(DEBUG, "Canceling\n");
110 }
Neil Balch07fee582018-01-27 15:46:49 -0800111
Austin Schuhab15c4d2018-03-09 21:21:03 -0800112 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800113 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800114 if (!superstructure_queue.status.get() ||
115 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800116 LOG(ERROR, "Got no superstructure status packet.\n");
117 return;
118 }
119
Austin Schuh47d74942018-03-04 01:15:59 -0800120 if (data.IsPressed(kIntakeClosed)) {
Neil Balch07fee582018-01-27 15:46:49 -0800121 // Deploy the intake.
Austin Schuhab15c4d2018-03-09 21:21:03 -0800122 if (superstructure_queue.position->box_back_beambreak_triggered) {
123 intake_goal_ = 0.30;
124 } else {
125 intake_goal_ = 0.07;
126 }
127 } else {
128 // Bring in the intake.
Austin Schuhf88d3ac2018-03-05 00:26:36 -0800129 intake_goal_ = -3.3;
Neil Balch07fee582018-01-27 15:46:49 -0800130 }
131
132 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 Schuhab15c4d2018-03-09 21:21:03 -0800137 if (data.IsPressed(kIntakeIn) || data.IsPressed(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800138 // Turn on the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800139 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 Schuhb874fd32018-03-05 00:27:10 -0800148 // If we are disabled, stay at the node closest to where we start. This
149 // should remove long motions when enabled.
150 if (!data.GetControlBit(ControlBit::kEnabled)) {
151 arm_goal_position_ = superstructure_queue.status->arm.current_node;
152 }
153
Austin Schuhab15c4d2018-03-09 21:21:03 -0800154 bool grab_box = false;
155 if (data.IsPressed(kArmPickupBoxFromIntake)) {
156 arm_goal_position_ = arm::NeutralIndex();
157 grab_box = true;
158 } else if (data.IsPressed(kArmNeutral)) {
159 arm_goal_position_ = arm::NeutralIndex();
160 } else if (data.IsPressed(kArmUp)) {
161 arm_goal_position_ = arm::UpIndex();
162 } else if (data.IsPressed(kArmFrontSwitch)) {
163 arm_goal_position_ = arm::FrontSwitchIndex();
164 } else if (data.IsPressed(kArmFrontHighBox) ||
165 data.IsPressed(kArmFrontExtraHighBox)) {
166 arm_goal_position_ = arm::FrontHighBoxIndex();
167 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
168 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
169 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
170 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
171 } else if (data.IsPressed(kArmFrontLowBox)) {
172 arm_goal_position_ = arm::FrontLowBoxIndex();
173 } else if (data.IsPressed(kArmBackHighBox) ||
174 data.IsPressed(kArmBackExtraHighBox)) {
175 arm_goal_position_ = arm::BackHighBoxIndex();
176 } else if (data.IsPressed(kArmBackMiddle2Box)) {
177 arm_goal_position_ = arm::BackMiddle2BoxIndex();
178 } else if (data.IsPressed(kArmBackMiddle1Box)) {
179 arm_goal_position_ = arm::BackMiddle1BoxIndex();
180 } else if (data.IsPressed(kArmBackLowBox)) {
181 arm_goal_position_ = arm::BackLowBoxIndex();
182 } else if (data.IsPressed(kArmBackSwitch)) {
183 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800184 } else if (data.IsPressed(kArmAboveHang)) {
185 if (data.IsPressed(kIntakeIn)) {
186 arm_goal_position_ = arm::SelfHangIndex();
187 } else if (data.IsPressed(kIntakeOut)) {
188 arm_goal_position_ = arm::PartnerHangIndex();
189 } else {
190 arm_goal_position_ = arm::AboveHangIndex();
191 }
192 } else if (data.IsPressed(kArmBelowHang)) {
193 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800194 }
195
Austin Schuh17e484e2018-03-11 01:11:36 -0800196 new_superstructure_goal->deploy_fork =
197 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
198
199 if (new_superstructure_goal->deploy_fork) {
200 intake_goal_ = -2.0;
201 }
202
203 if (data.IsPressed(kWinch)) {
204 new_superstructure_goal->voltage_winch = 12.0;
205 } else {
206 new_superstructure_goal->voltage_winch = 0.0;
207 }
208
209 new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang);
210
Austin Schuhcb091712018-02-21 20:01:55 -0800211 new_superstructure_goal->arm_goal_position = arm_goal_position_;
212
Austin Schuhb874fd32018-03-05 00:27:10 -0800213 if (data.IsPressed(kClawOpen) || data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800214 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800215 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800216 new_superstructure_goal->open_claw = false;
217 }
218
219 if (data.IsPressed(kForkDeploy)) {
220 new_superstructure_goal->deploy_fork = true;
221 } else if (data.IsPressed(kForkStow)) {
222 new_superstructure_goal->deploy_fork = false;
223 }
Austin Schuhab15c4d2018-03-09 21:21:03 -0800224 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800225
226 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
227 if (!new_superstructure_goal.Send()) {
228 LOG(ERROR, "Sending superstructure goal failed.\n");
229 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800230 }
231
232 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800233 void StartAuto() {
234 LOG(INFO, "Starting auto mode\n");
235
236 ::frc971::autonomous::AutonomousActionParams params;
237 ::frc971::autonomous::auto_mode.FetchLatest();
238 if (::frc971::autonomous::auto_mode.get() != nullptr) {
239 params.mode = ::frc971::autonomous::auto_mode->mode;
240 } else {
241 LOG(WARNING, "no auto mode values\n");
242 params.mode = 0;
243 }
244 action_queue_.EnqueueAction(
245 ::frc971::autonomous::MakeAutonomousAction(params));
246 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800247
248 void StopAuto() {
249 LOG(INFO, "Stopping auto mode\n");
250 action_queue_.CancelAllActions();
251 }
252
Neil Balch07fee582018-01-27 15:46:49 -0800253 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800254 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800255
Neil Balchacfca5b2018-01-28 14:04:08 -0800256 bool was_running_ = false;
257 bool auto_running_ = false;
258
Austin Schuhcb091712018-02-21 20:01:55 -0800259 int arm_goal_position_ = 0;
260
Neil Balchacfca5b2018-01-28 14:04:08 -0800261 ::aos::common::actions::ActionQueue action_queue_;
262};
263
264} // namespace joysticks
265} // namespace input
266} // namespace y2018
267
268int main() {
269 ::aos::Init(-1);
270 ::y2018::input::joysticks::Reader reader;
271 reader.Run();
272 ::aos::Cleanup();
273}