blob: 9d00ac6261af3720ad587ded36f055f138852b4b [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
Neil Balch07fee582018-01-27 15:46:49 -080031//TODO(Neil): Change button locations to real ones on driverstation.
32const ButtonLocation kIntakeDown(3, 9);
33const POVLocation kIntakeUp(3, 90);
34const ButtonLocation kIntakeIn(3, 12);
35const ButtonLocation kIntakeOut(3, 8);
36
37const ButtonLocation kArmDown(3, 3);
38const ButtonLocation kArmSwitch(3, 7);
39const ButtonLocation kArmScale(3, 6);
40
41const ButtonLocation kClawOpen(3, 5);
42const ButtonLocation kClawClose(3, 4);
43
44const ButtonLocation kForkDeploy(3, 11);
45const ButtonLocation kForkStow(3, 10);
46
Neil Balchacfca5b2018-01-28 14:04:08 -080047std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
48
49class Reader : public ::aos::input::JoystickInput {
50 public:
51 Reader() {
52 drivetrain_input_reader_ = DrivetrainInputReader::Make(
53 DrivetrainInputReader::InputType::kSteeringWheel,
54 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
55 }
56
57 void RunIteration(const ::aos::input::driver_station::Data &data) override {
58 bool last_auto_running = auto_running_;
59 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
60 data.GetControlBit(ControlBit::kEnabled);
61 if (auto_running_ != last_auto_running) {
62 if (auto_running_) {
63 StartAuto();
64 } else {
65 StopAuto();
66 }
67 }
68
69 if (!auto_running_) {
70 HandleDrivetrain(data);
71 HandleTeleop(data);
72 }
73
74 // Process any pending actions.
75 action_queue_.Tick();
76 was_running_ = action_queue_.Running();
77 }
78
79 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
80 drivetrain_input_reader_->HandleDrivetrain(data);
81 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
82 }
83
84 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
85 if (!data.GetControlBit(ControlBit::kEnabled)) {
86 action_queue_.CancelAllActions();
87 LOG(DEBUG, "Canceling\n");
88 }
Neil Balch07fee582018-01-27 15:46:49 -080089
90 superstructure_queue.status.FetchLatest();
91 if (!superstructure_queue.status.get()) {
92 LOG(ERROR, "Got no superstructure status packet.\n");
93 return;
94 }
95
96 if (data.IsPressed(kIntakeUp)) {
97 // Bring in the intake.
98 intake_goal_ = -M_PI; //TODO(Neil): Add real value here once we have one.
99 }
100 if (data.IsPressed(kIntakeDown)) {
101 // Deploy the intake.
102 intake_goal_ = 0; //TODO(Neil): Add real value here once we have one.
103 }
104
105 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
106
107 new_superstructure_goal->intake.intake_angle = intake_goal_;
108
109 if (data.IsPressed(kIntakeIn)) {
110 // Turn on the rollers.
111 new_superstructure_goal->intake.roller_voltage =
112 9.0; //TODO(Neil): Add real value once we have one.
113 } else if (data.IsPressed(kIntakeOut)) {
114 // Turn off the rollers.
115 new_superstructure_goal->intake.roller_voltage =
116 -9.0; //TODO(Neil): Add real value once we have one.
117 } 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.
124 new_superstructure_goal->arm.proximal_position =
125 1.0; // TODO(Neil): Add real value once we have it.
126 new_superstructure_goal->arm.distal_position =
127 0.0; // TODO(Neil): Add real value once we have it.
128 } else if (data.IsPressed(kArmSwitch)) {
129 // Put the arm up to the level of the switch.
130 new_superstructure_goal->arm.proximal_position =
131 1.5; // TODO(Neil): Add real value once we have it.
132 new_superstructure_goal->arm.distal_position =
133 0.5; // TODO(Neil): Add real value once we have it.
134 } else if (data.IsPressed(kArmScale)) {
135 // Put the arm up to the level of the switch.
136 new_superstructure_goal->arm.proximal_position =
137 1.5; // TODO(Neil): Add real value once we have it.
138 new_superstructure_goal->arm.distal_position =
139 2.0; // TODO(Neil): Add real value once we have it.
140 }
141
142 if (data.IsPressed(kClawOpen)) {
143 new_superstructure_goal->open_claw = true;
144 } else if (data.IsPressed(kClawClose)) {
145 new_superstructure_goal->open_claw = false;
146 }
147
148 if (data.IsPressed(kForkDeploy)) {
149 new_superstructure_goal->deploy_fork = true;
150 } else if (data.IsPressed(kForkStow)) {
151 new_superstructure_goal->deploy_fork = false;
152 }
153
154 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
155 if (!new_superstructure_goal.Send()) {
156 LOG(ERROR, "Sending superstructure goal failed.\n");
157 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800158 }
159
160 private:
161 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
162
163 void StopAuto() {
164 LOG(INFO, "Stopping auto mode\n");
165 action_queue_.CancelAllActions();
166 }
167
Neil Balch07fee582018-01-27 15:46:49 -0800168 // Current goals to send to the robot.
169 double intake_goal_ = 0.0;
170
Neil Balchacfca5b2018-01-28 14:04:08 -0800171 bool was_running_ = false;
172 bool auto_running_ = false;
173
174 double robot_velocity_ = 0.0;
175
176 ::aos::common::actions::ActionQueue action_queue_;
177};
178
179} // namespace joysticks
180} // namespace input
181} // namespace y2018
182
183int main() {
184 ::aos::Init(-1);
185 ::y2018::input::joysticks::Reader reader;
186 reader.Run();
187 ::aos::Cleanup();
188}