blob: 963c7d1048054afe85f562eecd42c44ad0b4512e [file] [log] [blame]
Sabina Davis82b19182017-11-10 09:30:25 -08001#include <math.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/actions/actions.h"
7#include "aos/input/driver_station_data.h"
8#include "aos/logging/logging.h"
9#include "aos/time/time.h"
10#include "aos/util/log_interval.h"
Sabina Davis92d2efa2017-11-04 22:35:25 -070011#include "aos/input/drivetrain_input.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080012#include "aos/input/joystick_input.h"
John Park398c74a2018-10-20 21:17:39 -070013#include "aos/init.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080014#include "frc971/autonomous/auto.q.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080015#include "frc971/autonomous/base_autonomous_actor.h"
16#include "frc971/control_loops/drivetrain/drivetrain.q.h"
17#include "y2017/constants.h"
18#include "y2017/control_loops/superstructure/superstructure.q.h"
Sabina Davis82b19182017-11-10 09:30:25 -080019#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080020
21using ::frc971::control_loops::drivetrain_queue;
22using ::y2017::control_loops::superstructure_queue;
23
24using ::aos::input::driver_station::ButtonLocation;
25using ::aos::input::driver_station::ControlBit;
26using ::aos::input::driver_station::JoystickAxis;
27using ::aos::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070028using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080029
30namespace y2017 {
31namespace input {
32namespace joysticks {
33
Austin Schuh55c8d302017-04-05 19:25:37 -070034const ButtonLocation kGearSlotBack(2, 11);
35
Campbell Crowley71b5f132017-02-18 13:16:08 -080036const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070037const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080038const ButtonLocation kIntakeIn(3, 12);
39const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080040const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070041const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080042const ButtonLocation kMiddleShot(3, 6);
43const POVLocation kFarShot(3, 270);
44
45const ButtonLocation kVisionAlign(3, 5);
46
47const ButtonLocation kReverseIndexer(3, 4);
48const ButtonLocation kExtra1(3, 11);
49const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070050const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080051
Sabina Davis92d2efa2017-11-04 22:35:25 -070052std::unique_ptr<DrivetrainInputReader> drivetrain_input_reader_;
53
Campbell Crowley71b5f132017-02-18 13:16:08 -080054class Reader : public ::aos::input::JoystickInput {
55 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080056 Reader(::aos::EventLoop *event_loop)
Austin Schuh1bf8a212019-05-26 22:13:14 -070057 : ::aos::input::JoystickInput(event_loop),
58 autonomous_action_factory_(
59 ::frc971::autonomous::BaseAutonomousActor::MakeFactory(
60 event_loop)) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070061 drivetrain_input_reader_ = DrivetrainInputReader::Make(
Sabina Davis82b19182017-11-10 09:30:25 -080062 DrivetrainInputReader::InputType::kSteeringWheel,
63 ::y2017::control_loops::drivetrain::GetDrivetrainConfig());
Sabina Davis92d2efa2017-11-04 22:35:25 -070064 }
Campbell Crowley71b5f132017-02-18 13:16:08 -080065
Parker Schuh208a58d2017-04-12 20:51:38 -070066 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070067
Austin Schuh4b5f7df2017-04-16 20:31:12 -070068 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
Austin Schuhd0629b12017-03-22 22:37:16 -070069
Campbell Crowley71b5f132017-02-18 13:16:08 -080070 void RunIteration(const ::aos::input::driver_station::Data &data) override {
71 bool last_auto_running = auto_running_;
72 auto_running_ = data.GetControlBit(ControlBit::kAutonomous) &&
73 data.GetControlBit(ControlBit::kEnabled);
74 if (auto_running_ != last_auto_running) {
75 if (auto_running_) {
76 StartAuto();
77 } else {
78 StopAuto();
79 }
80 }
81
Campbell Crowley71b5f132017-02-18 13:16:08 -080082 if (!auto_running_) {
83 HandleDrivetrain(data);
84 HandleTeleop(data);
85 }
86
87 // Process any pending actions.
88 action_queue_.Tick();
89 was_running_ = action_queue_.Running();
90 }
Austin Schuhd0629b12017-03-22 22:37:16 -070091 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -080092
93 void HandleDrivetrain(const ::aos::input::driver_station::Data &data) {
Sabina Davis92d2efa2017-11-04 22:35:25 -070094 drivetrain_input_reader_->HandleDrivetrain(data);
95 robot_velocity_ = drivetrain_input_reader_->robot_velocity();
Campbell Crowley71b5f132017-02-18 13:16:08 -080096 }
97
98 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
99 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -0700100 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800101 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700102 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800103
104 if (!data.GetControlBit(ControlBit::kEnabled)) {
105 action_queue_.CancelAllActions();
106 LOG(DEBUG, "Canceling\n");
107 }
108
109 superstructure_queue.status.FetchLatest();
110 if (!superstructure_queue.status.get()) {
111 LOG(ERROR, "Got no superstructure status packet.\n");
112 return;
113 }
114
Austin Schuhd0629b12017-03-22 22:37:16 -0700115 if (data.IsPressed(kIntakeUp)) {
116 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -0700117 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700118 }
Austin Schuh405724e2017-04-09 18:34:18 -0700119 if (data.IsPressed(kIntakeDown)) {
120 intake_goal_ = 0.235;
121 // Don't go quite so far out since we have a gear mech out now.
122 if (data.IsPressed(kIntakeUp)) {
123 intake_goal_ = 0.160;
124 }
125 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700126
Campbell Crowley71b5f132017-02-18 13:16:08 -0800127 if (data.IsPressed(kVisionAlign)) {
128 // Align shot using vision
129 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -0800130 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700131 vision_track = true;
132 }
133 if (data.PosEdge(kMiddleShot)) {
134 turret_goal_ = -M_PI;
135 }
136 if (data.PosEdge(kFarShot)) {
137 turret_goal_ = 0.0;
138 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700139 if (data.PosEdge(kVisionDistanceShot)) {
140 turret_goal_ = 0.0;
141 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700142
Parker Schuh208a58d2017-04-12 20:51:38 -0700143 if (data.IsPressed(kVisionDistanceShot)) {
144 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800145 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700146 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800147 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700148 last_shot_distance_ = ShotDistance::FAR_SHOT;
149 }
150
Parker Schuh208a58d2017-04-12 20:51:38 -0700151 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700152 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
153 data.IsPressed(kFire)) {
154 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700155 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700156 hood_goal_ = 0.10;
157 shooter_velocity_ = 300.0;
158
Parker Schuh208a58d2017-04-12 20:51:38 -0700159 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700160 break;
161 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700162 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700163 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700164 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700165 break;
166 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700167 hood_goal_ = 0.47;
168 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700169 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700170 break;
171 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800172 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700173 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800174 shooter_velocity_ = 0.0;
175 }
176
177 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700178 //turret_goal_ = -M_PI * 3.0 / 4.0;
179 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800180 }
181 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700182 //turret_goal_ = M_PI * 3.0 / 4.0;
183 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800184 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700185 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800186
187 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700188 if (data.IsPressed(kVisionAlign)) {
189 fire_ = fire_ && superstructure_queue.status->turret.vision_tracking;
190 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800191
192 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700193 if (data.IsPressed(kHang)) {
194 intake_goal_ = 0.23;
195 }
196
Campbell Crowley71b5f132017-02-18 13:16:08 -0800197 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800198 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800199 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700200 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800201 new_superstructure_goal->hood.angle = hood_goal_;
202 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
203
Austin Schuh6a8131b2017-04-08 15:39:22 -0700204 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700205 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700206 } else {
207 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700208 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700209 }
210
Campbell Crowley71b5f132017-02-18 13:16:08 -0800211 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800212 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
213
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700214 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700215 if (vision_track) {
216 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
217 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
218 } else {
219 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
220 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
221 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800222 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
223
Austin Schuh3028b1d2017-03-11 22:12:13 -0800224 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800225 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800226
Parker Schuh208a58d2017-04-12 20:51:38 -0700227 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
228 new_superstructure_goal->use_vision_for_shots = true;
229 } else {
230 new_superstructure_goal->use_vision_for_shots = false;
231 }
232
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700233 if (superstructure_queue.status->intake.position >
234 superstructure_queue.status->intake.unprofiled_goal_position + 0.01) {
235 intake_accumulator_ = 10;
236 }
237 if (intake_accumulator_ > 0) {
238 --intake_accumulator_;
239 if (!superstructure_queue.status->intake.estopped) {
240 new_superstructure_goal->intake.voltage_rollers = 10.0;
241 }
242 }
243
Campbell Crowley71b5f132017-02-18 13:16:08 -0800244 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700245 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800246 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700247 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
248 if (robot_velocity_ > 2.0) {
249 new_superstructure_goal->intake.voltage_rollers = 12.0;
250 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700251 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700252 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800253 } else if (data.IsPressed(kIntakeOut)) {
254 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800255 }
256 if (intake_goal_ < 0.1) {
257 new_superstructure_goal->intake.voltage_rollers =
258 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800259 }
260
261 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700262 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800263 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800264 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800265 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700266 new_superstructure_goal->indexer.voltage_rollers = 12.0;
267 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700268 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700269 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700270 break;
271 case ShotDistance::MIDDLE_SHOT:
272 case ShotDistance::FAR_SHOT:
273 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
274 break;
275 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800276 } else {
277 new_superstructure_goal->indexer.voltage_rollers = 0.0;
278 new_superstructure_goal->indexer.angular_velocity = 0.0;
279 }
280
281 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
282 if (!new_superstructure_goal.Send()) {
283 LOG(ERROR, "Sending superstructure goal failed.\n");
284 }
285 }
286
287 private:
Austin Schuh3028b1d2017-03-11 22:12:13 -0800288 void StartAuto() {
289 LOG(INFO, "Starting auto mode\n");
290
291 ::frc971::autonomous::AutonomousActionParams params;
292 ::frc971::autonomous::auto_mode.FetchLatest();
293 if (::frc971::autonomous::auto_mode.get() != nullptr) {
294 params.mode = ::frc971::autonomous::auto_mode->mode;
295 } else {
296 LOG(WARNING, "no auto mode values\n");
297 params.mode = 0;
298 }
Austin Schuh1bf8a212019-05-26 22:13:14 -0700299 action_queue_.EnqueueAction(autonomous_action_factory_.Make(params));
Austin Schuh3028b1d2017-03-11 22:12:13 -0800300 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800301
302 void StopAuto() {
303 LOG(INFO, "Stopping auto mode\n");
304 action_queue_.CancelAllActions();
305 }
306
307 // Current goals to send to the robot.
308 double intake_goal_ = 0.0;
309 double turret_goal_ = 0.0;
310 double hood_goal_ = 0.3;
311 double shooter_velocity_ = 0.0;
312
Campbell Crowley71b5f132017-02-18 13:16:08 -0800313 bool was_running_ = false;
314 bool auto_running_ = false;
315
Parker Schuh208a58d2017-04-12 20:51:38 -0700316 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800317
318 bool fire_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700319 double robot_velocity_ = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800320
321 ::aos::common::actions::ActionQueue action_queue_;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700322
323 ::frc971::autonomous::BaseAutonomousActor::Factory autonomous_action_factory_;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800324};
325
326} // namespace joysticks
327} // namespace input
328} // namespace y2017
329
330int main() {
331 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800332 ::aos::ShmEventLoop event_loop;
333 ::y2017::input::joysticks::Reader reader(&event_loop);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800334 reader.Run();
335 ::aos::Cleanup();
336}