blob: a916218325339112f0a24ec126b1905d8286cd06 [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.
123 intake_goal_ = -M_PI * 2.0 / 3.0;
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 Schuhab15c4d2018-03-09 21:21:03 -0800142 bool grab_box = false;
143 if (data.IsPressed(kArmPickupBoxFromIntake)) {
144 arm_goal_position_ = arm::NeutralIndex();
145 grab_box = true;
146 } else if (data.IsPressed(kArmNeutral)) {
147 arm_goal_position_ = arm::NeutralIndex();
148 } else if (data.IsPressed(kArmUp)) {
149 arm_goal_position_ = arm::UpIndex();
150 } else if (data.IsPressed(kArmFrontSwitch)) {
151 arm_goal_position_ = arm::FrontSwitchIndex();
152 } else if (data.IsPressed(kArmFrontHighBox) ||
153 data.IsPressed(kArmFrontExtraHighBox)) {
154 arm_goal_position_ = arm::FrontHighBoxIndex();
155 } else if (data.IsPressed(kArmFrontMiddle2Box)) {
156 arm_goal_position_ = arm::FrontMiddle2BoxIndex();
157 } else if (data.IsPressed(kArmFrontMiddle1Box)) {
158 arm_goal_position_ = arm::FrontMiddle1BoxIndex();
159 } else if (data.IsPressed(kArmFrontLowBox)) {
160 arm_goal_position_ = arm::FrontLowBoxIndex();
161 } else if (data.IsPressed(kArmBackHighBox) ||
162 data.IsPressed(kArmBackExtraHighBox)) {
163 arm_goal_position_ = arm::BackHighBoxIndex();
164 } else if (data.IsPressed(kArmBackMiddle2Box)) {
165 arm_goal_position_ = arm::BackMiddle2BoxIndex();
166 } else if (data.IsPressed(kArmBackMiddle1Box)) {
167 arm_goal_position_ = arm::BackMiddle1BoxIndex();
168 } else if (data.IsPressed(kArmBackLowBox)) {
169 arm_goal_position_ = arm::BackLowBoxIndex();
170 } else if (data.IsPressed(kArmBackSwitch)) {
171 arm_goal_position_ = arm::BackSwitchIndex();
Neil Balch07fee582018-01-27 15:46:49 -0800172 }
173
Austin Schuhcb091712018-02-21 20:01:55 -0800174 new_superstructure_goal->arm_goal_position = arm_goal_position_;
175
Neil Balch07fee582018-01-27 15:46:49 -0800176 if (data.IsPressed(kClawOpen)) {
177 new_superstructure_goal->open_claw = true;
Austin Schuhab15c4d2018-03-09 21:21:03 -0800178 } else {
Neil Balch07fee582018-01-27 15:46:49 -0800179 new_superstructure_goal->open_claw = false;
180 }
181
182 if (data.IsPressed(kForkDeploy)) {
183 new_superstructure_goal->deploy_fork = true;
184 } else if (data.IsPressed(kForkStow)) {
185 new_superstructure_goal->deploy_fork = false;
186 }
Austin Schuhab15c4d2018-03-09 21:21:03 -0800187 new_superstructure_goal->grab_box = grab_box;
Neil Balch07fee582018-01-27 15:46:49 -0800188
189 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
190 if (!new_superstructure_goal.Send()) {
191 LOG(ERROR, "Sending superstructure goal failed.\n");
192 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800193 }
194
195 private:
196 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
197
198 void StopAuto() {
199 LOG(INFO, "Stopping auto mode\n");
200 action_queue_.CancelAllActions();
201 }
202
Neil Balch07fee582018-01-27 15:46:49 -0800203 // Current goals to send to the robot.
Austin Schuh17dd0892018-03-02 20:06:31 -0800204 double intake_goal_ = -M_PI * 2.0 / 3.0;
Neil Balch07fee582018-01-27 15:46:49 -0800205
Neil Balchacfca5b2018-01-28 14:04:08 -0800206 bool was_running_ = false;
207 bool auto_running_ = false;
208
209 double robot_velocity_ = 0.0;
210
Austin Schuhcb091712018-02-21 20:01:55 -0800211 int arm_goal_position_ = 0;
212
Neil Balchacfca5b2018-01-28 14:04:08 -0800213 ::aos::common::actions::ActionQueue action_queue_;
214};
215
216} // namespace joysticks
217} // namespace input
218} // namespace y2018
219
220int main() {
221 ::aos::Init(-1);
222 ::y2018::input::joysticks::Reader reader;
223 reader.Run();
224 ::aos::Cleanup();
225}