blob: e52708e7258ec485d7f7048ca71a2323f69def4c [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() &&
154 superstructure_status_fetcher_->intake()->position() > -0.5) {
155 roller_speed = std::max(roller_speed, 6.0f);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700156 roller_speed_compensation = 2.0f;
Austin Schuh43a220f2020-02-26 22:02:34 -0800157 }
158
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800159 if (data.IsPressed(kIntakeIn)) {
160 roller_speed = 6.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700161 roller_speed_compensation = 2.0f;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700162 preload_intake = true;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800163 } else if (data.IsPressed(kSpit)) {
164 roller_speed = -6.0f;
165 }
166
Tyler Chatow1039e432020-02-28 21:37:50 -0800167 if (data.IsPressed(kWinch)) {
168 climber_speed = 12.0f;
169 }
170
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700171 if (data.IsPressed(kLocalizerReset)) {
172 ResetLocalizer();
173 }
174
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800175 auto builder = superstructure_goal_sender_.MakeBuilder();
176
177 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
178 {
179 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
180 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800181 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800182 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800183
184 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
185 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800186 *builder.fbb(), intake_pos,
187 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800188
189 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
190 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaule7e6f322021-09-25 17:24:00 -0700191 *builder.fbb(), turret_pos,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800192 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800193
194 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800195 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
196 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800197
198 superstructure::Goal::Builder superstructure_goal_builder =
199 builder.MakeBuilder<superstructure::Goal>();
200
201 superstructure_goal_builder.add_hood(hood_offset);
202 superstructure_goal_builder.add_intake(intake_offset);
203 superstructure_goal_builder.add_turret(turret_offset);
204 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700205 superstructure_goal_builder.add_roller_speed_compensation(
206 roller_speed_compensation);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800207 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700208 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed) ||
209 data.IsPressed(kFeedDriver));
Tyler Chatow1039e432020-02-28 21:37:50 -0800210 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800211
James Kuszmaulccad78d2021-04-03 18:28:58 -0700212 superstructure_goal_builder.add_turret_tracking(auto_track);
Austin Schuh3fdf2412021-10-16 13:56:46 -0700213 superstructure_goal_builder.add_hood_tracking(
214 auto_track && !data.IsPressed(kAutoNoHood));
James Kuszmaulccad78d2021-04-03 18:28:58 -0700215 superstructure_goal_builder.add_shooter_tracking(auto_track);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700216 superstructure_goal_builder.add_intake_preloading(preload_intake);
James Kuszmaulccad78d2021-04-03 18:28:58 -0700217
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800218 if (!builder.Send(superstructure_goal_builder.Finish())) {
219 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
220 }
221 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800222 }
223
224 private:
225 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
226
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700227 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
228 localizer_control_sender_;
229
Stephan Massaltd021f972020-01-05 20:41:23 -0800230 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800231
232 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800233};
234
235} // namespace joysticks
236} // namespace input
237} // namespace y2020
238
Austin Schuh094d09b2020-11-20 23:26:52 -0800239int main(int argc, char **argv) {
240 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800241
242 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
243 aos::configuration::ReadConfig("config.json");
244
245 ::aos::ShmEventLoop event_loop(&config.message());
246 ::y2020::input::joysticks::Reader reader(&event_loop);
247
248 event_loop.Run();
249
Austin Schuhae87e312020-08-01 16:15:01 -0700250 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800251}