blob: 44d8a4215a22008334b7c5ee24b6c5cb1d6b1fc7 [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;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800104 float roller_speed = 0.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700105 float roller_speed_compensation = 0.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800106 double accelerator_speed = 0.0;
107 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800108 double climber_speed = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800109
James Kuszmaulccad78d2021-04-03 18:28:58 -0700110 const bool auto_track = data.IsPressed(kAutoTrack);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800111
Austin Schuhf0a637c2020-02-25 23:44:12 -0800112 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800113 hood_pos = 0.45;
114 } else {
115 if (setpoint_fetcher_.get()) {
116 hood_pos = setpoint_fetcher_->hood();
117 } else {
118 hood_pos = 0.58;
119 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800120 }
121
122 if (data.IsPressed(kShootFast)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800123 if (setpoint_fetcher_.get()) {
124 accelerator_speed = setpoint_fetcher_->accelerator();
125 finisher_speed = setpoint_fetcher_->finisher();
126 } else {
127 accelerator_speed = 250.0;
128 finisher_speed = 500.0;
129 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800130 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800131 accelerator_speed = 180.0;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700132 finisher_speed = 300.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800133 }
134
135 if (data.IsPressed(kIntakeExtend)) {
Austin Schuh13e55522020-02-29 23:11:17 -0800136 intake_pos = 1.2;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700137 roller_speed = 7.0f;
138 roller_speed_compensation = 2.0f;
Austin Schuh13e55522020-02-29 23:11:17 -0800139 }
140
141 if (superstructure_status_fetcher_.get() &&
142 superstructure_status_fetcher_->intake()->position() > -0.5) {
143 roller_speed = std::max(roller_speed, 6.0f);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700144 roller_speed_compensation = 2.0f;
Austin Schuh43a220f2020-02-26 22:02:34 -0800145 }
146
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800147 if (data.IsPressed(kIntakeIn)) {
148 roller_speed = 6.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700149 roller_speed_compensation = 2.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800150 } else if (data.IsPressed(kSpit)) {
151 roller_speed = -6.0f;
152 }
153
Tyler Chatow1039e432020-02-28 21:37:50 -0800154 if (data.IsPressed(kWinch)) {
155 climber_speed = 12.0f;
156 }
157
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700158 if (data.IsPressed(kLocalizerReset)) {
159 ResetLocalizer();
160 }
161
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800162 auto builder = superstructure_goal_sender_.MakeBuilder();
163
164 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
165 {
166 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
167 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800168 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800169 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800170
171 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
172 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800173 *builder.fbb(), intake_pos,
174 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800175
176 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
177 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaulccad78d2021-04-03 18:28:58 -0700178 *builder.fbb(), 0.0,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800179 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800180
181 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800182 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
183 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800184
185 superstructure::Goal::Builder superstructure_goal_builder =
186 builder.MakeBuilder<superstructure::Goal>();
187
188 superstructure_goal_builder.add_hood(hood_offset);
189 superstructure_goal_builder.add_intake(intake_offset);
190 superstructure_goal_builder.add_turret(turret_offset);
191 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700192 superstructure_goal_builder.add_roller_speed_compensation(
193 roller_speed_compensation);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800194 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh43a220f2020-02-26 22:02:34 -0800195 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed));
Tyler Chatow1039e432020-02-28 21:37:50 -0800196 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800197
James Kuszmaulccad78d2021-04-03 18:28:58 -0700198 superstructure_goal_builder.add_turret_tracking(auto_track);
199 superstructure_goal_builder.add_hood_tracking(auto_track);
200 superstructure_goal_builder.add_shooter_tracking(auto_track);
201
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800202 if (!builder.Send(superstructure_goal_builder.Finish())) {
203 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
204 }
205 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800206 }
207
208 private:
209 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
210
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700211 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
212 localizer_control_sender_;
213
Stephan Massaltd021f972020-01-05 20:41:23 -0800214 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800215
216 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800217};
218
219} // namespace joysticks
220} // namespace input
221} // namespace y2020
222
Austin Schuh094d09b2020-11-20 23:26:52 -0800223int main(int argc, char **argv) {
224 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800225
226 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
227 aos::configuration::ReadConfig("config.json");
228
229 ::aos::ShmEventLoop event_loop(&config.message());
230 ::y2020::input::joysticks::Reader reader(&event_loop);
231
232 event_loop.Run();
233
Austin Schuhae87e312020-08-01 16:15:01 -0700234 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800235}