blob: b9bbe3d1641766e3d1d80f09e94878850f2835f0 [file] [log] [blame]
Sabina Davis82b19182017-11-10 09:30:25 -08001#include <math.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/actions/actions.h"
7#include "aos/input/driver_station_data.h"
8#include "aos/logging/logging.h"
9#include "aos/time/time.h"
10#include "aos/util/log_interval.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -070011#include "aos/input/drivetrain_input.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080012#include "aos/input/joystick_input.h"
13#include "aos/linux_code/init.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080014#include "frc971/autonomous/auto.q.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080015#include "frc971/autonomous/base_autonomous_actor.h"
16#include "frc971/control_loops/drivetrain/drivetrain.q.h"
17#include "y2017/constants.h"
18#include "y2017/control_loops/superstructure/superstructure.q.h"
Sabina Davis82b19182017-11-10 09:30:25 -080019#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080020
21using ::frc971::control_loops::drivetrain_queue;
22using ::y2017::control_loops::superstructure_queue;
23
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::ControlBit;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070028using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080029
30namespace y2017 {
31namespace input {
32namespace joysticks {
33
Austin Schuh55c8d302017-04-05 19:25:37 -070034const ButtonLocation kGearSlotBack(2, 11);
35
Campbell Crowley71b5f132017-02-18 13:16:08 -080036const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070037const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080038const ButtonLocation kIntakeIn(3, 12);
39const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080040const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070041const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080042const ButtonLocation kMiddleShot(3, 6);
43const POVLocation kFarShot(3, 270);
44
45const ButtonLocation kVisionAlign(3, 5);
46
47const ButtonLocation kReverseIndexer(3, 4);
48const ButtonLocation kExtra1(3, 11);
49const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070050const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080051
Sabina Davis92d2efa2017-11-04 22:35:25 -070052std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
53
Campbell Crowley71b5f132017-02-18 13:16:08 -080054class Reader : public ::aos::input::JoystickInput {
55 public:
Sabina Davis92d2efa2017-11-04 22:35:25 -070056 Reader() {
57 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Sabina Davis82b19182017-11-10 09:30:25 -080058 DrivetrainInputReader::InputType::kSteeringWheel,
59 ::y2017::control_loops::drivetrain::GetDrivetrainConfig());
Sabina Davis92d2efa2017-11-04 22:35:25 -070060 }
Campbell Crowley71b5f132017-02-18 13:16:08 -080061
Parker Schuh208a58d2017-04-12 20:51:38 -070062 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070063
Austin Schuh4b5f7df2017-04-16 20:31:12 -070064 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
Austin Schuhd0629b12017-03-22 22:37:16 -070065
Campbell Crowley71b5f132017-02-18 13:16:08 -080066 void RunIteration(const ::aos::input::driver_station::Data &data) override {
67 bool last_auto_running = auto_running_;
68 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
69 data.GetControlBit(ControlBit::kEnabled);
70 if (auto_running_ != last_auto_running) {
71 if (auto_running_) {
72 StartAuto();
73 } else {
74 StopAuto();
75 }
76 }
77
Campbell Crowley71b5f132017-02-18 13:16:08 -080078 if (!auto_running_) {
79 HandleDrivetrain(data);
80 HandleTeleop(data);
81 }
82
83 // Process any pending actions.
84 action_queue_.Tick();
85 was_running_ = action_queue_.Running();
86 }
Austin Schuhd0629b12017-03-22 22:37:16 -070087 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -080088
89 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070090 drivetrain_input_reader_->HandleDrivetrain(data);
91 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
Campbell Crowley71b5f132017-02-18 13:16:08 -080092 }
93
94 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
95 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070096 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080097 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070098 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -080099
100 if (!data.GetControlBit(ControlBit::kEnabled)) {
101 action_queue_.CancelAllActions();
102 LOG(DEBUG, "Canceling\n");
103 }
104
105 superstructure_queue.status.FetchLatest();
106 if (!superstructure_queue.status.get()) {
107 LOG(ERROR, "Got no superstructure status packet.\n");
108 return;
109 }
110
Austin Schuhd0629b12017-03-22 22:37:16 -0700111 if (data.IsPressed(kIntakeUp)) {
112 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -0700113 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700114 }
Austin Schuh405724e2017-04-09 18:34:18 -0700115 if (data.IsPressed(kIntakeDown)) {
116 intake_goal_ = 0.235;
117 // Don't go quite so far out since we have a gear mech out now.
118 if (data.IsPressed(kIntakeUp)) {
119 intake_goal_ = 0.160;
120 }
121 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700122
Campbell Crowley71b5f132017-02-18 13:16:08 -0800123 if (data.IsPressed(kVisionAlign)) {
124 // Align shot using vision
125 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -0800126 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700127 vision_track = true;
128 }
129 if (data.PosEdge(kMiddleShot)) {
130 turret_goal_ = -M_PI;
131 }
132 if (data.PosEdge(kFarShot)) {
133 turret_goal_ = 0.0;
134 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700135 if (data.PosEdge(kVisionDistanceShot)) {
136 turret_goal_ = 0.0;
137 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700138
Parker Schuh208a58d2017-04-12 20:51:38 -0700139 if (data.IsPressed(kVisionDistanceShot)) {
140 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800141 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700142 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800143 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700144 last_shot_distance_ = ShotDistance::FAR_SHOT;
145 }
146
Parker Schuh208a58d2017-04-12 20:51:38 -0700147 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700148 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
149 data.IsPressed(kFire)) {
150 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700151 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700152 hood_goal_ = 0.10;
153 shooter_velocity_ = 300.0;
154
Parker Schuh208a58d2017-04-12 20:51:38 -0700155 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700156 break;
157 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700158 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700159 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700160 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700161 break;
162 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700163 hood_goal_ = 0.47;
164 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700165 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700166 break;
167 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800168 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700169 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800170 shooter_velocity_ = 0.0;
171 }
172
173 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700174 //turret_goal_ = -M_PI * 3.0 / 4.0;
175 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800176 }
177 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700178 //turret_goal_ = M_PI * 3.0 / 4.0;
179 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800180 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700181 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800182
183 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700184 if (data.IsPressed(kVisionAlign)) {
185 fire_ = fire_ && superstructure_queue.status->turret.vision_tracking;
186 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800187
188 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700189 if (data.IsPressed(kHang)) {
190 intake_goal_ = 0.23;
191 }
192
Campbell Crowley71b5f132017-02-18 13:16:08 -0800193 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800194 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800195 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700196 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800197 new_superstructure_goal->hood.angle = hood_goal_;
198 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
199
Austin Schuh6a8131b2017-04-08 15:39:22 -0700200 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700201 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700202 } else {
203 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700204 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700205 }
206
Campbell Crowley71b5f132017-02-18 13:16:08 -0800207 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800208 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
209
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700210 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700211 if (vision_track) {
212 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
213 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
214 } else {
215 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
216 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
217 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800218 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
219
Austin Schuh3028b1d2017-03-11 22:12:13 -0800220 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800221 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800222
Parker Schuh208a58d2017-04-12 20:51:38 -0700223 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
224 new_superstructure_goal->use_vision_for_shots = true;
225 } else {
226 new_superstructure_goal->use_vision_for_shots = false;
227 }
228
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700229 if (superstructure_queue.status->intake.position >
230 superstructure_queue.status->intake.unprofiled_goal_position + 0.01) {
231 intake_accumulator_ = 10;
232 }
233 if (intake_accumulator_ > 0) {
234 --intake_accumulator_;
235 if (!superstructure_queue.status->intake.estopped) {
236 new_superstructure_goal->intake.voltage_rollers = 10.0;
237 }
238 }
239
Campbell Crowley71b5f132017-02-18 13:16:08 -0800240 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700241 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800242 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700243 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
244 if (robot_velocity_ > 2.0) {
245 new_superstructure_goal->intake.voltage_rollers = 12.0;
246 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700247 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700248 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800249 } else if (data.IsPressed(kIntakeOut)) {
250 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800251 }
252 if (intake_goal_ < 0.1) {
253 new_superstructure_goal->intake.voltage_rollers =
254 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800255 }
256
257 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700258 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800259 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800260 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800261 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700262 new_superstructure_goal->indexer.voltage_rollers = 12.0;
263 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700264 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700265 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700266 break;
267 case ShotDistance::MIDDLE_SHOT:
268 case ShotDistance::FAR_SHOT:
269 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
270 break;
271 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800272 } else {
273 new_superstructure_goal->indexer.voltage_rollers = 0.0;
274 new_superstructure_goal->indexer.angular_velocity = 0.0;
275 }
276
277 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
278 if (!new_superstructure_goal.Send()) {
279 LOG(ERROR, "Sending superstructure goal failed.\n");
280 }
281 }
282
283 private:
Austin Schuh3028b1d2017-03-11 22:12:13 -0800284 void StartAuto() {
285 LOG(INFO, "Starting auto mode\n");
286
287 ::frc971::autonomous::AutonomousActionParams params;
288 ::frc971::autonomous::auto_mode.FetchLatest();
289 if (::frc971::autonomous::auto_mode.get() != nullptr) {
290 params.mode = ::frc971::autonomous::auto_mode->mode;
291 } else {
292 LOG(WARNING, "no auto mode values\n");
293 params.mode = 0;
294 }
295 action_queue_.EnqueueAction(
296 ::frc971::autonomous::MakeAutonomousAction(params));
297 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800298
299 void StopAuto() {
300 LOG(INFO, "Stopping auto mode\n");
301 action_queue_.CancelAllActions();
302 }
303
304 // Current goals to send to the robot.
305 double intake_goal_ = 0.0;
306 double turret_goal_ = 0.0;
307 double hood_goal_ = 0.3;
308 double shooter_velocity_ = 0.0;
309
Campbell Crowley71b5f132017-02-18 13:16:08 -0800310 bool was_running_ = false;
311 bool auto_running_ = false;
312
Parker Schuh208a58d2017-04-12 20:51:38 -0700313 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800314
315 bool fire_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700316 double robot_velocity_ = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800317
318 ::aos::common::actions::ActionQueue action_queue_;
319};
320
321} // namespace joysticks
322} // namespace input
323} // namespace y2017
324
325int main() {
326 ::aos::Init(-1);
327 ::y2017::input::joysticks::Reader reader;
328 reader.Run();
329 ::aos::Cleanup();
330}