blob: b682b8525341b793b1bb533b3f9355810ce5fdf8 [file] [log] [blame]
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -07001#ifndef FRC971_CONTROL_LOOPS_COERCE_GOAL_H_
2#define FRC971_CONTROL_LOOPS_COERCE_GOAL_H_
3
4#include "Eigen/Dense"
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/controls/polytope.h"
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -07007
8namespace frc971 {
9namespace control_loops {
10
Austin Schuhbcce26a2018-03-26 23:41:24 -070011template <typename Scalar = double>
12Eigen::Matrix<Scalar, 2, 1> DoCoerceGoal(
13 const aos::controls::HVPolytope<2, 4, 4, Scalar> &region,
14 const Eigen::Matrix<Scalar, 1, 2> &K, Scalar w,
15 const Eigen::Matrix<Scalar, 2, 1> &R, bool *is_inside);
Brian Silverman6dd2c532014-03-29 23:34:39 -070016
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -070017// Intersects a line with a region, and finds the closest point to R.
18// Finds a point that is closest to R inside the region, and on the line
19// defined by K X = w. If it is not possible to find a point on the line,
20// finds a point that is inside the region and closest to the line.
Austin Schuhbcce26a2018-03-26 23:41:24 -070021template <typename Scalar = double>
22static inline Eigen::Matrix<Scalar, 2, 1> CoerceGoal(
23 const aos::controls::HVPolytope<2, 4, 4, Scalar> &region,
24 const Eigen::Matrix<Scalar, 1, 2> &K, Scalar w,
25 const Eigen::Matrix<Scalar, 2, 1> &R) {
Brian Silverman6dd2c532014-03-29 23:34:39 -070026 return DoCoerceGoal(region, K, w, R, nullptr);
27}
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -070028
Austin Schuhbcce26a2018-03-26 23:41:24 -070029template <typename Scalar>
30Eigen::Matrix<Scalar, 2, 1> DoCoerceGoal(
31 const aos::controls::HVPolytope<2, 4, 4, Scalar> &region,
32 const Eigen::Matrix<Scalar, 1, 2> &K, Scalar w,
33 const Eigen::Matrix<Scalar, 2, 1> &R, bool *is_inside) {
34 if (region.IsInside(R)) {
35 if (is_inside) *is_inside = true;
36 return R;
37 }
James Kuszmaul0ee1fce2020-01-11 16:59:21 -080038 const Scalar norm_K = K.norm();
Austin Schuhbcce26a2018-03-26 23:41:24 -070039 Eigen::Matrix<Scalar, 2, 1> parallel_vector;
40 Eigen::Matrix<Scalar, 2, 1> perpendicular_vector;
James Kuszmaul0ee1fce2020-01-11 16:59:21 -080041 // Calculate a vector that is perpendicular to the line defined by K * x = w.
42 perpendicular_vector = K.transpose() / norm_K;
43 // Calculate a vector that is parallel to the line defined by K * x = w.
Austin Schuhbcce26a2018-03-26 23:41:24 -070044 parallel_vector << perpendicular_vector(1, 0), -perpendicular_vector(0, 0);
45
James Kuszmaul0ee1fce2020-01-11 16:59:21 -080046 // Calculate the location along the K x = w line where each boundary of the
47 // polytope would intersect.
48 // I.e., we want to calculate the distance along the K x = w line, as
49 // represented by
50 // parallel_vector * dist + perpendicular_vector * w / norm(K),
51 // such that it intersects with H_i * x = k_i.
52 // This gives us H_i * (parallel * dist + perp * w / norm(K)) = k_i.
53 // dist = (k_i - H_i * perp * w / norm(K)) / (H_i * parallel)
54 // projectedh is the numerator, projectedk is the denominator.
55 // The case where H_i * parallel is zero indicates a scenario where the given
56 // boundary of the polytope is parallel to the line, and so there is no
57 // meaningful value of dist to return.
58 // Note that the sign of projectedh will also indicate whether the distance is
59 // an upper or lower bound. If since valid points satisfy H_i * x < k_i, then
60 // if H_i * parallel is less than zero, then dist will be a lower bound and
61 // if it is greater than zero, then dist will be an upper bound.
62 const Eigen::Matrix<Scalar, 4, 1> projectedh =
63 region.static_H() * parallel_vector;
64 const Eigen::Matrix<Scalar, 4, 1> projectedk =
65 region.static_k() - region.static_H() * perpendicular_vector * w / norm_K;
Austin Schuhbcce26a2018-03-26 23:41:24 -070066
67 Scalar min_boundary = ::std::numeric_limits<Scalar>::lowest();
68 Scalar max_boundary = ::std::numeric_limits<Scalar>::max();
69 for (int i = 0; i < 4; ++i) {
70 if (projectedh(i, 0) > 0) {
71 max_boundary =
72 ::std::min(max_boundary, projectedk(i, 0) / projectedh(i, 0));
Lee Mracek65686142020-01-10 22:21:39 -050073 } else if (projectedh(i, 0) != 0) {
Austin Schuhbcce26a2018-03-26 23:41:24 -070074 min_boundary =
75 ::std::max(min_boundary, projectedk(i, 0) / projectedh(i, 0));
76 }
77 }
78
79 Eigen::Matrix<Scalar, 1, 2> vertices;
80 vertices << max_boundary, min_boundary;
81
82 if (max_boundary > min_boundary) {
James Kuszmaul0ee1fce2020-01-11 16:59:21 -080083 // The line goes through the region (if the line just intersects a single
84 // vertex, then we fall through to the next clause), but R is not
85 // inside the region. The returned point will be one of the two points where
86 // the line intersects the edge of the region.
Austin Schuhbcce26a2018-03-26 23:41:24 -070087 Scalar min_distance_sqr = 0;
88 Eigen::Matrix<Scalar, 2, 1> closest_point;
89 for (int i = 0; i < vertices.innerSize(); i++) {
90 Eigen::Matrix<Scalar, 2, 1> point;
James Kuszmaul0ee1fce2020-01-11 16:59:21 -080091 point =
92 parallel_vector * vertices(0, i) + perpendicular_vector * w / norm_K;
Austin Schuhbcce26a2018-03-26 23:41:24 -070093 const Scalar length = (R - point).squaredNorm();
94 if (i == 0 || length < min_distance_sqr) {
95 closest_point = point;
96 min_distance_sqr = length;
97 }
98 }
99 if (is_inside) *is_inside = true;
100 return closest_point;
101 } else {
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800102 // The line does not pass through the region; identify the vertex closest to
103 // the line.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700104 Eigen::Matrix<Scalar, 2, 4> region_vertices = region.StaticVertices();
105#ifdef __linux__
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 CHECK_GT(reinterpret_cast<ssize_t>(region_vertices.outerSize()), 0);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700107#else
108 assert(region_vertices.outerSize() > 0);
109#endif
110 Scalar min_distance = INFINITY;
111 int closest_i = 0;
112 for (int i = 0; i < region_vertices.outerSize(); i++) {
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800113 // Calculate the distance of the vertex from the line. The closest vertex
114 // will be the one to return.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700115 const Scalar length = ::std::abs(
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800116 (perpendicular_vector.transpose() * (region_vertices.col(i)))(0, 0) -
117 w);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700118 if (i == 0 || length < min_distance) {
119 closest_i = i;
120 min_distance = length;
121 }
122 }
123 if (is_inside) *is_inside = false;
124 return (Eigen::Matrix<Scalar, 2, 1>() << region_vertices(0, closest_i),
125 region_vertices(1, closest_i))
126 .finished();
127 }
128}
129
James Kuszmaulfb0e0ae2014-03-25 07:04:47 -0700130} // namespace control_loops
131} // namespace frc971
132
133#endif // FRC971_CONTROL_LOOPS_COERCE_GOAL_H_