blob: def6df1b22c4518228be10bf33abb406ea944330 [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 -080031const ButtonLocation kIntakeDown(3, 9);
32const POVLocation kIntakeUp(3, 90);
33const ButtonLocation kIntakeIn(3, 12);
34const ButtonLocation kIntakeOut(3, 8);
35
Austin Schuhcb091712018-02-21 20:01:55 -080036const ButtonLocation kArmDown(3, 12);
37const ButtonLocation kArmSwitch(3, 8);
Neil Balch07fee582018-01-27 15:46:49 -080038const ButtonLocation kArmScale(3, 6);
39
40const ButtonLocation kClawOpen(3, 5);
41const ButtonLocation kClawClose(3, 4);
42
43const ButtonLocation kForkDeploy(3, 11);
44const ButtonLocation kForkStow(3, 10);
45
Neil Balchacfca5b2018-01-28 14:04:08 -080046std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
47
48class Reader : public ::aos::input::JoystickInput {
49 public:
50 Reader() {
51 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Austin Schuh2b1fce02018-03-02 20:05:20 -080052 DrivetrainInputReader::InputType::kPistol,
Neil Balchacfca5b2018-01-28 14:04:08 -080053 ::y2018::control_loops::drivetrain::GetDrivetrainConfig());
54 }
55
56 void RunIteration(const ::aos::input::driver_station::Data &data) override {
57 bool last_auto_running = auto_running_;
58 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
59 data.GetControlBit(ControlBit::kEnabled);
60 if (auto_running_ != last_auto_running) {
61 if (auto_running_) {
62 StartAuto();
63 } else {
64 StopAuto();
65 }
66 }
67
68 if (!auto_running_) {
69 HandleDrivetrain(data);
70 HandleTeleop(data);
71 }
72
73 // Process any pending actions.
74 action_queue_.Tick();
75 was_running_ = action_queue_.Running();
76 }
77
78 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
79 drivetrain_input_reader_->HandleDrivetrain(data);
80 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
81 }
82
83 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
84 if (!data.GetControlBit(ControlBit::kEnabled)) {
85 action_queue_.CancelAllActions();
86 LOG(DEBUG, "Canceling\n");
87 }
Neil Balch07fee582018-01-27 15:46:49 -080088
89 superstructure_queue.status.FetchLatest();
90 if (!superstructure_queue.status.get()) {
91 LOG(ERROR, "Got no superstructure status packet.\n");
92 return;
93 }
94
95 if (data.IsPressed(kIntakeUp)) {
96 // Bring in the intake.
97 intake_goal_ = -M_PI; //TODO(Neil): Add real value here once we have one.
98 }
99 if (data.IsPressed(kIntakeDown)) {
100 // Deploy the intake.
101 intake_goal_ = 0; //TODO(Neil): Add real value here once we have one.
102 }
103
104 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
105
Sabina Davisfdd7a112018-02-04 16:16:23 -0800106 new_superstructure_goal->intake.left_intake_angle = intake_goal_;
107 new_superstructure_goal->intake.right_intake_angle = intake_goal_;
Neil Balch07fee582018-01-27 15:46:49 -0800108
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.
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 Schuhcb091712018-02-21 20:01:55 -0800127 arm_goal_position_ = 1;
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 Schuhcb091712018-02-21 20:01:55 -0800130 arm_goal_position_ = 1;
Neil Balch07fee582018-01-27 15:46:49 -0800131 }
132
Austin Schuhcb091712018-02-21 20:01:55 -0800133 new_superstructure_goal->arm_goal_position = arm_goal_position_;
134
Neil Balch07fee582018-01-27 15:46:49 -0800135 if (data.IsPressed(kClawOpen)) {
136 new_superstructure_goal->open_claw = true;
137 } else if (data.IsPressed(kClawClose)) {
138 new_superstructure_goal->open_claw = false;
139 }
140
141 if (data.IsPressed(kForkDeploy)) {
142 new_superstructure_goal->deploy_fork = true;
143 } else if (data.IsPressed(kForkStow)) {
144 new_superstructure_goal->deploy_fork = false;
145 }
146
147 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
148 if (!new_superstructure_goal.Send()) {
149 LOG(ERROR, "Sending superstructure goal failed.\n");
150 }
Neil Balchacfca5b2018-01-28 14:04:08 -0800151 }
152
153 private:
154 void StartAuto() { LOG(INFO, "Starting auto mode\n"); }
155
156 void StopAuto() {
157 LOG(INFO, "Stopping auto mode\n");
158 action_queue_.CancelAllActions();
159 }
160
Neil Balch07fee582018-01-27 15:46:49 -0800161 // Current goals to send to the robot.
162 double intake_goal_ = 0.0;
163
Neil Balchacfca5b2018-01-28 14:04:08 -0800164 bool was_running_ = false;
165 bool auto_running_ = false;
166
167 double robot_velocity_ = 0.0;
168
Austin Schuhcb091712018-02-21 20:01:55 -0800169 int arm_goal_position_ = 0;
170
Neil Balchacfca5b2018-01-28 14:04:08 -0800171 ::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}