blob: 9286163c16f98a0a04f5b93207ba67a48b751970 [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
Sabina Davisfdd7a112018-02-04 16:16:23 -0800107 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
108 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800109
110 if (data.IsPressed(kIntakeIn)) {
111 // Turn on the rollers.
112 new_superstructure_goal->intake.roller_voltage =
113 9.0; //TODO(Neil): Add real value once we have one.
114 } else if (data.IsPressed(kIntakeOut)) {
115 // Turn off the rollers.
116 new_superstructure_goal->intake.roller_voltage =
117 -9.0; //TODO(Neil): Add real value once we have one.
118 } else {
119 // We don't want the rollers on.
120 new_superstructure_goal->intake.roller_voltage = 0.0;
121 }
122
123 if (data.IsPressed(kArmDown)) {
124 // Put the arm down to the intake level.
Sabina Davisfdd7a112018-02-04 16:16:23 -0800125 new_superstructure_goal->arm_goal_position =
126 1; // TODO(Neil): Add real value once we have it.
Neil Balch07fee582018-01-27 15:46:49 -0800127 } else if (data.IsPressed(kArmSwitch)) {
128 // Put the arm up to the level of the switch.
Sabina Davisfdd7a112018-02-04 16:16:23 -0800129 new_superstructure_goal->arm_goal_position =
130 1; // TODO(Neil): Add real value once we have it.
Neil Balch07fee582018-01-27 15:46:49 -0800131 } else if (data.IsPressed(kArmScale)) {
132 // Put the arm up to the level of the switch.
Sabina Davisfdd7a112018-02-04 16:16:23 -0800133 new_superstructure_goal->arm_goal_position =
134 1; // TODO(Neil): Add real value once we have it.
Neil Balch07fee582018-01-27 15:46:49 -0800135 }
136
137 if (data.IsPressed(kClawOpen)) {
138 new_superstructure_goal->open_claw = true;
139 } else if (data.IsPressed(kClawClose)) {
140 new_superstructure_goal->open_claw = false;
141 }
142
143 if (data.IsPressed(kForkDeploy)) {
144 new_superstructure_goal->deploy_fork = true;
145 } else if (data.IsPressed(kForkStow)) {
146 new_superstructure_goal->deploy_fork = false;
147 }
148
149 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
150 if (!new_superstructure_goal.Send()) {
151 LOG(ERROR, "Sending superstructure goal failed.\n");
152 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800153 }
154
155 private:
156 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
157
158 void StopAuto() {
159 LOG(INFO, "Stopping auto mode\n");
160 action_queue_.CancelAllActions();
161 }
162
Neil Balch07fee582018-01-27 15:46:49 -0800163 // Current goals to send to the robot.
164 double intake_goal_ = 0.0;
165
Neil Balchacfca5b2018-01-28 14:04:08 -0800166 bool was_running_ = false;
167 bool auto_running_ = false;
168
169 double robot_velocity_ = 0.0;
170
171 ::aos::common::actions::ActionQueue action_queue_;
172};
173
174} // namespace joysticks
175} // namespace input
176} // namespace y2018
177
178int main() {
179 ::aos::Init(-1);
180 ::y2018::input::joysticks::Reader reader;
181 reader.Run();
182 ::aos::Cleanup();
183}