blob: e6c2f54c283558d51ace5d7ef8a0ea585c1274ce [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"
Neil Balch07fee582018-01-27 15:46:49 -080016#include "y2018/control_loops/superstructure/superstructure.q.h"
Neil Balchacfca5b2018-01-28 14:04:08 -080017
18using ::frc971::control_loops::drivetrain_queue;
Neil Balch07fee582018-01-27 15:46:49 -080019using ::y2018::control_loops::superstructure_queue;
Neil Balchacfca5b2018-01-28 14:04:08 -080020
21using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::ControlBit;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::POVLocation;
25using ::aos::input::DrivetrainInputReader;
26
27namespace y2018 {
28namespace input {
29namespace joysticks {
30
Austin Schuh47d74942018-03-04 01:15:59 -080031const ButtonLocation kIntakeClosed(4, 5);
32const ButtonLocation kIntakeOpen(4, 4);
Neil Balch07fee582018-01-27 15:46:49 -080033
Austin Schuh47d74942018-03-04 01:15:59 -080034const ButtonLocation kIntakeIn(4, 3);
35const ButtonLocation kIntakeOut(4, 8);
36
37const ButtonLocation kArmDown(3, 6);
38const ButtonLocation kArmSwitch(4, 10);
39const ButtonLocation kArmScale(3, 10);
40const ButtonLocation kArmBack(4, 9);
Neil Balch07fee582018-01-27 15:46:49 -080041
42const ButtonLocation kClawOpen(3, 5);
43const ButtonLocation kClawClose(3, 4);
44
45const ButtonLocation kForkDeploy(3, 11);
46const ButtonLocation kForkStow(3, 10);
47
Neil Balchacfca5b2018-01-28 14:04:08 -080048std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
49
50class Reader : public ::aos::input::JoystickInput {
51 public:
52 Reader() {
53 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080054 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080055 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
56 }
57
58 void RunIteration(const ::aos::input::driver_station::Data &data) override {
59 bool last_auto_running = auto_running_;
60 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
61 data.GetControlBit(ControlBit::kEnabled);
62 if (auto_running_ != last_auto_running) {
63 if (auto_running_) {
64 StartAuto();
65 } else {
66 StopAuto();
67 }
68 }
69
70 if (!auto_running_) {
71 HandleDrivetrain(data);
72 HandleTeleop(data);
73 }
74
75 // Process any pending actions.
76 action_queue_.Tick();
77 was_running_ = action_queue_.Running();
78 }
79
80 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
81 drivetrain_input_reader_->HandleDrivetrain(data);
82 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
83 }
84
85 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
86 if (!data.GetControlBit(ControlBit::kEnabled)) {
87 action_queue_.CancelAllActions();
88 LOG(DEBUG, "Canceling\n");
89 }
Neil Balch07fee582018-01-27 15:46:49 -080090
91 superstructure_queue.status.FetchLatest();
92 if (!superstructure_queue.status.get()) {
93 LOG(ERROR, "Got no superstructure status packet.\n");
94 return;
95 }
96
Austin Schuh47d74942018-03-04 01:15:59 -080097 if (data.IsPressed(kIntakeOpen)) {
Neil Balch07fee582018-01-27 15:46:49 -080098 // Bring in the intake.
Austin Schuh17dd0892018-03-02 20:06:31 -080099 intake_goal_ = -M_PI * 2.0 / 3.0;
Neil Balch07fee582018-01-27 15:46:49 -0800100 }
Austin Schuh47d74942018-03-04 01:15:59 -0800101 if (data.IsPressed(kIntakeClosed)) {
Neil Balch07fee582018-01-27 15:46:49 -0800102 // Deploy the intake.
Austin Schuh17dd0892018-03-02 20:06:31 -0800103 intake_goal_ = 0.25;
Neil Balch07fee582018-01-27 15:46:49 -0800104 }
105
106 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
107
Sabina Davisfdd7a112018-02-04 16:16:23 -0800108 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
109 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800110
111 if (data.IsPressed(kIntakeIn)) {
112 // Turn on the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800113 new_superstructure_goal->intake.roller_voltage = 8.0;
Neil Balch07fee582018-01-27 15:46:49 -0800114 } else if (data.IsPressed(kIntakeOut)) {
115 // Turn off the rollers.
Austin Schuh17dd0892018-03-02 20:06:31 -0800116 new_superstructure_goal->intake.roller_voltage = -12.0;
Neil Balch07fee582018-01-27 15:46:49 -0800117 } else {
118 // We don't want the rollers on.
119 new_superstructure_goal->intake.roller_voltage = 0.0;
120 }
121
122 if (data.IsPressed(kArmDown)) {
123 // Put the arm down to the intake level.
Austin Schuhcb091712018-02-21 20:01:55 -0800124 arm_goal_position_ = 0;
Neil Balch07fee582018-01-27 15:46:49 -0800125 } else if (data.IsPressed(kArmSwitch)) {
126 // Put the arm up to the level of the switch.
Austin Schuh47d74942018-03-04 01:15:59 -0800127 arm_goal_position_ = 14;
Neil Balch07fee582018-01-27 15:46:49 -0800128 } else if (data.IsPressed(kArmScale)) {
129 // Put the arm up to the level of the switch.
Austin Schuh47d74942018-03-04 01:15:59 -0800130 arm_goal_position_ = 4;
131 } else if (data.IsPressed(kArmBack)) {
132 // Put the arm up to the level of the switch.
133 arm_goal_position_ = 10;
Neil Balch07fee582018-01-27 15:46:49 -0800134 }
135
Austin Schuhcb091712018-02-21 20:01:55 -0800136 new_superstructure_goal->arm_goal_position = arm_goal_position_;
137
Neil Balch07fee582018-01-27 15:46:49 -0800138 if (data.IsPressed(kClawOpen)) {
139 new_superstructure_goal->open_claw = true;
140 } else if (data.IsPressed(kClawClose)) {
141 new_superstructure_goal->open_claw = false;
142 }
143
144 if (data.IsPressed(kForkDeploy)) {
145 new_superstructure_goal->deploy_fork = true;
146 } else if (data.IsPressed(kForkStow)) {
147 new_superstructure_goal->deploy_fork = false;
148 }
149
150 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
151 if (!new_superstructure_goal.Send()) {
152 LOG(ERROR, "Sending superstructure goal failed.\n");
153 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800154 }
155
156 private:
157 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
158
159 void StopAuto() {
160 LOG(INFO, "Stopping auto mode\n");
161 action_queue_.CancelAllActions();
162 }
163
Neil Balch07fee582018-01-27 15:46:49 -0800164 // Current goals to send to the robot.
Austin Schuh17dd0892018-03-02 20:06:31 -0800165 double intake_goal_ = -M_PI * 2.0 / 3.0;
Neil Balch07fee582018-01-27 15:46:49 -0800166
Neil Balchacfca5b2018-01-28 14:04:08 -0800167 bool was_running_ = false;
168 bool auto_running_ = false;
169
170 double robot_velocity_ = 0.0;
171
Austin Schuhcb091712018-02-21 20:01:55 -0800172 int arm_goal_position_ = 0;
173
Neil Balchacfca5b2018-01-28 14:04:08 -0800174 ::aos::common::actions::ActionQueue action_queue_;
175};
176
177} // namespace joysticks
178} // namespace input
179} // namespace y2018
180
181int main() {
182 ::aos::Init(-1);
183 ::y2018::input::joysticks::Reader reader;
184 reader.Run();
185 ::aos::Cleanup();
186}