blob: 7945e5e4533a21097316123fe10a169136207889 [file] [log] [blame]
James Kuszmaul9f9676d2019-01-25 21:27:58 -08001#ifndef FRC971_CONTROL_LOOPS_POSE_H_
2#define FRC971_CONTROL_LOOPS_POSE_H_
3
James Kuszmaul090563a2019-02-09 14:43:20 -08004#include <vector>
5
James Kuszmaul9f9676d2019-01-25 21:27:58 -08006#include "Eigen/Dense"
7#include "aos/util/math.h"
8
9namespace frc971 {
10namespace control_loops {
11
James Kuszmaul3ca28612020-02-15 17:52:27 -080012// Constructs a homogeneous transformation matrix for rotating about the Z axis.
13template <typename Scalar>
14Eigen::Matrix<Scalar, 4, 4> TransformationMatrixForYaw(Scalar yaw) {
15 Eigen::Matrix<Scalar, 4, 4> matrix;
16 matrix.setIdentity();
17 const Scalar stheta = std::sin(yaw);
18 const Scalar ctheta = std::cos(yaw);
19 matrix(0, 0) = ctheta;
20 matrix(1, 1) = ctheta;
21 matrix(0, 1) = -stheta;
22 matrix(1, 0) = stheta;
23 return matrix;
24}
25
James Kuszmaul9f9676d2019-01-25 21:27:58 -080026// Provides a representation of a transformation on the field.
27// Currently, this is heavily geared towards things that occur in a 2-D plane.
28// The Z-axis is rarely used (but still relevant; e.g., in 2019 some of the
29// targets are at a different height).
30// For rotations, we currently just represent the yaw axis (the rotation about
31// the Z-axis).
32// As a convention, we use right-handed coordinate systems; the positive Z
33// axis will go up on the field, the positive X axis shall be "forwards" for
34// some relevant meaning of forwards, and the origin shall be chosen as
35// appropriate.
36// For 2019, at least, the global origin will be on the ground at the center
37// of the driver's station wall of your current alliance and the positive X-axis
38// will point straight into the field from the driver's station.
39// In future years this may need to change if the field's symmetry changes and
40// we can't interchangeably handle which side of the field we are on.
41// This means that if we had a Pose for the center of mass of the robot with a
42// position of (10, -5, 0) and a yaw of pi / 2, that suggests the robot is
43// facing straight to the left from the driver's perspective and is placed 10m
44// from the driver's station wall and 5m to the right of the center of the wall.
James Kuszmaula53c3ac2020-02-22 19:36:01 -080045// For 2020, we move the origin to be the center of the field and make positive
46// x always point towards the red alliance driver stations.
James Kuszmaul9f9676d2019-01-25 21:27:58 -080047//
48// Furthermore, Poses can be chained such that a Pose can be placed relative to
49// another Pose; the other Pose can dynamically update, thus allowing us to,
50// e.g., provide a Pose for a camera that is relative to the Pose of the robot.
51// Poses can also be in the global frame with no parent Pose.
52template <typename Scalar = double>
53class TypedPose {
54 public:
55 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
56
57 // The type that contains the translational (x, y, z) component of the Pose.
58 typedef Eigen::Matrix<Scalar, 3, 1> Pos;
59
James Kuszmaul3ca28612020-02-15 17:52:27 -080060 // Provide a default constructor that creates a pose at the origin.
James Kuszmaul090563a2019-02-09 14:43:20 -080061 TypedPose() : TypedPose({0.0, 0.0, 0.0}, 0.0) {}
62
James Kuszmaul9f9676d2019-01-25 21:27:58 -080063 // Construct a Pose in the absolute frame with a particular position and yaw.
64 TypedPose(const Pos &abs_pos, Scalar theta) : pos_(abs_pos), theta_(theta) {}
65 // Construct a Pose relative to another Pose (base).
66 // If you provide a base of nullptr, then this will
67 // construct a Pose in the global frame.
68 // Note that the lifetime of base should be greater than the lifetime of
69 // the object being constructed.
70 TypedPose(const TypedPose<Scalar> *base, const Pos &rel_pos, Scalar rel_theta)
71 : base_(base), pos_(rel_pos), theta_(rel_theta) {}
72
James Kuszmaul3ca28612020-02-15 17:52:27 -080073 // Constructs a Pose from a homogeneous transformation matrix. Ignores the
74 // pitch/roll components of the rotation. Ignores the bottom row.
75 TypedPose(const Eigen::Matrix<Scalar, 4, 4> &H) {
76 pos_ = H.template block<3, 1>(0, 3);
James Kuszmauld478f872020-03-16 20:54:27 -070077 const Eigen::Matrix<Scalar, 3, 1> rotated_x =
78 H.template block<3, 3>(0, 0) * Eigen::Matrix<Scalar, 3, 1>::UnitX();
James Kuszmaul3ca28612020-02-15 17:52:27 -080079 theta_ = std::atan2(rotated_x.y(), rotated_x.x());
80 }
81
James Kuszmaul9f9676d2019-01-25 21:27:58 -080082 // Calculate the current global position of this Pose.
83 Pos abs_pos() const {
84 if (base_ == nullptr) {
85 return pos_;
86 }
87 Pos base_pos = base_->abs_pos();
88 Scalar base_theta = base_->abs_theta();
89 return base_pos + YawRotation(base_theta) * pos_;
90 }
91 // Calculate the absolute yaw of this Pose. Since we only have a single
92 // rotational axis, we can just sum the angle with that of the base Pose.
93 Scalar abs_theta() const {
94 if (base_ == nullptr) {
95 return theta_;
96 }
97 return aos::math::NormalizeAngle(theta_ + base_->abs_theta());
98 }
99 // Provide access to the position and yaw relative to the base Pose.
100 Pos rel_pos() const { return pos_; }
101 Scalar rel_theta() const { return theta_; }
James Kuszmaul090563a2019-02-09 14:43:20 -0800102 const TypedPose<Scalar> *base() const { return base_; }
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800103
104 Pos *mutable_pos() { return &pos_; }
105 void set_theta(Scalar theta) { theta_ = theta; }
James Kuszmaul090563a2019-02-09 14:43:20 -0800106 // Swap out the base Pose, keeping the current relative position/angle.
107 void set_base(const TypedPose<Scalar> *new_base) { base_ = new_base; }
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800108
109 // For 2-D calculation, provide the heading, which is distinct from the
110 // yaw/theta value. heading is the heading relative to the base Pose if you
111 // were to draw a line from the base to this Pose. i.e., if heading() is zero
112 // then you are directly in front of the base Pose.
113 Scalar heading() const { return ::std::atan2(pos_.y(), pos_.x()); }
114 // The 2-D distance from the base Pose to this Pose.
115 Scalar xy_norm() const { return pos_.template topRows<2>().norm(); }
116 // Return the absolute xy position.
117 Eigen::Matrix<Scalar, 2, 1> abs_xy() const {
118 return abs_pos().template topRows<2>();
119 }
120
James Kuszmaul3ca28612020-02-15 17:52:27 -0800121 // Returns a transformation matrix representing this pose--note that this
122 // explicitly does not include the base position, so this is equivalent to a
123 // translation and rotation by rel_pos and rel_theta.
124 Eigen::Matrix<Scalar, 4, 4> AsTransformationMatrix() const {
125 Eigen::Matrix<Scalar, 4, 4> matrix = TransformationMatrixForYaw(theta_);
126 matrix.template block<3, 1>(0, 3) = pos_;
127 return matrix;
128 }
129
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800130 // Provide a copy of this that is set to have the same
131 // current absolute Pose as this, but have a different base.
132 // This can be used, e.g., to compute a Pose for a vision target that is
133 // relative to the camera instead of relative to the field. You can then
134 // access the rel_* variables to get what the position of the target is
135 // relative to the robot/camera.
136 // If new_base == nullptr, provides a Pose referenced to the global frame.
137 // Note that the lifetime of new_base should be greater than the lifetime of
138 // the returned object (unless new_base == nullptr).
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800139 [[nodiscard]] TypedPose Rebase(const TypedPose<Scalar> *new_base) const;
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800140
James Kuszmaul3ca28612020-02-15 17:52:27 -0800141 // Convert this pose to the heading/distance/skew numbers that we
142 // traditionally use for EKF corrections.
143 Eigen::Matrix<Scalar, 3, 1> ToHeadingDistanceSkew() const {
144 const Scalar target_heading = heading();
145 return {target_heading, xy_norm(),
146 aos::math::NormalizeAngle(rel_theta() - target_heading)};
147 }
148
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800149 private:
150 // A rotation-matrix like representation of the rotation for a given angle.
151 inline static Eigen::AngleAxis<Scalar> YawRotation(double theta) {
152 return Eigen::AngleAxis<Scalar>(theta, Pos::UnitZ());
153 }
154
155 // A pointer to the base Pose. If uninitialized, then this Pose is in the
156 // global frame.
157 const TypedPose<Scalar> *base_ = nullptr;
158 // Position and yaw relative to base_.
159 Pos pos_;
160 Scalar theta_;
161}; // class TypedPose
162
163typedef TypedPose<double> Pose;
164
165template <typename Scalar>
166TypedPose<Scalar> TypedPose<Scalar>::Rebase(
167 const TypedPose<Scalar> *new_base) const {
168 if (new_base == nullptr) {
169 return TypedPose<Scalar>(nullptr, abs_pos(), abs_theta());
170 }
171 // Calculate the absolute position/yaws of this and of the new_base, and then
172 // calculate where we are relative to new_base, essentially reversing the
173 // calculation in abs_*.
174 Pos base_pos = new_base->abs_pos();
175 Scalar base_theta = new_base->abs_theta();
176 Pos self_pos = abs_pos();
177 Scalar self_theta = abs_theta();
178 Scalar diff_theta = ::aos::math::DiffAngle(self_theta, base_theta);
179 Pos diff_pos = YawRotation(-base_theta) * (self_pos - base_pos);
180 return TypedPose<Scalar>(new_base, diff_pos, diff_theta);
181}
182
183// Represents a 2D line segment constructed from a pair of Poses.
184// The line segment goes between the two Poses, but for calculating
185// intersections we use the 2D projection of the Poses onto the global X-Y
186// plane.
187template <typename Scalar = double>
188class TypedLineSegment {
189 public:
James Kuszmaul090563a2019-02-09 14:43:20 -0800190 TypedLineSegment() {}
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800191 TypedLineSegment(const TypedPose<Scalar> &pose1,
192 const TypedPose<Scalar> &pose2)
193 : pose1_(pose1), pose2_(pose2) {}
194 // Detects if two line segments intersect.
195 // When at least one end of one line segment is collinear with the other,
196 // the line segments are treated as not intersecting.
197 bool Intersects(const TypedLineSegment<Scalar> &other) const {
198 // Source for algorithm:
199 // https://bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
200 // Method:
201 // We will consider the four triangles that can be made out of any 3 points
202 // from the pair of line segments.
203 // Basically, if you consider one line segment the base of the triangle,
204 // then the two points of the other line segment should be on opposite
205 // sides of the first line segment (we use the PointsAreCCW function for
206 // this). This must hold when splitting off of both line segments.
207 Eigen::Matrix<Scalar, 2, 1> p1 = pose1_.abs_xy();
208 Eigen::Matrix<Scalar, 2, 1> p2 = pose2_.abs_xy();
209 Eigen::Matrix<Scalar, 2, 1> q1 = other.pose1_.abs_xy();
210 Eigen::Matrix<Scalar, 2, 1> q2 = other.pose2_.abs_xy();
211 return (::aos::math::PointsAreCCW<Scalar>(p1, q1, q2) !=
212 ::aos::math::PointsAreCCW<Scalar>(p2, q1, q2)) &&
213 (::aos::math::PointsAreCCW<Scalar>(p1, p2, q1) !=
214 ::aos::math::PointsAreCCW<Scalar>(p1, p2, q2));
215 }
James Kuszmaul090563a2019-02-09 14:43:20 -0800216
217 TypedPose<Scalar> pose1() const { return pose1_; }
218 TypedPose<Scalar> pose2() const { return pose2_; }
219 TypedPose<Scalar> *mutable_pose1() { return &pose1_; }
220 TypedPose<Scalar> *mutable_pose2() { return &pose2_; }
221
222 ::std::vector<TypedPose<Scalar>> PlotPoints() const {
223 return {pose1_, pose2_};
224 }
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800225 private:
James Kuszmaul090563a2019-02-09 14:43:20 -0800226 TypedPose<Scalar> pose1_;
227 TypedPose<Scalar> pose2_;
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800228}; // class TypedLineSegment
229
230typedef TypedLineSegment<double> LineSegment;
231
232} // namespace control_loops
233} // namespace frc971
234
235#endif // FRC971_CONTROL_LOOPS_POSE_H_