Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 1 | #include "frc971/vision/target_mapper.h" |
| 2 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 3 | #include "absl/strings/str_format.h" |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 4 | #include "frc971/control_loops/control_loop.h" |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 5 | #include "frc971/vision/ceres/pose_graph_3d_error_term.h" |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 6 | #include "frc971/vision/geometry.h" |
| 7 | |
| 8 | DEFINE_uint64(max_num_iterations, 100, |
| 9 | "Maximum number of iterations for the ceres solver"); |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 10 | DEFINE_double(distortion_noise_scalar, 1.0, |
| 11 | "Scale the target pose distortion factor by this when computing " |
| 12 | "the noise."); |
milind-u | 6ff399f | 2023-03-24 18:33:38 -0700 | [diff] [blame^] | 13 | DEFINE_uint64( |
| 14 | frozen_target_id, 1, |
| 15 | "Freeze the pose of this target so the map can have one fixed point."); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 16 | |
| 17 | namespace frc971::vision { |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 18 | Eigen::Affine3d PoseUtils::Pose3dToAffine3d( |
| 19 | const ceres::examples::Pose3d &pose3d) { |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 20 | Eigen::Affine3d H_world_pose = |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 21 | Eigen::Translation3d(pose3d.p(0), pose3d.p(1), pose3d.p(2)) * pose3d.q; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 22 | return H_world_pose; |
| 23 | } |
| 24 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 25 | ceres::examples::Pose3d PoseUtils::Affine3dToPose3d(const Eigen::Affine3d &H) { |
| 26 | return ceres::examples::Pose3d{.p = H.translation(), |
| 27 | .q = Eigen::Quaterniond(H.rotation())}; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 30 | ceres::examples::Pose3d PoseUtils::ComputeRelativePose( |
| 31 | const ceres::examples::Pose3d &pose_1, |
| 32 | const ceres::examples::Pose3d &pose_2) { |
| 33 | Eigen::Affine3d H_world_1 = Pose3dToAffine3d(pose_1); |
| 34 | Eigen::Affine3d H_world_2 = Pose3dToAffine3d(pose_2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 35 | |
| 36 | // Get the location of 2 in the 1 frame |
| 37 | Eigen::Affine3d H_1_2 = H_world_1.inverse() * H_world_2; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 38 | return Affine3dToPose3d(H_1_2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 41 | ceres::examples::Pose3d PoseUtils::ComputeOffsetPose( |
| 42 | const ceres::examples::Pose3d &pose_1, |
| 43 | const ceres::examples::Pose3d &pose_2_relative) { |
| 44 | auto H_world_1 = Pose3dToAffine3d(pose_1); |
| 45 | auto H_1_2 = Pose3dToAffine3d(pose_2_relative); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 46 | auto H_world_2 = H_world_1 * H_1_2; |
| 47 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 48 | return Affine3dToPose3d(H_world_2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 51 | Eigen::Quaterniond PoseUtils::EulerAnglesToQuaternion( |
| 52 | const Eigen::Vector3d &rpy) { |
| 53 | Eigen::AngleAxisd roll_angle(rpy.x(), Eigen::Vector3d::UnitX()); |
| 54 | Eigen::AngleAxisd pitch_angle(rpy.y(), Eigen::Vector3d::UnitY()); |
| 55 | Eigen::AngleAxisd yaw_angle(rpy.z(), Eigen::Vector3d::UnitZ()); |
| 56 | |
| 57 | return yaw_angle * pitch_angle * roll_angle; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 60 | Eigen::Vector3d PoseUtils::QuaternionToEulerAngles( |
| 61 | const Eigen::Quaterniond &q) { |
| 62 | return RotationMatrixToEulerAngles(q.toRotationMatrix()); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 65 | Eigen::Vector3d PoseUtils::RotationMatrixToEulerAngles( |
| 66 | const Eigen::Matrix3d &R) { |
| 67 | double roll = aos::math::NormalizeAngle(std::atan2(R(2, 1), R(2, 2))); |
| 68 | double pitch = aos::math::NormalizeAngle(-std::asin(R(2, 0))); |
| 69 | double yaw = aos::math::NormalizeAngle(std::atan2(R(1, 0), R(0, 0))); |
| 70 | |
| 71 | return Eigen::Vector3d(roll, pitch, yaw); |
| 72 | } |
| 73 | |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 74 | flatbuffers::Offset<TargetPoseFbs> PoseUtils::TargetPoseToFbs( |
| 75 | const TargetMapper::TargetPose &target_pose, |
| 76 | flatbuffers::FlatBufferBuilder *fbb) { |
| 77 | const auto position_offset = |
| 78 | CreatePosition(*fbb, target_pose.pose.p(0), target_pose.pose.p(1), |
| 79 | target_pose.pose.p(2)); |
| 80 | const auto orientation_offset = |
| 81 | CreateQuaternion(*fbb, target_pose.pose.q.w(), target_pose.pose.q.x(), |
| 82 | target_pose.pose.q.y(), target_pose.pose.q.z()); |
| 83 | return CreateTargetPoseFbs(*fbb, target_pose.id, position_offset, |
| 84 | orientation_offset); |
| 85 | } |
| 86 | |
| 87 | TargetMapper::TargetPose PoseUtils::TargetPoseFromFbs( |
| 88 | const TargetPoseFbs &target_pose_fbs) { |
| 89 | return {.id = static_cast<TargetMapper::TargetId>(target_pose_fbs.id()), |
| 90 | .pose = ceres::examples::Pose3d{ |
| 91 | Eigen::Vector3d(target_pose_fbs.position()->x(), |
| 92 | target_pose_fbs.position()->y(), |
| 93 | target_pose_fbs.position()->z()), |
| 94 | Eigen::Quaterniond(target_pose_fbs.orientation()->w(), |
| 95 | target_pose_fbs.orientation()->x(), |
| 96 | target_pose_fbs.orientation()->y(), |
| 97 | target_pose_fbs.orientation()->z())}}; |
| 98 | } |
| 99 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 100 | ceres::examples::VectorOfConstraints DataAdapter::MatchTargetDetections( |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 101 | const std::vector<DataAdapter::TimestampedDetection> |
| 102 | ×tamped_target_detections, |
| 103 | aos::distributed_clock::duration max_dt) { |
| 104 | CHECK_GE(timestamped_target_detections.size(), 2ul) |
| 105 | << "Must have at least 2 detections"; |
| 106 | |
| 107 | // Match consecutive detections |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 108 | ceres::examples::VectorOfConstraints target_constraints; |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 109 | for (auto detection = timestamped_target_detections.begin() + 1; |
| 110 | detection < timestamped_target_detections.end(); detection++) { |
| 111 | auto last_detection = detection - 1; |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 112 | |
| 113 | // Skip two consecutive detections of the same target, because the solver |
| 114 | // doesn't allow this |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 115 | if (detection->id == last_detection->id) { |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 116 | continue; |
| 117 | } |
| 118 | |
| 119 | // Don't take into account constraints too far apart in time, because the |
| 120 | // recording device could have moved too much |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 121 | if ((detection->time - last_detection->time) > max_dt) { |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 122 | continue; |
| 123 | } |
| 124 | |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 125 | auto confidence = ComputeConfidence(*last_detection, *detection); |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 126 | target_constraints.emplace_back( |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 127 | ComputeTargetConstraint(*last_detection, *detection, confidence)); |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return target_constraints; |
| 131 | } |
| 132 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 133 | TargetMapper::ConfidenceMatrix DataAdapter::ComputeConfidence( |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 134 | const TimestampedDetection &detection_start, |
| 135 | const TimestampedDetection &detection_end) { |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 136 | constexpr size_t kX = 0; |
| 137 | constexpr size_t kY = 1; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 138 | constexpr size_t kZ = 2; |
| 139 | constexpr size_t kOrientation1 = 3; |
| 140 | constexpr size_t kOrientation2 = 4; |
| 141 | constexpr size_t kOrientation3 = 5; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 142 | |
| 143 | // Uncertainty matrix between start and end |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 144 | TargetMapper::ConfidenceMatrix P = TargetMapper::ConfidenceMatrix::Zero(); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 145 | |
| 146 | { |
| 147 | // Noise for odometry-based robot position measurements |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 148 | TargetMapper::ConfidenceMatrix Q_odometry = |
| 149 | TargetMapper::ConfidenceMatrix::Zero(); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 150 | Q_odometry(kX, kX) = std::pow(0.045, 2); |
| 151 | Q_odometry(kY, kY) = std::pow(0.045, 2); |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 152 | Q_odometry(kZ, kZ) = std::pow(0.045, 2); |
| 153 | Q_odometry(kOrientation1, kOrientation1) = std::pow(0.01, 2); |
| 154 | Q_odometry(kOrientation2, kOrientation2) = std::pow(0.01, 2); |
| 155 | Q_odometry(kOrientation3, kOrientation3) = std::pow(0.01, 2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 156 | |
| 157 | // Add uncertainty for robot position measurements from start to end |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 158 | int iterations = (detection_end.time - detection_start.time) / |
| 159 | frc971::controls::kLoopFrequency; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 160 | P += static_cast<double>(iterations) * Q_odometry; |
| 161 | } |
| 162 | |
| 163 | { |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 164 | // Noise for vision-based target localizations. Multiplying this matrix by |
milind-u | 6ff399f | 2023-03-24 18:33:38 -0700 | [diff] [blame^] | 165 | // the distance from camera to target squared results in the uncertainty |
| 166 | // in that measurement |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 167 | TargetMapper::ConfidenceMatrix Q_vision = |
| 168 | TargetMapper::ConfidenceMatrix::Zero(); |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 169 | Q_vision(kX, kX) = std::pow(0.045, 2); |
| 170 | Q_vision(kY, kY) = std::pow(0.045, 2); |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 171 | Q_vision(kZ, kZ) = std::pow(0.045, 2); |
| 172 | Q_vision(kOrientation1, kOrientation1) = std::pow(0.02, 2); |
| 173 | Q_vision(kOrientation2, kOrientation2) = std::pow(0.02, 2); |
| 174 | Q_vision(kOrientation3, kOrientation3) = std::pow(0.02, 2); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 175 | |
| 176 | // Add uncertainty for the 2 vision measurements (1 at start and 1 at end) |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 177 | P += Q_vision * std::pow(detection_start.distance_from_camera, 2) * |
| 178 | (1.0 + |
| 179 | FLAGS_distortion_noise_scalar * detection_start.distortion_factor); |
| 180 | P += |
| 181 | Q_vision * std::pow(detection_end.distance_from_camera, 2) * |
| 182 | (1.0 + FLAGS_distortion_noise_scalar * detection_end.distortion_factor); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return P.inverse(); |
| 186 | } |
| 187 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 188 | ceres::examples::Constraint3d DataAdapter::ComputeTargetConstraint( |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 189 | const TimestampedDetection &target_detection_start, |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 190 | const TimestampedDetection &target_detection_end, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 191 | const TargetMapper::ConfidenceMatrix &confidence) { |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 192 | // Compute the relative pose (constraint) between the two targets |
| 193 | Eigen::Affine3d H_targetstart_targetend = |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 194 | target_detection_start.H_robot_target.inverse() * |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 195 | target_detection_end.H_robot_target; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 196 | ceres::examples::Pose3d target_constraint = |
| 197 | PoseUtils::Affine3dToPose3d(H_targetstart_targetend); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 198 | |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 199 | const auto constraint_3d = |
| 200 | ceres::examples::Constraint3d{target_detection_start.id, |
| 201 | target_detection_end.id, |
| 202 | {target_constraint.p, target_constraint.q}, |
| 203 | confidence}; |
| 204 | |
| 205 | VLOG(2) << "Computed constraint: " << constraint_3d; |
| 206 | return constraint_3d; |
Milind Upadhyay | ec49391 | 2022-12-18 21:33:15 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 209 | TargetMapper::TargetMapper( |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 210 | std::string_view target_poses_path, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 211 | const ceres::examples::VectorOfConstraints &target_constraints) |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 212 | : target_constraints_(target_constraints) { |
| 213 | aos::FlatbufferDetachedBuffer<TargetMap> target_map = |
| 214 | aos::JsonFileToFlatbuffer<TargetMap>(target_poses_path); |
| 215 | for (const auto *target_pose_fbs : *target_map.message().target_poses()) { |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 216 | target_poses_[target_pose_fbs->id()] = |
| 217 | PoseUtils::TargetPoseFromFbs(*target_pose_fbs).pose; |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | TargetMapper::TargetMapper( |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 222 | const ceres::examples::MapOfPoses &target_poses, |
| 223 | const ceres::examples::VectorOfConstraints &target_constraints) |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 224 | : target_poses_(target_poses), target_constraints_(target_constraints) {} |
| 225 | |
| 226 | std::optional<TargetMapper::TargetPose> TargetMapper::GetTargetPoseById( |
| 227 | std::vector<TargetMapper::TargetPose> target_poses, TargetId target_id) { |
| 228 | for (auto target_pose : target_poses) { |
| 229 | if (target_pose.id == target_id) { |
| 230 | return target_pose; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return std::nullopt; |
| 235 | } |
| 236 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 237 | std::optional<TargetMapper::TargetPose> TargetMapper::GetTargetPoseById( |
| 238 | TargetId target_id) { |
| 239 | if (target_poses_.count(target_id) > 0) { |
| 240 | return TargetMapper::TargetPose{target_id, target_poses_[target_id]}; |
| 241 | } |
| 242 | |
| 243 | return std::nullopt; |
| 244 | } |
| 245 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 246 | // Taken from ceres/examples/slam/pose_graph_3d/pose_graph_3d.cc |
| 247 | // Constructs the nonlinear least squares optimization problem from the pose |
| 248 | // graph constraints. |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 249 | void TargetMapper::BuildOptimizationProblem( |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 250 | const ceres::examples::VectorOfConstraints &constraints, |
| 251 | ceres::examples::MapOfPoses *poses, ceres::Problem *problem) { |
| 252 | CHECK(poses != nullptr); |
| 253 | CHECK(problem != nullptr); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 254 | if (constraints.empty()) { |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 255 | LOG(INFO) << "No constraints, no problem to optimize."; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
milind-u | 13ff1a5 | 2023-01-22 17:10:49 -0800 | [diff] [blame] | 259 | ceres::LossFunction *loss_function = new ceres::HuberLoss(2.0); |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 260 | ceres::LocalParameterization *quaternion_local_parameterization = |
| 261 | new ceres::EigenQuaternionParameterization; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 262 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 263 | for (ceres::examples::VectorOfConstraints::const_iterator constraints_iter = |
| 264 | constraints.begin(); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 265 | constraints_iter != constraints.end(); ++constraints_iter) { |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 266 | const ceres::examples::Constraint3d &constraint = *constraints_iter; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 267 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 268 | ceres::examples::MapOfPoses::iterator pose_begin_iter = |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 269 | poses->find(constraint.id_begin); |
| 270 | CHECK(pose_begin_iter != poses->end()) |
| 271 | << "Pose with ID: " << constraint.id_begin << " not found."; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 272 | ceres::examples::MapOfPoses::iterator pose_end_iter = |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 273 | poses->find(constraint.id_end); |
| 274 | CHECK(pose_end_iter != poses->end()) |
| 275 | << "Pose with ID: " << constraint.id_end << " not found."; |
| 276 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 277 | const Eigen::Matrix<double, 6, 6> sqrt_information = |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 278 | constraint.information.llt().matrixL(); |
| 279 | // Ceres will take ownership of the pointer. |
| 280 | ceres::CostFunction *cost_function = |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 281 | ceres::examples::PoseGraph3dErrorTerm::Create(constraint.t_be, |
| 282 | sqrt_information); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 283 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 284 | problem->AddResidualBlock(cost_function, loss_function, |
| 285 | pose_begin_iter->second.p.data(), |
| 286 | pose_begin_iter->second.q.coeffs().data(), |
| 287 | pose_end_iter->second.p.data(), |
| 288 | pose_end_iter->second.q.coeffs().data()); |
| 289 | |
| 290 | problem->SetParameterization(pose_begin_iter->second.q.coeffs().data(), |
| 291 | quaternion_local_parameterization); |
| 292 | problem->SetParameterization(pose_end_iter->second.q.coeffs().data(), |
| 293 | quaternion_local_parameterization); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 296 | // The pose graph optimization problem has six DOFs that are not fully |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 297 | // constrained. This is typically referred to as gauge freedom. You can |
| 298 | // apply a rigid body transformation to all the nodes and the optimization |
| 299 | // problem will still have the exact same cost. The Levenberg-Marquardt |
| 300 | // algorithm has internal damping which mitigates this issue, but it is |
| 301 | // better to properly constrain the gauge freedom. This can be done by |
| 302 | // setting one of the poses as constant so the optimizer cannot change it. |
milind-u | 6ff399f | 2023-03-24 18:33:38 -0700 | [diff] [blame^] | 303 | CHECK_NE(poses->count(FLAGS_frozen_target_id), 0ul) |
| 304 | << "Got no poses for frozen target id " << FLAGS_frozen_target_id; |
| 305 | ceres::examples::MapOfPoses::iterator pose_start_iter = |
| 306 | poses->find(FLAGS_frozen_target_id); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 307 | CHECK(pose_start_iter != poses->end()) << "There are no poses."; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 308 | problem->SetParameterBlockConstant(pose_start_iter->second.p.data()); |
| 309 | problem->SetParameterBlockConstant(pose_start_iter->second.q.coeffs().data()); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 312 | // Taken from ceres/examples/slam/pose_graph_3d/pose_graph_3d.cc |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 313 | bool TargetMapper::SolveOptimizationProblem(ceres::Problem *problem) { |
| 314 | CHECK_NOTNULL(problem); |
| 315 | |
| 316 | ceres::Solver::Options options; |
| 317 | options.max_num_iterations = FLAGS_max_num_iterations; |
| 318 | options.linear_solver_type = ceres::SPARSE_NORMAL_CHOLESKY; |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 319 | options.minimizer_progress_to_stdout = true; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 320 | |
| 321 | ceres::Solver::Summary summary; |
| 322 | ceres::Solve(options, problem, &summary); |
| 323 | |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 324 | LOG(INFO) << summary.FullReport() << '\n'; |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 325 | |
| 326 | return summary.IsSolutionUsable(); |
| 327 | } |
| 328 | |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 329 | void TargetMapper::Solve(std::string_view field_name, |
| 330 | std::optional<std::string_view> output_dir) { |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 331 | ceres::Problem problem; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 332 | BuildOptimizationProblem(target_constraints_, &target_poses_, &problem); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 333 | |
| 334 | CHECK(SolveOptimizationProblem(&problem)) |
| 335 | << "The solve was not successful, exiting."; |
| 336 | |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 337 | auto map_json = MapToJson(field_name); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 338 | VLOG(1) << "Solved target poses: " << map_json; |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 339 | |
| 340 | if (output_dir.has_value()) { |
| 341 | std::string output_path = |
| 342 | absl::StrCat(output_dir.value(), "/", field_name, ".json"); |
| 343 | LOG(INFO) << "Writing map to file: " << output_path; |
| 344 | aos::util::WriteStringToFileOrDie(output_path, map_json); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 348 | std::string TargetMapper::MapToJson(std::string_view field_name) const { |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 349 | flatbuffers::FlatBufferBuilder fbb; |
| 350 | |
| 351 | // Convert poses to flatbuffers |
| 352 | std::vector<flatbuffers::Offset<TargetPoseFbs>> target_poses_fbs; |
| 353 | for (const auto &[id, pose] : target_poses_) { |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 354 | target_poses_fbs.emplace_back( |
| 355 | PoseUtils::TargetPoseToFbs(TargetPose{.id = id, .pose = pose}, &fbb)); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Milind Upadhyay | 05652cb | 2022-12-07 20:51:51 -0800 | [diff] [blame] | 358 | const auto field_name_offset = fbb.CreateString(field_name); |
| 359 | flatbuffers::Offset<TargetMap> target_map_offset = CreateTargetMap( |
| 360 | fbb, fbb.CreateVector(target_poses_fbs), field_name_offset); |
Milind Upadhyay | cd677a3 | 2022-12-04 13:06:43 -0800 | [diff] [blame] | 361 | |
| 362 | return aos::FlatbufferToJson( |
| 363 | flatbuffers::GetMutableTemporaryPointer(fbb, target_map_offset), |
| 364 | {.multi_line = true}); |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 367 | std::ostream &operator<<(std::ostream &os, ceres::examples::Pose3d pose) { |
| 368 | auto rpy = PoseUtils::QuaternionToEulerAngles(pose.q); |
| 369 | os << absl::StrFormat( |
| 370 | "{x: %.3f, y: %.3f, z: %.3f, roll: %.3f, pitch: " |
| 371 | "%.3f, yaw: %.3f}", |
| 372 | pose.p(0), pose.p(1), pose.p(2), rpy(0), rpy(1), rpy(2)); |
| 373 | return os; |
| 374 | } |
| 375 | |
| 376 | std::ostream &operator<<(std::ostream &os, |
| 377 | ceres::examples::Constraint3d constraint) { |
| 378 | os << absl::StrFormat("{id_begin: %d, id_end: %d, pose: ", |
| 379 | constraint.id_begin, constraint.id_end) |
| 380 | << constraint.t_be << "}"; |
| 381 | return os; |
| 382 | } |
| 383 | |
Milind Upadhyay | 7c20522 | 2022-11-16 18:20:58 -0800 | [diff] [blame] | 384 | } // namespace frc971::vision |