blob: 78f81c7c9f0ab90dee055226d027b8d06c1e8760 [file] [log] [blame]
Campbell Crowley71b5f132017-02-18 13:16:08 -08001#include <unistd.h>
Campbell Crowley71b5f132017-02-18 13:16:08 -08002
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
4#include <cstdio>
5#include <cstring>
6
John Park33858a32018-09-28 23:05:48 -07007#include "aos/actions/actions.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
10#include "aos/time/time.h"
11#include "aos/util/log_interval.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080012#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070013#include "frc971/input/action_joystick_input.h"
14#include "frc971/input/driver_station_data.h"
15#include "frc971/input/drivetrain_input.h"
Austin Schuh3028b1d2017-03-11 22:12:13 -080016#include "y2017/constants.h"
Sabina Davis82b19182017-11-10 09:30:25 -080017#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "y2017/control_loops/superstructure/superstructure_goal_generated.h"
19#include "y2017/control_loops/superstructure/superstructure_status_generated.h"
Campbell Crowley71b5f132017-02-18 13:16:08 -080020
James Kuszmaul7077d342021-06-09 20:23:58 -070021using ::frc971::input::DrivetrainInputReader;
22using ::frc971::input::driver_station::ButtonLocation;
23using ::frc971::input::driver_station::ControlBit;
24using ::frc971::input::driver_station::JoystickAxis;
25using ::frc971::input::driver_station::POVLocation;
Campbell Crowley71b5f132017-02-18 13:16:08 -080026
Stephan Pleinesf63bde82024-01-13 15:59:33 -080027namespace y2017::input::joysticks {
Campbell Crowley71b5f132017-02-18 13:16:08 -080028
Alex Perrycb7da4b2019-08-28 19:35:56 -070029namespace superstructure = control_loops::superstructure;
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
James Kuszmaul7077d342021-06-09 20:23:58 -070049class Reader : public ::frc971::input::ActionJoystickInput {
Campbell Crowley71b5f132017-02-18 13:16:08 -080050 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080051 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070052 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070053 event_loop,
54 ::y2017::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuh402722c2019-06-29 21:27:06 -070055 DrivetrainInputReader::InputType::kSteeringWheel, {}),
56 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 event_loop
58 ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
59 "/superstructure")),
Austin Schuh402722c2019-06-29 21:27:06 -070060 superstructure_goal_sender_(
61 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 ->MakeSender<::y2017::control_loops::superstructure::Goal>(
63 "/superstructure")) {}
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
James Kuszmaul7077d342021-06-09 20:23:58 -070067 void HandleTeleop(const ::frc971::input::driver_station::Data &data) {
Campbell Crowley71b5f132017-02-18 13:16:08 -080068 // 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
James Kuszmaul7077d342021-06-09 20:23:58 -0700115 if (data.IsPressed(kVisionAlign) || data.IsPressed(kMiddleShot) ||
116 data.IsPressed(kFarShot) || data.IsPressed(kFire)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700117 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700118 case ShotDistance::VISION_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700119 hood_goal_ = 0.10;
120 shooter_velocity_ = 300.0;
121
Parker Schuh208a58d2017-04-12 20:51:38 -0700122 vision_distance_shot_ = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700123 break;
124 case ShotDistance::MIDDLE_SHOT:
Austin Schuh405724e2017-04-09 18:34:18 -0700125 hood_goal_ = 0.43 - 0.00;
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700126 shooter_velocity_ = 364.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700127 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700128 break;
129 case ShotDistance::FAR_SHOT:
Austin Schuh4b5f7df2017-04-16 20:31:12 -0700130 hood_goal_ = 0.47;
131 shooter_velocity_ = 410.0;
Parker Schuh208a58d2017-04-12 20:51:38 -0700132 vision_distance_shot_ = false;
Austin Schuhd0629b12017-03-22 22:37:16 -0700133 break;
134 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800135 } else {
James Kuszmaul7077d342021-06-09 20:23:58 -0700136 // hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800137 shooter_velocity_ = 0.0;
138 }
139
140 if (data.IsPressed(kExtra1)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700141 // turret_goal_ = -M_PI * 3.0 / 4.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700142 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800143 }
144 if (data.IsPressed(kExtra2)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700145 // turret_goal_ = M_PI * 3.0 / 4.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700146 turret_goal_ -= 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800147 }
Austin Schuhd0629b12017-03-22 22:37:16 -0700148 turret_goal_ = ::std::max(::std::min(turret_goal_, M_PI), -M_PI);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800149
150 fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700151 if (data.IsPressed(kVisionAlign)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700152 fire_ =
153 fire_ && superstructure_status_fetcher_->turret()->vision_tracking();
Austin Schuhd0629b12017-03-22 22:37:16 -0700154 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800155
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuhd0629b12017-03-22 22:37:16 -0700157 if (data.IsPressed(kHang)) {
158 intake_goal_ = 0.23;
159 }
160
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 bool intake_disable_intake = false;
162 double intake_gear_servo = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800163
Austin Schuh6a8131b2017-04-08 15:39:22 -0700164 if (data.IsPressed(kIntakeUp)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165 intake_gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700166 } else {
167 // Clamp the gear
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168 intake_gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700169 }
170
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171 double intake_voltage_rollers = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800172
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 if (superstructure_status_fetcher_->intake()->position() >
174 superstructure_status_fetcher_->intake()->unprofiled_goal_position() +
Austin Schuh402722c2019-06-29 21:27:06 -0700175 0.01) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700176 intake_accumulator_ = 10;
177 }
178 if (intake_accumulator_ > 0) {
179 --intake_accumulator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700180 if (!superstructure_status_fetcher_->intake()->estopped()) {
181 intake_voltage_rollers = 10.0;
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700182 }
183 }
184
Campbell Crowley71b5f132017-02-18 13:16:08 -0800185 if (data.IsPressed(kHang)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186 intake_voltage_rollers = -10.0;
187 intake_disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700188 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700189 if (robot_velocity() > 2.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190 intake_voltage_rollers = 12.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700191 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 intake_voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700193 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800194 } else if (data.IsPressed(kIntakeOut)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 intake_voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800196 }
197 if (intake_goal_ < 0.1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700198 intake_voltage_rollers = ::std::min(8.0, intake_voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800199 }
200
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201 double indexer_voltage_rollers = 0.0;
202 double indexer_angular_velocity = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800203 if (data.IsPressed(kReverseIndexer)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 indexer_voltage_rollers = -12.0;
205 indexer_angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800206 } else if (fire_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 indexer_voltage_rollers = 12.0;
James Kuszmaul7077d342021-06-09 20:23:58 -0700208 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700209 case ShotDistance::VISION_SHOT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210 indexer_angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700211 break;
212 case ShotDistance::MIDDLE_SHOT:
213 case ShotDistance::FAR_SHOT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214 indexer_angular_velocity = -2.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700215 break;
216 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800217 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700218 indexer_voltage_rollers = 0.0;
219 indexer_angular_velocity = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800220 }
221
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 superstructure::IndexerGoal::Builder indexer_goal_builder =
223 builder.MakeBuilder<superstructure::IndexerGoal>();
224 indexer_goal_builder.add_angular_velocity(indexer_angular_velocity);
225 indexer_goal_builder.add_voltage_rollers(indexer_voltage_rollers);
226
227 flatbuffers::Offset<superstructure::IndexerGoal> indexer_goal_offset =
228 indexer_goal_builder.Finish();
229
230 flatbuffers::Offset<frc971::ProfileParameters>
231 turret_profile_parameters_offset;
232 if (vision_track) {
233 turret_profile_parameters_offset =
234 frc971::CreateProfileParameters(*builder.fbb(), 10.0, 35.0);
235 } else {
236 turret_profile_parameters_offset =
237 frc971::CreateProfileParameters(*builder.fbb(), 6.0, 15.0);
238 }
239 superstructure::TurretGoal::Builder turret_goal_builder =
240 builder.MakeBuilder<superstructure::TurretGoal>();
241 turret_goal_builder.add_angle(turret_goal_);
242 turret_goal_builder.add_track(vision_track);
243 turret_goal_builder.add_profile_params(turret_profile_parameters_offset);
244 flatbuffers::Offset<superstructure::TurretGoal> turret_goal_offset =
245 turret_goal_builder.Finish();
246
247 flatbuffers::Offset<frc971::ProfileParameters>
248 intake_profile_parameters_offset =
James Kuszmaul7077d342021-06-09 20:23:58 -0700249 frc971::CreateProfileParameters(*builder.fbb(), 0.5, 3.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700250
251 superstructure::IntakeGoal::Builder intake_goal_builder =
252 builder.MakeBuilder<superstructure::IntakeGoal>();
253 intake_goal_builder.add_voltage_rollers(intake_voltage_rollers);
254 intake_goal_builder.add_distance(intake_goal_);
255 intake_goal_builder.add_disable_intake(intake_disable_intake);
256 intake_goal_builder.add_gear_servo(intake_gear_servo);
257 intake_goal_builder.add_profile_params(intake_profile_parameters_offset);
258 flatbuffers::Offset<superstructure::IntakeGoal> intake_goal_offset =
259 intake_goal_builder.Finish();
260
261 flatbuffers::Offset<frc971::ProfileParameters>
262 hood_profile_parameters_offset =
263 frc971::CreateProfileParameters(*builder.fbb(), 5.0, 25.0);
264
265 superstructure::HoodGoal::Builder hood_goal_builder =
266 builder.MakeBuilder<superstructure::HoodGoal>();
267 hood_goal_builder.add_angle(hood_goal_);
268 hood_goal_builder.add_profile_params(hood_profile_parameters_offset);
269 flatbuffers::Offset<superstructure::HoodGoal> hood_goal_offset =
270 hood_goal_builder.Finish();
271
272 superstructure::ShooterGoal::Builder shooter_goal_builder =
273 builder.MakeBuilder<superstructure::ShooterGoal>();
274 shooter_goal_builder.add_angular_velocity(shooter_velocity_);
275 flatbuffers::Offset<superstructure::ShooterGoal> shooter_goal_offset =
276 shooter_goal_builder.Finish();
277
278 superstructure::Goal::Builder goal_builder =
279 builder.MakeBuilder<superstructure::Goal>();
280 goal_builder.add_lights_on(lights_on);
281 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
282 goal_builder.add_use_vision_for_shots(true);
283 } else {
284 goal_builder.add_use_vision_for_shots(false);
285 }
286
287 goal_builder.add_intake(intake_goal_offset);
288 goal_builder.add_indexer(indexer_goal_offset);
289 goal_builder.add_turret(turret_goal_offset);
290 goal_builder.add_hood(hood_goal_offset);
291 goal_builder.add_shooter(shooter_goal_offset);
292
Philipp Schrader790cb542023-07-05 21:06:52 -0700293 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700294 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -0800295 }
296 }
297
298 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 ::aos::Fetcher<::y2017::control_loops::superstructure::Status>
Austin Schuh402722c2019-06-29 21:27:06 -0700300 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 ::aos::Sender<::y2017::control_loops::superstructure::Goal>
Austin Schuh402722c2019-06-29 21:27:06 -0700302 superstructure_goal_sender_;
303
304 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
305
Campbell Crowley71b5f132017-02-18 13:16:08 -0800306 // Current goals to send to the robot.
307 double intake_goal_ = 0.0;
308 double turret_goal_ = 0.0;
309 double hood_goal_ = 0.3;
310 double shooter_velocity_ = 0.0;
Austin Schuha250b2d2019-05-27 16:14:02 -0700311 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800312
Parker Schuh208a58d2017-04-12 20:51:38 -0700313 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800314
315 bool fire_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800316};
317
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800318} // namespace y2017::input::joysticks
Campbell Crowley71b5f132017-02-18 13:16:08 -0800319
Austin Schuh094d09b2020-11-20 23:26:52 -0800320int main(int argc, char **argv) {
321 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700322
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800324 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325
326 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800327 ::y2017::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700328
329 event_loop.Run();
330
Austin Schuhae87e312020-08-01 16:15:01 -0700331 return 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800332}