blob: 4223332d72e13206dd30b48295c5bbd9d2f591fb [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
125 if (!spline1.WaitForPlan()) return;
126 spline1.Start();
127
128 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
129 }
130}
131
milind upadhyay47a0ab32020-11-25 19:34:41 -0800132void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800133 SplineHandle spline1 = PlanSpline(
134 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800135 *builder) {
136 flatbuffers::Offset<frc971::MultiSpline> target_spline;
137 target_spline = auto_splines_.TestSpline(builder);
138 const frc971::MultiSpline *const spline =
139 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
140 SendStartingPosition(CHECK_NOTNULL(spline));
141 return target_spline;
142 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800143 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800144
milind upadhyay47a0ab32020-11-25 19:34:41 -0800145 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800146 spline1.Start();
147
milind upadhyay47a0ab32020-11-25 19:34:41 -0800148 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800149}
150
kyle96c406e2021-02-27 14:07:22 -0800151void AutonomousActor::SendStartingPosition(
152 const frc971::MultiSpline *const spline) {
153 float x = spline->spline_x()->Get(0);
154 float y = spline->spline_y()->Get(0);
155
156 Eigen::Matrix<double, 2, 6> control_points;
157 for (size_t ii = 0; ii < 6; ++ii) {
158 control_points(0, ii) = spline->spline_x()->Get(ii);
159 control_points(1, ii) = spline->spline_y()->Get(ii);
160 }
161
162 frc971::control_loops::drivetrain::Spline spline_object(control_points);
163
164 SendStartingPosition(x, y, spline_object.Theta(0));
165}
166
milind upadhyay47a0ab32020-11-25 19:34:41 -0800167ProfileParametersT MakeProfileParametersT(const float max_velocity,
168 const float max_acceleration) {
169 ProfileParametersT params;
170 params.max_velocity = max_velocity;
171 params.max_acceleration = max_acceleration;
172 return params;
173}
174
175bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800176 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800177 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800178 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800179 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800180 return WaitForDriveDone();
181}
Stephan Massaltd021f972020-01-05 20:41:23 -0800182} // namespace actors
183} // namespace y2020