blob: 0f9418c0c3bf6a2238eb6af6d35761cc2e9c5a62 [file] [log] [blame]
Alex Perry731b4602019-02-02 22:13:01 -08001#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
2
3#include "Eigen/Dense"
James Kuszmaul75a18c52021-03-10 22:02:07 -08004#include "aos/json_to_flatbuffer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include "aos/realtime.h"
James Kuszmaulc73bb222019-04-07 12:15:35 -07006#include "aos/util/math.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "frc971/control_loops/control_loops_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -08008#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
10#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
11#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -080012
Alex Perry731b4602019-02-02 22:13:01 -080013namespace frc971 {
14namespace control_loops {
15namespace drivetrain {
16
17SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config)
James Kuszmaul75a18c52021-03-10 22:02:07 -080018 : dt_config_(dt_config),
Austin Schuhf7c65202022-11-04 21:28:20 -070019 velocity_drivetrain_(
20 std::make_shared<StateFeedbackLoop<2, 2, 2, double,
21 StateFeedbackHybridPlant<2, 2, 2>,
22 HybridKalman<2, 2, 2>>>(
23 dt_config_.make_hybrid_drivetrain_velocity_loop())),
James Kuszmaul75a18c52021-03-10 22:02:07 -080024 current_xva_(Eigen::Vector3d::Zero()),
25 next_xva_(Eigen::Vector3d::Zero()),
26 next_U_(Eigen::Vector2d::Zero()) {}
Alex Perrye32eabc2019-02-08 19:51:19 -080027
28void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -080029 output_was_capped_ =
30 ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0;
Alex Perrye32eabc2019-02-08 19:51:19 -080031
Alex Perrycc3ee4c2019-02-09 21:20:41 -080032 if (output_was_capped_) {
Alex Perrye32eabc2019-02-08 19:51:19 -080033 *U *= 12.0 / U->lpNorm<Eigen::Infinity>();
34 }
35}
Alex Perry731b4602019-02-02 22:13:01 -080036
Alex Perrycc3ee4c2019-02-09 21:20:41 -080037void SplineDrivetrain::SetGoal(
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 const ::frc971::control_loops::drivetrain::Goal *goal) {
James Kuszmauldc534432023-02-05 14:51:11 -080039 UpdateSplineHandles(goal->has_spline_handle()
40 ? std::make_optional<int>(goal->spline_handle())
41 : std::nullopt);
James Kuszmaul75a18c52021-03-10 22:02:07 -080042}
43
James Kuszmaul99af8b52021-03-28 10:50:15 -070044bool SplineDrivetrain::IsCurrentTrajectory(
45 const fb::Trajectory *trajectory) const {
James Kuszmauldc534432023-02-05 14:51:11 -080046 const FinishedTrajectory *current = current_trajectory();
47 return (current != nullptr &&
48 current->spline_handle() == trajectory->handle());
James Kuszmaul99af8b52021-03-28 10:50:15 -070049}
50
James Kuszmaul75a18c52021-03-10 22:02:07 -080051bool SplineDrivetrain::HasTrajectory(const fb::Trajectory *trajectory) const {
52 if (trajectory == nullptr) {
53 return false;
54 }
55 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -080056 if (trajectories_[ii].spline_handle() == trajectory->handle()) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080057 return true;
58 }
59 }
60 return false;
61}
62
James Kuszmaul99af8b52021-03-28 10:50:15 -070063void SplineDrivetrain::DeleteTrajectory(const fb::Trajectory *trajectory) {
64 CHECK(trajectory != nullptr);
65
66 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -080067 if (trajectories_[ii].spline_handle() == trajectory->handle()) {
James Kuszmaul99af8b52021-03-28 10:50:15 -070068 trajectories_.erase(trajectories_.begin() + ii);
69 return;
70 }
71 }
72
73 LOG(FATAL) << "Trying to remove unknown trajectory " << trajectory->handle();
74}
75
James Kuszmaul75a18c52021-03-10 22:02:07 -080076void SplineDrivetrain::AddTrajectory(const fb::Trajectory *trajectory) {
James Kuszmauldc534432023-02-05 14:51:11 -080077 CHECK_LT(trajectories_.size(), trajectories_.capacity());
78 trajectories_.emplace_back(dt_config_, trajectory, velocity_drivetrain_);
79 UpdateSplineHandles(commanded_spline_);
James Kuszmaul75a18c52021-03-10 22:02:07 -080080}
81
82void SplineDrivetrain::DeleteCurrentSpline() {
James Kuszmauldc534432023-02-05 14:51:11 -080083 DeleteTrajectory(&CHECK_NOTNULL(current_trajectory())->trajectory());
James Kuszmaul75a18c52021-03-10 22:02:07 -080084 executing_spline_ = false;
James Kuszmauldc534432023-02-05 14:51:11 -080085 commanded_spline_.reset();
James Kuszmaul75a18c52021-03-10 22:02:07 -080086 current_xva_.setZero();
87}
88
James Kuszmauldc534432023-02-05 14:51:11 -080089void SplineDrivetrain::UpdateSplineHandles(
90 std::optional<int> commanded_spline) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080091 // If we are currently executing a spline and have received a change
92 if (executing_spline_) {
James Kuszmauldc534432023-02-05 14:51:11 -080093 if (!commanded_spline) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080094 // We've been told to stop executing a spline; remove it from our queue,
95 // and clean up.
96 DeleteCurrentSpline();
97 return;
98 } else {
99 if (executing_spline_ &&
James Kuszmauldc534432023-02-05 14:51:11 -0800100 CHECK_NOTNULL(current_trajectory())->spline_handle() !=
101 *commanded_spline) {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800102 // If we are executing a spline, and the handle has changed, garbage
103 // collect the old spline.
104 DeleteCurrentSpline();
105 }
106 }
107 }
James Kuszmauldc534432023-02-05 14:51:11 -0800108 commanded_spline_ = commanded_spline;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800109 // We've now cleaned up the previous state; handle any new commands.
110 if (!commanded_spline_) {
111 return;
112 }
113 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -0800114 if (trajectories_[ii].spline_handle() == *commanded_spline_) {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800115 executing_spline_ = true;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800116 }
117 }
118 // If we didn't find the commanded spline in the list of available splines,
119 // that's fine; it just means, it hasn't been fully planned yet.
Alex Perrya71badb2019-02-06 19:40:41 -0800120}
121
James Kuszmauldc534432023-02-05 14:51:11 -0800122FinishedTrajectory *SplineDrivetrain::current_trajectory() {
123 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
124 if (trajectories_[ii].spline_handle() == *commanded_spline_) {
125 return &trajectories_[ii];
126 }
127 }
128 return nullptr;
129}
130
131const FinishedTrajectory *SplineDrivetrain::current_trajectory() const {
132 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
133 if (trajectories_[ii].spline_handle() == *commanded_spline_) {
134 return &trajectories_[ii];
135 }
136 }
137 return nullptr;
138}
139
Alex Perrye32eabc2019-02-08 19:51:19 -0800140// TODO(alex): Hold position when done following the spline.
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700141void SplineDrivetrain::Update(
142 bool enable, const ::Eigen::Matrix<double, 5, 1> &state,
143 const ::Eigen::Matrix<double, 2, 1> &voltage_error) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800144 next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrye32eabc2019-02-08 19:51:19 -0800145 enable_ = enable;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800146 if (enable && executing_spline_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800147 ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero();
James Kuszmauldc534432023-02-05 14:51:11 -0800148 const FinishedTrajectory *const trajectory =
149 CHECK_NOTNULL(current_trajectory());
James Kuszmaul75a18c52021-03-10 22:02:07 -0800150 if (!IsAtEnd() && executing_spline_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800151 // TODO(alex): It takes about a cycle for the outputs to propagate to the
152 // motors. Consider delaying the output by a cycle.
James Kuszmauldc534432023-02-05 14:51:11 -0800153 U_ff = trajectory->FFVoltage(current_xva_(0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800154 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800155
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700156 const double current_distance = current_xva_(0);
Alex Perrye32eabc2019-02-08 19:51:19 -0800157 ::Eigen::Matrix<double, 2, 5> K =
James Kuszmauldc534432023-02-05 14:51:11 -0800158 trajectory->GainForDistance(current_distance);
James Kuszmaul1057ce82019-02-09 17:58:24 -0800159 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
James Kuszmauldc534432023-02-05 14:51:11 -0800160 const bool backwards = trajectory->drive_spline_backwards();
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700161 if (backwards) {
James Kuszmaulc73bb222019-04-07 12:15:35 -0700162 ::Eigen::Matrix<double, 2, 1> swapU(U_ff(1, 0), U_ff(0, 0));
163 U_ff = -swapU;
164 goal_state(2, 0) += M_PI;
165 double left_goal = goal_state(3, 0);
166 double right_goal = goal_state(4, 0);
167 goal_state(3, 0) = -right_goal;
168 goal_state(4, 0) = -left_goal;
169 }
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700170 const Eigen::Matrix<double, 5, 1> relative_goal =
James Kuszmauldc534432023-02-05 14:51:11 -0800171 trajectory->StateToPathRelativeState(current_distance, goal_state,
172 backwards);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700173 const Eigen::Matrix<double, 5, 1> relative_state =
James Kuszmauldc534432023-02-05 14:51:11 -0800174 trajectory->StateToPathRelativeState(current_distance, state,
175 backwards);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700176 Eigen::Matrix<double, 5, 1> state_error = relative_goal - relative_state;
James Kuszmaulc73bb222019-04-07 12:15:35 -0700177 state_error(2, 0) = ::aos::math::NormalizeAngle(state_error(2, 0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800178 ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800179
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700180 if (backwards) {
181 Eigen::Matrix<double, 2, 1> swapU(U_fb(1), U_fb(0));
182 U_fb = -swapU;
183 }
184
Austin Schuhd749d932020-12-30 21:38:40 -0800185 ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2, 1>(0, 0);
James Kuszmauldc534432023-02-05 14:51:11 -0800186 next_xva_ = trajectory->GetNextXVA(dt_config_.dt, &xv_state);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700187 next_U_ = U_ff + U_fb - voltage_error;
Alex Perrye32eabc2019-02-08 19:51:19 -0800188 uncapped_U_ = next_U_;
189 ScaleCapU(&next_U_);
Alex Perry731b4602019-02-02 22:13:01 -0800190 }
191}
192
Alex Perry731b4602019-02-02 22:13:01 -0800193void SplineDrivetrain::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 ::frc971::control_loops::drivetrain::OutputT *output) {
Alex Perry731b4602019-02-02 22:13:01 -0800195 if (!output) {
196 return;
197 }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800198 if (executing_spline_) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800199 if (!IsAtEnd()) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800200 output->left_voltage = next_U_(0);
201 output->right_voltage = next_U_(1);
Alex Perrya71badb2019-02-06 19:40:41 -0800202 current_xva_ = next_xva_;
Alex Perry731b4602019-02-02 22:13:01 -0800203 }
204 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800205 output->left_voltage = next_U_(0);
206 output->right_voltage = next_U_(1);
Alex Perry731b4602019-02-02 22:13:01 -0800207}
208
Alex Perrye32eabc2019-02-08 19:51:19 -0800209void SplineDrivetrain::PopulateStatus(
Austin Schuhd749d932020-12-30 21:38:40 -0800210 drivetrain::Status::Builder *builder) const {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 if (builder && enable_) {
212 builder->add_uncapped_left_voltage(uncapped_U_(0));
213 builder->add_uncapped_right_voltage(uncapped_U_(1));
214 builder->add_robot_speed(current_xva_(1));
215 builder->add_output_was_capped(output_was_capped_);
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800216 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700217}
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800218
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging(
220 flatbuffers::FlatBufferBuilder *builder) const {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800221 int *spline_handles;
222 const flatbuffers::Offset<flatbuffers::Vector<int>> handles_vector =
223 builder->CreateUninitializedVector(trajectories_.size(), &spline_handles);
224
225 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
James Kuszmauldc534432023-02-05 14:51:11 -0800226 spline_handles[ii] = trajectories_[ii].spline_handle();
James Kuszmaul75a18c52021-03-10 22:02:07 -0800227 }
228
Alex Perrycb7da4b2019-08-28 19:35:56 -0700229 drivetrain::TrajectoryLogging::Builder trajectory_logging_builder(*builder);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800230 if (executing_spline_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700231 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
232 trajectory_logging_builder.add_x(goal_state(0));
233 trajectory_logging_builder.add_y(goal_state(1));
James Kuszmauldc534432023-02-05 14:51:11 -0800234 if (CHECK_NOTNULL(current_trajectory())->drive_spline_backwards()) {
James Kuszmaul5e8ce312021-03-27 14:59:17 -0700235 trajectory_logging_builder.add_left_velocity(-goal_state(4));
236 trajectory_logging_builder.add_right_velocity(-goal_state(3));
237 trajectory_logging_builder.add_theta(
238 ::aos::math::NormalizeAngle(goal_state(2) + M_PI));
239 } else {
240 trajectory_logging_builder.add_theta(
241 ::aos::math::NormalizeAngle(goal_state(2)));
242 trajectory_logging_builder.add_left_velocity(goal_state(3));
243 trajectory_logging_builder.add_right_velocity(goal_state(4));
244 }
Alex Perrye32eabc2019-02-08 19:51:19 -0800245 }
James Kuszmauldc534432023-02-05 14:51:11 -0800246 trajectory_logging_builder.add_is_executing(!IsAtEnd() && executing_spline_);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800247 trajectory_logging_builder.add_is_executed(executing_spline_ && IsAtEnd());
248 if (commanded_spline_) {
249 trajectory_logging_builder.add_goal_spline_handle(*commanded_spline_);
250 if (executing_spline_) {
251 trajectory_logging_builder.add_current_spline_idx(*commanded_spline_);
252 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700253 }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800254 trajectory_logging_builder.add_distance_remaining(
James Kuszmauldc534432023-02-05 14:51:11 -0800255 executing_spline_
256 ? CHECK_NOTNULL(current_trajectory())->length() - current_xva_.x()
257 : 0.0);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800258 trajectory_logging_builder.add_available_splines(handles_vector);
259
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 return trajectory_logging_builder.Finish();
261}
262
263flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging(
264 aos::Sender<drivetrain::Status>::Builder *builder) const {
265 return MakeTrajectoryLogging(builder->fbb());
Alex Perrye32eabc2019-02-08 19:51:19 -0800266}
267
Alex Perry731b4602019-02-02 22:13:01 -0800268} // namespace drivetrain
269} // namespace control_loops
270} // namespace frc971