blob: 9cee19f781a6741775e702c027c97a3ac5622f52 [file] [log] [blame]
Campbell Crowley71b5f132017-02-18 13:16:08 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <math.h>
5
Austin Schuh3028b1d2017-03-11 22:12:13 -08006#include "aos/common/actions/actions.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -08007#include "aos/common/input/driver_station_data.h"
8#include "aos/common/logging/logging.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -08009#include "aos/common/time.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080010#include "aos/common/util/log_interval.h"
11#include "aos/input/joystick_input.h"
12#include "aos/linux_code/init.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080013#include "frc971/autonomous/auto.q.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080014#include "frc971/autonomous/base_autonomous_actor.h"
15#include "frc971/control_loops/drivetrain/drivetrain.q.h"
16#include "y2017/constants.h"
17#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080018
19using ::frc971::control_loops::drivetrain_queue;
20using ::y2017::control_loops::superstructure_queue;
21
22using ::aos::input::driver_station::ButtonLocation;
23using ::aos::input::driver_station::ControlBit;
24using ::aos::input::driver_station::JoystickAxis;
25using ::aos::input::driver_station::POVLocation;
26
27namespace y2017 {
28namespace input {
29namespace joysticks {
30
31const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2);
32const ButtonLocation kQuickTurn(1, 5);
33
34const ButtonLocation kTurn1(1, 7);
35const ButtonLocation kTurn2(1, 11);
36
37const ButtonLocation kIntakeDown(3, 9);
38const ButtonLocation kIntakeIn(3, 12);
39const ButtonLocation kIntakeOut(3, 8);
40const POVLocation kHang(3, 90);
41const ButtonLocation kFire(3, 3);
42const ButtonLocation kCloseShot(3, 7);
43const ButtonLocation kMiddleShot(3, 6);
44const POVLocation kFarShot(3, 270);
45
46const ButtonLocation kVisionAlign(3, 5);
47
48const ButtonLocation kReverseIndexer(3, 4);
49const ButtonLocation kExtra1(3, 11);
50const ButtonLocation kExtra2(3, 10);
51const ButtonLocation kExtra3(3, 2);
52
53class Reader : public ::aos::input::JoystickInput {
54 public:
55 Reader() {}
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 vision_valid_ = false;
70
71 if (!auto_running_) {
72 HandleDrivetrain(data);
73 HandleTeleop(data);
74 }
75
76 // Process any pending actions.
77 action_queue_.Tick();
78 was_running_ = action_queue_.Running();
79 }
80
81 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
82 bool is_control_loop_driving = false;
83
84 const double wheel = -data.GetAxis(kSteeringWheel);
85 const double throttle = -data.GetAxis(kDriveThrottle);
86 drivetrain_queue.status.FetchLatest();
87
88 if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) {
89 if (drivetrain_queue.status.get()) {
90 left_goal_ = drivetrain_queue.status->estimated_left_position;
91 right_goal_ = drivetrain_queue.status->estimated_right_position;
92 }
93 }
94 if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) {
95 is_control_loop_driving = true;
96 }
97 if (!drivetrain_queue.goal.MakeWithBuilder()
98 .steering(wheel)
99 .throttle(throttle)
100 .quickturn(data.IsPressed(kQuickTurn))
101 .control_loop_driving(is_control_loop_driving)
102 .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3)
103 .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3)
104 .left_velocity_goal(0)
105 .right_velocity_goal(0)
106 .Send()) {
107 LOG(WARNING, "sending stick values failed\n");
108 }
109 }
110
111 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
112 // Default the intake to in.
113 intake_goal_ = constants::Values::kIntakeRange.lower;
114
115 if (!data.GetControlBit(ControlBit::kEnabled)) {
116 action_queue_.CancelAllActions();
117 LOG(DEBUG, "Canceling\n");
118 }
119
120 superstructure_queue.status.FetchLatest();
121 if (!superstructure_queue.status.get()) {
122 LOG(ERROR, "Got no superstructure status packet.\n");
123 return;
124 }
125
126 if (data.IsPressed(kIntakeDown)) {
Austin Schuh3028b1d2017-03-11 22:12:13 -0800127 intake_goal_ = 0.223;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800128 }
129
130 if (data.IsPressed(kVisionAlign)) {
131 // Align shot using vision
132 // TODO(campbell): Add vision aligning.
133 shooter_velocity_ = 100.0;
134 } else if (data.IsPressed(kCloseShot)) {
135 // Close shot
136 hood_goal_ = 0.5;
137 shooter_velocity_ = 350.0;
138 } else if (data.IsPressed(kMiddleShot)) {
139 // Medium distance shot
140 hood_goal_ = 0.4;
141 shooter_velocity_ = 350.0;
142 } else if (data.IsPressed(kFarShot)) {
143 // Far shot
144 hood_goal_ = 0.6;
145 shooter_velocity_ = 250.0;
146 } else {
147 hood_goal_ = 0.15;
148 shooter_velocity_ = 0.0;
149 }
150
151 if (data.IsPressed(kExtra1)) {
Austin Schuh3028b1d2017-03-11 22:12:13 -0800152 turret_goal_ = -M_PI * 3.0 / 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800153 }
154 if (data.IsPressed(kExtra2)) {
155 turret_goal_ = 0.0;
156 }
157 if (data.IsPressed(kExtra3)) {
Austin Schuh3028b1d2017-03-11 22:12:13 -0800158 turret_goal_ = M_PI * 3.0 / 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800159 }
160
161 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
162
163 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
164 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800165 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800166 new_superstructure_goal->turret.angle = turret_goal_;
167 new_superstructure_goal->hood.angle = hood_goal_;
168 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
169
170 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
171 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
172 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
173
174 new_superstructure_goal->intake.profile_params.max_acceleration = 5.0;
175 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
176 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
177
Austin Schuh3028b1d2017-03-11 22:12:13 -0800178 new_superstructure_goal->intake.voltage_rollers = 0.0;
179
Campbell Crowley71b5f132017-02-18 13:16:08 -0800180 if (data.IsPressed(kHang)) {
181 new_superstructure_goal->intake.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800182 new_superstructure_goal->intake.disable_intake = true;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800183 } else if (data.IsPressed(kIntakeIn)) {
184 new_superstructure_goal->intake.voltage_rollers = 12.0;
185 } else if (data.IsPressed(kIntakeOut)) {
186 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800187 }
188 if (intake_goal_ < 0.1) {
189 new_superstructure_goal->intake.voltage_rollers =
190 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800191 }
192
193 if (data.IsPressed(kReverseIndexer)) {
194 new_superstructure_goal->indexer.voltage_rollers = -4.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800195 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800196 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800197 } else if (fire_) {
198 new_superstructure_goal->indexer.angular_velocity = -3.0 * M_PI;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800199 } else {
200 new_superstructure_goal->indexer.voltage_rollers = 0.0;
201 new_superstructure_goal->indexer.angular_velocity = 0.0;
202 }
203
204 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
205 if (!new_superstructure_goal.Send()) {
206 LOG(ERROR, "Sending superstructure goal failed.\n");
207 }
208 }
209
210 private:
Austin Schuh3028b1d2017-03-11 22:12:13 -0800211 void StartAuto() {
212 LOG(INFO, "Starting auto mode\n");
213
214 ::frc971::autonomous::AutonomousActionParams params;
215 ::frc971::autonomous::auto_mode.FetchLatest();
216 if (::frc971::autonomous::auto_mode.get() != nullptr) {
217 params.mode = ::frc971::autonomous::auto_mode->mode;
218 } else {
219 LOG(WARNING, "no auto mode values\n");
220 params.mode = 0;
221 }
222 action_queue_.EnqueueAction(
223 ::frc971::autonomous::MakeAutonomousAction(params));
224 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800225
226 void StopAuto() {
227 LOG(INFO, "Stopping auto mode\n");
228 action_queue_.CancelAllActions();
229 }
230
231 // Current goals to send to the robot.
232 double intake_goal_ = 0.0;
233 double turret_goal_ = 0.0;
234 double hood_goal_ = 0.3;
235 double shooter_velocity_ = 0.0;
236
237 // Goals to send to the drivetrain in closed loop mode.
Austin Schuh3028b1d2017-03-11 22:12:13 -0800238 double left_goal_ = 0.0;
239 double right_goal_ = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800240
241 bool was_running_ = false;
242 bool auto_running_ = false;
243
244 bool vision_valid_ = false;
245
246 bool fire_ = false;
247
248 ::aos::common::actions::ActionQueue action_queue_;
249};
250
251} // namespace joysticks
252} // namespace input
253} // namespace y2017
254
255int main() {
256 ::aos::Init(-1);
257 ::y2017::input::joysticks::Reader reader;
258 reader.Run();
259 ::aos::Cleanup();
260}