blob: 2baa14a720a4b83b6be137c950c56996c6bafeff [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"
14#include "frc971/control_loops/drivetrain/drivetrain.q.h"
15#include "y2018/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhab15c4d2018-03-09 21:21:03 -080016#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Neil Balch07fee582018-01-27 15:46:49 -080017#include "y2018/control_loops/superstructure/superstructure.q.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080018
19using ::frc971::control_loops::drivetrain_queue;
Neil Balch07fee582018-01-27 15:46:49 -080020using ::y2018::control_loops::superstructure_queue;
Neil Balchacfca5b2018-01-28 14:04:08 -080021
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::ControlBit;
24using ::aos::input::driver_station::JoystickAxis;
25using ::aos::input::driver_station::POVLocation;
26using ::aos::input::DrivetrainInputReader;
27
28namespace y2018 {
29namespace input {
30namespace joysticks {
31
Austin Schuhab15c4d2018-03-09 21:21:03 -080032namespace arm = ::y2018::control_loops::superstructure::arm;
Neil Balch07fee582018-01-27 15:46:49 -080033
Austin Schuhab15c4d2018-03-09 21:21:03 -080034const ButtonLocation kIntakeClosed(4, 1);
Austin Schuh47d74942018-03-04 01:15:59 -080035
Austin Schuhab15c4d2018-03-09 21:21:03 -080036const ButtonLocation kIntakeIn(3, 16);
37const ButtonLocation kIntakeOut(4, 3);
Neil Balch07fee582018-01-27 15:46:49 -080038
Austin Schuhab15c4d2018-03-09 21:21:03 -080039const ButtonLocation kArmFrontHighBox(4, 5);
40const ButtonLocation kArmFrontExtraHighBox(3, 8);
41const ButtonLocation kArmFrontMiddle2Box(4, 7);
42const ButtonLocation kArmFrontMiddle1Box(4, 9);
43const ButtonLocation kArmFrontLowBox(4, 11);
44const ButtonLocation kArmFrontSwitch(3, 14);
45
46const ButtonLocation kArmBackHighBox(4, 6);
47const ButtonLocation kArmBackExtraHighBox(3, 10);
48const ButtonLocation kArmBackMiddle2Box(4, 8);
49const ButtonLocation kArmBackMiddle1Box(4, 10);
50const ButtonLocation kArmBackLowBox(4, 12);
51const ButtonLocation kArmBackSwitch(3, 12);
52
53const ButtonLocation kArmNeutral(3, 13);
54const ButtonLocation kArmUp(3, 9);
55
56const ButtonLocation kArmPickupBoxFromIntake(3, 6);
57
58const ButtonLocation kClawOpen(3, 1);
Neil Balch07fee582018-01-27 15:46:49 -080059
60const ButtonLocation kForkDeploy(3, 11);
61const ButtonLocation kForkStow(3, 10);
62
Neil Balchacfca5b2018-01-28 14:04:08 -080063std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
64
65class Reader : public ::aos::input::JoystickInput {
66 public:
67 Reader() {
68 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080069 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080070 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
71 }
72
73 void RunIteration(const ::aos::input::driver_station::Data &data) override {
74 bool last_auto_running = auto_running_;
75 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
76 data.GetControlBit(ControlBit::kEnabled);
77 if (auto_running_ != last_auto_running) {
78 if (auto_running_) {
79 StartAuto();
80 } else {
81 StopAuto();
82 }
83 }
84
85 if (!auto_running_) {
86 HandleDrivetrain(data);
87 HandleTeleop(data);
88 }
89
90 // Process any pending actions.
91 action_queue_.Tick();
92 was_running_ = action_queue_.Running();
93 }
94
95 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
96 drivetrain_input_reader_->HandleDrivetrain(data);
97 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
98 }
99
100 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
101 if (!data.GetControlBit(ControlBit::kEnabled)) {
102 action_queue_.CancelAllActions();
103 LOG(DEBUG, "Canceling\n");
104 }
Neil Balch07fee582018-01-27 15:46:49 -0800105
Austin Schuhab15c4d2018-03-09 21:21:03 -0800106 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800107 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800108 if (!superstructure_queue.status.get() ||
109 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800110 LOG(ERROR, "Got no superstructure status packet.\n");
111 return;
112 }
113
Austin Schuh47d74942018-03-04 01:15:59 -0800114 if (data.IsPressed(kIntakeClosed)) {
Neil Balch07fee582018-01-27 15:46:49 -0800115 // Deploy the intake.
Austin Schuhab15c4d2018-03-09 21:21:03 -0800116 if (superstructure_queue.position->box_back_beambreak_triggered) {
117 intake_goal_ = 0.30;
118 } else {
119 intake_goal_ = 0.07;
120 }
121 } else {
122 // Bring in the intake.
Austin Schuhf88d3ac2018-03-05 00:26:36 -0800123 intake_goal_ = -3.3;
Neil Balch07fee582018-01-27 15:46:49 -0800124 }
125
126 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
127
Sabina Davisfdd7a112018-02-04 16:16:23 -0800128 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
129 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800130
Austin Schuhab15c4d2018-03-09 21:21:03 -0800131 if (data.IsPressed(kIntakeIn) || data.IsPressed(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800132 // Turn on the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800133 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800134 } else if (data.IsPressed(kIntakeOut)) {
135 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800136 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800137 } else {
138 // We don't want the rollers on.
139 new_superstructure_goal->intake.roller_voltage = 0.0;
140 }
141
Austin Schuhb874fd32018-03-05 00:27:10 -0800142 // If we are disabled, stay at the node closest to where we start. This
143 // should remove long motions when enabled.
144 if (!data.GetControlBit(ControlBit::kEnabled)) {
145 arm_goal_position_ = superstructure_queue.status->arm.current_node;
146 }
147
Austin Schuhab15c4d2018-03-09 21:21:03 -0800148 bool grab_box = false;
149 if (data.IsPressed(kArmPickupBoxFromIntake)) {
150 arm_goal_position_ = arm::NeutralIndex();
151 grab_box = true;
152 } else if (data.IsPressed(kArmNeutral)) {
153 arm_goal_position_ = arm::NeutralIndex();
154 } else if (data.IsPressed(kArmUp)) {
155 arm_goal_position_ = arm::UpIndex();
156 } else if (data.IsPressed(kArmFrontSwitch)) {
157 arm_goal_position_ = arm::FrontSwitchIndex();
158 } else if (data.IsPressed(kArmFrontHighBox) ||
159 data.IsPressed(kArmFrontExtraHighBox)) {
160 arm_goal_position_ = arm::FrontHighBoxIndex();
161 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
162 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
163 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
164 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
165 } else if (data.IsPressed(kArmFrontLowBox)) {
166 arm_goal_position_ = arm::FrontLowBoxIndex();
167 } else if (data.IsPressed(kArmBackHighBox) ||
168 data.IsPressed(kArmBackExtraHighBox)) {
169 arm_goal_position_ = arm::BackHighBoxIndex();
170 } else if (data.IsPressed(kArmBackMiddle2Box)) {
171 arm_goal_position_ = arm::BackMiddle2BoxIndex();
172 } else if (data.IsPressed(kArmBackMiddle1Box)) {
173 arm_goal_position_ = arm::BackMiddle1BoxIndex();
174 } else if (data.IsPressed(kArmBackLowBox)) {
175 arm_goal_position_ = arm::BackLowBoxIndex();
176 } else if (data.IsPressed(kArmBackSwitch)) {
177 arm_goal_position_ = arm::BackSwitchIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800178 }
179
Austin Schuhcb091712018-02-21 20:01:55 -0800180 new_superstructure_goal->arm_goal_position = arm_goal_position_;
181
Austin Schuhb874fd32018-03-05 00:27:10 -0800182 if (data.IsPressed(kClawOpen) || data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800183 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800184 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800185 new_superstructure_goal->open_claw = false;
186 }
187
188 if (data.IsPressed(kForkDeploy)) {
189 new_superstructure_goal->deploy_fork = true;
190 } else if (data.IsPressed(kForkStow)) {
191 new_superstructure_goal->deploy_fork = false;
192 }
Austin Schuhab15c4d2018-03-09 21:21:03 -0800193 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800194
195 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
196 if (!new_superstructure_goal.Send()) {
197 LOG(ERROR, "Sending superstructure goal failed.\n");
198 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800199 }
200
201 private:
202 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
203
204 void StopAuto() {
205 LOG(INFO, "Stopping auto mode\n");
206 action_queue_.CancelAllActions();
207 }
208
Neil Balch07fee582018-01-27 15:46:49 -0800209 // Current goals to send to the robot.
Austin Schuh17dd0892018-03-02 20:06:31 -0800210 double intake_goal_ = -M_PI * 2.0 / 3.0;
Neil Balch07fee582018-01-27 15:46:49 -0800211
Neil Balchacfca5b2018-01-28 14:04:08 -0800212 bool was_running_ = false;
213 bool auto_running_ = false;
214
215 double robot_velocity_ = 0.0;
216
Austin Schuhcb091712018-02-21 20:01:55 -0800217 int arm_goal_position_ = 0;
218
Neil Balchacfca5b2018-01-28 14:04:08 -0800219 ::aos::common::actions::ActionQueue action_queue_;
220};
221
222} // namespace joysticks
223} // namespace input
224} // namespace y2018
225
226int main() {
227 ::aos::Init(-1);
228 ::y2018::input::joysticks::Reader reader;
229 reader.Run();
230 ::aos::Cleanup();
231}