blob: f718c3d59ace972a29bfc9baa32aff58d75812d4 [file] [log] [blame]
Sabina Davis91b23602019-01-21 00:06:01 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "aos/actions/actions.h"
7#include "aos/init.h"
8#include "aos/input/action_joystick_input.h"
9#include "aos/input/driver_station_data.h"
10#include "aos/input/drivetrain_input.h"
11#include "aos/input/joystick_input.h"
12#include "aos/logging/logging.h"
13#include "aos/logging/logging.h"
14#include "aos/util/log_interval.h"
15#include "frc971/autonomous/auto.q.h"
16#include "frc971/autonomous/base_autonomous_actor.h"
17#include "frc971/control_loops/drivetrain/drivetrain.q.h"
18
19#include "y2019/control_loops/drivetrain/drivetrain_base.h"
20#include "y2019/control_loops/superstructure/superstructure.q.h"
21
22using ::y2019::control_loops::superstructure::superstructure_queue;
23using ::aos::input::driver_station::ButtonLocation;
24using ::aos::input::driver_station::ControlBit;
25using ::aos::input::driver_station::JoystickAxis;
26using ::aos::input::driver_station::POVLocation;
27
28namespace y2019 {
29namespace input {
30namespace joysticks {
31
32// TODO(sabina): update button locations when the board is done
Austin Schuh2cf16b82019-02-15 23:23:22 -080033const ButtonLocation kElevatorUp(4, 4);
34const ButtonLocation kIntakeOut(3, 3);
Sabina Davis91b23602019-01-21 00:06:01 -080035const ButtonLocation kElevatorDown(0, 0);
Austin Schuh2cf16b82019-02-15 23:23:22 -080036const ButtonLocation kElevator1(4, 1);
37const ButtonLocation kElevator2(4, 11);
38const ButtonLocation kElevator3(4, 9);
39const ButtonLocation kElevator4(4, 7);
40const ButtonLocation kElevator5(4, 5);
41
Sabina Davis91b23602019-01-21 00:06:01 -080042const ButtonLocation kDiskLoad(0, 0);
43const ButtonLocation kDiskRocketMiddle(0, 0);
44const ButtonLocation kDiskRocketTop(0, 0);
45const ButtonLocation kCargoLoad(0, 0);
46const ButtonLocation kCargoBay(0, 0);
47const ButtonLocation kCargoRocketBase(0, 0);
48const ButtonLocation kCargoRocketMiddle(0, 0);
49const ButtonLocation kCargoRocketTop(0, 0);
50const ButtonLocation kStow(0, 0);
51const ButtonLocation kIntakeExtend(0, 0);
52const ButtonLocation kIntake(0, 0);
53const ButtonLocation kSpit(0, 0);
54const ButtonLocation kCargoSuction(0, 0);
55const ButtonLocation kDiskSuction(0, 0);
56const ButtonLocation kSuctionOut(0, 0);
Austin Schuh2cf16b82019-02-15 23:23:22 -080057const ButtonLocation kDeployStilt(3, 8);
Sabina Davis91b23602019-01-21 00:06:01 -080058const ButtonLocation kRetractStilt(0, 0);
59const ButtonLocation kBackwards(0, 0);
60
Austin Schuh2cf16b82019-02-15 23:23:22 -080061const ButtonLocation kWristDown(3, 1);
62
Sabina Davis91b23602019-01-21 00:06:01 -080063class Reader : public ::aos::input::ActionJoystickInput {
64 public:
65 Reader(::aos::EventLoop *event_loop)
66 : ::aos::input::ActionJoystickInput(
67 event_loop,
68 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {}
69
70 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
71 superstructure_queue.position.FetchLatest();
72 superstructure_queue.status.FetchLatest();
73 if (!superstructure_queue.status.get() ||
74 !superstructure_queue.position.get()) {
75 LOG(ERROR, "Got no superstructure status packet.\n");
76 return;
77 }
78
79 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
80
Austin Schuh2cf16b82019-02-15 23:23:22 -080081 /*
Sabina Davis91b23602019-01-21 00:06:01 -080082 if (data.IsPressed(kElevatorUp)) {
83 elevator_height_ += 0.1;
84 } else if (data.IsPressed(kElevatorDown)) {
85 elevator_height_ -= 0.1;
86 } else if (data.IsPressed(kDiskLoad)) {
87 elevator_height_ = 0.48;
88 wrist_angle_ = M_PI;
89 } else if (data.IsPressed(kDiskRocketMiddle)) {
90 elevator_height_ = 1.19;
91 wrist_angle_ = M_PI;
92 } else if (data.IsPressed(kDiskRocketTop)) {
93 elevator_height_ = 1.90;
94 wrist_angle_ = M_PI;
95 }
96
97 // TODO(sabina): do we need an angle here?
98 else if (data.IsPressed(kCargoLoad)) {
99 elevator_height_ = 1.12;
100 wrist_angle_ = M_PI;
101 } else if (data.IsPressed(kCargoBay)) {
102 elevator_height_ = 0.0;
103 wrist_angle_ = M_PI / 3;
104 } else if (data.IsPressed(kCargoRocketBase)) {
105 elevator_height_ = 0.7;
106 wrist_angle_ = M_PI;
107 } else if (data.IsPressed(kCargoRocketMiddle)) {
108 elevator_height_ = 1.41;
109 wrist_angle_ = M_PI;
110 } else if (data.IsPressed(kCargoRocketTop)) {
111 elevator_height_ = 2.12;
112 wrist_angle_ = M_PI;
113 } else if (data.IsPressed(kStow)) {
114 elevator_height_ = 0.5;
115 wrist_angle_ = 0.0;
116 } else {
117 }
Austin Schuh2cf16b82019-02-15 23:23:22 -0800118 */
Sabina Davis91b23602019-01-21 00:06:01 -0800119
Austin Schuh2cf16b82019-02-15 23:23:22 -0800120 /*
Sabina Davis91b23602019-01-21 00:06:01 -0800121 // TODO(sabina): get accurate angle.
122 if (data.IsPressed(kIntakeExtend)) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800123 new_superstructure_goal->intake.unsafe_goal = 0.5;
Sabina Davis91b23602019-01-21 00:06:01 -0800124 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800125 new_superstructure_goal->intake.unsafe_goal = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800126 }
127
128 if (data.IsPressed(kIntake)) {
129 new_superstructure_goal->suction.bottom = true;
130 if (superstructure_queue.status->has_piece == false) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800131 new_superstructure_goal->roller_voltage = 12.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800132 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800133 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800134 }
135 } else if (data.IsPressed(kSpit)) {
136 new_superstructure_goal->suction.bottom = false;
137 if (superstructure_queue.status->has_piece == false) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800138 new_superstructure_goal->roller_voltage = 12.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800139 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800140 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800141 }
142 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800143 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800144 }
Austin Schuh2cf16b82019-02-15 23:23:22 -0800145 */
Sabina Davis91b23602019-01-21 00:06:01 -0800146
147 // TODO(sabina): decide if we should really have disk suction as its own
148 // button
149 if (data.IsPressed(kCargoSuction)) {
150 new_superstructure_goal->suction.top = false;
151 new_superstructure_goal->suction.bottom = true;
152 } else if (data.IsPressed(kDiskSuction)) {
153 new_superstructure_goal->suction.top = true;
154 new_superstructure_goal->suction.bottom = true;
155 } else if (data.IsPressed(kSuctionOut)) {
156 new_superstructure_goal->suction.top = true;
157 new_superstructure_goal->suction.bottom = true;
Sabina Davis91b23602019-01-21 00:06:01 -0800158 }
159
160 // TODO(sabina): max height please?
161 if (data.IsPressed(kDeployStilt)) {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800162 new_superstructure_goal->stilts.unsafe_goal = 0.3;
Sabina Davis91b23602019-01-21 00:06:01 -0800163 } else {
Austin Schuh2cf16b82019-02-15 23:23:22 -0800164 new_superstructure_goal->stilts.unsafe_goal = 0.01;
Sabina Davis91b23602019-01-21 00:06:01 -0800165 }
166
Austin Schuh2cf16b82019-02-15 23:23:22 -0800167 if (data.IsPressed(kIntakeOut)) {
168 new_superstructure_goal->intake.unsafe_goal = 0.8;
169 new_superstructure_goal->roller_voltage = 5.0;
170 } else {
171 new_superstructure_goal->intake.unsafe_goal = -1.2;
172 new_superstructure_goal->roller_voltage = 0.0;
173 }
174
175 if (data.IsPressed(kElevator1)) {
176 elevator_height_ = 1.5;
177 } else if (data.IsPressed(kElevator2)) {
178 elevator_height_ = 1.2;
179 } else if (data.IsPressed(kElevator3)) {
180 elevator_height_ = 0.8;
181 } else if (data.IsPressed(kElevator4)) {
182 elevator_height_ = 0.3;
183 } else if (data.IsPressed(kElevator5)) {
184 elevator_height_ = 0.01;
185 }
186
187 if (data.IsPressed(kWristDown)) {
188 wrist_angle_ = -M_PI / 3.0;
189 } else {
190 wrist_angle_ = M_PI / 3.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800191 }
192
Theo Bafrali00e42272019-02-12 01:07:46 -0800193 new_superstructure_goal->elevator.unsafe_goal = elevator_height_;
194 new_superstructure_goal->wrist.unsafe_goal = wrist_angle_;
Sabina Davis91b23602019-01-21 00:06:01 -0800195
196 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
197 if (!new_superstructure_goal.Send()) {
198 LOG(ERROR, "Sending superstructure goal failed.\n");
199 }
200 }
201
202 private:
203 // Current goals here.
204 double elevator_height_ = 0.0;
205 double wrist_angle_ = 0.0;
206};
207
208} // namespace joysticks
209} // namespace input
210} // namespace y2019
211
212int main() {
213 ::aos::Init(-1);
214 ::aos::ShmEventLoop event_loop;
215 ::y2019::input::joysticks::Reader reader(&event_loop);
216 reader.Run();
217 ::aos::Cleanup();
218}