blob: 9934571a57d473f9769587702b00dfd00306c164 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include <unistd.h>
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cmath>
4#include <cstdio>
5#include <cstring>
6
Stephan Massaltd021f972020-01-05 20:41:23 -08007#include "aos/actions/actions.h"
8#include "aos/init.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08009#include "aos/logging/logging.h"
10#include "aos/network/team_number.h"
11#include "aos/util/log_interval.h"
12#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070013#include "frc971/control_loops/drivetrain/localizer_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070014#include "frc971/input/action_joystick_input.h"
15#include "frc971/input/driver_station_data.h"
16#include "frc971/input/drivetrain_input.h"
17#include "frc971/input/joystick_input.h"
Sabina Davisa8fed3d2020-02-22 21:44:57 -080018#include "y2020/constants.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080019#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080020#include "y2020/control_loops/superstructure/superstructure_goal_generated.h"
21#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080022#include "y2020/setpoint_generated.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080023
James Kuszmaul7077d342021-06-09 20:23:58 -070024using frc971::input::driver_station::ButtonLocation;
25using frc971::input::driver_station::ControlBit;
26using frc971::input::driver_station::JoystickAxis;
27using frc971::input::driver_station::POVLocation;
Stephan Massaltd021f972020-01-05 20:41:23 -080028
Austin Schuhf0a637c2020-02-25 23:44:12 -080029using frc971::CreateProfileParameters;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080030using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
31using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
32
Stephan Massaltd021f972020-01-05 20:41:23 -080033namespace y2020 {
34namespace input {
35namespace joysticks {
36
37namespace superstructure = y2020::control_loops::superstructure;
38
Sabina Davisa8fed3d2020-02-22 21:44:57 -080039// TODO(sabina): fix button locations.
40
Austin Schuhf0a637c2020-02-25 23:44:12 -080041const ButtonLocation kShootFast(3, 16);
Austin Schuh3fdf2412021-10-16 13:56:46 -070042const ButtonLocation kAutoTrack(3, 3);
43const ButtonLocation kAutoNoHood(3, 5);
44const ButtonLocation kHood(3, 4);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080045const ButtonLocation kShootSlow(4, 2);
Austin Schuh43a220f2020-02-26 22:02:34 -080046const ButtonLocation kFeed(4, 1);
Austin Schuh3fdf2412021-10-16 13:56:46 -070047const ButtonLocation kFeedDriver(1, 2);
Austin Schuhf0a637c2020-02-25 23:44:12 -080048const ButtonLocation kIntakeExtend(3, 9);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080049const ButtonLocation kIntakeIn(4, 4);
Tyler Chatow1039e432020-02-28 21:37:50 -080050const ButtonLocation kSpit(4, 3);
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070051const ButtonLocation kLocalizerReset(3, 8);
Tyler Chatow1039e432020-02-28 21:37:50 -080052
53const ButtonLocation kWinch(3, 14);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080054
James Kuszmaul7077d342021-06-09 20:23:58 -070055class Reader : public ::frc971::input::ActionJoystickInput {
Stephan Massaltd021f972020-01-05 20:41:23 -080056 public:
57 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070058 : ::frc971::input::ActionJoystickInput(
Stephan Massaltd021f972020-01-05 20:41:23 -080059 event_loop,
60 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -070061 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Stephan Massaltd021f972020-01-05 20:41:23 -080062 superstructure_goal_sender_(
63 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070064 localizer_control_sender_(
65 event_loop->MakeSender<
66 ::frc971::control_loops::drivetrain::LocalizerControl>(
67 "/drivetrain")),
Stephan Massaltd021f972020-01-05 20:41:23 -080068 superstructure_status_fetcher_(
Austin Schuhd58b2902020-03-01 19:28:04 -080069 event_loop->MakeFetcher<superstructure::Status>("/superstructure")),
70 setpoint_fetcher_(event_loop->MakeFetcher<y2020::joysticks::Setpoint>(
71 "/superstructure")) {}
Stephan Massaltd021f972020-01-05 20:41:23 -080072
73 void AutoEnded() override {
74 AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n");
75 }
76
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070077 void ResetLocalizer() {
78 auto builder = localizer_control_sender_.MakeBuilder();
79
80 // Start roughly in front of the red-team goal, robot pointed away from
81 // goal.
82 frc971::control_loops::drivetrain::LocalizerControl::Builder
83 localizer_control_builder = builder.MakeBuilder<
84 frc971::control_loops::drivetrain::LocalizerControl>();
85 localizer_control_builder.add_x(5.0);
86 localizer_control_builder.add_y(-2.0);
87 localizer_control_builder.add_theta(M_PI);
88 localizer_control_builder.add_theta_uncertainty(0.01);
89 if (!builder.Send(localizer_control_builder.Finish())) {
90 AOS_LOG(ERROR, "Failed to reset localizer.\n");
91 }
92 }
93
James Kuszmaul7077d342021-06-09 20:23:58 -070094 void HandleTeleop(
95 const ::frc971::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080096 superstructure_status_fetcher_.Fetch();
97 if (!superstructure_status_fetcher_.get()) {
98 AOS_LOG(ERROR, "Got no superstructure status message.\n");
99 return;
100 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800101
Austin Schuhd58b2902020-03-01 19:28:04 -0800102 setpoint_fetcher_.Fetch();
103
Austin Schuhf0a637c2020-02-25 23:44:12 -0800104 double hood_pos = constants::Values::kHoodRange().middle();
105 double intake_pos = -0.89;
James Kuszmaule7e6f322021-09-25 17:24:00 -0700106 double turret_pos = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800107 float roller_speed = 0.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700108 float roller_speed_compensation = 0.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800109 double accelerator_speed = 0.0;
110 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800111 double climber_speed = 0.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700112 bool preload_intake = false;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800113
Austin Schuh3fdf2412021-10-16 13:56:46 -0700114 const bool auto_track =
115 data.IsPressed(kAutoTrack) || data.IsPressed(kAutoNoHood);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800116
Austin Schuhf0a637c2020-02-25 23:44:12 -0800117 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800118 hood_pos = 0.45;
119 } else {
120 if (setpoint_fetcher_.get()) {
121 hood_pos = setpoint_fetcher_->hood();
122 } else {
123 hood_pos = 0.58;
124 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800125 }
126
James Kuszmaule7e6f322021-09-25 17:24:00 -0700127 if (setpoint_fetcher_.get()) {
128 turret_pos = setpoint_fetcher_->turret();
129 } else {
130 turret_pos = 0.0;
131 }
132
Austin Schuhf0a637c2020-02-25 23:44:12 -0800133 if (data.IsPressed(kShootFast)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800134 if (setpoint_fetcher_.get()) {
135 accelerator_speed = setpoint_fetcher_->accelerator();
136 finisher_speed = setpoint_fetcher_->finisher();
137 } else {
138 accelerator_speed = 250.0;
139 finisher_speed = 500.0;
140 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800141 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800142 accelerator_speed = 180.0;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700143 finisher_speed = 300.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800144 }
145
146 if (data.IsPressed(kIntakeExtend)) {
Austin Schuh13e55522020-02-29 23:11:17 -0800147 intake_pos = 1.2;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700148 roller_speed = 7.0f;
149 roller_speed_compensation = 2.0f;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700150 preload_intake = true;
Austin Schuh13e55522020-02-29 23:11:17 -0800151 }
152
153 if (superstructure_status_fetcher_.get() &&
Ravago Jonesa69ed2f2021-10-16 17:39:52 -0700154 superstructure_status_fetcher_->intake()->zeroed() &&
Austin Schuh13e55522020-02-29 23:11:17 -0800155 superstructure_status_fetcher_->intake()->position() > -0.5) {
156 roller_speed = std::max(roller_speed, 6.0f);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700157 roller_speed_compensation = 2.0f;
Austin Schuh43a220f2020-02-26 22:02:34 -0800158 }
159
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800160 if (data.IsPressed(kIntakeIn)) {
161 roller_speed = 6.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700162 roller_speed_compensation = 2.0f;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700163 preload_intake = true;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800164 } else if (data.IsPressed(kSpit)) {
165 roller_speed = -6.0f;
166 }
167
Tyler Chatow1039e432020-02-28 21:37:50 -0800168 if (data.IsPressed(kWinch)) {
169 climber_speed = 12.0f;
170 }
171
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700172 if (data.IsPressed(kLocalizerReset)) {
173 ResetLocalizer();
174 }
175
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800176 auto builder = superstructure_goal_sender_.MakeBuilder();
177
178 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
179 {
180 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
181 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800182 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800183 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800184
185 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
186 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800187 *builder.fbb(), intake_pos,
188 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800189
190 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
191 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaule7e6f322021-09-25 17:24:00 -0700192 *builder.fbb(), turret_pos,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800193 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800194
195 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800196 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
197 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800198
199 superstructure::Goal::Builder superstructure_goal_builder =
200 builder.MakeBuilder<superstructure::Goal>();
201
202 superstructure_goal_builder.add_hood(hood_offset);
203 superstructure_goal_builder.add_intake(intake_offset);
204 superstructure_goal_builder.add_turret(turret_offset);
205 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700206 superstructure_goal_builder.add_roller_speed_compensation(
207 roller_speed_compensation);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800208 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700209 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed) ||
210 data.IsPressed(kFeedDriver));
Tyler Chatow1039e432020-02-28 21:37:50 -0800211 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800212
James Kuszmaulccad78d2021-04-03 18:28:58 -0700213 superstructure_goal_builder.add_turret_tracking(auto_track);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700214 superstructure_goal_builder.add_hood_tracking(
215 auto_track && !data.IsPressed(kAutoNoHood));
James Kuszmaulccad78d2021-04-03 18:28:58 -0700216 superstructure_goal_builder.add_shooter_tracking(auto_track);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700217 superstructure_goal_builder.add_intake_preloading(preload_intake);
James Kuszmaulccad78d2021-04-03 18:28:58 -0700218
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800219 if (!builder.Send(superstructure_goal_builder.Finish())) {
220 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
221 }
222 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800223 }
224
225 private:
226 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
227
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700228 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
229 localizer_control_sender_;
230
Stephan Massaltd021f972020-01-05 20:41:23 -0800231 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800232
233 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800234};
235
236} // namespace joysticks
237} // namespace input
238} // namespace y2020
239
Austin Schuh094d09b2020-11-20 23:26:52 -0800240int main(int argc, char **argv) {
241 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800242
243 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
244 aos::configuration::ReadConfig("config.json");
245
246 ::aos::ShmEventLoop event_loop(&config.message());
247 ::y2020::input::joysticks::Reader reader(&event_loop);
248
249 event_loop.Run();
250
Austin Schuhae87e312020-08-01 16:15:01 -0700251 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800252}