blob: 02e7e655f010119ef0a1c94fbf8f3c2d8a7c2b56 [file] [log] [blame]
James Kuszmaul57c7c9b2019-01-27 16:16:01 -08001#ifndef Y2019_CONTROL_LOOPS_DRIVETRAIN_CAMERA_H_
2#define Y2019_CONTROL_LOOPS_DRIVETRAIN_CAMERA_H_
3
4#include <vector>
5
6#include "aos/containers/sized_array.h"
7#include "frc971/control_loops/pose.h"
8
James Kuszmaulf4ede202020-02-14 08:47:40 -08009namespace frc971 {
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080010namespace control_loops {
11
12// Represents a target on the field. Currently just consists of a pose and a
13// indicator for whether it is occluded (occlusion is only used by the simulator
14// for testing).
15// Orientation convention:
16// -The absolute position of the pose is the center of the vision target on the
17// field.
18// -The yaw of the pose shall be such that the positive X-axis in the Target's
19// frame wil be pointed straight through the target--i.e., if you are looking
20// at the target head-on, then you will be facing in the same direction as the
21// positive X-axis.
22// E.g., if the Target has a global position of (1, 1, 0) and yaw of pi / 2,
23// then it is at position (1, 1, 0) on the field and is oriented so that if
24// someone were to stand at (1, 0, 0) and turn themselves to have a yaw of
25// pi / 2, they would see the target 1 meter straight ahead of them.
26//
27// Generally, the position of a target should not change dynamically; if we do
28// start having targets that move, we may want to start optimizing certain
29// things (e.g., caching the position of the Target--currently, if the Pose of a
30// target is in an absolute frame, then calling abs_pos will be inexpensive; if
31// that changes, then we start having to recalculate transformations on every
32// frame).
33template <typename Scalar = double>
34class TypedTarget {
35 public:
36 typedef ::frc971::control_loops::TypedPose<Scalar> Pose;
James Kuszmaule093f512019-03-20 06:14:05 -070037 // The nature of the target as a goal--to mark what modes it is a valid
38 // potential goal pose and to mark targets on the opposite side of the field
39 // as not being viable targets.
40 enum class GoalType {
41 // None marks targets that are on the opposite side of the field and not
42 // viable goal poses.
43 kNone,
44 // Spots where we can touch hatch panels.
45 kHatches,
46 // Spots where we can mess with balls.
47 kBalls,
48 // Spots for both (cargo ship, human loading).
49 kBoth,
50 };
James Kuszmaul7d1ef442019-03-23 20:20:50 -070051 // Which target this is within a given field quadrant:
52 enum class TargetType {
53 kHPSlot,
54 kFaceCargoBay,
55 kNearSideCargoBay,
56 kMidSideCargoBay,
57 kFarSideCargoBay,
58 kNearRocket,
59 kFarRocket,
60 kRocketPortal,
61 };
James Kuszmaule093f512019-03-20 06:14:05 -070062 TypedTarget(const Pose &pose, double radius = 0,
James Kuszmaul7d1ef442019-03-23 20:20:50 -070063 TargetType target_type = TargetType::kHPSlot,
James Kuszmaule093f512019-03-20 06:14:05 -070064 GoalType goal_type = GoalType::kBoth)
James Kuszmaul7d1ef442019-03-23 20:20:50 -070065 : pose_(pose),
66 radius_(radius),
67 target_type_(target_type),
68 goal_type_(goal_type) {}
James Kuszmaul090563a2019-02-09 14:43:20 -080069 TypedTarget() {}
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080070 Pose pose() const { return pose_; }
James Kuszmaule093f512019-03-20 06:14:05 -070071 Pose *mutable_pose() { return &pose_; }
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080072
73 bool occluded() const { return occluded_; }
74 void set_occluded(bool occluded) { occluded_ = occluded; }
James Kuszmaule093f512019-03-20 06:14:05 -070075 double radius() const { return radius_; }
76 GoalType goal_type() const { return goal_type_; }
77 void set_goal_type(GoalType goal_type) { goal_type_ = goal_type; }
James Kuszmaul7d1ef442019-03-23 20:20:50 -070078 TargetType target_type() const { return target_type_; }
79 void set_target_type(TargetType target_type) { target_type_ = target_type; }
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080080
81 // Get a list of points for plotting. These points should be plotted on
82 // an x/y plane in the global frame with lines connecting the points.
83 // Essentially, this provides a Polygon that is a reasonable representation
84 // of a Target.
85 // This should not be called from real-time code, as it will probably
86 // dynamically allocate memory.
87 ::std::vector<Pose> PlotPoints() const {
88 // For the actual representation, we will use a triangle such that the
89 // base of the triangle corresponds to the surface the target is on.
90 // The third point is shown behind the target, so that the user can
91 // visually identify which side of the target is the front.
92 Pose base1(&pose_, {0, 0.125, 0}, 0);
93 Pose base2(&pose_, {0, -0.125, 0}, 0);
94 Pose behind(&pose_, {0.05, 0, 0}, 0);
95 // Include behind at the start and end to indicate that we want to draw
96 // a closed polygon.
97 return {behind, base1, base2, behind};
98 }
99
100 private:
101 Pose pose_;
102 bool occluded_ = false;
James Kuszmaule093f512019-03-20 06:14:05 -0700103 // The effective radius of the target--for placing discs, this should be the
104 // radius of the disc; for fetching discs and working with balls this should
105 // be near zero.
106 // TODO(james): We may actually want a non-zero (possibly negative?) number
107 // here for balls.
108 double radius_ = 0.0;
James Kuszmaul7d1ef442019-03-23 20:20:50 -0700109 TargetType target_type_ = TargetType::kHPSlot;
James Kuszmaule093f512019-03-20 06:14:05 -0700110 GoalType goal_type_ = GoalType::kBoth;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800111}; // class TypedTarget
112
113typedef TypedTarget<double> Target;
114
115// Represents a camera that can see targets and provide information about their
116// relative positions.
117// Note on coordinate systems:
118// -The camera's Pose shall be such that the camera frame's positive X-axis is
119// pointed straight out of the lens (as always, positive Z will be up; we
120// assume that all cameras mounted level, or compensated for such that this
121// code won't care).
122// -The origin of the camera shall be "at" the camera. For this code, I don't
123// think we care too much about the details of the camera model, so we can just
124// assume that it is an idealized pinhole camera with the pinhole being the
125// location of the camera.
126//
127// Template parameters:
128// -num_targets: The number of targets on the field, should be the same for
129// all the actual cameras on the robot (although it may change in tests).
130// -Scalar: The floating point type to use (double vs. float).
131// -num_obstacles: The number of obstacles on the field to account for; like
132// the number of targets, it should be consistent across actual cameras,
133// although for simulation we may add extra obstacles for testing.
134template <int num_targets, int num_obstacles, typename Scalar = double>
135class TypedCamera {
136 public:
137 typedef ::frc971::control_loops::TypedPose<Scalar> Pose;
138 typedef ::frc971::control_loops::TypedLineSegment<Scalar> LineSegment;
139
140 // TargetView contains the information associated with a sensor reading
141 // from the camera--the readings themselves and noise values, *from the
142 // Camera's persective* for each reading.
143 // Note that the noise terms are just accounting for the inaccuracy you
144 // expect to get due to visual noise, pixel-level resolution, etc. These
145 // do not account for the fact that, e.g., there is noise in the Pose of the
146 // robot which can translate into noise in the target reading.
147 // The noise terms are standard deviations, and so have units identical
148 // to that of the actual readings.
149 struct TargetView {
150 struct Reading {
151 // The heading as reported from the camera; zero = straight ahead,
152 // positive = target in the left half of the image.
153 Scalar heading; // radians
154 // The distance from the camera to the target.
155 Scalar distance; // meters
James Kuszmaul81df16a2019-03-03 17:17:34 -0800156 // Height of the target from the camera.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800157 Scalar height; // meters
James Kuszmaul289756f2019-03-05 21:52:10 -0800158 // The angle of the target relative to line between the camera and
159 // the center of the target.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800160 Scalar skew; // radians
161 };
162 Reading reading;
163 Reading noise;
164
165 // The target that this view corresponds to.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800166 const TypedTarget<Scalar> *target = nullptr;
James Kuszmaul090563a2019-02-09 14:43:20 -0800167 // The Pose the camera was at when viewing the target:
168 Pose camera_pose;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800169 };
170
171 // Important parameters for dealing with camera noise calculations.
172 // Ultimately, this should end up coming from the constants file.
173 struct NoiseParameters {
174 // The maximum distance from which we can see a target head-on (when the
175 // target is not head-on, we adjust for that).
176 Scalar max_viewable_distance; // meters
177
178 // All noises are standard deviations of the noise, assuming an ~normal
179 // distribution.
180
181 // Noise in the heading measurement, which should be constant regardless of
182 // other factors.
183 Scalar heading_noise; // radians
184 // Noise in the distance measurement when the target is 1m away and head-on
185 // to us. This is adjusted by assuming the noise is proportional to the
186 // apparent width of the target (because the target also has height, this
187 // may not be strictly correct).
188 // TODO(james): Is this a good model? It should be reasonable, but there
189 // may be more complexity somewhere.
190 Scalar nominal_distance_noise; // meters
191 // The noise in the skew measurement when the target is 1m away and head-on
192 // to us. Calculated in the same manner with the same caveats as the
193 // distance noise.
194 Scalar nominal_skew_noise; // radians
195 // Noise in the height measurement, same rules as for skew and distance.
196 // TODO(james): Figure out how much noise we will actually get in the
197 // height, since there will be extremely low resolution on it.
198 Scalar nominal_height_noise; // meters
199 };
200
James Kuszmaul09f564a2019-02-18 17:37:09 -0800201 // Provide a default constructor to make life easier.
202 TypedCamera() {}
203
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800204 // Creates a camera:
205 // pose: The Pose of the camera, relative to the robot at least transitively.
206 // fov: The field-of-view of the camera, in radians. Note that this is the
207 // *total* field-of-view in the horizontal plane (left-right), so the angle
208 // from the left edge of the image to the right edge.
209 // targets: The list of targets on the field that could be seen by the camera.
210 // obstacles: The list of known obstacles on the field.
211 TypedCamera(const Pose &pose, Scalar fov,
212 const NoiseParameters &noise_parameters,
213 const ::std::array<TypedTarget<Scalar>, num_targets> &targets,
214 const ::std::array<LineSegment, num_obstacles> &obstacles)
215 : pose_(pose),
216 fov_(fov),
217 noise_parameters_(noise_parameters),
218 targets_(targets),
219 obstacles_(obstacles) {}
220
221 // Returns a list of TargetViews for all the currently visible targets.
222 // These will contain ground-truth TargetViews, so the readings will be
223 // perfect; a pseudo-random number generator should be used to add noise
224 // separately for simulation.
225 ::aos::SizedArray<TargetView, num_targets> target_views() const {
226 ::aos::SizedArray<TargetView, num_targets> views;
James Kuszmaul090563a2019-02-09 14:43:20 -0800227 Pose camera_abs_pose = pose_.Rebase(nullptr);
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800228 // Because there are num_targets in targets_ and because AddTargetIfVisible
229 // adds at most 1 view to views, we should never exceed the size of
230 // SizedArray.
231 for (const auto &target : targets_) {
James Kuszmaul090563a2019-02-09 14:43:20 -0800232 AddTargetIfVisible(target, camera_abs_pose, &views);
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800233 }
234 return views;
235 }
236
237 // Returns a list of list of points for plotting. Each list of points should
238 // be plotted as a line; currently, each list is just a pair drawing a line
239 // from the camera aperture to the target location.
240 // This should not be called from real-time code, as it will probably
241 // dynamically allocate memory.
242 ::std::vector<::std::vector<Pose>> PlotPoints() const {
243 ::std::vector<::std::vector<Pose>> list_of_lists;
244 for (const auto &view : target_views()) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800245 list_of_lists.push_back({pose_, view.target->pose().Rebase(&pose_)});
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800246 }
247 return list_of_lists;
248 }
249
James Kuszmaul090563a2019-02-09 14:43:20 -0800250 const Pose &pose() const { return pose_; }
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800251 Scalar fov() const { return fov_; }
252
James Kuszmaul09f564a2019-02-18 17:37:09 -0800253 // Estimates the noise values of a target based on the raw readings.
254 // Also estimates whether we would expect the target to be visible, and
255 // populates is_visible if is_visible is not nullptr.
256 void PopulateNoise(TargetView *view, bool *is_visible = nullptr) const {
257 // Calculate the width of the target as it appears in the image.
258 // This number is unitless and if greater than 1, implies that the target is
259 // visible to the camera and if less than 1 implies it is too small to be
260 // registered on the camera.
James Kuszmaul6f941b72019-03-08 18:12:25 -0800261 const Scalar cosskew = ::std::cos(view->reading.skew);
262 Scalar apparent_width = ::std::max<Scalar>(
263 0.0, cosskew * noise_parameters_.max_viewable_distance /
264 view->reading.distance);
265 // If we got wildly invalid distance or skew measurements, then set a very
266 // small apparent width.
267 if (view->reading.distance < 0 || cosskew < 0) {
268 apparent_width = 0.01;
269 }
James Kuszmaul09f564a2019-02-18 17:37:09 -0800270 // As both a sanity check and for the sake of numerical stability, manually
James Kuszmaul6f941b72019-03-08 18:12:25 -0800271 // set apparent_width to something "very small" if it is near zero.
272 if (apparent_width < 0.01) {
James Kuszmaul09f564a2019-02-18 17:37:09 -0800273 apparent_width = 0.01;
274 }
275
276 if (is_visible != nullptr) {
277 *is_visible = apparent_width >= 1.0;
278 }
279
280 view->noise.heading = noise_parameters_.heading_noise;
281
282 const Scalar normalized_width =
283 apparent_width / noise_parameters_.max_viewable_distance;
284 view->noise.distance =
285 noise_parameters_.nominal_distance_noise / normalized_width;
286 view->noise.skew =
287 noise_parameters_.nominal_skew_noise / normalized_width;
288 view->noise.height =
289 noise_parameters_.nominal_height_noise / normalized_width;
290 }
291
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800292 private:
293
294 // If the specified target is visible from the current camera Pose, adds it to
295 // the views array.
296 void AddTargetIfVisible(
James Kuszmaul090563a2019-02-09 14:43:20 -0800297 const TypedTarget<Scalar> &target, const Pose &camera_abs_pose,
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800298 ::aos::SizedArray<TargetView, num_targets> *views) const;
299
300 // The Pose of this camera.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800301 Pose pose_;
James Kuszmaul090563a2019-02-09 14:43:20 -0800302
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800303 // Field of view of the camera, in radians.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800304 Scalar fov_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800305
306 // Various constants to calclate sensor noise; see definition of
307 // NoiseParameters for more detail.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800308 NoiseParameters noise_parameters_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800309
310 // A list of all the targets on the field.
James Kuszmaul090563a2019-02-09 14:43:20 -0800311 // TODO(james): Is it worth creating some sort of cache for the targets and
312 // obstacles? e.g., passing around pointer to the targets/obstacles.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800313 ::std::array<TypedTarget<Scalar>, num_targets> targets_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800314 // Known obstacles on the field which can interfere with our view of the
315 // targets. An "obstacle" is a line segment which we cannot see through, as
316 // such a logical obstacle (e.g., the cargo ship) may consist of many
317 // obstacles in this list to account for all of its sides.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800318 ::std::array<LineSegment, num_obstacles> obstacles_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800319}; // class TypedCamera
320
321template <int num_targets, int num_obstacles, typename Scalar>
322void TypedCamera<num_targets, num_obstacles, Scalar>::AddTargetIfVisible(
James Kuszmaul090563a2019-02-09 14:43:20 -0800323 const TypedTarget<Scalar> &target, const Pose &camera_abs_pose,
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800324 ::aos::SizedArray<TargetView, num_targets> *views) const {
325 if (target.occluded()) {
326 return;
327 }
328
329 // Precompute the current absolute pose of the camera, because we will reuse
330 // it a bunch.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800331 const Pose relative_pose = target.pose().Rebase(&camera_abs_pose);
332 const Scalar heading = relative_pose.heading();
333 const Scalar distance = relative_pose.xy_norm();
James Kuszmaul289756f2019-03-05 21:52:10 -0800334 const Scalar skew =
335 ::aos::math::NormalizeAngle(relative_pose.rel_theta() - heading);
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800336
337 // Check if the camera is in the angular FOV.
338 if (::std::abs(heading) > fov_ / 2.0) {
339 return;
340 }
341
James Kuszmaul09f564a2019-02-18 17:37:09 -0800342 TargetView view;
343 view.reading.heading = heading;
344 view.reading.distance = distance;
345 view.reading.skew = skew;
James Kuszmaul81df16a2019-03-03 17:17:34 -0800346 view.reading.height = relative_pose.rel_pos().z();
James Kuszmaul09f564a2019-02-18 17:37:09 -0800347 view.target = &target;
348 view.camera_pose = camera_abs_pose;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800349
James Kuszmaul09f564a2019-02-18 17:37:09 -0800350 bool is_visible = false;
351
352 PopulateNoise(&view, &is_visible);
353
354 if (!is_visible) {
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800355 return;
356 }
357
358 // Final visibility check is for whether there are any obstacles blocking or
359 // line of sight.
360 for (const auto &obstacle : obstacles_) {
361 if (obstacle.Intersects({camera_abs_pose, target.pose()})) {
362 return;
363 }
364 }
365
James Kuszmaul09f564a2019-02-18 17:37:09 -0800366 // At this point, we've passed all the checks to ensure that the target is
367 // visible and we can add it to the list of targets.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800368 views->push_back(view);
369}
370
371} // namespace control_loops
James Kuszmaulf4ede202020-02-14 08:47:40 -0800372} // namespace frc971
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800373
374#endif // Y2019_CONTROL_LOOPS_DRIVETRAIN_CAMERA_H_