blob: fd8dc3841e36b99988dfd9b126d2ad4944d8303c [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
27namespace y2017 {
28namespace input {
29namespace joysticks {
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031namespace superstructure = control_loops::superstructure;
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
James Kuszmaul7077d342021-06-09 20:23:58 -070051class Reader : public ::frc971::input::ActionJoystickInput {
Campbell Crowley71b5f132017-02-18 13:16:08 -080052 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080053 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070054 : ::frc971::input::ActionJoystickInput(
Austin Schuha250b2d2019-05-27 16:14:02 -070055 event_loop,
56 ::y2017::control_loops::drivetrain::GetDrivetrainConfig(),
Austin Schuh402722c2019-06-29 21:27:06 -070057 DrivetrainInputReader::InputType::kSteeringWheel, {}),
58 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 event_loop
60 ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
61 "/superstructure")),
Austin Schuh402722c2019-06-29 21:27:06 -070062 superstructure_goal_sender_(
63 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070064 ->MakeSender<::y2017::control_loops::superstructure::Goal>(
65 "/superstructure")) {}
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
James Kuszmaul7077d342021-06-09 20:23:58 -070069 void HandleTeleop(const ::frc971::input::driver_station::Data &data) {
Campbell Crowley71b5f132017-02-18 13:16:08 -080070 // 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()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070077 AOS_LOG(ERROR, "Got no superstructure status packet.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -080078 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
James Kuszmaul7077d342021-06-09 20:23:58 -0700117 if (data.IsPressed(kVisionAlign) || data.IsPressed(kMiddleShot) ||
118 data.IsPressed(kFarShot) || data.IsPressed(kFire)) {
Austin Schuhd0629b12017-03-22 22:37:16 -0700119 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 {
James Kuszmaul7077d342021-06-09 20:23:58 -0700138 // hood_goal_ = 0.15;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800139 shooter_velocity_ = 0.0;
140 }
141
142 if (data.IsPressed(kExtra1)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700143 // turret_goal_ = -M_PI * 3.0 / 4.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700144 turret_goal_ += 0.150;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800145 }
146 if (data.IsPressed(kExtra2)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700147 // turret_goal_ = M_PI * 3.0 / 4.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700148 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)) {
James Kuszmaul7077d342021-06-09 20:23:58 -0700154 fire_ =
155 fire_ && superstructure_status_fetcher_->turret()->vision_tracking();
Austin Schuhd0629b12017-03-22 22:37:16 -0700156 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800157
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuhd0629b12017-03-22 22:37:16 -0700159 if (data.IsPressed(kHang)) {
160 intake_goal_ = 0.23;
161 }
162
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 bool intake_disable_intake = false;
164 double intake_gear_servo = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800165
Austin Schuh6a8131b2017-04-08 15:39:22 -0700166 if (data.IsPressed(kIntakeUp)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167 intake_gear_servo = 0.30;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700168 } else {
169 // Clamp the gear
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170 intake_gear_servo = 0.66;
Austin Schuh6a8131b2017-04-08 15:39:22 -0700171 }
172
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 double intake_voltage_rollers = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800174
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 if (superstructure_status_fetcher_->intake()->position() >
176 superstructure_status_fetcher_->intake()->unprofiled_goal_position() +
Austin Schuh402722c2019-06-29 21:27:06 -0700177 0.01) {
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700178 intake_accumulator_ = 10;
179 }
180 if (intake_accumulator_ > 0) {
181 --intake_accumulator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 if (!superstructure_status_fetcher_->intake()->estopped()) {
183 intake_voltage_rollers = 10.0;
Austin Schuh8e4a7ee2017-04-05 19:26:06 -0700184 }
185 }
186
Campbell Crowley71b5f132017-02-18 13:16:08 -0800187 if (data.IsPressed(kHang)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 intake_voltage_rollers = -10.0;
189 intake_disable_intake = true;
Austin Schuhd0629b12017-03-22 22:37:16 -0700190 } else if (data.IsPressed(kIntakeIn) || data.IsPressed(kFire)) {
Austin Schuha250b2d2019-05-27 16:14:02 -0700191 if (robot_velocity() > 2.0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 intake_voltage_rollers = 12.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700193 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 intake_voltage_rollers = 11.0;
Austin Schuhd0629b12017-03-22 22:37:16 -0700195 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800196 } else if (data.IsPressed(kIntakeOut)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 intake_voltage_rollers = -8.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800198 }
199 if (intake_goal_ < 0.1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 intake_voltage_rollers = ::std::min(8.0, intake_voltage_rollers);
Campbell Crowley71b5f132017-02-18 13:16:08 -0800201 }
202
Alex Perrycb7da4b2019-08-28 19:35:56 -0700203 double indexer_voltage_rollers = 0.0;
204 double indexer_angular_velocity = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800205 if (data.IsPressed(kReverseIndexer)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206 indexer_voltage_rollers = -12.0;
207 indexer_angular_velocity = 1.0;
Austin Schuh3028b1d2017-03-11 22:12:13 -0800208 } else if (fire_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209 indexer_voltage_rollers = 12.0;
James Kuszmaul7077d342021-06-09 20:23:58 -0700210 switch (last_shot_distance_) {
Parker Schuh208a58d2017-04-12 20:51:38 -0700211 case ShotDistance::VISION_SHOT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700212 indexer_angular_velocity = -1.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700213 break;
214 case ShotDistance::MIDDLE_SHOT:
215 case ShotDistance::FAR_SHOT:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700216 indexer_angular_velocity = -2.25 * M_PI;
Austin Schuhd0629b12017-03-22 22:37:16 -0700217 break;
218 }
Campbell Crowley71b5f132017-02-18 13:16:08 -0800219 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220 indexer_voltage_rollers = 0.0;
221 indexer_angular_velocity = 0.0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800222 }
223
Alex Perrycb7da4b2019-08-28 19:35:56 -0700224 superstructure::IndexerGoal::Builder indexer_goal_builder =
225 builder.MakeBuilder<superstructure::IndexerGoal>();
226 indexer_goal_builder.add_angular_velocity(indexer_angular_velocity);
227 indexer_goal_builder.add_voltage_rollers(indexer_voltage_rollers);
228
229 flatbuffers::Offset<superstructure::IndexerGoal> indexer_goal_offset =
230 indexer_goal_builder.Finish();
231
232 flatbuffers::Offset<frc971::ProfileParameters>
233 turret_profile_parameters_offset;
234 if (vision_track) {
235 turret_profile_parameters_offset =
236 frc971::CreateProfileParameters(*builder.fbb(), 10.0, 35.0);
237 } else {
238 turret_profile_parameters_offset =
239 frc971::CreateProfileParameters(*builder.fbb(), 6.0, 15.0);
240 }
241 superstructure::TurretGoal::Builder turret_goal_builder =
242 builder.MakeBuilder<superstructure::TurretGoal>();
243 turret_goal_builder.add_angle(turret_goal_);
244 turret_goal_builder.add_track(vision_track);
245 turret_goal_builder.add_profile_params(turret_profile_parameters_offset);
246 flatbuffers::Offset<superstructure::TurretGoal> turret_goal_offset =
247 turret_goal_builder.Finish();
248
249 flatbuffers::Offset<frc971::ProfileParameters>
250 intake_profile_parameters_offset =
James Kuszmaul7077d342021-06-09 20:23:58 -0700251 frc971::CreateProfileParameters(*builder.fbb(), 0.5, 3.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700252
253 superstructure::IntakeGoal::Builder intake_goal_builder =
254 builder.MakeBuilder<superstructure::IntakeGoal>();
255 intake_goal_builder.add_voltage_rollers(intake_voltage_rollers);
256 intake_goal_builder.add_distance(intake_goal_);
257 intake_goal_builder.add_disable_intake(intake_disable_intake);
258 intake_goal_builder.add_gear_servo(intake_gear_servo);
259 intake_goal_builder.add_profile_params(intake_profile_parameters_offset);
260 flatbuffers::Offset<superstructure::IntakeGoal> intake_goal_offset =
261 intake_goal_builder.Finish();
262
263 flatbuffers::Offset<frc971::ProfileParameters>
264 hood_profile_parameters_offset =
265 frc971::CreateProfileParameters(*builder.fbb(), 5.0, 25.0);
266
267 superstructure::HoodGoal::Builder hood_goal_builder =
268 builder.MakeBuilder<superstructure::HoodGoal>();
269 hood_goal_builder.add_angle(hood_goal_);
270 hood_goal_builder.add_profile_params(hood_profile_parameters_offset);
271 flatbuffers::Offset<superstructure::HoodGoal> hood_goal_offset =
272 hood_goal_builder.Finish();
273
274 superstructure::ShooterGoal::Builder shooter_goal_builder =
275 builder.MakeBuilder<superstructure::ShooterGoal>();
276 shooter_goal_builder.add_angular_velocity(shooter_velocity_);
277 flatbuffers::Offset<superstructure::ShooterGoal> shooter_goal_offset =
278 shooter_goal_builder.Finish();
279
280 superstructure::Goal::Builder goal_builder =
281 builder.MakeBuilder<superstructure::Goal>();
282 goal_builder.add_lights_on(lights_on);
283 if (data.IsPressed(kVisionAlign) && vision_distance_shot_) {
284 goal_builder.add_use_vision_for_shots(true);
285 } else {
286 goal_builder.add_use_vision_for_shots(false);
287 }
288
289 goal_builder.add_intake(intake_goal_offset);
290 goal_builder.add_indexer(indexer_goal_offset);
291 goal_builder.add_turret(turret_goal_offset);
292 goal_builder.add_hood(hood_goal_offset);
293 goal_builder.add_shooter(shooter_goal_offset);
294
milind1f1dca32021-07-03 13:50:07 -0700295 if (builder.Send(goal_builder.Finish()) !=
296 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700297 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Campbell Crowley71b5f132017-02-18 13:16:08 -0800298 }
299 }
300
301 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 ::aos::Fetcher<::y2017::control_loops::superstructure::Status>
Austin Schuh402722c2019-06-29 21:27:06 -0700303 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 ::aos::Sender<::y2017::control_loops::superstructure::Goal>
Austin Schuh402722c2019-06-29 21:27:06 -0700305 superstructure_goal_sender_;
306
307 ShotDistance last_shot_distance_ = ShotDistance::VISION_SHOT;
308
Campbell Crowley71b5f132017-02-18 13:16:08 -0800309 // Current goals to send to the robot.
310 double intake_goal_ = 0.0;
311 double turret_goal_ = 0.0;
312 double hood_goal_ = 0.3;
313 double shooter_velocity_ = 0.0;
Austin Schuha250b2d2019-05-27 16:14:02 -0700314 int intake_accumulator_ = 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800315
Parker Schuh208a58d2017-04-12 20:51:38 -0700316 bool vision_distance_shot_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800317
318 bool fire_ = false;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800319};
320
321} // namespace joysticks
322} // namespace input
323} // namespace y2017
324
Austin Schuh094d09b2020-11-20 23:26:52 -0800325int main(int argc, char **argv) {
326 ::aos::InitGoogle(&argc, &argv);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700327
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800329 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700330
331 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh3e45c752019-02-02 12:19:11 -0800332 ::y2017::input::joysticks::Reader reader(&event_loop);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700333
334 event_loop.Run();
335
Austin Schuhae87e312020-08-01 16:15:01 -0700336 return 0;
Campbell Crowley71b5f132017-02-18 13:16:08 -0800337}