blob: 8df5a8a03393a11653a80e3a9198adabc48a42e4 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include <math.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "aos/actions/actions.h"
7#include "aos/init.h"
8#include "aos/input/action_joystick_input.h"
9#include "aos/input/driver_station_data.h"
10#include "aos/input/drivetrain_input.h"
11#include "aos/input/joystick_input.h"
12#include "aos/logging/logging.h"
13#include "aos/network/team_number.h"
14#include "aos/util/log_interval.h"
15#include "frc971/autonomous/base_autonomous_actor.h"
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -070016#include "frc971/control_loops/drivetrain/localizer_generated.h"
Sabina Davisa8fed3d2020-02-22 21:44:57 -080017#include "y2020/constants.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080018#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080019#include "y2020/control_loops/superstructure/superstructure_goal_generated.h"
20#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuhd58b2902020-03-01 19:28:04 -080021#include "y2020/setpoint_generated.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080022
23using aos::input::driver_station::ButtonLocation;
24using aos::input::driver_station::ControlBit;
25using aos::input::driver_station::JoystickAxis;
26using aos::input::driver_station::POVLocation;
27
Austin Schuhf0a637c2020-02-25 23:44:12 -080028using frc971::CreateProfileParameters;
Sabina Davisa8fed3d2020-02-22 21:44:57 -080029using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
30using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
31
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
Stephan Massaltd021f972020-01-05 20:41:23 -080053class Reader : public ::aos::input::ActionJoystickInput {
54 public:
55 Reader(::aos::EventLoop *event_loop)
56 : ::aos::input::ActionJoystickInput(
57 event_loop,
58 ::y2020::control_loops::drivetrain::GetDrivetrainConfig(),
59 ::aos::input::DrivetrainInputReader::InputType::kPistol, {}),
60 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
Sabina Davisa8fed3d2020-02-22 21:44:57 -080092 void HandleTeleop(const ::aos::input::driver_station::Data &data) override {
Stephan Massaltd021f972020-01-05 20:41:23 -080093 superstructure_status_fetcher_.Fetch();
94 if (!superstructure_status_fetcher_.get()) {
95 AOS_LOG(ERROR, "Got no superstructure status message.\n");
96 return;
97 }
Sabina Davisa8fed3d2020-02-22 21:44:57 -080098
Austin Schuhd58b2902020-03-01 19:28:04 -080099 setpoint_fetcher_.Fetch();
100
Austin Schuhf0a637c2020-02-25 23:44:12 -0800101 double hood_pos = constants::Values::kHoodRange().middle();
102 double intake_pos = -0.89;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800103 float roller_speed = 0.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700104 float roller_speed_compensation = 0.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800105 double accelerator_speed = 0.0;
106 double finisher_speed = 0.0;
Tyler Chatow1039e432020-02-28 21:37:50 -0800107 double climber_speed = 0.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800108
James Kuszmaulccad78d2021-04-03 18:28:58 -0700109 const bool auto_track = data.IsPressed(kAutoTrack);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800110
Austin Schuhf0a637c2020-02-25 23:44:12 -0800111 if (data.IsPressed(kHood)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800112 hood_pos = 0.45;
113 } else {
114 if (setpoint_fetcher_.get()) {
115 hood_pos = setpoint_fetcher_->hood();
116 } else {
117 hood_pos = 0.58;
118 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800119 }
120
121 if (data.IsPressed(kShootFast)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800122 if (setpoint_fetcher_.get()) {
123 accelerator_speed = setpoint_fetcher_->accelerator();
124 finisher_speed = setpoint_fetcher_->finisher();
125 } else {
126 accelerator_speed = 250.0;
127 finisher_speed = 500.0;
128 }
Austin Schuhf0a637c2020-02-25 23:44:12 -0800129 } else if (data.IsPressed(kShootSlow)) {
Austin Schuhd58b2902020-03-01 19:28:04 -0800130 accelerator_speed = 180.0;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700131 finisher_speed = 300.0;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800132 }
133
134 if (data.IsPressed(kIntakeExtend)) {
Austin Schuh13e55522020-02-29 23:11:17 -0800135 intake_pos = 1.2;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700136 roller_speed = 7.0f;
137 roller_speed_compensation = 2.0f;
Austin Schuh13e55522020-02-29 23:11:17 -0800138 }
139
140 if (superstructure_status_fetcher_.get() &&
141 superstructure_status_fetcher_->intake()->position() > -0.5) {
142 roller_speed = std::max(roller_speed, 6.0f);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700143 roller_speed_compensation = 2.0f;
Austin Schuh43a220f2020-02-26 22:02:34 -0800144 }
145
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800146 if (data.IsPressed(kIntakeIn)) {
147 roller_speed = 6.0f;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700148 roller_speed_compensation = 2.0f;
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800149 } else if (data.IsPressed(kSpit)) {
150 roller_speed = -6.0f;
151 }
152
Tyler Chatow1039e432020-02-28 21:37:50 -0800153 if (data.IsPressed(kWinch)) {
154 climber_speed = 12.0f;
155 }
156
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700157 if (data.IsPressed(kLocalizerReset)) {
158 ResetLocalizer();
159 }
160
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800161 auto builder = superstructure_goal_sender_.MakeBuilder();
162
163 flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset;
164 {
165 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
166 hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800167 *builder.fbb(), hood_pos,
Austin Schuh2efe1682021-03-06 22:47:15 -0800168 CreateProfileParameters(*builder.fbb(), 5.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800169
170 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
171 intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
Austin Schuhf0a637c2020-02-25 23:44:12 -0800172 *builder.fbb(), intake_pos,
173 CreateProfileParameters(*builder.fbb(), 10.0, 30.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800174
175 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
176 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
James Kuszmaulccad78d2021-04-03 18:28:58 -0700177 *builder.fbb(), 0.0,
Austin Schuhf0a637c2020-02-25 23:44:12 -0800178 CreateProfileParameters(*builder.fbb(), 6.0, 20.0));
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800179
180 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
Austin Schuhf0a637c2020-02-25 23:44:12 -0800181 superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed,
182 finisher_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800183
184 superstructure::Goal::Builder superstructure_goal_builder =
185 builder.MakeBuilder<superstructure::Goal>();
186
187 superstructure_goal_builder.add_hood(hood_offset);
188 superstructure_goal_builder.add_intake(intake_offset);
189 superstructure_goal_builder.add_turret(turret_offset);
190 superstructure_goal_builder.add_roller_voltage(roller_speed);
milind upadhyayaec1aee2020-10-13 13:44:33 -0700191 superstructure_goal_builder.add_roller_speed_compensation(
192 roller_speed_compensation);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800193 superstructure_goal_builder.add_shooter(shooter_offset);
Austin Schuh43a220f2020-02-26 22:02:34 -0800194 superstructure_goal_builder.add_shooting(data.IsPressed(kFeed));
Tyler Chatow1039e432020-02-28 21:37:50 -0800195 superstructure_goal_builder.add_climber_voltage(climber_speed);
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800196
James Kuszmaulccad78d2021-04-03 18:28:58 -0700197 superstructure_goal_builder.add_turret_tracking(auto_track);
198 superstructure_goal_builder.add_hood_tracking(auto_track);
199 superstructure_goal_builder.add_shooter_tracking(auto_track);
200
Sabina Davisa8fed3d2020-02-22 21:44:57 -0800201 if (!builder.Send(superstructure_goal_builder.Finish())) {
202 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
203 }
204 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800205 }
206
207 private:
208 ::aos::Sender<superstructure::Goal> superstructure_goal_sender_;
209
James Kuszmaul0c7cc2d2021-04-04 16:57:22 -0700210 ::aos::Sender<frc971::control_loops::drivetrain::LocalizerControl>
211 localizer_control_sender_;
212
Stephan Massaltd021f972020-01-05 20:41:23 -0800213 ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_;
Austin Schuhd58b2902020-03-01 19:28:04 -0800214
215 ::aos::Fetcher<y2020::joysticks::Setpoint> setpoint_fetcher_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800216};
217
218} // namespace joysticks
219} // namespace input
220} // namespace y2020
221
Austin Schuh094d09b2020-11-20 23:26:52 -0800222int main(int argc, char **argv) {
223 ::aos::InitGoogle(&argc, &argv);
Stephan Massaltd021f972020-01-05 20:41:23 -0800224
225 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
226 aos::configuration::ReadConfig("config.json");
227
228 ::aos::ShmEventLoop event_loop(&config.message());
229 ::y2020::input::joysticks::Reader reader(&event_loop);
230
231 event_loop.Run();
232
Austin Schuhae87e312020-08-01 16:15:01 -0700233 return 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800234}