blob: cdedce0f6daaec61af88a256b55aaaa36a1a17c7 [file] [log] [blame]
Alex Perry731b4602019-02-02 22:13:01 -08001#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
2
3#include "Eigen/Dense"
4
James Kuszmaul75a18c52021-03-10 22:02:07 -08005#include "aos/json_to_flatbuffer.h"
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/realtime.h"
James Kuszmaulc73bb222019-04-07 12:15:35 -07008#include "aos/util/math.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "frc971/control_loops/control_loops_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -080010#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
12#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -080014
Alex Perry731b4602019-02-02 22:13:01 -080015namespace frc971 {
16namespace control_loops {
17namespace drivetrain {
18
19SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config)
James Kuszmaul75a18c52021-03-10 22:02:07 -080020 : dt_config_(dt_config),
21 current_xva_(Eigen::Vector3d::Zero()),
22 next_xva_(Eigen::Vector3d::Zero()),
23 next_U_(Eigen::Vector2d::Zero()) {}
Alex Perrye32eabc2019-02-08 19:51:19 -080024
25void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -080026 output_was_capped_ =
27 ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0;
Alex Perrye32eabc2019-02-08 19:51:19 -080028
Alex Perrycc3ee4c2019-02-09 21:20:41 -080029 if (output_was_capped_) {
Alex Perrye32eabc2019-02-08 19:51:19 -080030 *U *= 12.0 / U->lpNorm<Eigen::Infinity>();
31 }
32}
Alex Perry731b4602019-02-02 22:13:01 -080033
Alex Perrycc3ee4c2019-02-09 21:20:41 -080034void SplineDrivetrain::SetGoal(
Alex Perrycb7da4b2019-08-28 19:35:56 -070035 const ::frc971::control_loops::drivetrain::Goal *goal) {
James Kuszmaul75a18c52021-03-10 22:02:07 -080036 if (goal->has_spline_handle()) {
37 commanded_spline_ = goal->spline_handle();
Austin Schuhb574fe42019-12-06 23:51:47 -080038 } else {
James Kuszmaul75a18c52021-03-10 22:02:07 -080039 commanded_spline_.reset();
Alex Perrya71badb2019-02-06 19:40:41 -080040 }
James Kuszmaul75a18c52021-03-10 22:02:07 -080041 UpdateSplineHandles();
42}
43
44bool SplineDrivetrain::HasTrajectory(const fb::Trajectory *trajectory) const {
45 if (trajectory == nullptr) {
46 return false;
47 }
48 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
49 if (trajectories_[ii]->spline_handle() == trajectory->handle()) {
50 return true;
51 }
52 }
53 return false;
54}
55
56void SplineDrivetrain::AddTrajectory(const fb::Trajectory *trajectory) {
57 trajectories_.emplace_back(
58 std::make_unique<FinishedTrajectory>(dt_config_, trajectory));
59 UpdateSplineHandles();
60}
61
62void SplineDrivetrain::DeleteCurrentSpline() {
63 CHECK(current_trajectory_index_);
64 CHECK_LT(*current_trajectory_index_, trajectories_.size());
65 trajectories_.erase(trajectories_.begin() + *current_trajectory_index_);
66 executing_spline_ = false;
67 current_trajectory_index_.reset();
68 current_xva_.setZero();
69}
70
71void SplineDrivetrain::UpdateSplineHandles() {
72 // If we are currently executing a spline and have received a change
73 if (executing_spline_) {
74 if (!commanded_spline_) {
75 // We've been told to stop executing a spline; remove it from our queue,
76 // and clean up.
77 DeleteCurrentSpline();
78 return;
79 } else {
80 if (executing_spline_ &&
81 current_trajectory().spline_handle() != *commanded_spline_) {
82 // If we are executing a spline, and the handle has changed, garbage
83 // collect the old spline.
84 DeleteCurrentSpline();
85 }
86 }
87 }
88 // We've now cleaned up the previous state; handle any new commands.
89 if (!commanded_spline_) {
90 return;
91 }
92 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
93 if (trajectories_[ii]->spline_handle() == *commanded_spline_) {
94 executing_spline_ = true;
95 current_trajectory_index_ = ii;
96 }
97 }
98 // If we didn't find the commanded spline in the list of available splines,
99 // that's fine; it just means, it hasn't been fully planned yet.
Alex Perrya71badb2019-02-06 19:40:41 -0800100}
101
Alex Perrye32eabc2019-02-08 19:51:19 -0800102// TODO(alex): Hold position when done following the spline.
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700103void SplineDrivetrain::Update(
104 bool enable, const ::Eigen::Matrix<double, 5, 1> &state,
105 const ::Eigen::Matrix<double, 2, 1> &voltage_error) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800106 next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrye32eabc2019-02-08 19:51:19 -0800107 enable_ = enable;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800108 if (enable && executing_spline_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800109 ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero();
James Kuszmaul75a18c52021-03-10 22:02:07 -0800110 if (!IsAtEnd() && executing_spline_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800111 // TODO(alex): It takes about a cycle for the outputs to propagate to the
112 // motors. Consider delaying the output by a cycle.
James Kuszmaul75a18c52021-03-10 22:02:07 -0800113 U_ff = current_trajectory().FFVoltage(current_xva_(0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800114 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800115
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700116 const double current_distance = current_xva_(0);
Alex Perrye32eabc2019-02-08 19:51:19 -0800117 ::Eigen::Matrix<double, 2, 5> K =
James Kuszmaul75a18c52021-03-10 22:02:07 -0800118 current_trajectory().GainForDistance(current_distance);
James Kuszmaul1057ce82019-02-09 17:58:24 -0800119 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
James Kuszmaul75a18c52021-03-10 22:02:07 -0800120 if (current_trajectory().drive_spline_backwards()) {
James Kuszmaulc73bb222019-04-07 12:15:35 -0700121 ::Eigen::Matrix<double, 2, 1> swapU(U_ff(1, 0), U_ff(0, 0));
122 U_ff = -swapU;
123 goal_state(2, 0) += M_PI;
124 double left_goal = goal_state(3, 0);
125 double right_goal = goal_state(4, 0);
126 goal_state(3, 0) = -right_goal;
127 goal_state(4, 0) = -left_goal;
128 }
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700129 const Eigen::Matrix<double, 5, 1> relative_goal =
James Kuszmaul75a18c52021-03-10 22:02:07 -0800130 current_trajectory().StateToPathRelativeState(current_distance,
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700131 goal_state);
132 const Eigen::Matrix<double, 5, 1> relative_state =
James Kuszmaul75a18c52021-03-10 22:02:07 -0800133 current_trajectory().StateToPathRelativeState(current_distance, state);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700134 Eigen::Matrix<double, 5, 1> state_error = relative_goal - relative_state;
James Kuszmaulc73bb222019-04-07 12:15:35 -0700135 state_error(2, 0) = ::aos::math::NormalizeAngle(state_error(2, 0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800136 ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800137
Austin Schuhd749d932020-12-30 21:38:40 -0800138 ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2, 1>(0, 0);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800139 next_xva_ = current_trajectory().GetNextXVA(dt_config_.dt, &xv_state);
James Kuszmaulaa2499d2020-06-02 21:31:19 -0700140 next_U_ = U_ff + U_fb - voltage_error;
Alex Perrye32eabc2019-02-08 19:51:19 -0800141 uncapped_U_ = next_U_;
142 ScaleCapU(&next_U_);
Alex Perry731b4602019-02-02 22:13:01 -0800143 }
144}
145
Alex Perry731b4602019-02-02 22:13:01 -0800146void SplineDrivetrain::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 ::frc971::control_loops::drivetrain::OutputT *output) {
Alex Perry731b4602019-02-02 22:13:01 -0800148 if (!output) {
149 return;
150 }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800151 if (executing_spline_) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800152 if (!IsAtEnd()) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800153 output->left_voltage = next_U_(0);
154 output->right_voltage = next_U_(1);
Alex Perrya71badb2019-02-06 19:40:41 -0800155 current_xva_ = next_xva_;
Alex Perry731b4602019-02-02 22:13:01 -0800156 }
157 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800158 output->left_voltage = next_U_(0);
159 output->right_voltage = next_U_(1);
Alex Perry731b4602019-02-02 22:13:01 -0800160}
161
Alex Perrye32eabc2019-02-08 19:51:19 -0800162void SplineDrivetrain::PopulateStatus(
Austin Schuhd749d932020-12-30 21:38:40 -0800163 drivetrain::Status::Builder *builder) const {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 if (builder && enable_) {
165 builder->add_uncapped_left_voltage(uncapped_U_(0));
166 builder->add_uncapped_right_voltage(uncapped_U_(1));
167 builder->add_robot_speed(current_xva_(1));
168 builder->add_output_was_capped(output_was_capped_);
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800169 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170}
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800171
Alex Perrycb7da4b2019-08-28 19:35:56 -0700172flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging(
173 flatbuffers::FlatBufferBuilder *builder) const {
James Kuszmaul75a18c52021-03-10 22:02:07 -0800174 int *spline_handles;
175 const flatbuffers::Offset<flatbuffers::Vector<int>> handles_vector =
176 builder->CreateUninitializedVector(trajectories_.size(), &spline_handles);
177
178 for (size_t ii = 0; ii < trajectories_.size(); ++ii) {
179 spline_handles[ii] = trajectories_[ii]->spline_handle();
180 }
181
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 drivetrain::TrajectoryLogging::Builder trajectory_logging_builder(*builder);
James Kuszmaul75a18c52021-03-10 22:02:07 -0800183 if (executing_spline_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700184 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
185 trajectory_logging_builder.add_x(goal_state(0));
186 trajectory_logging_builder.add_y(goal_state(1));
187 trajectory_logging_builder.add_theta(::aos::math::NormalizeAngle(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800188 goal_state(2) +
189 (current_trajectory().drive_spline_backwards() ? M_PI : 0.0)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190 trajectory_logging_builder.add_left_velocity(goal_state(3));
191 trajectory_logging_builder.add_right_velocity(goal_state(4));
Alex Perrye32eabc2019-02-08 19:51:19 -0800192 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 trajectory_logging_builder.add_is_executing(!IsAtEnd() &&
James Kuszmaul75a18c52021-03-10 22:02:07 -0800194 executing_spline_);
195 trajectory_logging_builder.add_is_executed(executing_spline_ && IsAtEnd());
196 if (commanded_spline_) {
197 trajectory_logging_builder.add_goal_spline_handle(*commanded_spline_);
198 if (executing_spline_) {
199 trajectory_logging_builder.add_current_spline_idx(*commanded_spline_);
200 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201 }
James Kuszmaul75a18c52021-03-10 22:02:07 -0800202 trajectory_logging_builder.add_distance_remaining(
203 executing_spline_ ? current_trajectory().length() - current_xva_.x()
204 : 0.0);
205 trajectory_logging_builder.add_available_splines(handles_vector);
206
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 return trajectory_logging_builder.Finish();
208}
209
210flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging(
211 aos::Sender<drivetrain::Status>::Builder *builder) const {
212 return MakeTrajectoryLogging(builder->fbb());
Alex Perrye32eabc2019-02-08 19:51:19 -0800213}
214
Alex Perry731b4602019-02-02 22:13:01 -0800215} // namespace drivetrain
216} // namespace control_loops
217} // namespace frc971