Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
| 2 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 3 | #include "glog/logging.h" |
| 4 | |
Austin Schuh | a6e7b21 | 2019-01-20 13:53:01 -0800 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 6 | #include "frc971/control_loops/drivetrain/spline.h" |
| 7 | |
| 8 | namespace frc971 { |
| 9 | namespace control_loops { |
| 10 | namespace drivetrain { |
| 11 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 12 | ::std::vector<float> DistanceSpline::BuildDistances(size_t num_alpha) { |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 13 | num_alpha = num_alpha == 0 ? 100 * splines().size() : num_alpha; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 14 | ::std::vector<float> distances; |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 15 | distances.push_back(0.0); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 16 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 17 | if (splines().size() > 1) { |
Austin Schuh | a6e7b21 | 2019-01-20 13:53:01 -0800 | [diff] [blame] | 18 | // We've got a multispline to follow! |
Ethan Wing | 20da69a | 2021-10-13 20:37:36 -0700 | [diff] [blame] | 19 | // Confirm that the ends line up to the correct number of derivatives. |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 20 | for (size_t i = 1; i < splines().size(); ++i) { |
| 21 | const Spline &spline0 = splines()[i - 1]; |
| 22 | const Spline &spline1 = splines()[i]; |
Austin Schuh | a6e7b21 | 2019-01-20 13:53:01 -0800 | [diff] [blame] | 23 | |
| 24 | const ::Eigen::Matrix<double, 2, 1> end0 = spline0.Point(1.0); |
| 25 | const ::Eigen::Matrix<double, 2, 1> start1 = spline1.Point(0.0); |
| 26 | |
| 27 | if (!end0.isApprox(start1, 1e-6)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 28 | AOS_LOG(ERROR, |
| 29 | "Splines %d and %d don't line up. [%f, %f] != [%f, %f]\n", |
| 30 | static_cast<int>(i - 1), static_cast<int>(i), end0(0, 0), |
| 31 | end0(1, 0), start1(0, 0), start1(1, 0)); |
Austin Schuh | a6e7b21 | 2019-01-20 13:53:01 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | const ::Eigen::Matrix<double, 2, 1> dend0 = spline0.DPoint(1.0); |
| 35 | const ::Eigen::Matrix<double, 2, 1> dstart1 = spline1.DPoint(0.0); |
| 36 | |
| 37 | if (!dend0.isApprox(dstart1, 1e-6)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 38 | AOS_LOG( |
| 39 | ERROR, |
Ethan Wing | 20da69a | 2021-10-13 20:37:36 -0700 | [diff] [blame] | 40 | "Splines %d and %d don't line up in the first derivative. [%f, " |
Austin Schuh | a6e7b21 | 2019-01-20 13:53:01 -0800 | [diff] [blame] | 41 | "%f] != [%f, %f]\n", |
| 42 | static_cast<int>(i - 1), static_cast<int>(i), dend0(0, 0), |
| 43 | dend0(1, 0), dstart1(0, 0), dstart1(1, 0)); |
| 44 | } |
| 45 | |
| 46 | const ::Eigen::Matrix<double, 2, 1> ddend0 = spline0.DDPoint(1.0); |
| 47 | const ::Eigen::Matrix<double, 2, 1> ddstart1 = spline1.DDPoint(0.0); |
| 48 | |
| 49 | if (!ddend0.isApprox(ddstart1, 1e-6)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 50 | AOS_LOG( |
| 51 | ERROR, |
Austin Schuh | 7a105de | 2023-04-09 20:04:53 -0700 | [diff] [blame] | 52 | "Splines %d and %d don't line up in the second derivative. [%.7f, " |
| 53 | "%.7f] != [%.7f, %.7f]\n", |
Austin Schuh | a6e7b21 | 2019-01-20 13:53:01 -0800 | [diff] [blame] | 54 | static_cast<int>(i - 1), static_cast<int>(i), ddend0(0, 0), |
| 55 | ddend0(1, 0), ddstart1(0, 0), ddstart1(1, 0)); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 60 | const double dalpha = static_cast<double>(splines().size()) / |
| 61 | static_cast<double>(num_alpha - 1); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 62 | double last_alpha = 0.0; |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 63 | for (size_t i = 1; i < num_alpha; ++i) { |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 64 | const double alpha = dalpha * i; |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 65 | distances.push_back(distances.back() + |
| 66 | GaussianQuadrature5( |
| 67 | [this](double alpha) { |
| 68 | const size_t spline_index = ::std::min( |
| 69 | static_cast<size_t>(::std::floor(alpha)), |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 70 | splines().size() - 1); |
| 71 | return this->splines()[spline_index] |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 72 | .DPoint(alpha - spline_index) |
| 73 | .norm(); |
| 74 | }, |
| 75 | last_alpha, alpha)); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 76 | last_alpha = alpha; |
| 77 | } |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 78 | return distances; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 79 | } |
| 80 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 81 | std::vector<Spline> FlatbufferToSplines(const MultiSpline *fb) { |
| 82 | CHECK_NOTNULL(fb); |
| 83 | const size_t spline_count = fb->spline_count(); |
| 84 | CHECK_EQ(fb->spline_x()->size(), static_cast<size_t>(spline_count * 5 + 1)); |
| 85 | CHECK_EQ(fb->spline_y()->size(), static_cast<size_t>(spline_count * 5 + 1)); |
| 86 | std::vector<Spline> splines; |
| 87 | for (size_t ii = 0; ii < spline_count; ++ii) { |
| 88 | Eigen::Matrix<double, 2, 6> points; |
| 89 | for (int jj = 0; jj < 6; ++jj) { |
| 90 | points(0, jj) = fb->spline_x()->Get(ii * 5 + jj); |
| 91 | points(1, jj) = fb->spline_y()->Get(ii * 5 + jj); |
| 92 | } |
| 93 | splines.emplace_back(Spline(points)); |
| 94 | } |
| 95 | return splines; |
| 96 | } |
| 97 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 98 | aos::SizedArray<Spline, FinishedDistanceSpline::kMaxSplines> |
| 99 | SizedFlatbufferToSplines(const MultiSpline *fb) { |
| 100 | CHECK_NOTNULL(fb); |
| 101 | const size_t spline_count = fb->spline_count(); |
| 102 | CHECK_EQ(fb->spline_x()->size(), static_cast<size_t>(spline_count * 5 + 1)); |
| 103 | CHECK_EQ(fb->spline_y()->size(), static_cast<size_t>(spline_count * 5 + 1)); |
| 104 | aos::SizedArray<Spline, FinishedDistanceSpline::kMaxSplines> splines; |
| 105 | for (size_t ii = 0; ii < spline_count; ++ii) { |
| 106 | Eigen::Matrix<double, 2, 6> points; |
| 107 | for (int jj = 0; jj < 6; ++jj) { |
| 108 | points(0, jj) = fb->spline_x()->Get(ii * 5 + jj); |
| 109 | points(1, jj) = fb->spline_y()->Get(ii * 5 + jj); |
| 110 | } |
| 111 | splines.emplace_back(Spline(points)); |
| 112 | } |
| 113 | return splines; |
| 114 | } |
| 115 | |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 116 | DistanceSpline::DistanceSpline(::std::vector<Spline> &&splines, int num_alpha) |
| 117 | : splines_(::std::move(splines)), distances_(BuildDistances(num_alpha)) {} |
| 118 | |
| 119 | DistanceSpline::DistanceSpline(const Spline &spline, int num_alpha) |
| 120 | : splines_({spline}), distances_(BuildDistances(num_alpha)) {} |
| 121 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 122 | DistanceSpline::DistanceSpline(const MultiSpline *fb, int num_alpha) |
| 123 | : splines_(FlatbufferToSplines(fb)), |
| 124 | distances_(BuildDistances(num_alpha)) {} |
| 125 | |
| 126 | // TODO(james): Directly use the flatbuffer vector for accessing distances, |
| 127 | // rather than doing this redundant copy. |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 128 | FinishedDistanceSpline::FinishedDistanceSpline(const fb::DistanceSpline &fb) |
| 129 | : splines_(SizedFlatbufferToSplines(fb.spline())), |
| 130 | distances_(fb.distances()->data(), fb.distances()->size()) {} |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 131 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 132 | flatbuffers::Offset<fb::DistanceSpline> DistanceSplineBase::Serialize( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 133 | flatbuffers::FlatBufferBuilder *fbb, |
| 134 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Constraint>>> |
| 135 | constraints) const { |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 136 | if (splines().empty()) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 137 | return {}; |
| 138 | } |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 139 | const size_t num_points = splines().size() * 5 + 1; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 140 | float *spline_x_vector = nullptr; |
| 141 | float *spline_y_vector = nullptr; |
| 142 | const flatbuffers::Offset<flatbuffers::Vector<float>> spline_x_offset = |
| 143 | fbb->CreateUninitializedVector(num_points, &spline_x_vector); |
| 144 | const flatbuffers::Offset<flatbuffers::Vector<float>> spline_y_offset = |
| 145 | fbb->CreateUninitializedVector(num_points, &spline_y_vector); |
| 146 | CHECK_NOTNULL(spline_x_vector); |
| 147 | CHECK_NOTNULL(spline_y_vector); |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 148 | spline_x_vector[0] = splines()[0].control_points()(0, 0); |
| 149 | spline_y_vector[0] = splines()[0].control_points()(1, 0); |
| 150 | for (size_t spline_index = 0; spline_index < splines().size(); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 151 | ++spline_index) { |
| 152 | for (size_t point = 1; point < 6u; ++point) { |
| 153 | spline_x_vector[spline_index * 5 + point] = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 154 | splines()[spline_index].control_points()(0, point); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 155 | spline_y_vector[spline_index * 5 + point] = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 156 | splines()[spline_index].control_points()(1, point); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | MultiSpline::Builder multi_spline_builder(*fbb); |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 160 | multi_spline_builder.add_spline_count(splines().size()); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 161 | multi_spline_builder.add_spline_x(spline_x_offset); |
| 162 | multi_spline_builder.add_spline_y(spline_y_offset); |
| 163 | multi_spline_builder.add_constraints(constraints); |
| 164 | const flatbuffers::Offset<MultiSpline> multi_spline_offset = |
| 165 | multi_spline_builder.Finish(); |
| 166 | const flatbuffers::Offset<flatbuffers::Vector<float>> distances_offset = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 167 | fbb->CreateVector(distances().data(), distances().size()); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 168 | fb::DistanceSpline::Builder spline_builder(*fbb); |
| 169 | spline_builder.add_spline(multi_spline_offset); |
| 170 | spline_builder.add_distances(distances_offset); |
| 171 | return spline_builder.Finish(); |
| 172 | } |
| 173 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 174 | ::Eigen::Matrix<double, 2, 1> DistanceSplineBase::DDXY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 175 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 176 | const ::Eigen::Matrix<double, 2, 1> dspline_point = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 177 | splines()[a.index].DPoint(a.alpha); |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 178 | const ::Eigen::Matrix<double, 2, 1> ddspline_point = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 179 | splines()[a.index].DDPoint(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 180 | |
| 181 | const double squared_norm = dspline_point.squaredNorm(); |
| 182 | |
| 183 | return ddspline_point / squared_norm - |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 184 | dspline_point * |
| 185 | (dspline_point(0) * ddspline_point(0) + |
| 186 | dspline_point(1) * ddspline_point(1)) / |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 187 | ::std::pow(squared_norm, 2); |
| 188 | } |
| 189 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 190 | double DistanceSplineBase::DDTheta(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 191 | const AlphaAndIndex a = DistanceToAlpha(distance); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 192 | |
| 193 | // TODO(austin): We are re-computing DPoint here even worse |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 194 | const ::Eigen::Matrix<double, 2, 1> dspline_point = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 195 | splines()[a.index].DPoint(a.alpha); |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 196 | const ::Eigen::Matrix<double, 2, 1> ddspline_point = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 197 | splines()[a.index].DDPoint(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 198 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 199 | const double dtheta = splines()[a.index].DTheta(a.alpha); |
| 200 | const double ddtheta = splines()[a.index].DDTheta(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 201 | |
| 202 | const double squared_norm = dspline_point.squaredNorm(); |
| 203 | |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 204 | return ddtheta / squared_norm - dtheta * |
| 205 | (dspline_point(0) * ddspline_point(0) + |
| 206 | dspline_point(1) * ddspline_point(1)) / |
| 207 | ::std::pow(squared_norm, 2); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 208 | } |
| 209 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 210 | DistanceSplineBase::AlphaAndIndex DistanceSplineBase::DistanceToAlpha( |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 211 | double distance) const { |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 212 | if (distance <= 0.0) { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 213 | return {0, 0.0}; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 214 | } |
| 215 | if (distance >= length()) { |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 216 | return {splines().size() - 1, 1.0}; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Find the distance right below our number using a binary search. |
| 220 | size_t after = ::std::distance( |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 221 | distances().begin(), |
| 222 | ::std::lower_bound(distances().begin(), distances().end(), distance)); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 223 | size_t before = after - 1; |
| 224 | const double distance_step_size = |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 225 | (splines().size() / static_cast<double>(distances().size() - 1)); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 226 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 227 | const double alpha = (distance - distances()[before]) / |
| 228 | (distances()[after] - distances()[before]) * |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 229 | distance_step_size + |
| 230 | static_cast<double>(before) * distance_step_size; |
| 231 | const size_t index = static_cast<size_t>(::std::floor(alpha)); |
| 232 | |
| 233 | return {index, alpha - index}; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | } // namespace drivetrain |
| 237 | } // namespace control_loops |
| 238 | } // namespace frc971 |