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 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 6 | #include "absl/strings/str_format.h" |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 7 | #include "ceres/ceres.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | |
| 9 | #include "aos/events/simulated_event_loop.h" |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 10 | #include "frc971/vision/ceres/types.h" |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 11 | #include "frc971/vision/target_map_generated.h" |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 12 | #include "frc971/vision/visualize_robot.h" |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 13 | |
| 14 | namespace frc971::vision { |
| 15 | |
| 16 | // Estimates positions of vision targets (ex. April Tags) using |
| 17 | // target detections relative to a robot (which were computed using robot |
| 18 | // positions at the time of those detections). Solves SLAM problem to estimate |
| 19 | // target locations using deltas between consecutive target detections. |
| 20 | class TargetMapper { |
| 21 | public: |
| 22 | using TargetId = int; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 23 | using ConfidenceMatrix = Eigen::Matrix<double, 6, 6>; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 24 | |
| 25 | struct TargetPose { |
| 26 | TargetId id; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 27 | ceres::examples::Pose3d pose; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 30 | // target_poses_path is the path to a TargetMap json with initial guesses for |
| 31 | // the actual locations of the targets on the field. |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 32 | // target_constraints are the deltas between consecutive target detections, |
| 33 | // and are usually prepared by the DataAdapter class below. |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 34 | TargetMapper(std::string_view target_poses_path, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 35 | const ceres::examples::VectorOfConstraints &target_constraints); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 36 | // Alternate constructor for tests. |
| 37 | // Takes in the actual intial guesses instead of a file containing them |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 38 | TargetMapper(const ceres::examples::MapOfPoses &target_poses, |
| 39 | const ceres::examples::VectorOfConstraints &target_constraints); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 40 | |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 41 | // Solves for the target map. If output_dir is set, the map will be saved to |
| 42 | // output_dir/field_name.json |
| 43 | void Solve(std::string_view field_name, |
| 44 | std::optional<std::string_view> output_dir = std::nullopt); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 45 | |
| 46 | // Prints target poses into a TargetMap flatbuffer json |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 47 | std::string MapToJson(std::string_view field_name) const; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 48 | |
| 49 | static std::optional<TargetPose> GetTargetPoseById( |
| 50 | std::vector<TargetPose> target_poses, TargetId target_id); |
| 51 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 52 | // Version that gets based on internal target_poses |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 53 | std::optional<TargetPose> GetTargetPoseById(TargetId target_id) const; |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 54 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 55 | ceres::examples::MapOfPoses target_poses() { return target_poses_; } |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 56 | |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 57 | // Cost function for the secondary solver finding out where the whole map fits |
| 58 | // in the world |
| 59 | template <typename S> |
| 60 | bool operator()(const S *const translation, const S *const rotation, |
| 61 | S *residual) const; |
| 62 | |
milind-u | 401de98 | 2023-04-14 17:32:03 -0700 | [diff] [blame] | 63 | void DumpConstraints(std::string_view path) const; |
| 64 | void DumpStats(std::string_view path) const; |
Jim Ostrowski | f41b094 | 2024-03-24 18:05:02 -0700 | [diff] [blame] | 65 | void PrintDiffs() const; |
milind-u | 401de98 | 2023-04-14 17:32:03 -0700 | [diff] [blame] | 66 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 67 | private: |
milind-u | 401de98 | 2023-04-14 17:32:03 -0700 | [diff] [blame] | 68 | // Error in an estimated pose |
| 69 | struct PoseError { |
| 70 | double angle; |
| 71 | double distance; |
| 72 | }; |
| 73 | |
| 74 | // Stores info on how much all the constraints differ from our solved target |
| 75 | // map |
| 76 | struct Stats { |
| 77 | // Average error for translation and rotation |
| 78 | PoseError avg_err; |
| 79 | // Standard deviation for translation and rotation error |
| 80 | PoseError std_dev; |
| 81 | // Maximum error for translation and rotation |
| 82 | PoseError max_err; |
| 83 | }; |
| 84 | |
| 85 | // Compute the error of a single constraint |
| 86 | PoseError ComputeError(const ceres::examples::Constraint3d &constraint) const; |
| 87 | // Compute cumulative stats for all constraints |
| 88 | Stats ComputeStats() const; |
| 89 | // Removes constraints with very large errors |
| 90 | void RemoveOutlierConstraints(); |
| 91 | |
milind-u | 526d567 | 2023-04-17 20:09:10 -0700 | [diff] [blame] | 92 | void CountConstraints(); |
| 93 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 94 | // Constructs the nonlinear least squares optimization problem from the |
| 95 | // pose graph constraints. |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 96 | void BuildTargetPoseOptimizationProblem( |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 97 | const ceres::examples::VectorOfConstraints &constraints, |
| 98 | ceres::examples::MapOfPoses *poses, ceres::Problem *problem); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 99 | |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 100 | // Constructs the nonlinear least squares optimization problem for the solved |
| 101 | // -> actual pose solver. |
milind-u | 401de98 | 2023-04-14 17:32:03 -0700 | [diff] [blame] | 102 | std::unique_ptr<ceres::CostFunction> BuildMapFittingOptimizationProblem( |
| 103 | ceres::Problem *problem); |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 104 | |
Jim Ostrowski | 4527dd7 | 2024-03-07 00:20:15 -0800 | [diff] [blame] | 105 | // Create and display a visualization of the graph connectivity of the |
| 106 | // constraints |
| 107 | void DisplayConstraintGraph(); |
| 108 | |
Jim Ostrowski | efc3bde | 2024-03-23 16:34:06 -0700 | [diff] [blame] | 109 | // Create and display a visualization of the map solution (vs. the input map) |
| 110 | void DisplaySolvedVsInitial(); |
| 111 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 112 | // Returns true if the solve was successful. |
| 113 | bool SolveOptimizationProblem(ceres::Problem *problem); |
| 114 | |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 115 | ceres::examples::MapOfPoses ideal_target_poses_; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 116 | ceres::examples::MapOfPoses target_poses_; |
| 117 | ceres::examples::VectorOfConstraints target_constraints_; |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 118 | |
milind-u | 526d567 | 2023-04-17 20:09:10 -0700 | [diff] [blame] | 119 | // Counts of each pair of target ids we observe, so we can scale cost based on |
| 120 | // the inverse of this and remove bias towards certain pairs |
| 121 | std::map<std::pair<TargetId, TargetId>, size_t> constraint_counts_; |
| 122 | |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 123 | // Transformation moving the target map we solved for to where it actually |
| 124 | // should be in the world |
| 125 | Eigen::Translation3d T_frozen_actual_; |
| 126 | Eigen::Quaterniond R_frozen_actual_; |
| 127 | |
Jim Ostrowski | 68e5617 | 2023-09-17 23:44:15 -0700 | [diff] [blame] | 128 | const double kFieldWidth_ = 20.0; // 20 meters across |
| 129 | const int kImageWidth_ = 1000; |
| 130 | const int kImageHeight_ = |
| 131 | kImageWidth_ * 3.0 / 4.0; // Roughly matches field aspect ratio |
milind-u | 8f4e43e | 2023-04-09 17:11:19 -0700 | [diff] [blame] | 132 | mutable VisualizeRobot vis_robot_; |
milind-u | 401de98 | 2023-04-14 17:32:03 -0700 | [diff] [blame] | 133 | |
| 134 | Stats stats_with_outliers_; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 137 | // Utility functions for dealing with ceres::examples::Pose3d structs |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 138 | class PoseUtils { |
| 139 | public: |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 140 | // Embeds a 3d pose into an affine transformation |
| 141 | static Eigen::Affine3d Pose3dToAffine3d( |
| 142 | const ceres::examples::Pose3d &pose3d); |
| 143 | // Inverse of above function |
| 144 | static ceres::examples::Pose3d Affine3dToPose3d(const Eigen::Affine3d &H); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 145 | |
| 146 | // Computes pose_2 relative to pose_1. This is equivalent to (pose_1^-1 * |
| 147 | // pose_2) |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 148 | static ceres::examples::Pose3d ComputeRelativePose( |
| 149 | const ceres::examples::Pose3d &pose_1, |
| 150 | const ceres::examples::Pose3d &pose_2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 151 | |
| 152 | // Computes pose_2 given a pose_1 and pose_2 relative to pose_1. This is |
| 153 | // equivalent to (pose_1 * pose_2_relative) |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 154 | static ceres::examples::Pose3d ComputeOffsetPose( |
| 155 | const ceres::examples::Pose3d &pose_1, |
| 156 | const ceres::examples::Pose3d &pose_2_relative); |
| 157 | |
| 158 | // Converts a rotation with roll, pitch, and yaw into a quaternion |
| 159 | static Eigen::Quaterniond EulerAnglesToQuaternion(const Eigen::Vector3d &rpy); |
| 160 | // Inverse of above function |
| 161 | static Eigen::Vector3d QuaternionToEulerAngles(const Eigen::Quaterniond &q); |
| 162 | // Converts a 3d rotation matrix into a rotation with roll, pitch, and yaw |
| 163 | static Eigen::Vector3d RotationMatrixToEulerAngles(const Eigen::Matrix3d &R); |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 164 | |
| 165 | // Builds a TargetPoseFbs from a TargetPose |
| 166 | static flatbuffers::Offset<TargetPoseFbs> TargetPoseToFbs( |
| 167 | const TargetMapper::TargetPose &target_pose, |
| 168 | flatbuffers::FlatBufferBuilder *fbb); |
| 169 | // Converts a TargetPoseFbs to a TargetPose |
| 170 | static TargetMapper::TargetPose TargetPoseFromFbs( |
| 171 | const TargetPoseFbs &target_pose_fbs); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | // Transforms robot position and target detection data into target constraints |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 175 | // to be used for mapping. |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 176 | class DataAdapter { |
| 177 | public: |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 178 | // Pairs target detection with a time point |
| 179 | struct TimestampedDetection { |
| 180 | aos::distributed_clock::time_point time; |
| 181 | // Pose of target relative to robot |
| 182 | Eigen::Affine3d H_robot_target; |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 183 | // Horizontal distance from camera to target, used for confidence |
| 184 | // calculation |
| 185 | double distance_from_camera; |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 186 | // A measure of how much distortion affected this detection from 0-1. |
| 187 | double distortion_factor; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 188 | TargetMapper::TargetId id; |
| 189 | }; |
| 190 | |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 191 | // Pairs consecutive target detections that are not too far apart in time into |
| 192 | // constraints. Meant to be used on a system without a position measurement. |
| 193 | // Assumes timestamped_target_detections is in chronological order. |
| 194 | // max_dt is the maximum time between two target detections to match them up. |
| 195 | // If too much time passes, the recoding device (box of pis) could have moved |
| 196 | // too much |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 197 | static ceres::examples::VectorOfConstraints MatchTargetDetections( |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 198 | const std::vector<TimestampedDetection> ×tamped_target_detections, |
milind-u | 8791bed | 2023-03-04 14:45:52 -0800 | [diff] [blame] | 199 | aos::distributed_clock::duration max_dt = std::chrono::milliseconds(10)); |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 200 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 201 | // Computes inverse of covariance matrix, assuming there was a target |
| 202 | // detection between robot movement over the given time period. Ceres calls |
| 203 | // this matrix the "information" |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 204 | static TargetMapper::ConfidenceMatrix ComputeConfidence( |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 205 | const TimestampedDetection &detection_start, |
| 206 | const TimestampedDetection &detection_end); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 207 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 208 | // Computes the constraint between the start and end pose of the targets: the |
| 209 | // 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] | 210 | // the start target. |
| 211 | static ceres::examples::Constraint3d ComputeTargetConstraint( |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 212 | const TimestampedDetection &target_detection_start, |
| 213 | const TimestampedDetection &target_detection_end, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 214 | const TargetMapper::ConfidenceMatrix &confidence); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
milind-u | fbc5c81 | 2023-04-06 21:24:29 -0700 | [diff] [blame] | 217 | } // namespace frc971::vision |
| 218 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 219 | namespace ceres::examples { |
| 220 | template <typename Sink> |
| 221 | void AbslStringify(Sink &sink, ceres::examples::Pose3d pose) { |
| 222 | auto rpy = frc971::vision::PoseUtils::QuaternionToEulerAngles(pose.q); |
| 223 | absl::Format(&sink, |
| 224 | "{x: %.3f, y: %.3f, z: %.3f, roll: %.3f, pitch: " |
| 225 | "%.3f, yaw: %.3f}", |
| 226 | pose.p(0), pose.p(1), pose.p(2), rpy(0), rpy(1), rpy(2)); |
| 227 | } |
| 228 | |
| 229 | template <typename Sink> |
| 230 | void AbslStringify(Sink &sink, ceres::examples::Constraint3d constraint) { |
| 231 | absl::Format(&sink, "{id_begin: %d, id_end: %d, pose: ", constraint.id_begin, |
| 232 | constraint.id_end); |
| 233 | AbslStringify(sink, constraint.t_be); |
| 234 | sink.Append("}"); |
| 235 | } |
| 236 | } // namespace ceres::examples |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 237 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 238 | #endif // FRC971_VISION_TARGET_MAPPER_H_ |