blob: 4303b24961d034340e10fea53975586cb15da178 [file] [log] [blame]
Milind Upadhyayf61e1482022-02-11 20:42:55 -08001#ifndef Y2022_VISION_TARGET_ESTIMATOR_H_
2#define Y2022_VISION_TARGET_ESTIMATOR_H_
milind-u92195982022-01-22 20:29:31 -08003
Milind Upadhyayf61e1482022-02-11 20:42:55 -08004#include <optional>
5
6#include "Eigen/Dense"
7#include "Eigen/Geometry"
8#include "opencv2/core/types.hpp"
milind-u92195982022-01-22 20:29:31 -08009#include "opencv2/imgproc.hpp"
10#include "y2022/vision/target_estimate_generated.h"
11
12namespace y2022::vision {
Milind Upadhyayf61e1482022-02-11 20:42:55 -080013// Class to estimate the polar coordinates and rotation from the camera to the
14// target.
milind-u92195982022-01-22 20:29:31 -080015class TargetEstimator {
16 public:
Milind Upadhyayf61e1482022-02-11 20:42:55 -080017 TargetEstimator(cv::Mat intrinsics, cv::Mat extrinsics);
18
19 // Runs the solver to estimate the target
20 // centroids must be in sorted order from left to right on the circle.
21 // TODO(milind): seems safer to sort them here.
22 // If image != std::nullopt, the solver's progress will be displayed
23 // graphically.
24 void Solve(const std::vector<cv::Point> &centroids,
25 std::optional<cv::Mat> image);
26
27 // Cost function for the solver.
28 // Takes in the rotation of the camera in the hub's frame, the horizontal
29 // polar coordinates of the camera in the hub's frame, and the height of the
30 // camera (can change if the robot is shaking).
31 // Hub frame is relative to the center of the bottom of the hub.
32 // Compares the projected pieces of tape with these values to the detected
33 // blobs for calculating the cost.
34 template <typename S>
35 bool operator()(const S *const roll, const S *const pitch, const S *const yaw,
36 const S *const distance, const S *const theta,
37 const S *const camera_height, S *residual) const;
38
39 inline double roll() const { return roll_; }
40 inline double pitch() const { return pitch_; }
41 inline double yaw() const { return yaw_; }
42
43 inline double distance() const { return distance_; }
44 inline double angle_to_camera() const { return angle_to_camera_; }
45 inline double angle_to_target() const { return M_PI - yaw_; }
46 inline double camera_height() const { return camera_height_; }
47
48 // Draws the distance, angle, and rotation on the given image
49 static void DrawEstimate(const TargetEstimate &target_estimate,
50 cv::Mat view_image);
51 void DrawEstimate(cv::Mat view_image) const;
milind-u92195982022-01-22 20:29:31 -080052
53 private:
54 // Height of the center of the tape (m)
55 static constexpr double kTapeHeight = 2.58 + (0.05 / 2);
56 // Horizontal distance from tape to center of hub (m)
57 static constexpr double kUpperHubRadius = 1.22 / 2;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080058 static constexpr size_t kNumPiecesOfTape = 16;
59
60 // 3d points of the visible pieces of tape in the hub's frame
61 static const std::vector<cv::Point3d> kTapePoints;
62 static std::vector<cv::Point3d> ComputeTapePoints();
63
64 template <typename S>
65 cv::Point_<S> DistanceFromTape(
66 size_t centroid_index,
67 const std::vector<cv::Point_<S>> &tape_points) const;
68
69 std::vector<cv::Point> centroids_;
70 std::optional<cv::Mat> image_;
71
72 Eigen::Matrix3d intrinsics_;
73 Eigen::Matrix4d extrinsics_;
74
75 double roll_;
76 double pitch_;
77 double yaw_;
78
79 double distance_;
80 double angle_to_camera_;
81 double camera_height_;
milind-u92195982022-01-22 20:29:31 -080082};
83
84} // namespace y2022::vision
85
Milind Upadhyayf61e1482022-02-11 20:42:55 -080086#endif // Y2022_VISION_TARGET_ESTIMATOR_H_