blob: 10bf20d3dbd680e9121373d97faa6a492846b8d0 [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
21using ::frc971::control_loops::drivetrain_queue;
Campbell Crowley71b5f132017-02-18 13:16:08 -080022
23using ::aos::input::driver_station::ButtonLocation;
24using ::aos::input::driver_station::ControlBit;
25using ::aos::input::driver_station::JoystickAxis;
26using ::aos::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070027using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080028
29namespace y2017 {
30namespace input {
31namespace joysticks {
32
Austin Schuh55c8d302017-04-05 19:25:37 -070033const ButtonLocation kGearSlotBack(2, 11);
34
Campbell Crowley71b5f132017-02-18 13:16:08 -080035const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070036const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080037const ButtonLocation kIntakeIn(3, 12);
38const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080039const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070040const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080041const ButtonLocation kMiddleShot(3, 6);
42const POVLocation kFarShot(3, 270);
43
44const ButtonLocation kVisionAlign(3, 5);
45
46const ButtonLocation kReverseIndexer(3, 4);
47const ButtonLocation kExtra1(3, 11);
48const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070049const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080050
Austin Schuha250b2d2019-05-27 16:14:02 -070051class Reader : public ::aos::input::ActionJoystickInput {
Campbell Crowley71b5f132017-02-18 13:16:08 -080052 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080053 Reader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -070054 : ::aos::input::ActionJoystickInput(
55 event_loop,
56 ::y2017::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuh402722c2019-06-29 21:27:06 -070057 DrivetrainInputReader::InputType::kSteeringWheel, {}),
58 superstructure_status_fetcher_(
59 event_loop->MakeFetcher<
60 ::y2017::control_loops::SuperstructureQueue::Status>(
61 ".y2017.control_loops.superstructure_queue.status")),
62 superstructure_goal_sender_(
63 event_loop
64 ->MakeSender<::y2017::control_loops::SuperstructureQueue::Goal>(
65 ".y2017.control_loops.superstructure_queue.goal")) {}
Campbell Crowley71b5f132017-02-18 13:16:08 -080066
Parker Schuh208a58d2017-04-12 20:51:38 -070067 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070068
Campbell Crowley71b5f132017-02-18 13:16:08 -080069 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
70 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070071 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080072 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070073 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -080074
Austin Schuh402722c2019-06-29 21:27:06 -070075 superstructure_status_fetcher_.Fetch();
76 if (!superstructure_status_fetcher_.get()) {
Campbell Crowley71b5f132017-02-18 13:16:08 -080077 LOG(ERROR, "Got no superstructure status packet.\n");
78 return;
79 }
80
Austin Schuhd0629b12017-03-22 22:37:16 -070081 if (data.IsPressed(kIntakeUp)) {
82 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -070083 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -070084 }
Austin Schuh405724e2017-04-09 18:34:18 -070085 if (data.IsPressed(kIntakeDown)) {
86 intake_goal_ = 0.235;
87 // Don't go quite so far out since we have a gear mech out now.
88 if (data.IsPressed(kIntakeUp)) {
89 intake_goal_ = 0.160;
90 }
91 }
Austin Schuhd0629b12017-03-22 22:37:16 -070092
Campbell Crowley71b5f132017-02-18 13:16:08 -080093 if (data.IsPressed(kVisionAlign)) {
94 // Align shot using vision
95 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -080096 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -070097 vision_track = true;
98 }
99 if (data.PosEdge(kMiddleShot)) {
100 turret_goal_ = -M_PI;
101 }
102 if (data.PosEdge(kFarShot)) {
103 turret_goal_ = 0.0;
104 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700105 if (data.PosEdge(kVisionDistanceShot)) {
106 turret_goal_ = 0.0;
107 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700108
Parker Schuh208a58d2017-04-12 20:51:38 -0700109 if (data.IsPressed(kVisionDistanceShot)) {
110 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800111 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700112 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800113 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700114 last_shot_distance_ = ShotDistance::FAR_SHOT;
115 }
116
Parker Schuh208a58d2017-04-12 20:51:38 -0700117 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700118 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
119 data.IsPressed(kFire)) {
120 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700121 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700122 hood_goal_ = 0.10;
123 shooter_velocity_ = 300.0;
124
Parker Schuh208a58d2017-04-12 20:51:38 -0700125 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700126 break;
127 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700128 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700129 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700130 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700131 break;
132 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700133 hood_goal_ = 0.47;
134 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700135 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700136 break;
137 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800138 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700139 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800140 shooter_velocity_ = 0.0;
141 }
142
143 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700144 //turret_goal_ = -M_PI * 3.0 / 4.0;
145 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800146 }
147 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700148 //turret_goal_ = M_PI * 3.0 / 4.0;
149 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800150 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700151 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800152
153 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700154 if (data.IsPressed(kVisionAlign)) {
Austin Schuh402722c2019-06-29 21:27:06 -0700155 fire_ = fire_ && superstructure_status_fetcher_->turret.vision_tracking;
Austin Schuhd0629b12017-03-22 22:37:16 -0700156 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800157
Austin Schuh402722c2019-06-29 21:27:06 -0700158 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700159 if (data.IsPressed(kHang)) {
160 intake_goal_ = 0.23;
161 }
162
Campbell Crowley71b5f132017-02-18 13:16:08 -0800163 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800164 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800165 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700166 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800167 new_superstructure_goal->hood.angle = hood_goal_;
168 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
169
Austin Schuh6a8131b2017-04-08 15:39:22 -0700170 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700171 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700172 } else {
173 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700174 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700175 }
176
Campbell Crowley71b5f132017-02-18 13:16:08 -0800177 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800178 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
179
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700180 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700181 if (vision_track) {
182 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
183 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
184 } else {
185 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
186 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
187 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800188 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
189
Austin Schuh3028b1d2017-03-11 22:12:13 -0800190 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800191 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800192
Parker Schuh208a58d2017-04-12 20:51:38 -0700193 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
194 new_superstructure_goal->use_vision_for_shots = true;
195 } else {
196 new_superstructure_goal->use_vision_for_shots = false;
197 }
198
Austin Schuh402722c2019-06-29 21:27:06 -0700199 if (superstructure_status_fetcher_->intake.position >
200 superstructure_status_fetcher_->intake.unprofiled_goal_position +
201 0.01) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700202 intake_accumulator_ = 10;
203 }
204 if (intake_accumulator_ > 0) {
205 --intake_accumulator_;
Austin Schuh402722c2019-06-29 21:27:06 -0700206 if (!superstructure_status_fetcher_->intake.estopped) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700207 new_superstructure_goal->intake.voltage_rollers = 10.0;
208 }
209 }
210
Campbell Crowley71b5f132017-02-18 13:16:08 -0800211 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700212 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800213 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700214 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700215 if (robot_velocity() > 2.0) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700216 new_superstructure_goal->intake.voltage_rollers = 12.0;
217 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700218 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700219 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800220 } else if (data.IsPressed(kIntakeOut)) {
221 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800222 }
223 if (intake_goal_ < 0.1) {
224 new_superstructure_goal->intake.voltage_rollers =
225 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800226 }
227
228 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700229 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800230 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800231 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800232 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700233 new_superstructure_goal->indexer.voltage_rollers = 12.0;
234 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700235 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700236 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700237 break;
238 case ShotDistance::MIDDLE_SHOT:
239 case ShotDistance::FAR_SHOT:
240 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
241 break;
242 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800243 } else {
244 new_superstructure_goal->indexer.voltage_rollers = 0.0;
245 new_superstructure_goal->indexer.angular_velocity = 0.0;
246 }
247
248 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
249 if (!new_superstructure_goal.Send()) {
250 LOG(ERROR, "Sending superstructure goal failed.\n");
251 }
252 }
253
254 private:
Austin Schuh402722c2019-06-29 21:27:06 -0700255 ::aos::Fetcher<::y2017::control_loops::SuperstructureQueue::Status>
256 superstructure_status_fetcher_;
257 ::aos::Sender<::y2017::control_loops::SuperstructureQueue::Goal>
258 superstructure_goal_sender_;
259
260 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
261
Campbell Crowley71b5f132017-02-18 13:16:08 -0800262 // Current goals to send to the robot.
263 double intake_goal_ = 0.0;
264 double turret_goal_ = 0.0;
265 double hood_goal_ = 0.3;
266 double shooter_velocity_ = 0.0;
Austin Schuha250b2d2019-05-27 16:14:02 -0700267 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800268
Parker Schuh208a58d2017-04-12 20:51:38 -0700269 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800270
271 bool fire_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800272};
273
274} // namespace joysticks
275} // namespace input
276} // namespace y2017
277
278int main() {
279 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800280 ::aos::ShmEventLoop event_loop;
281 ::y2017::input::joysticks::Reader reader(&event_loop);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800282 reader.Run();
283 ::aos::Cleanup();
284}