blob: 13bd6e683b614420783ee796dec90029c155ba7b [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
12// Provides a representation of a transformation on the field.
13// Currently, this is heavily geared towards things that occur in a 2-D plane.
14// The Z-axis is rarely used (but still relevant; e.g., in 2019 some of the
15// targets are at a different height).
16// For rotations, we currently just represent the yaw axis (the rotation about
17// the Z-axis).
18// As a convention, we use right-handed coordinate systems; the positive Z
19// axis will go up on the field, the positive X axis shall be "forwards" for
20// some relevant meaning of forwards, and the origin shall be chosen as
21// appropriate.
22// For 2019, at least, the global origin will be on the ground at the center
23// of the driver's station wall of your current alliance and the positive X-axis
24// will point straight into the field from the driver's station.
25// In future years this may need to change if the field's symmetry changes and
26// we can't interchangeably handle which side of the field we are on.
27// This means that if we had a Pose for the center of mass of the robot with a
28// position of (10, -5, 0) and a yaw of pi / 2, that suggests the robot is
29// facing straight to the left from the driver's perspective and is placed 10m
30// from the driver's station wall and 5m to the right of the center of the wall.
31//
32// Furthermore, Poses can be chained such that a Pose can be placed relative to
33// another Pose; the other Pose can dynamically update, thus allowing us to,
34// e.g., provide a Pose for a camera that is relative to the Pose of the robot.
35// Poses can also be in the global frame with no parent Pose.
36template <typename Scalar = double>
37class TypedPose {
38 public:
39 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
40
41 // The type that contains the translational (x, y, z) component of the Pose.
42 typedef Eigen::Matrix<Scalar, 3, 1> Pos;
43
James Kuszmaul090563a2019-02-09 14:43:20 -080044 // Provide a default constructor that crease a pose at the origin.
45 TypedPose() : TypedPose({0.0, 0.0, 0.0}, 0.0) {}
46
James Kuszmaul9f9676d2019-01-25 21:27:58 -080047 // Construct a Pose in the absolute frame with a particular position and yaw.
48 TypedPose(const Pos &abs_pos, Scalar theta) : pos_(abs_pos), theta_(theta) {}
49 // Construct a Pose relative to another Pose (base).
50 // If you provide a base of nullptr, then this will
51 // construct a Pose in the global frame.
52 // Note that the lifetime of base should be greater than the lifetime of
53 // the object being constructed.
54 TypedPose(const TypedPose<Scalar> *base, const Pos &rel_pos, Scalar rel_theta)
55 : base_(base), pos_(rel_pos), theta_(rel_theta) {}
56
57 // Calculate the current global position of this Pose.
58 Pos abs_pos() const {
59 if (base_ == nullptr) {
60 return pos_;
61 }
62 Pos base_pos = base_->abs_pos();
63 Scalar base_theta = base_->abs_theta();
64 return base_pos + YawRotation(base_theta) * pos_;
65 }
66 // Calculate the absolute yaw of this Pose. Since we only have a single
67 // rotational axis, we can just sum the angle with that of the base Pose.
68 Scalar abs_theta() const {
69 if (base_ == nullptr) {
70 return theta_;
71 }
72 return aos::math::NormalizeAngle(theta_ + base_->abs_theta());
73 }
74 // Provide access to the position and yaw relative to the base Pose.
75 Pos rel_pos() const { return pos_; }
76 Scalar rel_theta() const { return theta_; }
James Kuszmaul090563a2019-02-09 14:43:20 -080077 const TypedPose<Scalar> *base() const { return base_; }
James Kuszmaul9f9676d2019-01-25 21:27:58 -080078
79 Pos *mutable_pos() { return &pos_; }
80 void set_theta(Scalar theta) { theta_ = theta; }
James Kuszmaul090563a2019-02-09 14:43:20 -080081 // Swap out the base Pose, keeping the current relative position/angle.
82 void set_base(const TypedPose<Scalar> *new_base) { base_ = new_base; }
James Kuszmaul9f9676d2019-01-25 21:27:58 -080083
84 // For 2-D calculation, provide the heading, which is distinct from the
85 // yaw/theta value. heading is the heading relative to the base Pose if you
86 // were to draw a line from the base to this Pose. i.e., if heading() is zero
87 // then you are directly in front of the base Pose.
88 Scalar heading() const { return ::std::atan2(pos_.y(), pos_.x()); }
89 // The 2-D distance from the base Pose to this Pose.
90 Scalar xy_norm() const { return pos_.template topRows<2>().norm(); }
91 // Return the absolute xy position.
92 Eigen::Matrix<Scalar, 2, 1> abs_xy() const {
93 return abs_pos().template topRows<2>();
94 }
95
96 // Provide a copy of this that is set to have the same
97 // current absolute Pose as this, but have a different base.
98 // This can be used, e.g., to compute a Pose for a vision target that is
99 // relative to the camera instead of relative to the field. You can then
100 // access the rel_* variables to get what the position of the target is
101 // relative to the robot/camera.
102 // If new_base == nullptr, provides a Pose referenced to the global frame.
103 // Note that the lifetime of new_base should be greater than the lifetime of
104 // the returned object (unless new_base == nullptr).
105 TypedPose Rebase(const TypedPose<Scalar> *new_base) const;
106
107 private:
108 // A rotation-matrix like representation of the rotation for a given angle.
109 inline static Eigen::AngleAxis<Scalar> YawRotation(double theta) {
110 return Eigen::AngleAxis<Scalar>(theta, Pos::UnitZ());
111 }
112
113 // A pointer to the base Pose. If uninitialized, then this Pose is in the
114 // global frame.
115 const TypedPose<Scalar> *base_ = nullptr;
116 // Position and yaw relative to base_.
117 Pos pos_;
118 Scalar theta_;
119}; // class TypedPose
120
121typedef TypedPose<double> Pose;
122
123template <typename Scalar>
124TypedPose<Scalar> TypedPose<Scalar>::Rebase(
125 const TypedPose<Scalar> *new_base) const {
126 if (new_base == nullptr) {
127 return TypedPose<Scalar>(nullptr, abs_pos(), abs_theta());
128 }
129 // Calculate the absolute position/yaws of this and of the new_base, and then
130 // calculate where we are relative to new_base, essentially reversing the
131 // calculation in abs_*.
132 Pos base_pos = new_base->abs_pos();
133 Scalar base_theta = new_base->abs_theta();
134 Pos self_pos = abs_pos();
135 Scalar self_theta = abs_theta();
136 Scalar diff_theta = ::aos::math::DiffAngle(self_theta, base_theta);
137 Pos diff_pos = YawRotation(-base_theta) * (self_pos - base_pos);
138 return TypedPose<Scalar>(new_base, diff_pos, diff_theta);
139}
140
141// Represents a 2D line segment constructed from a pair of Poses.
142// The line segment goes between the two Poses, but for calculating
143// intersections we use the 2D projection of the Poses onto the global X-Y
144// plane.
145template <typename Scalar = double>
146class TypedLineSegment {
147 public:
James Kuszmaul090563a2019-02-09 14:43:20 -0800148 TypedLineSegment() {}
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800149 TypedLineSegment(const TypedPose<Scalar> &pose1,
150 const TypedPose<Scalar> &pose2)
151 : pose1_(pose1), pose2_(pose2) {}
152 // Detects if two line segments intersect.
153 // When at least one end of one line segment is collinear with the other,
154 // the line segments are treated as not intersecting.
155 bool Intersects(const TypedLineSegment<Scalar> &other) const {
156 // Source for algorithm:
157 // https://bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
158 // Method:
159 // We will consider the four triangles that can be made out of any 3 points
160 // from the pair of line segments.
161 // Basically, if you consider one line segment the base of the triangle,
162 // then the two points of the other line segment should be on opposite
163 // sides of the first line segment (we use the PointsAreCCW function for
164 // this). This must hold when splitting off of both line segments.
165 Eigen::Matrix<Scalar, 2, 1> p1 = pose1_.abs_xy();
166 Eigen::Matrix<Scalar, 2, 1> p2 = pose2_.abs_xy();
167 Eigen::Matrix<Scalar, 2, 1> q1 = other.pose1_.abs_xy();
168 Eigen::Matrix<Scalar, 2, 1> q2 = other.pose2_.abs_xy();
169 return (::aos::math::PointsAreCCW<Scalar>(p1, q1, q2) !=
170 ::aos::math::PointsAreCCW<Scalar>(p2, q1, q2)) &&
171 (::aos::math::PointsAreCCW<Scalar>(p1, p2, q1) !=
172 ::aos::math::PointsAreCCW<Scalar>(p1, p2, q2));
173 }
James Kuszmaul090563a2019-02-09 14:43:20 -0800174
175 TypedPose<Scalar> pose1() const { return pose1_; }
176 TypedPose<Scalar> pose2() const { return pose2_; }
177 TypedPose<Scalar> *mutable_pose1() { return &pose1_; }
178 TypedPose<Scalar> *mutable_pose2() { return &pose2_; }
179
180 ::std::vector<TypedPose<Scalar>> PlotPoints() const {
181 return {pose1_, pose2_};
182 }
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800183 private:
James Kuszmaul090563a2019-02-09 14:43:20 -0800184 TypedPose<Scalar> pose1_;
185 TypedPose<Scalar> pose2_;
James Kuszmaul9f9676d2019-01-25 21:27:58 -0800186}; // class TypedLineSegment
187
188typedef TypedLineSegment<double> LineSegment;
189
190} // namespace control_loops
191} // namespace frc971
192
193#endif // FRC971_CONTROL_LOOPS_POSE_H_