blob: 977529f42b1ea2e597d348b7e0cb72e49c4a69b4 [file] [log] [blame]
Alex Perry731b4602019-02-02 22:13:01 -08001#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
2
3#include "Eigen/Dense"
Philipp Schrader790cb542023-07-05 21:06:52 -07004
James Kuszmaul75a18c52021-03-10 22:02:07 -08005#include "aos/json_to_flatbuffer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/realtime.h"
James Kuszmaulc73bb222019-04-07 12:15:35 -07007#include "aos/util/math.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "frc971/control_loops/control_loops_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -08009#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
11#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
12#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -080013
Alex Perry731b4602019-02-02 22:13:01 -080014namespace frc971 {
15namespace control_loops {
16namespace drivetrain {
17
18SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config)
James Kuszmaul75a18c52021-03-10 22:02:07 -080019 : dt_config_(dt_config),
Austin Schuhf7c65202022-11-04 21:28:20 -070020 velocity_drivetrain_(
21 std::make_shared<StateFeedbackLoop<2, 2, 2, double,
22 StateFeedbackHybridPlant<2, 2, 2>,
23 HybridKalman<2, 2, 2>>>(
24 dt_config_.make_hybrid_drivetrain_velocity_loop())),
James Kuszmaul75a18c52021-03-10 22:02:07 -080025 current_xva_(Eigen::Vector3d::Zero()),
26 next_xva_(Eigen::Vector3d::Zero()),
27 next_U_(Eigen::Vector2d::Zero()) {}
Alex Perrye32eabc2019-02-08 19:51:19 -080028
29void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -080030 output_was_capped_ =
31 ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0;
Alex Perrye32eabc2019-02-08 19:51:19 -080032
Alex Perrycc3ee4c2019-02-09 21:20:41 -080033 if (output_was_capped_) {
Alex Perrye32eabc2019-02-08 19:51:19 -080034 *U *= 12.0 / U->lpNorm<Eigen::Infinity>();
35 }
36}
Alex Perry731b4602019-02-02 22:13:01 -080037
Alex Perrycc3ee4c2019-02-09 21:20:41 -080038void SplineDrivetrain::SetGoal(
Alex Perrycb7da4b2019-08-28 19:35:56 -070039 const ::frc971::control_loops::drivetrain::Goal *goal) {
James Kuszmauldc534432023-02-05 14:51:11 -080040 UpdateSplineHandles(goal->has_spline_handle()
41 ? std::make_optional<int>(goal->spline_handle())
42 : std::nullopt);
James Kuszmaul75a18c52021-03-10 22:02:07 -080043}
44
James Kuszmaul99af8b52021-03-28 10:50:15 -070045bool SplineDrivetrain::IsCurrentTrajectory(
46 const fb::Trajectory *trajectory) const {
James Kuszmauldc534432023-02-05 14:51:11 -080047 const FinishedTrajectory *current = current_trajectory();
48 return (current != nullptr &&
49 current->spline_handle() == trajectory->handle());
James Kuszmaul99af8b52021-03-28 10:50:15 -070050}
51
James Kuszmaul75a18c52021-03-10 22:02:07 -080052bool SplineDrivetrain::HasTrajectory(const fb::Trajectory *trajectory) const {
53 if (trajectory == nullptr) {
54 return false;
55 }
56 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -080057 if (trajectories_[ii].spline_handle() == trajectory->handle()) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080058 return true;
59 }
60 }
61 return false;
62}
63
James Kuszmaul99af8b52021-03-28 10:50:15 -070064void SplineDrivetrain::DeleteTrajectory(const fb::Trajectory *trajectory) {
65 CHECK(trajectory != nullptr);
66
67 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -080068 if (trajectories_[ii].spline_handle() == trajectory->handle()) {
James Kuszmaul99af8b52021-03-28 10:50:15 -070069 trajectories_.erase(trajectories_.begin() + ii);
70 return;
71 }
72 }
73
74 LOG(FATAL) << "Trying to remove unknown trajectory " << trajectory->handle();
75}
76
James Kuszmaul75a18c52021-03-10 22:02:07 -080077void SplineDrivetrain::AddTrajectory(const fb::Trajectory *trajectory) {
James Kuszmauldc534432023-02-05 14:51:11 -080078 CHECK_LT(trajectories_.size(), trajectories_.capacity());
79 trajectories_.emplace_back(dt_config_, trajectory, velocity_drivetrain_);
80 UpdateSplineHandles(commanded_spline_);
James Kuszmaul75a18c52021-03-10 22:02:07 -080081}
82
83void SplineDrivetrain::DeleteCurrentSpline() {
James Kuszmauldc534432023-02-05 14:51:11 -080084 DeleteTrajectory(&CHECK_NOTNULL(current_trajectory())->trajectory());
James Kuszmaul75a18c52021-03-10 22:02:07 -080085 executing_spline_ = false;
James Kuszmauldc534432023-02-05 14:51:11 -080086 commanded_spline_.reset();
James Kuszmaul75a18c52021-03-10 22:02:07 -080087 current_xva_.setZero();
88}
89
James Kuszmauldc534432023-02-05 14:51:11 -080090void SplineDrivetrain::UpdateSplineHandles(
91 std::optional<int> commanded_spline) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080092 // If we are currently executing a spline and have received a change
93 if (executing_spline_) {
James Kuszmauldc534432023-02-05 14:51:11 -080094 if (!commanded_spline) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080095 // We've been told to stop executing a spline; remove it from our queue,
96 // and clean up.
97 DeleteCurrentSpline();
98 return;
99 } else {
100 if (executing_spline_ &&
James Kuszmauldc534432023-02-05 14:51:11 -0800101 CHECK_NOTNULL(current_trajectory())->spline_handle() !=
102 *commanded_spline) {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800103 // If we are executing a spline, and the handle has changed, garbage
104 // collect the old spline.
105 DeleteCurrentSpline();
106 }
107 }
108 }
James Kuszmauldc534432023-02-05 14:51:11 -0800109 commanded_spline_ = commanded_spline;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800110 // We've now cleaned up the previous state; handle any new commands.
111 if (!commanded_spline_) {
112 return;
113 }
114 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -0800115 if (trajectories_[ii].spline_handle() == *commanded_spline_) {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800116 executing_spline_ = true;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800117 }
118 }
119 // If we didn't find the commanded spline in the list of available splines,
120 // that's fine; it just means, it hasn't been fully planned yet.
Alex Perrya71badb2019-02-06 19:40:41 -0800121}
122
James Kuszmauldc534432023-02-05 14:51:11 -0800123FinishedTrajectory *SplineDrivetrain::current_trajectory() {
124 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
125 if (trajectories_[ii].spline_handle() == *commanded_spline_) {
126 return &trajectories_[ii];
127 }
128 }
129 return nullptr;
130}
131
132const FinishedTrajectory *SplineDrivetrain::current_trajectory() const {
133 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
134 if (trajectories_[ii].spline_handle() == *commanded_spline_) {
135 return &trajectories_[ii];
136 }
137 }
138 return nullptr;
139}
140
Alex Perrye32eabc2019-02-08 19:51:19 -0800141// TODO(alex): Hold position when done following the spline.
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700142void SplineDrivetrain::Update(
143 bool enable, const ::Eigen::Matrix<double, 5, 1> &state,
144 const ::Eigen::Matrix<double, 2, 1> &voltage_error) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800145 next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrye32eabc2019-02-08 19:51:19 -0800146 enable_ = enable;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800147 if (enable && executing_spline_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800148 ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero();
James Kuszmauldc534432023-02-05 14:51:11 -0800149 const FinishedTrajectory *const trajectory =
150 CHECK_NOTNULL(current_trajectory());
James Kuszmaul75a18c52021-03-10 22:02:07 -0800151 if (!IsAtEnd() && executing_spline_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800152 // TODO(alex): It takes about a cycle for the outputs to propagate to the
153 // motors. Consider delaying the output by a cycle.
James Kuszmauldc534432023-02-05 14:51:11 -0800154 U_ff = trajectory->FFVoltage(current_xva_(0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800155 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800156
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700157 const double current_distance = current_xva_(0);
Alex Perrye32eabc2019-02-08 19:51:19 -0800158 ::Eigen::Matrix<double, 2, 5> K =
James Kuszmauldc534432023-02-05 14:51:11 -0800159 trajectory->GainForDistance(current_distance);
James Kuszmaul1057ce82019-02-09 17:58:24 -0800160 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
James Kuszmauldc534432023-02-05 14:51:11 -0800161 const bool backwards = trajectory->drive_spline_backwards();
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700162 if (backwards) {
James Kuszmaulc73bb222019-04-07 12:15:35 -0700163 ::Eigen::Matrix<double, 2, 1> swapU(U_ff(1, 0), U_ff(0, 0));
164 U_ff = -swapU;
165 goal_state(2, 0) += M_PI;
166 double left_goal = goal_state(3, 0);
167 double right_goal = goal_state(4, 0);
168 goal_state(3, 0) = -right_goal;
169 goal_state(4, 0) = -left_goal;
170 }
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700171 const Eigen::Matrix<double, 5, 1> relative_goal =
James Kuszmauldc534432023-02-05 14:51:11 -0800172 trajectory->StateToPathRelativeState(current_distance, goal_state,
173 backwards);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700174 const Eigen::Matrix<double, 5, 1> relative_state =
James Kuszmauldc534432023-02-05 14:51:11 -0800175 trajectory->StateToPathRelativeState(current_distance, state,
176 backwards);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700177 Eigen::Matrix<double, 5, 1> state_error = relative_goal - relative_state;
James Kuszmaulc73bb222019-04-07 12:15:35 -0700178 state_error(2, 0) = ::aos::math::NormalizeAngle(state_error(2, 0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800179 ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800180
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700181 if (backwards) {
182 Eigen::Matrix<double, 2, 1> swapU(U_fb(1), U_fb(0));
183 U_fb = -swapU;
184 }
185
Austin Schuhd749d932020-12-30 21:38:40 -0800186 ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2, 1>(0, 0);
James Kuszmauldc534432023-02-05 14:51:11 -0800187 next_xva_ = trajectory->GetNextXVA(dt_config_.dt, &xv_state);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700188 next_U_ = U_ff + U_fb - voltage_error;
Alex Perrye32eabc2019-02-08 19:51:19 -0800189 uncapped_U_ = next_U_;
190 ScaleCapU(&next_U_);
Alex Perry731b4602019-02-02 22:13:01 -0800191 }
192}
193
Alex Perry731b4602019-02-02 22:13:01 -0800194void SplineDrivetrain::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 ::frc971::control_loops::drivetrain::OutputT *output) {
Alex Perry731b4602019-02-02 22:13:01 -0800196 if (!output) {
197 return;
198 }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800199 if (executing_spline_) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800200 if (!IsAtEnd()) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800201 output->left_voltage = next_U_(0);
202 output->right_voltage = next_U_(1);
Alex Perrya71badb2019-02-06 19:40:41 -0800203 current_xva_ = next_xva_;
Alex Perry731b4602019-02-02 22:13:01 -0800204 }
205 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800206 output->left_voltage = next_U_(0);
207 output->right_voltage = next_U_(1);
Alex Perry731b4602019-02-02 22:13:01 -0800208}
209
Alex Perrye32eabc2019-02-08 19:51:19 -0800210void SplineDrivetrain::PopulateStatus(
Austin Schuhd749d932020-12-30 21:38:40 -0800211 drivetrain::Status::Builder *builder) const {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700212 if (builder && enable_) {
213 builder->add_uncapped_left_voltage(uncapped_U_(0));
214 builder->add_uncapped_right_voltage(uncapped_U_(1));
215 builder->add_robot_speed(current_xva_(1));
216 builder->add_output_was_capped(output_was_capped_);
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800217 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700218}
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800219
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging(
221 flatbuffers::FlatBufferBuilder *builder) const {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800222 int *spline_handles;
223 const flatbuffers::Offset<flatbuffers::Vector<int>> handles_vector =
224 builder->CreateUninitializedVector(trajectories_.size(), &spline_handles);
225
226 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -0800227 spline_handles[ii] = trajectories_[ii].spline_handle();
James Kuszmaul75a18c52021-03-10 22:02:07 -0800228 }
229
Alex Perrycb7da4b2019-08-28 19:35:56 -0700230 drivetrain::TrajectoryLogging::Builder trajectory_logging_builder(*builder);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800231 if (executing_spline_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700232 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
233 trajectory_logging_builder.add_x(goal_state(0));
234 trajectory_logging_builder.add_y(goal_state(1));
James Kuszmauldc534432023-02-05 14:51:11 -0800235 if (CHECK_NOTNULL(current_trajectory())->drive_spline_backwards()) {
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700236 trajectory_logging_builder.add_left_velocity(-goal_state(4));
237 trajectory_logging_builder.add_right_velocity(-goal_state(3));
238 trajectory_logging_builder.add_theta(
239 ::aos::math::NormalizeAngle(goal_state(2) + M_PI));
240 } else {
241 trajectory_logging_builder.add_theta(
242 ::aos::math::NormalizeAngle(goal_state(2)));
243 trajectory_logging_builder.add_left_velocity(goal_state(3));
244 trajectory_logging_builder.add_right_velocity(goal_state(4));
245 }
Alex Perrye32eabc2019-02-08 19:51:19 -0800246 }
James Kuszmauldc534432023-02-05 14:51:11 -0800247 trajectory_logging_builder.add_is_executing(!IsAtEnd() && executing_spline_);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800248 trajectory_logging_builder.add_is_executed(executing_spline_ && IsAtEnd());
249 if (commanded_spline_) {
250 trajectory_logging_builder.add_goal_spline_handle(*commanded_spline_);
251 if (executing_spline_) {
252 trajectory_logging_builder.add_current_spline_idx(*commanded_spline_);
253 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254 }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800255 trajectory_logging_builder.add_distance_remaining(
James Kuszmauldc534432023-02-05 14:51:11 -0800256 executing_spline_
257 ? CHECK_NOTNULL(current_trajectory())->length() - current_xva_.x()
258 : 0.0);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800259 trajectory_logging_builder.add_available_splines(handles_vector);
Austin Schuh1a843772023-04-09 22:18:05 -0700260 trajectory_logging_builder.add_distance_traveled(
261 executing_spline_ ? current_xva_.x() : 0.0);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800262
Alex Perrycb7da4b2019-08-28 19:35:56 -0700263 return trajectory_logging_builder.Finish();
264}
265
266flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging(
267 aos::Sender<drivetrain::Status>::Builder *builder) const {
268 return MakeTrajectoryLogging(builder->fbb());
Alex Perrye32eabc2019-02-08 19:51:19 -0800269}
270
Alex Perry731b4602019-02-02 22:13:01 -0800271} // namespace drivetrain
272} // namespace control_loops
273} // namespace frc971