blob: 894d1daf53a1cb4b1741be0cd0bd3bfec080dfa2 [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;
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
Austin Schuha250b2d2019-05-27 16:14:02 -070052class Reader : public ::aos::input::ActionJoystickInput {
Campbell Crowley71b5f132017-02-18 13:16:08 -080053 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080054 Reader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -070055 : ::aos::input::ActionJoystickInput(
56 event_loop,
57 ::y2017::control_loops::drivetrain::GetDrivetrainConfig(),
58 DrivetrainInputReader::InputType::kSteeringWheel, {}) {}
Campbell Crowley71b5f132017-02-18 13:16:08 -080059
Parker Schuh208a58d2017-04-12 20:51:38 -070060 enum class ShotDistance { VISION_SHOT, MIDDLE_SHOT, FAR_SHOT };
Austin Schuhd0629b12017-03-22 22:37:16 -070061
Austin Schuh4b5f7df2017-04-16 20:31:12 -070062 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
Austin Schuhd0629b12017-03-22 22:37:16 -070063
Campbell Crowley71b5f132017-02-18 13:16:08 -080064 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
65 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070066 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080067 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070068 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -080069
Campbell Crowley71b5f132017-02-18 13:16:08 -080070 superstructure_queue.status.FetchLatest();
71 if (!superstructure_queue.status.get()) {
72 LOG(ERROR, "Got no superstructure status packet.\n");
73 return;
74 }
75
Austin Schuhd0629b12017-03-22 22:37:16 -070076 if (data.IsPressed(kIntakeUp)) {
77 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -070078 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -070079 }
Austin Schuh405724e2017-04-09 18:34:18 -070080 if (data.IsPressed(kIntakeDown)) {
81 intake_goal_ = 0.235;
82 // Don't go quite so far out since we have a gear mech out now.
83 if (data.IsPressed(kIntakeUp)) {
84 intake_goal_ = 0.160;
85 }
86 }
Austin Schuhd0629b12017-03-22 22:37:16 -070087
Campbell Crowley71b5f132017-02-18 13:16:08 -080088 if (data.IsPressed(kVisionAlign)) {
89 // Align shot using vision
90 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -080091 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -070092 vision_track = true;
93 }
94 if (data.PosEdge(kMiddleShot)) {
95 turret_goal_ = -M_PI;
96 }
97 if (data.PosEdge(kFarShot)) {
98 turret_goal_ = 0.0;
99 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700100 if (data.PosEdge(kVisionDistanceShot)) {
101 turret_goal_ = 0.0;
102 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700103
Parker Schuh208a58d2017-04-12 20:51:38 -0700104 if (data.IsPressed(kVisionDistanceShot)) {
105 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800106 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700107 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800108 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700109 last_shot_distance_ = ShotDistance::FAR_SHOT;
110 }
111
Parker Schuh208a58d2017-04-12 20:51:38 -0700112 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700113 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
114 data.IsPressed(kFire)) {
115 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700116 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700117 hood_goal_ = 0.10;
118 shooter_velocity_ = 300.0;
119
Parker Schuh208a58d2017-04-12 20:51:38 -0700120 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700121 break;
122 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700123 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700124 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700125 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700126 break;
127 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700128 hood_goal_ = 0.47;
129 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700130 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700131 break;
132 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800133 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700134 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800135 shooter_velocity_ = 0.0;
136 }
137
138 if (data.IsPressed(kExtra1)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700139 //turret_goal_ = -M_PI * 3.0 / 4.0;
140 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800141 }
142 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700143 //turret_goal_ = M_PI * 3.0 / 4.0;
144 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800145 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700146 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800147
148 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700149 if (data.IsPressed(kVisionAlign)) {
150 fire_ = fire_ && superstructure_queue.status->turret.vision_tracking;
151 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800152
153 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
Austin Schuhd0629b12017-03-22 22:37:16 -0700154 if (data.IsPressed(kHang)) {
155 intake_goal_ = 0.23;
156 }
157
Campbell Crowley71b5f132017-02-18 13:16:08 -0800158 new_superstructure_goal->intake.distance = intake_goal_;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800159 new_superstructure_goal->intake.disable_intake = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800160 new_superstructure_goal->turret.angle = turret_goal_;
Austin Schuhd0629b12017-03-22 22:37:16 -0700161 new_superstructure_goal->turret.track = vision_track;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800162 new_superstructure_goal->hood.angle = hood_goal_;
163 new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
164
Austin Schuh6a8131b2017-04-08 15:39:22 -0700165 if (data.IsPressed(kIntakeUp)) {
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700166 new_superstructure_goal->intake.gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700167 } else {
168 // Clamp the gear
Austin Schuhd6fa5e02017-04-12 20:52:17 -0700169 new_superstructure_goal->intake.gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700170 }
171
Campbell Crowley71b5f132017-02-18 13:16:08 -0800172 new_superstructure_goal->intake.profile_params.max_velocity = 0.50;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800173 new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
174
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700175 new_superstructure_goal->intake.profile_params.max_acceleration = 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700176 if (vision_track) {
177 new_superstructure_goal->turret.profile_params.max_acceleration = 35.0;
178 new_superstructure_goal->turret.profile_params.max_velocity = 10.0;
179 } else {
180 new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
181 new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
182 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800183 new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
184
Austin Schuh3028b1d2017-03-11 22:12:13 -0800185 new_superstructure_goal->intake.voltage_rollers = 0.0;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800186 new_superstructure_goal->lights_on = lights_on;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800187
Parker Schuh208a58d2017-04-12 20:51:38 -0700188 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
189 new_superstructure_goal->use_vision_for_shots = true;
190 } else {
191 new_superstructure_goal->use_vision_for_shots = false;
192 }
193
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700194 if (superstructure_queue.status->intake.position >
195 superstructure_queue.status->intake.unprofiled_goal_position + 0.01) {
196 intake_accumulator_ = 10;
197 }
198 if (intake_accumulator_ > 0) {
199 --intake_accumulator_;
200 if (!superstructure_queue.status->intake.estopped) {
201 new_superstructure_goal->intake.voltage_rollers = 10.0;
202 }
203 }
204
Campbell Crowley71b5f132017-02-18 13:16:08 -0800205 if (data.IsPressed(kHang)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700206 new_superstructure_goal->intake.voltage_rollers = -10.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800207 new_superstructure_goal->intake.disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700208 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700209 if (robot_velocity() > 2.0) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700210 new_superstructure_goal->intake.voltage_rollers = 12.0;
211 } else {
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700212 new_superstructure_goal->intake.voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700213 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800214 } else if (data.IsPressed(kIntakeOut)) {
215 new_superstructure_goal->intake.voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800216 }
217 if (intake_goal_ < 0.1) {
218 new_superstructure_goal->intake.voltage_rollers =
219 ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800220 }
221
222 if (data.IsPressed(kReverseIndexer)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700223 new_superstructure_goal->indexer.voltage_rollers = -12.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800224 new_superstructure_goal->indexer.angular_velocity = 4.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800225 new_superstructure_goal->indexer.angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800226 } else if (fire_) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700227 new_superstructure_goal->indexer.voltage_rollers = 12.0;
228 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700229 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700230 new_superstructure_goal->indexer.angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700231 break;
232 case ShotDistance::MIDDLE_SHOT:
233 case ShotDistance::FAR_SHOT:
234 new_superstructure_goal->indexer.angular_velocity = -2.25 * M_PI;
235 break;
236 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800237 } else {
238 new_superstructure_goal->indexer.voltage_rollers = 0.0;
239 new_superstructure_goal->indexer.angular_velocity = 0.0;
240 }
241
242 LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal);
243 if (!new_superstructure_goal.Send()) {
244 LOG(ERROR, "Sending superstructure goal failed.\n");
245 }
246 }
247
248 private:
Campbell Crowley71b5f132017-02-18 13:16:08 -0800249 // Current goals to send to the robot.
250 double intake_goal_ = 0.0;
251 double turret_goal_ = 0.0;
252 double hood_goal_ = 0.3;
253 double shooter_velocity_ = 0.0;
Austin Schuha250b2d2019-05-27 16:14:02 -0700254 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800255
Parker Schuh208a58d2017-04-12 20:51:38 -0700256 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800257
258 bool fire_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800259};
260
261} // namespace joysticks
262} // namespace input
263} // namespace y2017
264
265int main() {
266 ::aos::Init(-1);
Austin Schuh3e45c752019-02-02 12:19:11 -0800267 ::aos::ShmEventLoop event_loop;
268 ::y2017::input::joysticks::Reader reader(&event_loop);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800269 reader.Run();
270 ::aos::Cleanup();
271}