blob: bb5799714e73781b46f31159705628b5380acd42 [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);
James Kuszmaulccad78d2021-04-03 18:28:58 -070042const ButtonLocation kAutoTrack(3, 15);
Austin Schuhf0a637c2020-02-25 23:44:12 -080043const ButtonLocation kHood(3, 3);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080044const ButtonLocation kShootSlow(4, 2);
Austin Schuh43a220f2020-02-26 22:02:34 -080045const ButtonLocation kFeed(4, 1);
Austin Schuhf0a637c2020-02-25 23:44:12 -080046const ButtonLocation kIntakeExtend(3, 9);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080047const ButtonLocation kIntakeIn(4, 4);
Tyler Chatow1039e432020-02-28 21:37:50 -080048const ButtonLocation kSpit(4, 3);
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070049const ButtonLocation kLocalizerReset(3, 8);
Tyler Chatow1039e432020-02-28 21:37:50 -080050
51const ButtonLocation kWinch(3, 14);
Sabina Davisa8fed3d2020-02-22 21:44:57 -080052
James Kuszmaul7077d342021-06-09 20:23:58 -070053class Reader : public ::frc971::input::ActionJoystickInput {
Stephan Massaltd021f972020-01-05 20:41:23 -080054 public:
55 Reader(::aos::EventLoop *event_loop)
James Kuszmaul7077d342021-06-09 20:23:58 -070056 : ::frc971::input::ActionJoystickInput(
Stephan Massaltd021f972020-01-05 20:41:23 -080057 event_loop,
58 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
James Kuszmaul7077d342021-06-09 20:23:58 -070059 ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}),
Stephan Massaltd021f972020-01-05 20:41:23 -080060 superstructure_goal_sender_(
61 event_loop->MakeSender<superstructure::Goal>("/superstructure")),
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070062 localizer_control_sender_(
63 event_loop->MakeSender<
64 ::frc971::control_loops::drivetrain::LocalizerControl>(
65 "/drivetrain")),
Stephan Massaltd021f972020-01-05 20:41:23 -080066 superstructure_status_fetcher_(
Austin Schuhd58b2902020-03-01 19:28:04 -080067 event_loop->MakeFetcher<superstructure::Status>("/superstructure")),
68 setpoint_fetcher_(event_loop->MakeFetcher<y2020::joysticks::Setpoint>(
69 "/superstructure")) {}
Stephan Massaltd021f972020-01-05 20:41:23 -080070
71 void AutoEnded() override {
72 AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n");
73 }
74
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070075 void ResetLocalizer() {
76 auto builder = localizer_control_sender_.MakeBuilder();
77
78 // Start roughly in front of the red-team goal, robot pointed away from
79 // goal.
80 frc971::control_loops::drivetrain::LocalizerControl::Builder
81 localizer_control_builder = builder.MakeBuilder<
82 frc971::control_loops::drivetrain::LocalizerControl>();
83 localizer_control_builder.add_x(5.0);
84 localizer_control_builder.add_y(-2.0);
85 localizer_control_builder.add_theta(M_PI);
86 localizer_control_builder.add_theta_uncertainty(0.01);
87 if (!builder.Send(localizer_control_builder.Finish())) {
88 AOS_LOG(ERROR, "Failed to reset localizer.\n");
89 }
90 }
91
James Kuszmaul7077d342021-06-09 20:23:58 -070092 void HandleTeleop(
93 const ::frc971::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080094 superstructure_status_fetcher_.Fetch();
95 if (!superstructure_status_fetcher_.get()) {
96 AOS_LOG(ERROR, "Got no superstructure status message.\n");
97 return;
98 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -080099
Austin Schuhd58b2902020-03-01 19:28:04 -0800100 setpoint_fetcher_.Fetch();
101
Austin Schuhf0a637c2020-02-25 23:44:12 -0800102 double hood_pos = constants::Values::kHoodRange().middle();
103 double intake_pos = -0.89;
James Kuszmaule7e6f322021-09-25 17:24:00 -0700104 double turret_pos = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800105 float roller_speed = 0.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700106 float roller_speed_compensation = 0.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800107 double accelerator_speed = 0.0;
108 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800109 double climber_speed = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800110
James Kuszmaulccad78d2021-04-03 18:28:58 -0700111 const bool auto_track = data.IsPressed(kAutoTrack);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800112
Austin Schuhf0a637c2020-02-25 23:44:12 -0800113 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800114 hood_pos = 0.45;
115 } else {
116 if (setpoint_fetcher_.get()) {
117 hood_pos = setpoint_fetcher_->hood();
118 } else {
119 hood_pos = 0.58;
120 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800121 }
122
James Kuszmaule7e6f322021-09-25 17:24:00 -0700123 if (setpoint_fetcher_.get()) {
124 turret_pos = setpoint_fetcher_->turret();
125 } else {
126 turret_pos = 0.0;
127 }
128
Austin Schuhf0a637c2020-02-25 23:44:12 -0800129 if (data.IsPressed(kShootFast)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800130 if (setpoint_fetcher_.get()) {
131 accelerator_speed = setpoint_fetcher_->accelerator();
132 finisher_speed = setpoint_fetcher_->finisher();
133 } else {
134 accelerator_speed = 250.0;
135 finisher_speed = 500.0;
136 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800137 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800138 accelerator_speed = 180.0;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700139 finisher_speed = 300.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800140 }
141
142 if (data.IsPressed(kIntakeExtend)) {
Austin Schuh13e55522020-02-29 23:11:17 -0800143 intake_pos = 1.2;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700144 roller_speed = 7.0f;
145 roller_speed_compensation = 2.0f;
Austin Schuh13e55522020-02-29 23:11:17 -0800146 }
147
148 if (superstructure_status_fetcher_.get() &&
149 superstructure_status_fetcher_->intake()->position() > -0.5) {
150 roller_speed = std::max(roller_speed, 6.0f);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700151 roller_speed_compensation = 2.0f;
Austin Schuh43a220f2020-02-26 22:02:34 -0800152 }
153
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800154 if (data.IsPressed(kIntakeIn)) {
155 roller_speed = 6.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700156 roller_speed_compensation = 2.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800157 } else if (data.IsPressed(kSpit)) {
158 roller_speed = -6.0f;
159 }
160
Tyler Chatow1039e432020-02-28 21:37:50 -0800161 if (data.IsPressed(kWinch)) {
162 climber_speed = 12.0f;
163 }
164
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700165 if (data.IsPressed(kLocalizerReset)) {
166 ResetLocalizer();
167 }
168
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800169 auto builder = superstructure_goal_sender_.MakeBuilder();
170
171 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
172 {
173 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
174 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800175 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800176 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800177
178 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
179 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800180 *builder.fbb(), intake_pos,
181 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800182
183 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
184 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaule7e6f322021-09-25 17:24:00 -0700185 *builder.fbb(), turret_pos,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800186 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800187
188 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800189 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
190 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800191
192 superstructure::Goal::Builder superstructure_goal_builder =
193 builder.MakeBuilder<superstructure::Goal>();
194
195 superstructure_goal_builder.add_hood(hood_offset);
196 superstructure_goal_builder.add_intake(intake_offset);
197 superstructure_goal_builder.add_turret(turret_offset);
198 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700199 superstructure_goal_builder.add_roller_speed_compensation(
200 roller_speed_compensation);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800201 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh43a220f2020-02-26 22:02:34 -0800202 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed));
Tyler Chatow1039e432020-02-28 21:37:50 -0800203 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800204
James Kuszmaulccad78d2021-04-03 18:28:58 -0700205 superstructure_goal_builder.add_turret_tracking(auto_track);
206 superstructure_goal_builder.add_hood_tracking(auto_track);
207 superstructure_goal_builder.add_shooter_tracking(auto_track);
208
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800209 if (!builder.Send(superstructure_goal_builder.Finish())) {
210 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
211 }
212 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800213 }
214
215 private:
216 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
217
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700218 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
219 localizer_control_sender_;
220
Stephan Massaltd021f972020-01-05 20:41:23 -0800221 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800222
223 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800224};
225
226} // namespace joysticks
227} // namespace input
228} // namespace y2020
229
Austin Schuh094d09b2020-11-20 23:26:52 -0800230int main(int argc, char **argv) {
231 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800232
233 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
234 aos::configuration::ReadConfig("config.json");
235
236 ::aos::ShmEventLoop event_loop(&config.message());
237 ::y2020::input::joysticks::Reader reader(&event_loop);
238
239 event_loop.Run();
240
Austin Schuhae87e312020-08-01 16:15:01 -0700241 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800242}