blob: ed57a2424c49dc855a0c6725bdd26307f4076936 [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
33const ButtonLocation kElevatorUp(0, 0);
34const ButtonLocation kElevatorDown(0, 0);
35const ButtonLocation kDiskLoad(0, 0);
36const ButtonLocation kDiskRocketMiddle(0, 0);
37const ButtonLocation kDiskRocketTop(0, 0);
38const ButtonLocation kCargoLoad(0, 0);
39const ButtonLocation kCargoBay(0, 0);
40const ButtonLocation kCargoRocketBase(0, 0);
41const ButtonLocation kCargoRocketMiddle(0, 0);
42const ButtonLocation kCargoRocketTop(0, 0);
43const ButtonLocation kStow(0, 0);
44const ButtonLocation kIntakeExtend(0, 0);
45const ButtonLocation kIntake(0, 0);
46const ButtonLocation kSpit(0, 0);
47const ButtonLocation kCargoSuction(0, 0);
48const ButtonLocation kDiskSuction(0, 0);
49const ButtonLocation kSuctionOut(0, 0);
50const ButtonLocation kDeployStilt(0, 0);
51const ButtonLocation kRetractStilt(0, 0);
52const ButtonLocation kBackwards(0, 0);
53
54class Reader : public ::aos::input::ActionJoystickInput {
55 public:
56 Reader(::aos::EventLoop *event_loop)
57 : ::aos::input::ActionJoystickInput(
58 event_loop,
59 ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {}
60
61 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
62 superstructure_queue.position.FetchLatest();
63 superstructure_queue.status.FetchLatest();
64 if (!superstructure_queue.status.get() ||
65 !superstructure_queue.position.get()) {
66 LOG(ERROR, "Got no superstructure status packet.\n");
67 return;
68 }
69
70 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
71
72 if (data.IsPressed(kElevatorUp)) {
73 elevator_height_ += 0.1;
74 } else if (data.IsPressed(kElevatorDown)) {
75 elevator_height_ -= 0.1;
76 } else if (data.IsPressed(kDiskLoad)) {
77 elevator_height_ = 0.48;
78 wrist_angle_ = M_PI;
79 } else if (data.IsPressed(kDiskRocketMiddle)) {
80 elevator_height_ = 1.19;
81 wrist_angle_ = M_PI;
82 } else if (data.IsPressed(kDiskRocketTop)) {
83 elevator_height_ = 1.90;
84 wrist_angle_ = M_PI;
85 }
86
87 // TODO(sabina): do we need an angle here?
88 else if (data.IsPressed(kCargoLoad)) {
89 elevator_height_ = 1.12;
90 wrist_angle_ = M_PI;
91 } else if (data.IsPressed(kCargoBay)) {
92 elevator_height_ = 0.0;
93 wrist_angle_ = M_PI / 3;
94 } else if (data.IsPressed(kCargoRocketBase)) {
95 elevator_height_ = 0.7;
96 wrist_angle_ = M_PI;
97 } else if (data.IsPressed(kCargoRocketMiddle)) {
98 elevator_height_ = 1.41;
99 wrist_angle_ = M_PI;
100 } else if (data.IsPressed(kCargoRocketTop)) {
101 elevator_height_ = 2.12;
102 wrist_angle_ = M_PI;
103 } else if (data.IsPressed(kStow)) {
104 elevator_height_ = 0.5;
105 wrist_angle_ = 0.0;
106 } else {
107 }
108
109 // TODO(sabina): get accurate angle.
110 if (data.IsPressed(kIntakeExtend)) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800111 new_superstructure_goal->intake.unsafe_goal = 0.5;
Sabina Davis91b23602019-01-21 00:06:01 -0800112 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800113 new_superstructure_goal->intake.unsafe_goal = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800114 }
115
116 if (data.IsPressed(kIntake)) {
117 new_superstructure_goal->suction.bottom = true;
118 if (superstructure_queue.status->has_piece == false) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800119 new_superstructure_goal->roller_voltage = 12.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800120 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800121 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800122 }
123 } else if (data.IsPressed(kSpit)) {
124 new_superstructure_goal->suction.bottom = false;
125 if (superstructure_queue.status->has_piece == false) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800126 new_superstructure_goal->roller_voltage = 12.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800127 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800128 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800129 }
130 } else {
Theo Bafrali00e42272019-02-12 01:07:46 -0800131 new_superstructure_goal->roller_voltage = 0.0;
Sabina Davis91b23602019-01-21 00:06:01 -0800132 }
133
134 // TODO(sabina): decide if we should really have disk suction as its own
135 // button
136 if (data.IsPressed(kCargoSuction)) {
137 new_superstructure_goal->suction.top = false;
138 new_superstructure_goal->suction.bottom = true;
139 } else if (data.IsPressed(kDiskSuction)) {
140 new_superstructure_goal->suction.top = true;
141 new_superstructure_goal->suction.bottom = true;
142 } else if (data.IsPressed(kSuctionOut)) {
143 new_superstructure_goal->suction.top = true;
144 new_superstructure_goal->suction.bottom = true;
145 } else {
146 }
147
148 // TODO(sabina): max height please?
149 if (data.IsPressed(kDeployStilt)) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800150 new_superstructure_goal->stilts.unsafe_goal= 0;
Sabina Davis91b23602019-01-21 00:06:01 -0800151 } else if (data.IsPressed(kRetractStilt)) {
Theo Bafrali00e42272019-02-12 01:07:46 -0800152 new_superstructure_goal->stilts.unsafe_goal = 0;
Sabina Davis91b23602019-01-21 00:06:01 -0800153 } else {
154 }
155
156 if (data.IsPressed(kBackwards)) {
157 wrist_angle_ = -wrist_angle_;
158 }
159
Theo Bafrali00e42272019-02-12 01:07:46 -0800160 new_superstructure_goal->elevator.unsafe_goal = elevator_height_;
161 new_superstructure_goal->wrist.unsafe_goal = wrist_angle_;
Sabina Davis91b23602019-01-21 00:06:01 -0800162
163 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
164 if (!new_superstructure_goal.Send()) {
165 LOG(ERROR, "Sending superstructure goal failed.\n");
166 }
167 }
168
169 private:
170 // Current goals here.
171 double elevator_height_ = 0.0;
172 double wrist_angle_ = 0.0;
173};
174
175} // namespace joysticks
176} // namespace input
177} // namespace y2019
178
179int main() {
180 ::aos::Init(-1);
181 ::aos::ShmEventLoop event_loop;
182 ::y2019::input::joysticks::Reader reader(&event_loop);
183 reader.Run();
184 ::aos::Cleanup();
185}