blob: bb6c1b1882cdb4737eccfbb439f68e12a3478c8d [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
55const ButtonLocation kArmNeutral(3, 13);
56const ButtonLocation kArmUp(3, 9);
57
58const ButtonLocation kArmPickupBoxFromIntake(3, 6);
59
60const ButtonLocation kClawOpen(3, 1);
Neil Balch07fee582018-01-27 15:46:49 -080061
62const ButtonLocation kForkDeploy(3, 11);
63const ButtonLocation kForkStow(3, 10);
64
Neil Balchacfca5b2018-01-28 14:04:08 -080065std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
66
67class Reader : public ::aos::input::JoystickInput {
68 public:
69 Reader() {
70 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080071 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080072 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
73 }
74
75 void RunIteration(const ::aos::input::driver_station::Data &data) override {
76 bool last_auto_running = auto_running_;
77 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
78 data.GetControlBit(ControlBit::kEnabled);
79 if (auto_running_ != last_auto_running) {
80 if (auto_running_) {
81 StartAuto();
82 } else {
83 StopAuto();
84 }
85 }
86
87 if (!auto_running_) {
88 HandleDrivetrain(data);
89 HandleTeleop(data);
90 }
91
92 // Process any pending actions.
93 action_queue_.Tick();
94 was_running_ = action_queue_.Running();
95 }
96
97 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
98 drivetrain_input_reader_->HandleDrivetrain(data);
99 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
100 }
101
102 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
103 if (!data.GetControlBit(ControlBit::kEnabled)) {
104 action_queue_.CancelAllActions();
105 LOG(DEBUG, "Canceling\n");
106 }
Neil Balch07fee582018-01-27 15:46:49 -0800107
Austin Schuhab15c4d2018-03-09 21:21:03 -0800108 superstructure_queue.position.FetchLatest();
Neil Balch07fee582018-01-27 15:46:49 -0800109 superstructure_queue.status.FetchLatest();
Austin Schuhab15c4d2018-03-09 21:21:03 -0800110 if (!superstructure_queue.status.get() ||
111 !superstructure_queue.position.get()) {
Neil Balch07fee582018-01-27 15:46:49 -0800112 LOG(ERROR, "Got no superstructure status packet.\n");
113 return;
114 }
115
Austin Schuh47d74942018-03-04 01:15:59 -0800116 if (data.IsPressed(kIntakeClosed)) {
Neil Balch07fee582018-01-27 15:46:49 -0800117 // Deploy the intake.
Austin Schuhab15c4d2018-03-09 21:21:03 -0800118 if (superstructure_queue.position->box_back_beambreak_triggered) {
119 intake_goal_ = 0.30;
120 } else {
121 intake_goal_ = 0.07;
122 }
123 } else {
124 // Bring in the intake.
Austin Schuhf88d3ac2018-03-05 00:26:36 -0800125 intake_goal_ = -3.3;
Neil Balch07fee582018-01-27 15:46:49 -0800126 }
127
128 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
129
Sabina Davisfdd7a112018-02-04 16:16:23 -0800130 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
131 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800132
Austin Schuhab15c4d2018-03-09 21:21:03 -0800133 if (data.IsPressed(kIntakeIn) || data.IsPressed(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800134 // Turn on the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800135 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800136 } else if (data.IsPressed(kIntakeOut)) {
137 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800138 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800139 } else {
140 // We don't want the rollers on.
141 new_superstructure_goal->intake.roller_voltage = 0.0;
142 }
143
Austin Schuhb874fd32018-03-05 00:27:10 -0800144 // If we are disabled, stay at the node closest to where we start. This
145 // should remove long motions when enabled.
146 if (!data.GetControlBit(ControlBit::kEnabled)) {
147 arm_goal_position_ = superstructure_queue.status->arm.current_node;
148 }
149
Austin Schuhab15c4d2018-03-09 21:21:03 -0800150 bool grab_box = false;
151 if (data.IsPressed(kArmPickupBoxFromIntake)) {
152 arm_goal_position_ = arm::NeutralIndex();
153 grab_box = true;
154 } else if (data.IsPressed(kArmNeutral)) {
155 arm_goal_position_ = arm::NeutralIndex();
156 } else if (data.IsPressed(kArmUp)) {
157 arm_goal_position_ = arm::UpIndex();
158 } else if (data.IsPressed(kArmFrontSwitch)) {
159 arm_goal_position_ = arm::FrontSwitchIndex();
160 } else if (data.IsPressed(kArmFrontHighBox) ||
161 data.IsPressed(kArmFrontExtraHighBox)) {
162 arm_goal_position_ = arm::FrontHighBoxIndex();
163 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
164 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
165 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
166 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
167 } else if (data.IsPressed(kArmFrontLowBox)) {
168 arm_goal_position_ = arm::FrontLowBoxIndex();
169 } else if (data.IsPressed(kArmBackHighBox) ||
170 data.IsPressed(kArmBackExtraHighBox)) {
171 arm_goal_position_ = arm::BackHighBoxIndex();
172 } else if (data.IsPressed(kArmBackMiddle2Box)) {
173 arm_goal_position_ = arm::BackMiddle2BoxIndex();
174 } else if (data.IsPressed(kArmBackMiddle1Box)) {
175 arm_goal_position_ = arm::BackMiddle1BoxIndex();
176 } else if (data.IsPressed(kArmBackLowBox)) {
177 arm_goal_position_ = arm::BackLowBoxIndex();
178 } else if (data.IsPressed(kArmBackSwitch)) {
179 arm_goal_position_ = arm::BackSwitchIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800180 }
181
Austin Schuhcb091712018-02-21 20:01:55 -0800182 new_superstructure_goal->arm_goal_position = arm_goal_position_;
183
Austin Schuhb874fd32018-03-05 00:27:10 -0800184 if (data.IsPressed(kClawOpen) || data.PosEdge(kArmPickupBoxFromIntake)) {
Neil Balch07fee582018-01-27 15:46:49 -0800185 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800186 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800187 new_superstructure_goal->open_claw = false;
188 }
189
190 if (data.IsPressed(kForkDeploy)) {
191 new_superstructure_goal->deploy_fork = true;
192 } else if (data.IsPressed(kForkStow)) {
193 new_superstructure_goal->deploy_fork = false;
194 }
Austin Schuhab15c4d2018-03-09 21:21:03 -0800195 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800196
197 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
198 if (!new_superstructure_goal.Send()) {
199 LOG(ERROR, "Sending superstructure goal failed.\n");
200 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800201 }
202
203 private:
Austin Schuha3c148e2018-03-09 21:04:05 -0800204 void StartAuto() {
205 LOG(INFO, "Starting auto mode\n");
206
207 ::frc971::autonomous::AutonomousActionParams params;
208 ::frc971::autonomous::auto_mode.FetchLatest();
209 if (::frc971::autonomous::auto_mode.get() != nullptr) {
210 params.mode = ::frc971::autonomous::auto_mode->mode;
211 } else {
212 LOG(WARNING, "no auto mode values\n");
213 params.mode = 0;
214 }
215 action_queue_.EnqueueAction(
216 ::frc971::autonomous::MakeAutonomousAction(params));
217 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800218
219 void StopAuto() {
220 LOG(INFO, "Stopping auto mode\n");
221 action_queue_.CancelAllActions();
222 }
223
Neil Balch07fee582018-01-27 15:46:49 -0800224 // Current goals to send to the robot.
Austin Schuh17dd0892018-03-02 20:06:31 -0800225 double intake_goal_ = -M_PI * 2.0 / 3.0;
Neil Balch07fee582018-01-27 15:46:49 -0800226
Neil Balchacfca5b2018-01-28 14:04:08 -0800227 bool was_running_ = false;
228 bool auto_running_ = false;
229
230 double robot_velocity_ = 0.0;
231
Austin Schuhcb091712018-02-21 20:01:55 -0800232 int arm_goal_position_ = 0;
233
Neil Balchacfca5b2018-01-28 14:04:08 -0800234 ::aos::common::actions::ActionQueue action_queue_;
235};
236
237} // namespace joysticks
238} // namespace input
239} // namespace y2018
240
241int main() {
242 ::aos::Init(-1);
243 ::y2018::input::joysticks::Reader reader;
244 reader.Run();
245 ::aos::Cleanup();
246}