blob: 73079c940b04ad4f46c8487516a6093d70175f57 [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
9namespace y2019 {
10namespace 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 };
51 TypedTarget(const Pose &pose, double radius = 0,
52 GoalType goal_type = GoalType::kBoth)
53 : pose_(pose), radius_(radius), goal_type_(goal_type) {}
James Kuszmaul090563a2019-02-09 14:43:20 -080054 TypedTarget() {}
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080055 Pose pose() const { return pose_; }
James Kuszmaule093f512019-03-20 06:14:05 -070056 Pose *mutable_pose() { return &pose_; }
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080057
58 bool occluded() const { return occluded_; }
59 void set_occluded(bool occluded) { occluded_ = occluded; }
James Kuszmaule093f512019-03-20 06:14:05 -070060 double radius() const { return radius_; }
61 GoalType goal_type() const { return goal_type_; }
62 void set_goal_type(GoalType goal_type) { goal_type_ = goal_type; }
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080063
64 // Get a list of points for plotting. These points should be plotted on
65 // an x/y plane in the global frame with lines connecting the points.
66 // Essentially, this provides a Polygon that is a reasonable representation
67 // of a Target.
68 // This should not be called from real-time code, as it will probably
69 // dynamically allocate memory.
70 ::std::vector<Pose> PlotPoints() const {
71 // For the actual representation, we will use a triangle such that the
72 // base of the triangle corresponds to the surface the target is on.
73 // The third point is shown behind the target, so that the user can
74 // visually identify which side of the target is the front.
75 Pose base1(&pose_, {0, 0.125, 0}, 0);
76 Pose base2(&pose_, {0, -0.125, 0}, 0);
77 Pose behind(&pose_, {0.05, 0, 0}, 0);
78 // Include behind at the start and end to indicate that we want to draw
79 // a closed polygon.
80 return {behind, base1, base2, behind};
81 }
82
83 private:
84 Pose pose_;
85 bool occluded_ = false;
James Kuszmaule093f512019-03-20 06:14:05 -070086 // The effective radius of the target--for placing discs, this should be the
87 // radius of the disc; for fetching discs and working with balls this should
88 // be near zero.
89 // TODO(james): We may actually want a non-zero (possibly negative?) number
90 // here for balls.
91 double radius_ = 0.0;
92 GoalType goal_type_ = GoalType::kBoth;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -080093}; // class TypedTarget
94
95typedef TypedTarget<double> Target;
96
97// Represents a camera that can see targets and provide information about their
98// relative positions.
99// Note on coordinate systems:
100// -The camera's Pose shall be such that the camera frame's positive X-axis is
101// pointed straight out of the lens (as always, positive Z will be up; we
102// assume that all cameras mounted level, or compensated for such that this
103// code won't care).
104// -The origin of the camera shall be "at" the camera. For this code, I don't
105// think we care too much about the details of the camera model, so we can just
106// assume that it is an idealized pinhole camera with the pinhole being the
107// location of the camera.
108//
109// Template parameters:
110// -num_targets: The number of targets on the field, should be the same for
111// all the actual cameras on the robot (although it may change in tests).
112// -Scalar: The floating point type to use (double vs. float).
113// -num_obstacles: The number of obstacles on the field to account for; like
114// the number of targets, it should be consistent across actual cameras,
115// although for simulation we may add extra obstacles for testing.
116template <int num_targets, int num_obstacles, typename Scalar = double>
117class TypedCamera {
118 public:
119 typedef ::frc971::control_loops::TypedPose<Scalar> Pose;
120 typedef ::frc971::control_loops::TypedLineSegment<Scalar> LineSegment;
121
122 // TargetView contains the information associated with a sensor reading
123 // from the camera--the readings themselves and noise values, *from the
124 // Camera's persective* for each reading.
125 // Note that the noise terms are just accounting for the inaccuracy you
126 // expect to get due to visual noise, pixel-level resolution, etc. These
127 // do not account for the fact that, e.g., there is noise in the Pose of the
128 // robot which can translate into noise in the target reading.
129 // The noise terms are standard deviations, and so have units identical
130 // to that of the actual readings.
131 struct TargetView {
132 struct Reading {
133 // The heading as reported from the camera; zero = straight ahead,
134 // positive = target in the left half of the image.
135 Scalar heading; // radians
136 // The distance from the camera to the target.
137 Scalar distance; // meters
James Kuszmaul81df16a2019-03-03 17:17:34 -0800138 // Height of the target from the camera.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800139 Scalar height; // meters
James Kuszmaul289756f2019-03-05 21:52:10 -0800140 // The angle of the target relative to line between the camera and
141 // the center of the target.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800142 Scalar skew; // radians
143 };
144 Reading reading;
145 Reading noise;
146
147 // The target that this view corresponds to.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800148 const TypedTarget<Scalar> *target = nullptr;
James Kuszmaul090563a2019-02-09 14:43:20 -0800149 // The Pose the camera was at when viewing the target:
150 Pose camera_pose;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800151 };
152
153 // Important parameters for dealing with camera noise calculations.
154 // Ultimately, this should end up coming from the constants file.
155 struct NoiseParameters {
156 // The maximum distance from which we can see a target head-on (when the
157 // target is not head-on, we adjust for that).
158 Scalar max_viewable_distance; // meters
159
160 // All noises are standard deviations of the noise, assuming an ~normal
161 // distribution.
162
163 // Noise in the heading measurement, which should be constant regardless of
164 // other factors.
165 Scalar heading_noise; // radians
166 // Noise in the distance measurement when the target is 1m away and head-on
167 // to us. This is adjusted by assuming the noise is proportional to the
168 // apparent width of the target (because the target also has height, this
169 // may not be strictly correct).
170 // TODO(james): Is this a good model? It should be reasonable, but there
171 // may be more complexity somewhere.
172 Scalar nominal_distance_noise; // meters
173 // The noise in the skew measurement when the target is 1m away and head-on
174 // to us. Calculated in the same manner with the same caveats as the
175 // distance noise.
176 Scalar nominal_skew_noise; // radians
177 // Noise in the height measurement, same rules as for skew and distance.
178 // TODO(james): Figure out how much noise we will actually get in the
179 // height, since there will be extremely low resolution on it.
180 Scalar nominal_height_noise; // meters
181 };
182
James Kuszmaul09f564a2019-02-18 17:37:09 -0800183 // Provide a default constructor to make life easier.
184 TypedCamera() {}
185
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800186 // Creates a camera:
187 // pose: The Pose of the camera, relative to the robot at least transitively.
188 // fov: The field-of-view of the camera, in radians. Note that this is the
189 // *total* field-of-view in the horizontal plane (left-right), so the angle
190 // from the left edge of the image to the right edge.
191 // targets: The list of targets on the field that could be seen by the camera.
192 // obstacles: The list of known obstacles on the field.
193 TypedCamera(const Pose &pose, Scalar fov,
194 const NoiseParameters &noise_parameters,
195 const ::std::array<TypedTarget<Scalar>, num_targets> &targets,
196 const ::std::array<LineSegment, num_obstacles> &obstacles)
197 : pose_(pose),
198 fov_(fov),
199 noise_parameters_(noise_parameters),
200 targets_(targets),
201 obstacles_(obstacles) {}
202
203 // Returns a list of TargetViews for all the currently visible targets.
204 // These will contain ground-truth TargetViews, so the readings will be
205 // perfect; a pseudo-random number generator should be used to add noise
206 // separately for simulation.
207 ::aos::SizedArray<TargetView, num_targets> target_views() const {
208 ::aos::SizedArray<TargetView, num_targets> views;
James Kuszmaul090563a2019-02-09 14:43:20 -0800209 Pose camera_abs_pose = pose_.Rebase(nullptr);
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800210 // Because there are num_targets in targets_ and because AddTargetIfVisible
211 // adds at most 1 view to views, we should never exceed the size of
212 // SizedArray.
213 for (const auto &target : targets_) {
James Kuszmaul090563a2019-02-09 14:43:20 -0800214 AddTargetIfVisible(target, camera_abs_pose, &views);
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800215 }
216 return views;
217 }
218
219 // Returns a list of list of points for plotting. Each list of points should
220 // be plotted as a line; currently, each list is just a pair drawing a line
221 // from the camera aperture to the target location.
222 // This should not be called from real-time code, as it will probably
223 // dynamically allocate memory.
224 ::std::vector<::std::vector<Pose>> PlotPoints() const {
225 ::std::vector<::std::vector<Pose>> list_of_lists;
226 for (const auto &view : target_views()) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800227 list_of_lists.push_back({pose_, view.target->pose().Rebase(&pose_)});
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800228 }
229 return list_of_lists;
230 }
231
James Kuszmaul090563a2019-02-09 14:43:20 -0800232 const Pose &pose() const { return pose_; }
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800233 Scalar fov() const { return fov_; }
234
James Kuszmaul09f564a2019-02-18 17:37:09 -0800235 // Estimates the noise values of a target based on the raw readings.
236 // Also estimates whether we would expect the target to be visible, and
237 // populates is_visible if is_visible is not nullptr.
238 void PopulateNoise(TargetView *view, bool *is_visible = nullptr) const {
239 // Calculate the width of the target as it appears in the image.
240 // This number is unitless and if greater than 1, implies that the target is
241 // visible to the camera and if less than 1 implies it is too small to be
242 // registered on the camera.
James Kuszmaul6f941b72019-03-08 18:12:25 -0800243 const Scalar cosskew = ::std::cos(view->reading.skew);
244 Scalar apparent_width = ::std::max<Scalar>(
245 0.0, cosskew * noise_parameters_.max_viewable_distance /
246 view->reading.distance);
247 // If we got wildly invalid distance or skew measurements, then set a very
248 // small apparent width.
249 if (view->reading.distance < 0 || cosskew < 0) {
250 apparent_width = 0.01;
251 }
James Kuszmaul09f564a2019-02-18 17:37:09 -0800252 // As both a sanity check and for the sake of numerical stability, manually
James Kuszmaul6f941b72019-03-08 18:12:25 -0800253 // set apparent_width to something "very small" if it is near zero.
254 if (apparent_width < 0.01) {
James Kuszmaul09f564a2019-02-18 17:37:09 -0800255 apparent_width = 0.01;
256 }
257
258 if (is_visible != nullptr) {
259 *is_visible = apparent_width >= 1.0;
260 }
261
262 view->noise.heading = noise_parameters_.heading_noise;
263
264 const Scalar normalized_width =
265 apparent_width / noise_parameters_.max_viewable_distance;
266 view->noise.distance =
267 noise_parameters_.nominal_distance_noise / normalized_width;
268 view->noise.skew =
269 noise_parameters_.nominal_skew_noise / normalized_width;
270 view->noise.height =
271 noise_parameters_.nominal_height_noise / normalized_width;
272 }
273
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800274 private:
275
276 // If the specified target is visible from the current camera Pose, adds it to
277 // the views array.
278 void AddTargetIfVisible(
James Kuszmaul090563a2019-02-09 14:43:20 -0800279 const TypedTarget<Scalar> &target, const Pose &camera_abs_pose,
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800280 ::aos::SizedArray<TargetView, num_targets> *views) const;
281
282 // The Pose of this camera.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800283 Pose pose_;
James Kuszmaul090563a2019-02-09 14:43:20 -0800284
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800285 // Field of view of the camera, in radians.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800286 Scalar fov_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800287
288 // Various constants to calclate sensor noise; see definition of
289 // NoiseParameters for more detail.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800290 NoiseParameters noise_parameters_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800291
292 // A list of all the targets on the field.
James Kuszmaul090563a2019-02-09 14:43:20 -0800293 // TODO(james): Is it worth creating some sort of cache for the targets and
294 // obstacles? e.g., passing around pointer to the targets/obstacles.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800295 ::std::array<TypedTarget<Scalar>, num_targets> targets_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800296 // Known obstacles on the field which can interfere with our view of the
297 // targets. An "obstacle" is a line segment which we cannot see through, as
298 // such a logical obstacle (e.g., the cargo ship) may consist of many
299 // obstacles in this list to account for all of its sides.
James Kuszmaul09f564a2019-02-18 17:37:09 -0800300 ::std::array<LineSegment, num_obstacles> obstacles_;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800301}; // class TypedCamera
302
303template <int num_targets, int num_obstacles, typename Scalar>
304void TypedCamera<num_targets, num_obstacles, Scalar>::AddTargetIfVisible(
James Kuszmaul090563a2019-02-09 14:43:20 -0800305 const TypedTarget<Scalar> &target, const Pose &camera_abs_pose,
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800306 ::aos::SizedArray<TargetView, num_targets> *views) const {
307 if (target.occluded()) {
308 return;
309 }
310
311 // Precompute the current absolute pose of the camera, because we will reuse
312 // it a bunch.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800313 const Pose relative_pose = target.pose().Rebase(&camera_abs_pose);
314 const Scalar heading = relative_pose.heading();
315 const Scalar distance = relative_pose.xy_norm();
James Kuszmaul289756f2019-03-05 21:52:10 -0800316 const Scalar skew =
317 ::aos::math::NormalizeAngle(relative_pose.rel_theta() - heading);
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800318
319 // Check if the camera is in the angular FOV.
320 if (::std::abs(heading) > fov_ / 2.0) {
321 return;
322 }
323
James Kuszmaul09f564a2019-02-18 17:37:09 -0800324 TargetView view;
325 view.reading.heading = heading;
326 view.reading.distance = distance;
327 view.reading.skew = skew;
James Kuszmaul81df16a2019-03-03 17:17:34 -0800328 view.reading.height = relative_pose.rel_pos().z();
James Kuszmaul09f564a2019-02-18 17:37:09 -0800329 view.target = &target;
330 view.camera_pose = camera_abs_pose;
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800331
James Kuszmaul09f564a2019-02-18 17:37:09 -0800332 bool is_visible = false;
333
334 PopulateNoise(&view, &is_visible);
335
336 if (!is_visible) {
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800337 return;
338 }
339
340 // Final visibility check is for whether there are any obstacles blocking or
341 // line of sight.
342 for (const auto &obstacle : obstacles_) {
343 if (obstacle.Intersects({camera_abs_pose, target.pose()})) {
344 return;
345 }
346 }
347
James Kuszmaul09f564a2019-02-18 17:37:09 -0800348 // At this point, we've passed all the checks to ensure that the target is
349 // visible and we can add it to the list of targets.
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800350 views->push_back(view);
351}
352
353} // namespace control_loops
354} // namespace y2019
355
356#endif // Y2019_CONTROL_LOOPS_DRIVETRAIN_CAMERA_H_