blob: 3bbbeb21bf8cc33f80d3f3a2defef378805987e8 [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 Schuha250b2d2019-05-27 16:14:02 -070012#include "aos/input/action_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
Campbell Crowley71b5f132017-02-18 13:16:08 -080021using ::aos::input::driver_station::ButtonLocation;
22using ::aos::input::driver_station::ControlBit;
23using ::aos::input::driver_station::JoystickAxis;
24using ::aos::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070025using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080026
27namespace y2017 {
28namespace input {
29namespace joysticks {
30
Austin Schuh55c8d302017-04-05 19:25:37 -070031const ButtonLocation kGearSlotBack(2, 11);
32
Campbell Crowley71b5f132017-02-18 13:16:08 -080033const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070034const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080035const ButtonLocation kIntakeIn(3, 12);
36const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080037const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070038const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080039const ButtonLocation kMiddleShot(3, 6);
40const POVLocation kFarShot(3, 270);
41
42const ButtonLocation kVisionAlign(3, 5);
43
44const ButtonLocation kReverseIndexer(3, 4);
45const ButtonLocation kExtra1(3, 11);
46const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070047const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080048
Austin Schuha250b2d2019-05-27 16:14:02 -070049class Reader : public ::aos::input::ActionJoystickInput {
Campbell Crowley71b5f132017-02-18 13:16:08 -080050 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080051 Reader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -070052 : ::aos::input::ActionJoystickInput(
53 event_loop,
54 ::y2017::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuh402722c2019-06-29 21:27:06 -070055 DrivetrainInputReader::InputType::kSteeringWheel, {}),
56 superstructure_status_fetcher_(
57 event_loop->MakeFetcher<
58 ::y2017::control_loops::SuperstructureQueue::Status>(
59 ".y2017.control_loops.superstructure_queue.status")),
60 superstructure_goal_sender_(
61 event_loop
62 ->MakeSender<::y2017::control_loops::SuperstructureQueue::Goal>(
63 ".y2017.control_loops.superstructure_queue.goal")) {}
Campbell Crowley71b5f132017-02-18 13:16:08 -080064
Parker Schuh208a58d2017-04-12 20:51:38 -070065 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070066
Campbell Crowley71b5f132017-02-18 13:16:08 -080067 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
68 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070069 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080070 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070071 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -080072
Austin Schuh402722c2019-06-29 21:27:06 -070073 superstructure_status_fetcher_.Fetch();
74 if (!superstructure_status_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070075 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -080076 return;
77 }
78
Austin Schuhd0629b12017-03-22 22:37:16 -070079 if (data.IsPressed(kIntakeUp)) {
80 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -070081 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -070082 }
Austin Schuh405724e2017-04-09 18:34:18 -070083 if (data.IsPressed(kIntakeDown)) {
84 intake_goal_ = 0.235;
85 // Don't go quite so far out since we have a gear mech out now.
86 if (data.IsPressed(kIntakeUp)) {
87 intake_goal_ = 0.160;
88 }
89 }
Austin Schuhd0629b12017-03-22 22:37:16 -070090
Campbell Crowley71b5f132017-02-18 13:16:08 -080091 if (data.IsPressed(kVisionAlign)) {
92 // Align shot using vision
93 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -080094 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -070095 vision_track = true;
96 }
97 if (data.PosEdge(kMiddleShot)) {
98 turret_goal_ = -M_PI;
99 }
100 if (data.PosEdge(kFarShot)) {
101 turret_goal_ = 0.0;
102 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700103 if (data.PosEdge(kVisionDistanceShot)) {
104 turret_goal_ = 0.0;
105 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700106
Parker Schuh208a58d2017-04-12 20:51:38 -0700107 if (data.IsPressed(kVisionDistanceShot)) {
108 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800109 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700110 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800111 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700112 last_shot_distance_ = ShotDistance::FAR_SHOT;
113 }
114
Parker Schuh208a58d2017-04-12 20:51:38 -0700115 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700116 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
117 data.IsPressed(kFire)) {
118 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700119 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700120 hood_goal_ = 0.10;
121 shooter_velocity_ = 300.0;
122
Parker Schuh208a58d2017-04-12 20:51:38 -0700123 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700124 break;
125 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700126 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700127 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700128 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700129 break;
130 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700131 hood_goal_ = 0.47;
132 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700133 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700134 break;
135 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800136 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700137 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800138 shooter_velocity_ = 0.0;
139 }
140
141 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700142 //turret_goal_ = -M_PI * 3.0 / 4.0;
143 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800144 }
145 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700146 //turret_goal_ = M_PI * 3.0 / 4.0;
147 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800148 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700149 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800150
151 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700152 if (data.IsPressed(kVisionAlign)) {
Austin Schuh402722c2019-06-29 21:27:06 -0700153 fire_ = fire_ && superstructure_status_fetcher_->turret.vision_tracking;
Austin Schuhd0629b12017-03-22 22:37:16 -0700154 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800155
Austin Schuh402722c2019-06-29 21:27:06 -0700156 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700157 if (data.IsPressed(kHang)) {
158 intake_goal_ = 0.23;
159 }
160
Campbell Crowley71b5f132017-02-18 13:16:08 -0800161 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800162 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800163 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700164 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800165 new_superstructure_goal->hood.angle = hood_goal_;
166 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
167
Austin Schuh6a8131b2017-04-08 15:39:22 -0700168 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700169 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700170 } else {
171 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700172 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700173 }
174
Campbell Crowley71b5f132017-02-18 13:16:08 -0800175 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800176 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
177
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700178 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700179 if (vision_track) {
180 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
181 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
182 } else {
183 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
184 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
185 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800186 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
187
Austin Schuh3028b1d2017-03-11 22:12:13 -0800188 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800189 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800190
Parker Schuh208a58d2017-04-12 20:51:38 -0700191 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
192 new_superstructure_goal->use_vision_for_shots = true;
193 } else {
194 new_superstructure_goal->use_vision_for_shots = false;
195 }
196
Austin Schuh402722c2019-06-29 21:27:06 -0700197 if (superstructure_status_fetcher_->intake.position >
198 superstructure_status_fetcher_->intake.unprofiled_goal_position +
199 0.01) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700200 intake_accumulator_ = 10;
201 }
202 if (intake_accumulator_ > 0) {
203 --intake_accumulator_;
Austin Schuh402722c2019-06-29 21:27:06 -0700204 if (!superstructure_status_fetcher_->intake.estopped) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700205 new_superstructure_goal->intake.voltage_rollers = 10.0;
206 }
207 }
208
Campbell Crowley71b5f132017-02-18 13:16:08 -0800209 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700210 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800211 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700212 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700213 if (robot_velocity() > 2.0) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700214 new_superstructure_goal->intake.voltage_rollers = 12.0;
215 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700216 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700217 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800218 } else if (data.IsPressed(kIntakeOut)) {
219 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800220 }
221 if (intake_goal_ < 0.1) {
222 new_superstructure_goal->intake.voltage_rollers =
223 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800224 }
225
226 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700227 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800228 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800229 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800230 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700231 new_superstructure_goal->indexer.voltage_rollers = 12.0;
232 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700233 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700234 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700235 break;
236 case ShotDistance::MIDDLE_SHOT:
237 case ShotDistance::FAR_SHOT:
238 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
239 break;
240 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800241 } else {
242 new_superstructure_goal->indexer.voltage_rollers = 0.0;
243 new_superstructure_goal->indexer.angular_velocity = 0.0;
244 }
245
Austin Schuhf257f3c2019-10-27 21:00:43 -0700246 AOS_LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800247 if (!new_superstructure_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700248 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -0800249 }
250 }
251
252 private:
Austin Schuh402722c2019-06-29 21:27:06 -0700253 ::aos::Fetcher<::y2017::control_loops::SuperstructureQueue::Status>
254 superstructure_status_fetcher_;
255 ::aos::Sender<::y2017::control_loops::SuperstructureQueue::Goal>
256 superstructure_goal_sender_;
257
258 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
259
Campbell Crowley71b5f132017-02-18 13:16:08 -0800260 // Current goals to send to the robot.
261 double intake_goal_ = 0.0;
262 double turret_goal_ = 0.0;
263 double hood_goal_ = 0.3;
264 double shooter_velocity_ = 0.0;
Austin Schuha250b2d2019-05-27 16:14:02 -0700265 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800266
Parker Schuh208a58d2017-04-12 20:51:38 -0700267 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800268
269 bool fire_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800270};
271
272} // namespace joysticks
273} // namespace input
274} // namespace y2017
275
276int main() {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700277 ::aos::InitNRT(true);
278
Austin Schuh3e45c752019-02-02 12:19:11 -0800279 ::aos::ShmEventLoop event_loop;
280 ::y2017::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700281
282 event_loop.Run();
283
Campbell Crowley71b5f132017-02-18 13:16:08 -0800284 ::aos::Cleanup();
285}