Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_TARGET_MAPPER_H_ |
| 2 | #define FRC971_VISION_TARGET_MAPPER_H_ |
| 3 | |
| 4 | #include <unordered_map> |
| 5 | |
| 6 | #include "aos/events/simulated_event_loop.h" |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 7 | #include "ceres/ceres.h" |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 8 | #include "frc971/vision/ceres/types.h" |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 9 | #include "frc971/vision/target_map_generated.h" |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 10 | |
| 11 | namespace frc971::vision { |
| 12 | |
| 13 | // Estimates positions of vision targets (ex. April Tags) using |
| 14 | // target detections relative to a robot (which were computed using robot |
| 15 | // positions at the time of those detections). Solves SLAM problem to estimate |
| 16 | // target locations using deltas between consecutive target detections. |
| 17 | class TargetMapper { |
| 18 | public: |
| 19 | using TargetId = int; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 20 | using ConfidenceMatrix = Eigen::Matrix<double, 6, 6>; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 21 | |
| 22 | struct TargetPose { |
| 23 | TargetId id; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 24 | ceres::examples::Pose3d pose; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 27 | // target_poses_path is the path to a TargetMap json with initial guesses for |
| 28 | // the actual locations of the targets on the field. |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 29 | // target_constraints are the deltas between consecutive target detections, |
| 30 | // and are usually prepared by the DataAdapter class below. |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 31 | TargetMapper(std::string_view target_poses_path, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 32 | const ceres::examples::VectorOfConstraints &target_constraints); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 33 | // Alternate constructor for tests. |
| 34 | // Takes in the actual intial guesses instead of a file containing them |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 35 | TargetMapper(const ceres::examples::MapOfPoses &target_poses, |
| 36 | const ceres::examples::VectorOfConstraints &target_constraints); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 37 | |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 38 | // Solves for the target map. If output_dir is set, the map will be saved to |
| 39 | // output_dir/field_name.json |
| 40 | void Solve(std::string_view field_name, |
| 41 | std::optional<std::string_view> output_dir = std::nullopt); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 42 | |
| 43 | // Prints target poses into a TargetMap flatbuffer json |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 44 | std::string MapToJson(std::string_view field_name) const; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 45 | |
| 46 | static std::optional<TargetPose> GetTargetPoseById( |
| 47 | std::vector<TargetPose> target_poses, TargetId target_id); |
| 48 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame^] | 49 | // Version that gets based on internal target_poses |
| 50 | std::optional<TargetPose> GetTargetPoseById(TargetId target_id); |
| 51 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 52 | ceres::examples::MapOfPoses target_poses() { return target_poses_; } |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 55 | // Constructs the nonlinear least squares optimization problem from the |
| 56 | // pose graph constraints. |
| 57 | void BuildOptimizationProblem( |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 58 | const ceres::examples::VectorOfConstraints &constraints, |
| 59 | ceres::examples::MapOfPoses *poses, ceres::Problem *problem); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 60 | |
| 61 | // Returns true if the solve was successful. |
| 62 | bool SolveOptimizationProblem(ceres::Problem *problem); |
| 63 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 64 | ceres::examples::MapOfPoses target_poses_; |
| 65 | ceres::examples::VectorOfConstraints target_constraints_; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 68 | // Utility functions for dealing with ceres::examples::Pose3d structs |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 69 | class PoseUtils { |
| 70 | public: |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 71 | // Embeds a 3d pose into an affine transformation |
| 72 | static Eigen::Affine3d Pose3dToAffine3d( |
| 73 | const ceres::examples::Pose3d &pose3d); |
| 74 | // Inverse of above function |
| 75 | static ceres::examples::Pose3d Affine3dToPose3d(const Eigen::Affine3d &H); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 76 | |
| 77 | // Computes pose_2 relative to pose_1. This is equivalent to (pose_1^-1 * |
| 78 | // pose_2) |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 79 | static ceres::examples::Pose3d ComputeRelativePose( |
| 80 | const ceres::examples::Pose3d &pose_1, |
| 81 | const ceres::examples::Pose3d &pose_2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 82 | |
| 83 | // Computes pose_2 given a pose_1 and pose_2 relative to pose_1. This is |
| 84 | // equivalent to (pose_1 * pose_2_relative) |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 85 | static ceres::examples::Pose3d ComputeOffsetPose( |
| 86 | const ceres::examples::Pose3d &pose_1, |
| 87 | const ceres::examples::Pose3d &pose_2_relative); |
| 88 | |
| 89 | // Converts a rotation with roll, pitch, and yaw into a quaternion |
| 90 | static Eigen::Quaterniond EulerAnglesToQuaternion(const Eigen::Vector3d &rpy); |
| 91 | // Inverse of above function |
| 92 | static Eigen::Vector3d QuaternionToEulerAngles(const Eigen::Quaterniond &q); |
| 93 | // Converts a 3d rotation matrix into a rotation with roll, pitch, and yaw |
| 94 | static Eigen::Vector3d RotationMatrixToEulerAngles(const Eigen::Matrix3d &R); |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 95 | |
| 96 | // Builds a TargetPoseFbs from a TargetPose |
| 97 | static flatbuffers::Offset<TargetPoseFbs> TargetPoseToFbs( |
| 98 | const TargetMapper::TargetPose &target_pose, |
| 99 | flatbuffers::FlatBufferBuilder *fbb); |
| 100 | // Converts a TargetPoseFbs to a TargetPose |
| 101 | static TargetMapper::TargetPose TargetPoseFromFbs( |
| 102 | const TargetPoseFbs &target_pose_fbs); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | // Transforms robot position and target detection data into target constraints |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 106 | // to be used for mapping. |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 107 | class DataAdapter { |
| 108 | public: |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 109 | // Pairs target detection with a time point |
| 110 | struct TimestampedDetection { |
| 111 | aos::distributed_clock::time_point time; |
| 112 | // Pose of target relative to robot |
| 113 | Eigen::Affine3d H_robot_target; |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 114 | // Horizontal distance from camera to target, used for confidence |
| 115 | // calculation |
| 116 | double distance_from_camera; |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 117 | // A measure of how much distortion affected this detection from 0-1. |
| 118 | double distortion_factor; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 119 | TargetMapper::TargetId id; |
| 120 | }; |
| 121 | |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 122 | // Pairs consecutive target detections that are not too far apart in time into |
| 123 | // constraints. Meant to be used on a system without a position measurement. |
| 124 | // Assumes timestamped_target_detections is in chronological order. |
| 125 | // max_dt is the maximum time between two target detections to match them up. |
| 126 | // If too much time passes, the recoding device (box of pis) could have moved |
| 127 | // too much |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 128 | static ceres::examples::VectorOfConstraints MatchTargetDetections( |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 129 | const std::vector<TimestampedDetection> ×tamped_target_detections, |
milind-u | 8791bed | 2023-03-04 14:45:52 -0800 | [diff] [blame] | 130 | aos::distributed_clock::duration max_dt = std::chrono::milliseconds(10)); |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 131 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 132 | // Computes inverse of covariance matrix, assuming there was a target |
| 133 | // detection between robot movement over the given time period. Ceres calls |
| 134 | // this matrix the "information" |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 135 | static TargetMapper::ConfidenceMatrix ComputeConfidence( |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 136 | const TimestampedDetection &detection_start, |
| 137 | const TimestampedDetection &detection_end); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 138 | |
| 139 | private: |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 140 | // Computes the constraint between the start and end pose of the targets: the |
| 141 | // relative pose between the start and end target locations in the frame of |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 142 | // the start target. |
| 143 | static ceres::examples::Constraint3d ComputeTargetConstraint( |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 144 | const TimestampedDetection &target_detection_start, |
| 145 | const TimestampedDetection &target_detection_end, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 146 | const TargetMapper::ConfidenceMatrix &confidence); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 149 | std::ostream &operator<<(std::ostream &os, ceres::examples::Pose3d pose); |
| 150 | std::ostream &operator<<(std::ostream &os, |
| 151 | ceres::examples::Constraint3d constraint); |
| 152 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 153 | } // namespace frc971::vision |
| 154 | |
| 155 | #endif // FRC971_VISION_TARGET_MAPPER_H_ |