blob: ce46ec78a4a2c610d5a705853923843ef6494929 [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 Schuh47d74942018-03-04 01:15:59 -080038
Austin Schuhab15c4d2018-03-09 21:21:03 -080039const ButtonLocation kIntakeIn(3, 16);
40const ButtonLocation kIntakeOut(4, 3);
Neil Balch07fee582018-01-27 15:46:49 -080041
Austin Schuhab15c4d2018-03-09 21:21:03 -080042const ButtonLocation kArmFrontHighBox(4, 5);
43const ButtonLocation kArmFrontExtraHighBox(3, 8);
44const ButtonLocation kArmFrontMiddle2Box(4, 7);
45const ButtonLocation kArmFrontMiddle1Box(4, 9);
46const ButtonLocation kArmFrontLowBox(4, 11);
47const ButtonLocation kArmFrontSwitch(3, 14);
48
49const ButtonLocation kArmBackHighBox(4, 6);
50const ButtonLocation kArmBackExtraHighBox(3, 10);
51const ButtonLocation kArmBackMiddle2Box(4, 8);
52const ButtonLocation kArmBackMiddle1Box(4, 10);
53const ButtonLocation kArmBackLowBox(4, 12);
54const ButtonLocation kArmBackSwitch(3, 12);
55
Austin Schuh17e484e2018-03-11 01:11:36 -080056const ButtonLocation kArmAboveHang(3, 7);
57const ButtonLocation kArmBelowHang(3, 2);
58
59const ButtonLocation kWinch(3, 5);
60
Austin Schuhab15c4d2018-03-09 21:21:03 -080061const ButtonLocation kArmNeutral(3, 13);
62const ButtonLocation kArmUp(3, 9);
63
64const ButtonLocation kArmPickupBoxFromIntake(3, 6);
65
66const ButtonLocation kClawOpen(3, 1);
Neil Balch07fee582018-01-27 15:46:49 -080067
68const ButtonLocation kForkDeploy(3, 11);
69const ButtonLocation kForkStow(3, 10);
70
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
Austin Schuh47d74942018-03-04 01:15:59 -0800132 if (data.IsPressed(kIntakeClosed)) {
Neil Balch07fee582018-01-27 15:46:49 -0800133 // Deploy the intake.
Austin Schuhab15c4d2018-03-09 21:21:03 -0800134 if (superstructure_queue.position->box_back_beambreak_triggered) {
135 intake_goal_ = 0.30;
136 } else {
137 intake_goal_ = 0.07;
138 }
139 } else {
140 // Bring in the intake.
Austin Schuhf88d3ac2018-03-05 00:26:36 -0800141 intake_goal_ = -3.3;
Neil Balch07fee582018-01-27 15:46:49 -0800142 }
143
144 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
145
Sabina Davisfdd7a112018-02-04 16:16:23 -0800146 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
147 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800148
Austin Schuhab15c4d2018-03-09 21:21:03 -0800149 if (data.IsPressed(kIntakeIn) || data.IsPressed(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800150 // Turn on the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800151 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800152 } else if (data.IsPressed(kIntakeOut)) {
153 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800154 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800155 } else {
156 // We don't want the rollers on.
157 new_superstructure_goal->intake.roller_voltage = 0.0;
158 }
159
Austin Schuhb874fd32018-03-05 00:27:10 -0800160 // If we are disabled, stay at the node closest to where we start. This
161 // should remove long motions when enabled.
162 if (!data.GetControlBit(ControlBit::kEnabled)) {
163 arm_goal_position_ = superstructure_queue.status->arm.current_node;
164 }
165
Austin Schuhab15c4d2018-03-09 21:21:03 -0800166 bool grab_box = false;
167 if (data.IsPressed(kArmPickupBoxFromIntake)) {
168 arm_goal_position_ = arm::NeutralIndex();
169 grab_box = true;
170 } else if (data.IsPressed(kArmNeutral)) {
171 arm_goal_position_ = arm::NeutralIndex();
172 } else if (data.IsPressed(kArmUp)) {
173 arm_goal_position_ = arm::UpIndex();
174 } else if (data.IsPressed(kArmFrontSwitch)) {
175 arm_goal_position_ = arm::FrontSwitchIndex();
176 } else if (data.IsPressed(kArmFrontHighBox) ||
177 data.IsPressed(kArmFrontExtraHighBox)) {
178 arm_goal_position_ = arm::FrontHighBoxIndex();
179 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
180 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
181 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
182 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
183 } else if (data.IsPressed(kArmFrontLowBox)) {
184 arm_goal_position_ = arm::FrontLowBoxIndex();
185 } else if (data.IsPressed(kArmBackHighBox) ||
186 data.IsPressed(kArmBackExtraHighBox)) {
187 arm_goal_position_ = arm::BackHighBoxIndex();
188 } else if (data.IsPressed(kArmBackMiddle2Box)) {
189 arm_goal_position_ = arm::BackMiddle2BoxIndex();
190 } else if (data.IsPressed(kArmBackMiddle1Box)) {
191 arm_goal_position_ = arm::BackMiddle1BoxIndex();
192 } else if (data.IsPressed(kArmBackLowBox)) {
193 arm_goal_position_ = arm::BackLowBoxIndex();
194 } else if (data.IsPressed(kArmBackSwitch)) {
195 arm_goal_position_ = arm::BackSwitchIndex();
Austin Schuh17e484e2018-03-11 01:11:36 -0800196 } else if (data.IsPressed(kArmAboveHang)) {
197 if (data.IsPressed(kIntakeIn)) {
198 arm_goal_position_ = arm::SelfHangIndex();
199 } else if (data.IsPressed(kIntakeOut)) {
200 arm_goal_position_ = arm::PartnerHangIndex();
201 } else {
202 arm_goal_position_ = arm::AboveHangIndex();
203 }
204 } else if (data.IsPressed(kArmBelowHang)) {
205 arm_goal_position_ = arm::BelowHangIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800206 }
207
Austin Schuh17e484e2018-03-11 01:11:36 -0800208 new_superstructure_goal->deploy_fork =
209 data.IsPressed(kArmAboveHang) && data.IsPressed(kClawOpen);
210
211 if (new_superstructure_goal->deploy_fork) {
212 intake_goal_ = -2.0;
213 }
214
215 if (data.IsPressed(kWinch)) {
216 new_superstructure_goal->voltage_winch = 12.0;
217 } else {
218 new_superstructure_goal->voltage_winch = 0.0;
219 }
220
221 new_superstructure_goal->hook_release = data.IsPressed(kArmBelowHang);
222
Austin Schuhcb091712018-02-21 20:01:55 -0800223 new_superstructure_goal->arm_goal_position = arm_goal_position_;
224
Austin Schuhb874fd32018-03-05 00:27:10 -0800225 if (data.IsPressed(kClawOpen) || data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800226 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800227 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800228 new_superstructure_goal->open_claw = false;
229 }
230
231 if (data.IsPressed(kForkDeploy)) {
232 new_superstructure_goal->deploy_fork = true;
233 } else if (data.IsPressed(kForkStow)) {
234 new_superstructure_goal->deploy_fork = false;
235 }
Austin Schuhab15c4d2018-03-09 21:21:03 -0800236 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800237
238 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
239 if (!new_superstructure_goal.Send()) {
240 LOG(ERROR, "Sending superstructure goal failed.\n");
241 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800242 }
243
244 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800245 void StartAuto() {
246 LOG(INFO, "Starting auto mode\n");
247
248 ::frc971::autonomous::AutonomousActionParams params;
249 ::frc971::autonomous::auto_mode.FetchLatest();
250 if (::frc971::autonomous::auto_mode.get() != nullptr) {
Austin Schuhc231df42018-03-21 20:43:24 -0700251 params.mode = ::frc971::autonomous::auto_mode->mode << 2;
Austin Schuha3c148e2018-03-09 21:04:05 -0800252 } else {
253 LOG(WARNING, "no auto mode values\n");
254 params.mode = 0;
255 }
Austin Schuhc231df42018-03-21 20:43:24 -0700256 // TODO(austin): use the mode later if we care. We don't care right now.
257 params.mode = static_cast<int>(::aos::joystick_state->switch_left) |
258 (static_cast<int>(::aos::joystick_state->scale_left) << 1);
Austin Schuha3c148e2018-03-09 21:04:05 -0800259 action_queue_.EnqueueAction(
260 ::frc971::autonomous::MakeAutonomousAction(params));
261 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800262
263 void StopAuto() {
264 LOG(INFO, "Stopping auto mode\n");
265 action_queue_.CancelAllActions();
266 }
267
Neil Balch07fee582018-01-27 15:46:49 -0800268 // Current goals to send to the robot.
Austin Schuh17e484e2018-03-11 01:11:36 -0800269 double intake_goal_ = 0.0;
Neil Balch07fee582018-01-27 15:46:49 -0800270
Neil Balchacfca5b2018-01-28 14:04:08 -0800271 bool was_running_ = false;
272 bool auto_running_ = false;
273
Austin Schuhcb091712018-02-21 20:01:55 -0800274 int arm_goal_position_ = 0;
275
Neil Balchacfca5b2018-01-28 14:04:08 -0800276 ::aos::common::actions::ActionQueue action_queue_;
277};
278
279} // namespace joysticks
280} // namespace input
281} // namespace y2018
282
283int main() {
284 ::aos::Init(-1);
285 ::y2018::input::joysticks::Reader reader;
286 reader.Run();
287 ::aos::Cleanup();
288}