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