blob: 58a39180e16dc005c5e5dcf3d9b050fbb925fe1e [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/init.h"
8#include "aos/input/action_joystick_input.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/input/driver_station_data.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/input/drivetrain_input.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/logging/logging.h"
12#include "aos/time/time.h"
13#include "aos/util/log_interval.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080014#include "frc971/autonomous/base_autonomous_actor.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080015#include "y2017/constants.h"
Sabina Davis82b19182017-11-10 09:30:25 -080016#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "y2017/control_loops/superstructure/superstructure_goal_generated.h"
18#include "y2017/control_loops/superstructure/superstructure_status_generated.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080019
Campbell Crowley71b5f132017-02-18 13:16:08 -080020using ::aos::input::driver_station::ButtonLocation;
21using ::aos::input::driver_station::ControlBit;
22using ::aos::input::driver_station::JoystickAxis;
23using ::aos::input::driver_station::POVLocation;
Sabina Davis92d2efa2017-11-04 22:35:25 -070024using ::aos::input::DrivetrainInputReader;
Campbell Crowley71b5f132017-02-18 13:16:08 -080025
26namespace y2017 {
27namespace input {
28namespace joysticks {
29
Alex Perrycb7da4b2019-08-28 19:35:56 -070030namespace superstructure = control_loops::superstructure;
31
Austin Schuh55c8d302017-04-05 19:25:37 -070032const ButtonLocation kGearSlotBack(2, 11);
33
Campbell Crowley71b5f132017-02-18 13:16:08 -080034const ButtonLocation kIntakeDown(3, 9);
Austin Schuhd0629b12017-03-22 22:37:16 -070035const POVLocation kIntakeUp(3, 90);
Campbell Crowley71b5f132017-02-18 13:16:08 -080036const ButtonLocation kIntakeIn(3, 12);
37const ButtonLocation kIntakeOut(3, 8);
Campbell Crowley71b5f132017-02-18 13:16:08 -080038const ButtonLocation kFire(3, 3);
Parker Schuh208a58d2017-04-12 20:51:38 -070039const ButtonLocation kVisionDistanceShot(3, 7);
Campbell Crowley71b5f132017-02-18 13:16:08 -080040const ButtonLocation kMiddleShot(3, 6);
41const POVLocation kFarShot(3, 270);
42
43const ButtonLocation kVisionAlign(3, 5);
44
45const ButtonLocation kReverseIndexer(3, 4);
46const ButtonLocation kExtra1(3, 11);
47const ButtonLocation kExtra2(3, 10);
Austin Schuhd0629b12017-03-22 22:37:16 -070048const ButtonLocation kHang(3, 2);
Campbell Crowley71b5f132017-02-18 13:16:08 -080049
Austin Schuha250b2d2019-05-27 16:14:02 -070050class Reader : public ::aos::input::ActionJoystickInput {
Campbell Crowley71b5f132017-02-18 13:16:08 -080051 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080052 Reader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -070053 : ::aos::input::ActionJoystickInput(
54 event_loop,
55 ::y2017::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuh402722c2019-06-29 21:27:06 -070056 DrivetrainInputReader::InputType::kSteeringWheel, {}),
57 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 event_loop
59 ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
60 "/superstructure")),
Austin Schuh402722c2019-06-29 21:27:06 -070061 superstructure_goal_sender_(
62 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 ->MakeSender<::y2017::control_loops::superstructure::Goal>(
64 "/superstructure")) {}
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
Campbell Crowley71b5f132017-02-18 13:16:08 -080068 void HandleTeleop(const ::aos::input::driver_station::Data &data) {
69 // Default the intake to in.
Austin Schuhd0629b12017-03-22 22:37:16 -070070 intake_goal_ = 0.07;
Adam Snaidere0554ef2017-03-11 23:02:45 -080071 bool lights_on = false;
Austin Schuhd0629b12017-03-22 22:37:16 -070072 bool vision_track = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -080073
Austin Schuh402722c2019-06-29 21:27:06 -070074 superstructure_status_fetcher_.Fetch();
75 if (!superstructure_status_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070076 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -080077 return;
78 }
79
Austin Schuhd0629b12017-03-22 22:37:16 -070080 if (data.IsPressed(kIntakeUp)) {
81 intake_goal_ = 0.0;
Austin Schuh3ae47432017-04-16 19:15:46 -070082 turret_goal_ = M_PI / 3.0;
Austin Schuhd0629b12017-03-22 22:37:16 -070083 }
Austin Schuh405724e2017-04-09 18:34:18 -070084 if (data.IsPressed(kIntakeDown)) {
85 intake_goal_ = 0.235;
86 // Don't go quite so far out since we have a gear mech out now.
87 if (data.IsPressed(kIntakeUp)) {
88 intake_goal_ = 0.160;
89 }
90 }
Austin Schuhd0629b12017-03-22 22:37:16 -070091
Campbell Crowley71b5f132017-02-18 13:16:08 -080092 if (data.IsPressed(kVisionAlign)) {
93 // Align shot using vision
94 // TODO(campbell): Add vision aligning.
Adam Snaidere0554ef2017-03-11 23:02:45 -080095 lights_on = true;
Austin Schuhd0629b12017-03-22 22:37:16 -070096 vision_track = true;
97 }
98 if (data.PosEdge(kMiddleShot)) {
99 turret_goal_ = -M_PI;
100 }
101 if (data.PosEdge(kFarShot)) {
102 turret_goal_ = 0.0;
103 }
Parker Schuh208a58d2017-04-12 20:51:38 -0700104 if (data.PosEdge(kVisionDistanceShot)) {
105 turret_goal_ = 0.0;
106 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700107
Parker Schuh208a58d2017-04-12 20:51:38 -0700108 if (data.IsPressed(kVisionDistanceShot)) {
109 last_shot_distance_ = ShotDistance::VISION_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800110 } else if (data.IsPressed(kMiddleShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700111 last_shot_distance_ = ShotDistance::MIDDLE_SHOT;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800112 } else if (data.IsPressed(kFarShot)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700113 last_shot_distance_ = ShotDistance::FAR_SHOT;
114 }
115
Parker Schuh208a58d2017-04-12 20:51:38 -0700116 if (data.IsPressed(kVisionAlign) ||
Austin Schuhd0629b12017-03-22 22:37:16 -0700117 data.IsPressed(kMiddleShot) || data.IsPressed(kFarShot) ||
118 data.IsPressed(kFire)) {
119 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700120 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700121 hood_goal_ = 0.10;
122 shooter_velocity_ = 300.0;
123
Parker Schuh208a58d2017-04-12 20:51:38 -0700124 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700125 break;
126 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700127 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700128 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700129 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700130 break;
131 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700132 hood_goal_ = 0.47;
133 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700134 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700135 break;
136 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800137 } else {
Austin Schuhd0629b12017-03-22 22:37:16 -0700138 //hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800139 shooter_velocity_ = 0.0;
140 }
141
142 if (data.IsPressed(kExtra1)) {
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 }
146 if (data.IsPressed(kExtra2)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700147 //turret_goal_ = M_PI * 3.0 / 4.0;
148 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800149 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700150 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800151
152 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700153 if (data.IsPressed(kVisionAlign)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154 fire_ = fire_ && superstructure_status_fetcher_->turret()->vision_tracking();
Austin Schuhd0629b12017-03-22 22:37:16 -0700155 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800156
Alex Perrycb7da4b2019-08-28 19:35:56 -0700157 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuhd0629b12017-03-22 22:37:16 -0700158 if (data.IsPressed(kHang)) {
159 intake_goal_ = 0.23;
160 }
161
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 bool intake_disable_intake = false;
163 double intake_gear_servo = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800164
Austin Schuh6a8131b2017-04-08 15:39:22 -0700165 if (data.IsPressed(kIntakeUp)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 intake_gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700167 } else {
168 // Clamp the gear
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 intake_gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700170 }
171
Alex Perrycb7da4b2019-08-28 19:35:56 -0700172 double intake_voltage_rollers = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800173
Alex Perrycb7da4b2019-08-28 19:35:56 -0700174 if (superstructure_status_fetcher_->intake()->position() >
175 superstructure_status_fetcher_->intake()->unprofiled_goal_position() +
Austin Schuh402722c2019-06-29 21:27:06 -0700176 0.01) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700177 intake_accumulator_ = 10;
178 }
179 if (intake_accumulator_ > 0) {
180 --intake_accumulator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700181 if (!superstructure_status_fetcher_->intake()->estopped()) {
182 intake_voltage_rollers = 10.0;
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700183 }
184 }
185
Campbell Crowley71b5f132017-02-18 13:16:08 -0800186 if (data.IsPressed(kHang)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 intake_voltage_rollers = -10.0;
188 intake_disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700189 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700190 if (robot_velocity() > 2.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700191 intake_voltage_rollers = 12.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700192 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 intake_voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700194 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800195 } else if (data.IsPressed(kIntakeOut)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700196 intake_voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800197 }
198 if (intake_goal_ < 0.1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 intake_voltage_rollers = ::std::min(8.0, intake_voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800200 }
201
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 double indexer_voltage_rollers = 0.0;
203 double indexer_angular_velocity = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800204 if (data.IsPressed(kReverseIndexer)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205 indexer_voltage_rollers = -12.0;
206 indexer_angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800207 } else if (fire_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208 indexer_voltage_rollers = 12.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700209 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700210 case ShotDistance::VISION_SHOT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 indexer_angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700212 break;
213 case ShotDistance::MIDDLE_SHOT:
214 case ShotDistance::FAR_SHOT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215 indexer_angular_velocity = -2.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700216 break;
217 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800218 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 indexer_voltage_rollers = 0.0;
220 indexer_angular_velocity = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800221 }
222
Alex Perrycb7da4b2019-08-28 19:35:56 -0700223 superstructure::IndexerGoal::Builder indexer_goal_builder =
224 builder.MakeBuilder<superstructure::IndexerGoal>();
225 indexer_goal_builder.add_angular_velocity(indexer_angular_velocity);
226 indexer_goal_builder.add_voltage_rollers(indexer_voltage_rollers);
227
228 flatbuffers::Offset<superstructure::IndexerGoal> indexer_goal_offset =
229 indexer_goal_builder.Finish();
230
231 flatbuffers::Offset<frc971::ProfileParameters>
232 turret_profile_parameters_offset;
233 if (vision_track) {
234 turret_profile_parameters_offset =
235 frc971::CreateProfileParameters(*builder.fbb(), 10.0, 35.0);
236 } else {
237 turret_profile_parameters_offset =
238 frc971::CreateProfileParameters(*builder.fbb(), 6.0, 15.0);
239 }
240 superstructure::TurretGoal::Builder turret_goal_builder =
241 builder.MakeBuilder<superstructure::TurretGoal>();
242 turret_goal_builder.add_angle(turret_goal_);
243 turret_goal_builder.add_track(vision_track);
244 turret_goal_builder.add_profile_params(turret_profile_parameters_offset);
245 flatbuffers::Offset<superstructure::TurretGoal> turret_goal_offset =
246 turret_goal_builder.Finish();
247
248 flatbuffers::Offset<frc971::ProfileParameters>
249 intake_profile_parameters_offset =
250 frc971::CreateProfileParameters(*builder.fbb(), 0.5, 3.0);
251
252 superstructure::IntakeGoal::Builder intake_goal_builder =
253 builder.MakeBuilder<superstructure::IntakeGoal>();
254 intake_goal_builder.add_voltage_rollers(intake_voltage_rollers);
255 intake_goal_builder.add_distance(intake_goal_);
256 intake_goal_builder.add_disable_intake(intake_disable_intake);
257 intake_goal_builder.add_gear_servo(intake_gear_servo);
258 intake_goal_builder.add_profile_params(intake_profile_parameters_offset);
259 flatbuffers::Offset<superstructure::IntakeGoal> intake_goal_offset =
260 intake_goal_builder.Finish();
261
262 flatbuffers::Offset<frc971::ProfileParameters>
263 hood_profile_parameters_offset =
264 frc971::CreateProfileParameters(*builder.fbb(), 5.0, 25.0);
265
266 superstructure::HoodGoal::Builder hood_goal_builder =
267 builder.MakeBuilder<superstructure::HoodGoal>();
268 hood_goal_builder.add_angle(hood_goal_);
269 hood_goal_builder.add_profile_params(hood_profile_parameters_offset);
270 flatbuffers::Offset<superstructure::HoodGoal> hood_goal_offset =
271 hood_goal_builder.Finish();
272
273 superstructure::ShooterGoal::Builder shooter_goal_builder =
274 builder.MakeBuilder<superstructure::ShooterGoal>();
275 shooter_goal_builder.add_angular_velocity(shooter_velocity_);
276 flatbuffers::Offset<superstructure::ShooterGoal> shooter_goal_offset =
277 shooter_goal_builder.Finish();
278
279 superstructure::Goal::Builder goal_builder =
280 builder.MakeBuilder<superstructure::Goal>();
281 goal_builder.add_lights_on(lights_on);
282 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
283 goal_builder.add_use_vision_for_shots(true);
284 } else {
285 goal_builder.add_use_vision_for_shots(false);
286 }
287
288 goal_builder.add_intake(intake_goal_offset);
289 goal_builder.add_indexer(indexer_goal_offset);
290 goal_builder.add_turret(turret_goal_offset);
291 goal_builder.add_hood(hood_goal_offset);
292 goal_builder.add_shooter(shooter_goal_offset);
293
294 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700295 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -0800296 }
297 }
298
299 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 ::aos::Fetcher<::y2017::control_loops::superstructure::Status>
Austin Schuh402722c2019-06-29 21:27:06 -0700301 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 ::aos::Sender<::y2017::control_loops::superstructure::Goal>
Austin Schuh402722c2019-06-29 21:27:06 -0700303 superstructure_goal_sender_;
304
305 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
306
Campbell Crowley71b5f132017-02-18 13:16:08 -0800307 // 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;
Austin Schuha250b2d2019-05-27 16:14:02 -0700312 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800313
Parker Schuh208a58d2017-04-12 20:51:38 -0700314 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800315
316 bool fire_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800317};
318
319} // namespace joysticks
320} // namespace input
321} // namespace y2017
322
Austin Schuh094d09b2020-11-20 23:26:52 -0800323int main(int argc, char **argv) {
324 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700325
Alex Perrycb7da4b2019-08-28 19:35:56 -0700326 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
327 aos::configuration::ReadConfig("config.json");
328
329 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800330 ::y2017::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700331
332 event_loop.Run();
333
Austin Schuhae87e312020-08-01 16:15:01 -0700334 return 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800335}