blob: 0708326a661959e37eb135f671c3e5fb321491f7 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
8#include "aos/logging/logging.h"
James Kuszmaulddd2ba62020-03-08 22:17:13 -07009#include "aos/util/math.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080010#include "frc971/control_loops/drivetrain/localizer_generated.h"
kyle96c406e2021-02-27 14:07:22 -080011#include "frc971/control_loops/drivetrain/spline.h"
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080012#include "y2020/actors/auto_splines.h"
Ravago Jonesc2a08022021-02-06 17:40:54 -080013#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080014
Austin Schuhfd1715f2021-01-30 16:58:24 -080015DEFINE_bool(spline_auto, true, "If true, define a spline autonomous mode");
kyle96c406e2021-02-27 14:07:22 -080016DEFINE_bool(galactic_search, false,
17 "If true, do the galactic search autonomous");
milind upadhyay47a0ab32020-11-25 19:34:41 -080018
Stephan Massaltd021f972020-01-05 20:41:23 -080019namespace y2020 {
20namespace actors {
21
22using ::aos::monotonic_clock;
23using ::frc971::ProfileParametersT;
24using frc971::control_loops::drivetrain::LocalizerControl;
25namespace chrono = ::std::chrono;
26
27AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
28 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080029 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080030 localizer_control_sender_(
31 event_loop->MakeSender<
32 ::frc971::control_loops::drivetrain::LocalizerControl>(
33 "/drivetrain")),
James Kuszmaulddd2ba62020-03-08 22:17:13 -070034 joystick_state_fetcher_(
Ravago Jonesc2a08022021-02-06 17:40:54 -080035 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
kyle96c406e2021-02-27 14:07:22 -080036 path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>(
37 "/pi1/camera")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080038 auto_splines_() {
milind upadhyay47a0ab32020-11-25 19:34:41 -080039 set_max_drivetrain_voltage(2.0);
40}
Stephan Massaltd021f972020-01-05 20:41:23 -080041
42void AutonomousActor::Reset() {
43 InitializeEncoders();
44 ResetDrivetrain();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080045
James Kuszmaulddd2ba62020-03-08 22:17:13 -070046 joystick_state_fetcher_.Fetch();
47 CHECK(joystick_state_fetcher_.get() != nullptr)
48 << "Expect at least one JoystickState message before running auto...";
49 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -080050}
51
52bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -080053 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -080054 Reset();
milind upadhyay47a0ab32020-11-25 19:34:41 -080055 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -070056 if (alliance_ == aos::Alliance::kInvalid) {
57 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
58 return false;
59 }
kyle96c406e2021-02-27 14:07:22 -080060 if (FLAGS_galactic_search) {
61 GalacticSearch();
62 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -080063 SplineAuto();
64 } else {
65 return DriveFwd();
66 }
67 return true;
68}
Stephan Massaltd021f972020-01-05 20:41:23 -080069
kyle96c406e2021-02-27 14:07:22 -080070void AutonomousActor::SendStartingPosition(double x, double y, double theta) {
71 // Set up the starting position for the blue alliance.
72 double starting_heading = theta;
73
74 // TODO(james): Resetting the localizer breaks the left/right statespace
75 // controller. That is a bug, but we can fix that later by not resetting.
76 auto builder = localizer_control_sender_.MakeBuilder();
77
78 LocalizerControl::Builder localizer_control_builder =
79 builder.MakeBuilder<LocalizerControl>();
80 localizer_control_builder.add_x(x);
81 localizer_control_builder.add_y(y);
82 localizer_control_builder.add_theta(starting_heading);
83 localizer_control_builder.add_theta_uncertainty(0.00001);
84 if (!builder.Send(localizer_control_builder.Finish())) {
85 AOS_LOG(ERROR, "Failed to reset localizer.\n");
86 }
87}
88
89void AutonomousActor::GalacticSearch() {
90 path_fetcher_.Fetch();
91 CHECK(path_fetcher_.get() != nullptr)
92 << "Expect at least one GalacticSearchPath message before running "
93 "auto...";
94 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
95 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
96 } else {
97 SplineHandle spline1 = PlanSpline(
98 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
99 *builder) {
100 flatbuffers::Offset<frc971::MultiSpline> target_spline;
101 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
102 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
103 target_spline = auto_splines_.SplineRedA(builder);
104 } else {
105 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
106 target_spline = auto_splines_.SplineRedB(builder);
107 }
108 } else {
109 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
110 target_spline = auto_splines_.SplineBlueA(builder);
111 } else {
112 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
113 target_spline = auto_splines_.SplineBlueB(builder);
114 }
115 }
116 const frc971::MultiSpline *const spline =
117 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
118
119 SendStartingPosition(CHECK_NOTNULL(spline));
120
121 return target_spline;
122 },
123 SplineDirection::kForward);
124
milind upadhyayb9dec712021-03-20 15:47:51 -0700125 set_intake_goal(1.2);
126 set_roller_voltage(7.0);
127 SendSuperstructureGoal();
128
kyle96c406e2021-02-27 14:07:22 -0800129 if (!spline1.WaitForPlan()) return;
130 spline1.Start();
131
132 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyayb9dec712021-03-20 15:47:51 -0700133 set_intake_goal(-0.89);
134 set_roller_voltage(0.0);
135 SendSuperstructureGoal();
kyle96c406e2021-02-27 14:07:22 -0800136 }
137}
138
milind upadhyay47a0ab32020-11-25 19:34:41 -0800139void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800140 SplineHandle spline1 = PlanSpline(
141 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800142 *builder) {
143 flatbuffers::Offset<frc971::MultiSpline> target_spline;
144 target_spline = auto_splines_.TestSpline(builder);
145 const frc971::MultiSpline *const spline =
146 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
147 SendStartingPosition(CHECK_NOTNULL(spline));
148 return target_spline;
149 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800150 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800151
milind upadhyay47a0ab32020-11-25 19:34:41 -0800152 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800153 spline1.Start();
154
milind upadhyay47a0ab32020-11-25 19:34:41 -0800155 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800156}
157
kyle96c406e2021-02-27 14:07:22 -0800158void AutonomousActor::SendStartingPosition(
159 const frc971::MultiSpline *const spline) {
160 float x = spline->spline_x()->Get(0);
161 float y = spline->spline_y()->Get(0);
162
163 Eigen::Matrix<double, 2, 6> control_points;
164 for (size_t ii = 0; ii < 6; ++ii) {
165 control_points(0, ii) = spline->spline_x()->Get(ii);
166 control_points(1, ii) = spline->spline_y()->Get(ii);
167 }
168
169 frc971::control_loops::drivetrain::Spline spline_object(control_points);
170
171 SendStartingPosition(x, y, spline_object.Theta(0));
172}
173
milind upadhyay47a0ab32020-11-25 19:34:41 -0800174ProfileParametersT MakeProfileParametersT(const float max_velocity,
175 const float max_acceleration) {
176 ProfileParametersT params;
177 params.max_velocity = max_velocity;
178 params.max_acceleration = max_acceleration;
179 return params;
180}
181
182bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800183 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800184 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800185 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800186 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800187 return WaitForDriveDone();
188}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800189
190void AutonomousActor::SendSuperstructureGoal() {
191
192 auto builder = superstructure_goal_sender_.MakeBuilder();
193
194 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
195 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700196
Sabina Leavera0b43b42021-03-03 20:30:04 -0800197 {
198 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
199 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
200
milind upadhyayb9dec712021-03-20 15:47:51 -0700201 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800202 builder.MakeBuilder<frc971::ProfileParameters>();
203 profile_params_builder.add_max_velocity(0.0);
204 profile_params_builder.add_max_acceleration(0.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700205 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800206 profile_params_builder.Finish();
207 intake_builder.add_unsafe_goal(intake_goal_);
208 intake_builder.add_profile_params(profile_params_offset);
209 intake_offset = intake_builder.Finish();
210 }
211
212 superstructure::Goal::Builder superstructure_builder =
213 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700214
Sabina Leavera0b43b42021-03-03 20:30:04 -0800215 superstructure_builder.add_intake(intake_offset);
216 superstructure_builder.add_roller_voltage(roller_voltage_);
217 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
218
219 if (!builder.Send(superstructure_builder.Finish())) {
220 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
221 }
222
223}
Stephan Massaltd021f972020-01-05 20:41:23 -0800224} // namespace actors
225} // namespace y2020