blob: c4af1638d0b5a27da06edc08699a2666ff4a77e6 [file] [log] [blame]
Milind Upadhyay7c205222022-11-16 18:20:58 -08001#include "frc971/vision/target_mapper.h"
2
Milind Upadhyayc5beba12022-12-17 17:41:20 -08003#include "absl/strings/str_format.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07004
Milind Upadhyay7c205222022-11-16 18:20:58 -08005#include "frc971/control_loops/control_loop.h"
Milind Upadhyayc5beba12022-12-17 17:41:20 -08006#include "frc971/vision/ceres/pose_graph_3d_error_term.h"
Milind Upadhyay7c205222022-11-16 18:20:58 -08007#include "frc971/vision/geometry.h"
8
9DEFINE_uint64(max_num_iterations, 100,
10 "Maximum number of iterations for the ceres solver");
milind-ud62f80a2023-03-04 16:37:09 -080011DEFINE_double(distortion_noise_scalar, 1.0,
12 "Scale the target pose distortion factor by this when computing "
13 "the noise.");
milind-u8f4e43e2023-04-09 17:11:19 -070014DEFINE_int32(
milind-u6ff399f2023-03-24 18:33:38 -070015 frozen_target_id, 1,
16 "Freeze the pose of this target so the map can have one fixed point.");
milind-u8f4e43e2023-04-09 17:11:19 -070017DEFINE_int32(min_target_id, 1, "Minimum target id to solve for");
18DEFINE_int32(max_target_id, 8, "Maximum target id to solve for");
19DEFINE_bool(visualize_solver, false, "If true, visualize the solving process.");
milind-u401de982023-04-14 17:32:03 -070020// This does seem like a strict threshold for a constaint to be considered an
21// outlier, but most inliers were very close together and this is what worked
22// best for map solving.
23DEFINE_double(outlier_std_devs, 1.0,
24 "Number of standard deviations above average error needed for a "
25 "constraint to be considered an outlier and get removed.");
Milind Upadhyay7c205222022-11-16 18:20:58 -080026
27namespace frc971::vision {
Milind Upadhyayc5beba12022-12-17 17:41:20 -080028Eigen::Affine3d PoseUtils::Pose3dToAffine3d(
29 const ceres::examples::Pose3d &pose3d) {
Milind Upadhyay7c205222022-11-16 18:20:58 -080030 Eigen::Affine3d H_world_pose =
Milind Upadhyayc5beba12022-12-17 17:41:20 -080031 Eigen::Translation3d(pose3d.p(0), pose3d.p(1), pose3d.p(2)) * pose3d.q;
Milind Upadhyay7c205222022-11-16 18:20:58 -080032 return H_world_pose;
33}
34
Milind Upadhyayc5beba12022-12-17 17:41:20 -080035ceres::examples::Pose3d PoseUtils::Affine3dToPose3d(const Eigen::Affine3d &H) {
36 return ceres::examples::Pose3d{.p = H.translation(),
37 .q = Eigen::Quaterniond(H.rotation())};
Milind Upadhyay7c205222022-11-16 18:20:58 -080038}
39
Milind Upadhyayc5beba12022-12-17 17:41:20 -080040ceres::examples::Pose3d PoseUtils::ComputeRelativePose(
41 const ceres::examples::Pose3d &pose_1,
42 const ceres::examples::Pose3d &pose_2) {
43 Eigen::Affine3d H_world_1 = Pose3dToAffine3d(pose_1);
44 Eigen::Affine3d H_world_2 = Pose3dToAffine3d(pose_2);
Milind Upadhyay7c205222022-11-16 18:20:58 -080045
46 // Get the location of 2 in the 1 frame
47 Eigen::Affine3d H_1_2 = H_world_1.inverse() * H_world_2;
Milind Upadhyayc5beba12022-12-17 17:41:20 -080048 return Affine3dToPose3d(H_1_2);
Milind Upadhyay7c205222022-11-16 18:20:58 -080049}
50
Milind Upadhyayc5beba12022-12-17 17:41:20 -080051ceres::examples::Pose3d PoseUtils::ComputeOffsetPose(
52 const ceres::examples::Pose3d &pose_1,
53 const ceres::examples::Pose3d &pose_2_relative) {
54 auto H_world_1 = Pose3dToAffine3d(pose_1);
55 auto H_1_2 = Pose3dToAffine3d(pose_2_relative);
Milind Upadhyay7c205222022-11-16 18:20:58 -080056 auto H_world_2 = H_world_1 * H_1_2;
57
Milind Upadhyayc5beba12022-12-17 17:41:20 -080058 return Affine3dToPose3d(H_world_2);
Milind Upadhyay7c205222022-11-16 18:20:58 -080059}
60
Milind Upadhyayc5beba12022-12-17 17:41:20 -080061Eigen::Quaterniond PoseUtils::EulerAnglesToQuaternion(
62 const Eigen::Vector3d &rpy) {
63 Eigen::AngleAxisd roll_angle(rpy.x(), Eigen::Vector3d::UnitX());
64 Eigen::AngleAxisd pitch_angle(rpy.y(), Eigen::Vector3d::UnitY());
65 Eigen::AngleAxisd yaw_angle(rpy.z(), Eigen::Vector3d::UnitZ());
66
67 return yaw_angle * pitch_angle * roll_angle;
Milind Upadhyay7c205222022-11-16 18:20:58 -080068}
69
Milind Upadhyayc5beba12022-12-17 17:41:20 -080070Eigen::Vector3d PoseUtils::QuaternionToEulerAngles(
71 const Eigen::Quaterniond &q) {
72 return RotationMatrixToEulerAngles(q.toRotationMatrix());
Milind Upadhyay7c205222022-11-16 18:20:58 -080073}
74
Milind Upadhyayc5beba12022-12-17 17:41:20 -080075Eigen::Vector3d PoseUtils::RotationMatrixToEulerAngles(
76 const Eigen::Matrix3d &R) {
77 double roll = aos::math::NormalizeAngle(std::atan2(R(2, 1), R(2, 2)));
78 double pitch = aos::math::NormalizeAngle(-std::asin(R(2, 0)));
79 double yaw = aos::math::NormalizeAngle(std::atan2(R(1, 0), R(0, 0)));
80
81 return Eigen::Vector3d(roll, pitch, yaw);
82}
83
milind-u3f5f83c2023-01-29 15:23:51 -080084flatbuffers::Offset<TargetPoseFbs> PoseUtils::TargetPoseToFbs(
85 const TargetMapper::TargetPose &target_pose,
86 flatbuffers::FlatBufferBuilder *fbb) {
87 const auto position_offset =
88 CreatePosition(*fbb, target_pose.pose.p(0), target_pose.pose.p(1),
89 target_pose.pose.p(2));
90 const auto orientation_offset =
91 CreateQuaternion(*fbb, target_pose.pose.q.w(), target_pose.pose.q.x(),
92 target_pose.pose.q.y(), target_pose.pose.q.z());
93 return CreateTargetPoseFbs(*fbb, target_pose.id, position_offset,
94 orientation_offset);
95}
96
97TargetMapper::TargetPose PoseUtils::TargetPoseFromFbs(
98 const TargetPoseFbs &target_pose_fbs) {
99 return {.id = static_cast<TargetMapper::TargetId>(target_pose_fbs.id()),
100 .pose = ceres::examples::Pose3d{
101 Eigen::Vector3d(target_pose_fbs.position()->x(),
102 target_pose_fbs.position()->y(),
103 target_pose_fbs.position()->z()),
104 Eigen::Quaterniond(target_pose_fbs.orientation()->w(),
105 target_pose_fbs.orientation()->x(),
106 target_pose_fbs.orientation()->y(),
107 target_pose_fbs.orientation()->z())}};
108}
109
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800110ceres::examples::VectorOfConstraints DataAdapter::MatchTargetDetections(
Milind Upadhyayec493912022-12-18 21:33:15 -0800111 const std::vector<DataAdapter::TimestampedDetection>
112 &timestamped_target_detections,
113 aos::distributed_clock::duration max_dt) {
114 CHECK_GE(timestamped_target_detections.size(), 2ul)
115 << "Must have at least 2 detections";
116
117 // Match consecutive detections
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800118 ceres::examples::VectorOfConstraints target_constraints;
milind-ud62f80a2023-03-04 16:37:09 -0800119 for (auto detection = timestamped_target_detections.begin() + 1;
120 detection < timestamped_target_detections.end(); detection++) {
121 auto last_detection = detection - 1;
Milind Upadhyayec493912022-12-18 21:33:15 -0800122
123 // Skip two consecutive detections of the same target, because the solver
124 // doesn't allow this
milind-ud62f80a2023-03-04 16:37:09 -0800125 if (detection->id == last_detection->id) {
Milind Upadhyayec493912022-12-18 21:33:15 -0800126 continue;
127 }
128
129 // Don't take into account constraints too far apart in time, because the
130 // recording device could have moved too much
milind-ud62f80a2023-03-04 16:37:09 -0800131 if ((detection->time - last_detection->time) > max_dt) {
Milind Upadhyayec493912022-12-18 21:33:15 -0800132 continue;
133 }
134
milind-ud62f80a2023-03-04 16:37:09 -0800135 auto confidence = ComputeConfidence(*last_detection, *detection);
Milind Upadhyayec493912022-12-18 21:33:15 -0800136 target_constraints.emplace_back(
milind-ud62f80a2023-03-04 16:37:09 -0800137 ComputeTargetConstraint(*last_detection, *detection, confidence));
Milind Upadhyayec493912022-12-18 21:33:15 -0800138 }
139
140 return target_constraints;
141}
142
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800143TargetMapper::ConfidenceMatrix DataAdapter::ComputeConfidence(
milind-ud62f80a2023-03-04 16:37:09 -0800144 const TimestampedDetection &detection_start,
145 const TimestampedDetection &detection_end) {
Milind Upadhyay7c205222022-11-16 18:20:58 -0800146 constexpr size_t kX = 0;
147 constexpr size_t kY = 1;
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800148 constexpr size_t kZ = 2;
149 constexpr size_t kOrientation1 = 3;
150 constexpr size_t kOrientation2 = 4;
151 constexpr size_t kOrientation3 = 5;
Milind Upadhyay7c205222022-11-16 18:20:58 -0800152
153 // Uncertainty matrix between start and end
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800154 TargetMapper::ConfidenceMatrix P = TargetMapper::ConfidenceMatrix::Zero();
Milind Upadhyay7c205222022-11-16 18:20:58 -0800155
156 {
157 // Noise for odometry-based robot position measurements
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800158 TargetMapper::ConfidenceMatrix Q_odometry =
159 TargetMapper::ConfidenceMatrix::Zero();
Milind Upadhyay7c205222022-11-16 18:20:58 -0800160 Q_odometry(kX, kX) = std::pow(0.045, 2);
161 Q_odometry(kY, kY) = std::pow(0.045, 2);
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800162 Q_odometry(kZ, kZ) = std::pow(0.045, 2);
163 Q_odometry(kOrientation1, kOrientation1) = std::pow(0.01, 2);
164 Q_odometry(kOrientation2, kOrientation2) = std::pow(0.01, 2);
165 Q_odometry(kOrientation3, kOrientation3) = std::pow(0.01, 2);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800166
167 // Add uncertainty for robot position measurements from start to end
milind-ud62f80a2023-03-04 16:37:09 -0800168 int iterations = (detection_end.time - detection_start.time) /
169 frc971::controls::kLoopFrequency;
Milind Upadhyay7c205222022-11-16 18:20:58 -0800170 P += static_cast<double>(iterations) * Q_odometry;
171 }
172
173 {
Milind Upadhyayebf93ee2023-01-05 14:12:58 -0800174 // Noise for vision-based target localizations. Multiplying this matrix by
milind-u6ff399f2023-03-24 18:33:38 -0700175 // the distance from camera to target squared results in the uncertainty
176 // in that measurement
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800177 TargetMapper::ConfidenceMatrix Q_vision =
178 TargetMapper::ConfidenceMatrix::Zero();
Milind Upadhyayebf93ee2023-01-05 14:12:58 -0800179 Q_vision(kX, kX) = std::pow(0.045, 2);
180 Q_vision(kY, kY) = std::pow(0.045, 2);
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800181 Q_vision(kZ, kZ) = std::pow(0.045, 2);
182 Q_vision(kOrientation1, kOrientation1) = std::pow(0.02, 2);
183 Q_vision(kOrientation2, kOrientation2) = std::pow(0.02, 2);
184 Q_vision(kOrientation3, kOrientation3) = std::pow(0.02, 2);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800185
186 // Add uncertainty for the 2 vision measurements (1 at start and 1 at end)
milind-ufbc5c812023-04-06 21:24:29 -0700187 P += Q_vision * std::pow(detection_start.distance_from_camera *
188 (1.0 + FLAGS_distortion_noise_scalar *
189 detection_start.distortion_factor),
190 2);
191 P += Q_vision * std::pow(detection_end.distance_from_camera *
192 (1.0 + FLAGS_distortion_noise_scalar *
193 detection_end.distortion_factor),
194 2);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800195 }
196
197 return P.inverse();
198}
199
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800200ceres::examples::Constraint3d DataAdapter::ComputeTargetConstraint(
Milind Upadhyay7c205222022-11-16 18:20:58 -0800201 const TimestampedDetection &target_detection_start,
Milind Upadhyay7c205222022-11-16 18:20:58 -0800202 const TimestampedDetection &target_detection_end,
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800203 const TargetMapper::ConfidenceMatrix &confidence) {
Milind Upadhyay7c205222022-11-16 18:20:58 -0800204 // Compute the relative pose (constraint) between the two targets
205 Eigen::Affine3d H_targetstart_targetend =
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800206 target_detection_start.H_robot_target.inverse() *
Milind Upadhyay7c205222022-11-16 18:20:58 -0800207 target_detection_end.H_robot_target;
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800208 ceres::examples::Pose3d target_constraint =
209 PoseUtils::Affine3dToPose3d(H_targetstart_targetend);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800210
milind-uf3ab8ba2023-02-04 17:56:16 -0800211 const auto constraint_3d =
212 ceres::examples::Constraint3d{target_detection_start.id,
213 target_detection_end.id,
214 {target_constraint.p, target_constraint.q},
215 confidence};
216
217 VLOG(2) << "Computed constraint: " << constraint_3d;
218 return constraint_3d;
Milind Upadhyayec493912022-12-18 21:33:15 -0800219}
220
Milind Upadhyay7c205222022-11-16 18:20:58 -0800221TargetMapper::TargetMapper(
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800222 std::string_view target_poses_path,
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800223 const ceres::examples::VectorOfConstraints &target_constraints)
milind-u8f4e43e2023-04-09 17:11:19 -0700224 : target_constraints_(target_constraints),
225 T_frozen_actual_(Eigen::Vector3d::Zero()),
226 R_frozen_actual_(Eigen::Quaterniond::Identity()),
Jim Ostrowski68e56172023-09-17 23:44:15 -0700227 vis_robot_(cv::Size(kImageWidth_, kImageHeight_)) {
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800228 aos::FlatbufferDetachedBuffer<TargetMap> target_map =
229 aos::JsonFileToFlatbuffer<TargetMap>(target_poses_path);
230 for (const auto *target_pose_fbs : *target_map.message().target_poses()) {
milind-u8f4e43e2023-04-09 17:11:19 -0700231 ideal_target_poses_[target_pose_fbs->id()] =
milind-u3f5f83c2023-01-29 15:23:51 -0800232 PoseUtils::TargetPoseFromFbs(*target_pose_fbs).pose;
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800233 }
milind-u8f4e43e2023-04-09 17:11:19 -0700234 target_poses_ = ideal_target_poses_;
milind-u526d5672023-04-17 20:09:10 -0700235 CountConstraints();
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800236}
237
238TargetMapper::TargetMapper(
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800239 const ceres::examples::MapOfPoses &target_poses,
240 const ceres::examples::VectorOfConstraints &target_constraints)
milind-u8f4e43e2023-04-09 17:11:19 -0700241 : ideal_target_poses_(target_poses),
242 target_poses_(ideal_target_poses_),
243 target_constraints_(target_constraints),
244 T_frozen_actual_(Eigen::Vector3d::Zero()),
245 R_frozen_actual_(Eigen::Quaterniond::Identity()),
Jim Ostrowski68e56172023-09-17 23:44:15 -0700246 vis_robot_(cv::Size(kImageWidth_, kImageHeight_)) {
milind-u526d5672023-04-17 20:09:10 -0700247 CountConstraints();
248}
249
250namespace {
251std::pair<TargetMapper::TargetId, TargetMapper::TargetId> MakeIdPair(
252 const ceres::examples::Constraint3d &constraint) {
253 auto min_id = std::min(constraint.id_begin, constraint.id_end);
254 auto max_id = std::max(constraint.id_begin, constraint.id_end);
255 return std::make_pair(min_id, max_id);
256}
257} // namespace
258
259void TargetMapper::CountConstraints() {
260 for (const auto &constraint : target_constraints_) {
261 auto id_pair = MakeIdPair(constraint);
262 if (constraint_counts_.count(id_pair) == 0) {
263 constraint_counts_[id_pair] = 0;
264 }
265 constraint_counts_[id_pair]++;
266 }
267}
Milind Upadhyay7c205222022-11-16 18:20:58 -0800268
269std::optional<TargetMapper::TargetPose> TargetMapper::GetTargetPoseById(
270 std::vector<TargetMapper::TargetPose> target_poses, TargetId target_id) {
271 for (auto target_pose : target_poses) {
272 if (target_pose.id == target_id) {
273 return target_pose;
274 }
275 }
276
277 return std::nullopt;
278}
279
Jim Ostrowski49be8232023-03-23 01:00:14 -0700280std::optional<TargetMapper::TargetPose> TargetMapper::GetTargetPoseById(
milind-u2ab4db12023-03-25 21:59:23 -0700281 TargetId target_id) const {
Jim Ostrowski49be8232023-03-23 01:00:14 -0700282 if (target_poses_.count(target_id) > 0) {
milind-u2ab4db12023-03-25 21:59:23 -0700283 return TargetMapper::TargetPose{target_id, target_poses_.at(target_id)};
Jim Ostrowski49be8232023-03-23 01:00:14 -0700284 }
285
286 return std::nullopt;
287}
288
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800289// Taken from ceres/examples/slam/pose_graph_3d/pose_graph_3d.cc
290// Constructs the nonlinear least squares optimization problem from the pose
291// graph constraints.
milind-u8f4e43e2023-04-09 17:11:19 -0700292void TargetMapper::BuildTargetPoseOptimizationProblem(
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800293 const ceres::examples::VectorOfConstraints &constraints,
294 ceres::examples::MapOfPoses *poses, ceres::Problem *problem) {
295 CHECK(poses != nullptr);
296 CHECK(problem != nullptr);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800297 if (constraints.empty()) {
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800298 LOG(INFO) << "No constraints, no problem to optimize.";
Milind Upadhyay7c205222022-11-16 18:20:58 -0800299 return;
300 }
301
milind-u13ff1a52023-01-22 17:10:49 -0800302 ceres::LossFunction *loss_function = new ceres::HuberLoss(2.0);
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800303 ceres::LocalParameterization *quaternion_local_parameterization =
304 new ceres::EigenQuaternionParameterization;
Milind Upadhyay7c205222022-11-16 18:20:58 -0800305
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800306 for (ceres::examples::VectorOfConstraints::const_iterator constraints_iter =
307 constraints.begin();
Milind Upadhyay7c205222022-11-16 18:20:58 -0800308 constraints_iter != constraints.end(); ++constraints_iter) {
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800309 const ceres::examples::Constraint3d &constraint = *constraints_iter;
Milind Upadhyay7c205222022-11-16 18:20:58 -0800310
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800311 ceres::examples::MapOfPoses::iterator pose_begin_iter =
Milind Upadhyay7c205222022-11-16 18:20:58 -0800312 poses->find(constraint.id_begin);
313 CHECK(pose_begin_iter != poses->end())
314 << "Pose with ID: " << constraint.id_begin << " not found.";
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800315 ceres::examples::MapOfPoses::iterator pose_end_iter =
Milind Upadhyay7c205222022-11-16 18:20:58 -0800316 poses->find(constraint.id_end);
317 CHECK(pose_end_iter != poses->end())
318 << "Pose with ID: " << constraint.id_end << " not found.";
319
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800320 const Eigen::Matrix<double, 6, 6> sqrt_information =
Milind Upadhyay7c205222022-11-16 18:20:58 -0800321 constraint.information.llt().matrixL();
milind-u526d5672023-04-17 20:09:10 -0700322
323 auto id_pair = MakeIdPair(constraint);
324 CHECK_GT(constraint_counts_.count(id_pair), 0ul)
325 << "Should have counted constraints for " << id_pair.first << "->"
326 << id_pair.second;
327
328 // Normalize constraint cost by occurances
329 size_t constraint_count = constraint_counts_[id_pair];
330 // Scale all costs so the total cost comes out to more reasonable numbers
331 constexpr double kGlobalWeight = 1000.0;
332 double constraint_weight =
333 kGlobalWeight / static_cast<double>(constraint_count);
334
Milind Upadhyay7c205222022-11-16 18:20:58 -0800335 // Ceres will take ownership of the pointer.
336 ceres::CostFunction *cost_function =
milind-u526d5672023-04-17 20:09:10 -0700337 ceres::examples::PoseGraph3dErrorTerm::Create(
338 constraint.t_be, sqrt_information, constraint_weight);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800339
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800340 problem->AddResidualBlock(cost_function, loss_function,
341 pose_begin_iter->second.p.data(),
342 pose_begin_iter->second.q.coeffs().data(),
343 pose_end_iter->second.p.data(),
344 pose_end_iter->second.q.coeffs().data());
345
346 problem->SetParameterization(pose_begin_iter->second.q.coeffs().data(),
347 quaternion_local_parameterization);
348 problem->SetParameterization(pose_end_iter->second.q.coeffs().data(),
349 quaternion_local_parameterization);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800350 }
351
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800352 // The pose graph optimization problem has six DOFs that are not fully
milind-u3f5f83c2023-01-29 15:23:51 -0800353 // constrained. This is typically referred to as gauge freedom. You can
354 // apply a rigid body transformation to all the nodes and the optimization
355 // problem will still have the exact same cost. The Levenberg-Marquardt
356 // algorithm has internal damping which mitigates this issue, but it is
357 // better to properly constrain the gauge freedom. This can be done by
358 // setting one of the poses as constant so the optimizer cannot change it.
milind-u6ff399f2023-03-24 18:33:38 -0700359 CHECK_NE(poses->count(FLAGS_frozen_target_id), 0ul)
360 << "Got no poses for frozen target id " << FLAGS_frozen_target_id;
361 ceres::examples::MapOfPoses::iterator pose_start_iter =
362 poses->find(FLAGS_frozen_target_id);
Milind Upadhyay7c205222022-11-16 18:20:58 -0800363 CHECK(pose_start_iter != poses->end()) << "There are no poses.";
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800364 problem->SetParameterBlockConstant(pose_start_iter->second.p.data());
365 problem->SetParameterBlockConstant(pose_start_iter->second.q.coeffs().data());
Milind Upadhyay7c205222022-11-16 18:20:58 -0800366}
367
milind-u401de982023-04-14 17:32:03 -0700368std::unique_ptr<ceres::CostFunction>
369TargetMapper::BuildMapFittingOptimizationProblem(ceres::Problem *problem) {
Philipp Schradera6712522023-07-05 20:25:11 -0700370 // Set up robot visualization.
milind-u8f4e43e2023-04-09 17:11:19 -0700371 vis_robot_.ClearImage();
Jim Ostrowski68e56172023-09-17 23:44:15 -0700372 // Compute focal length so that image shows field with viewpoint at 10m above
373 // it (default for viewer)
374 const double kFocalLength = kImageWidth_ * 10.0 / kFieldWidth_;
375 vis_robot_.SetDefaultViewpoint(kImageWidth_, kFocalLength);
milind-u8f4e43e2023-04-09 17:11:19 -0700376
377 const size_t num_targets = FLAGS_max_target_id - FLAGS_min_target_id;
378 // Translation and rotation error for each target
379 const size_t num_residuals = num_targets * 6;
380 // Set up the only cost function (also known as residual). This uses
381 // auto-differentiation to obtain the derivative (jacobian).
milind-u401de982023-04-14 17:32:03 -0700382 std::unique_ptr<ceres::CostFunction> cost_function = std::make_unique<
383 ceres::AutoDiffCostFunction<TargetMapper, ceres::DYNAMIC, 3, 4>>(
384 this, num_residuals, ceres::DO_NOT_TAKE_OWNERSHIP);
milind-u8f4e43e2023-04-09 17:11:19 -0700385
386 ceres::LossFunction *loss_function = new ceres::HuberLoss(2.0);
387 ceres::LocalParameterization *quaternion_local_parameterization =
388 new ceres::EigenQuaternionParameterization;
389
milind-u401de982023-04-14 17:32:03 -0700390 problem->AddResidualBlock(cost_function.get(), loss_function,
milind-u8f4e43e2023-04-09 17:11:19 -0700391 T_frozen_actual_.vector().data(),
392 R_frozen_actual_.coeffs().data());
393 problem->SetParameterization(R_frozen_actual_.coeffs().data(),
394 quaternion_local_parameterization);
milind-u401de982023-04-14 17:32:03 -0700395 return cost_function;
milind-u8f4e43e2023-04-09 17:11:19 -0700396}
397
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800398// Taken from ceres/examples/slam/pose_graph_3d/pose_graph_3d.cc
Milind Upadhyay7c205222022-11-16 18:20:58 -0800399bool TargetMapper::SolveOptimizationProblem(ceres::Problem *problem) {
400 CHECK_NOTNULL(problem);
401
402 ceres::Solver::Options options;
403 options.max_num_iterations = FLAGS_max_num_iterations;
404 options.linear_solver_type = ceres::SPARSE_NORMAL_CHOLESKY;
milind-u401de982023-04-14 17:32:03 -0700405 options.minimizer_progress_to_stdout = false;
Milind Upadhyay7c205222022-11-16 18:20:58 -0800406
407 ceres::Solver::Summary summary;
408 ceres::Solve(options, problem, &summary);
409
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800410 LOG(INFO) << summary.FullReport() << '\n';
Milind Upadhyay7c205222022-11-16 18:20:58 -0800411
412 return summary.IsSolutionUsable();
413}
414
Milind Upadhyay05652cb2022-12-07 20:51:51 -0800415void TargetMapper::Solve(std::string_view field_name,
416 std::optional<std::string_view> output_dir) {
milind-u401de982023-04-14 17:32:03 -0700417 ceres::Problem target_pose_problem_1;
milind-u8f4e43e2023-04-09 17:11:19 -0700418 BuildTargetPoseOptimizationProblem(target_constraints_, &target_poses_,
milind-u401de982023-04-14 17:32:03 -0700419 &target_pose_problem_1);
420 CHECK(SolveOptimizationProblem(&target_pose_problem_1))
421 << "The target pose solve 1 was not successful, exiting.";
Milind Upadhyay7c205222022-11-16 18:20:58 -0800422
milind-u401de982023-04-14 17:32:03 -0700423 RemoveOutlierConstraints();
424
425 // Solve again once we've thrown out bad constraints
426 ceres::Problem target_pose_problem_2;
427 BuildTargetPoseOptimizationProblem(target_constraints_, &target_poses_,
428 &target_pose_problem_2);
429 CHECK(SolveOptimizationProblem(&target_pose_problem_2))
430 << "The target pose solve 2 was not successful, exiting.";
431
Jim Ostrowski68e56172023-09-17 23:44:15 -0700432 LOG(INFO) << "Solving the overall map's best alignment to the previous map";
milind-u401de982023-04-14 17:32:03 -0700433 ceres::Problem map_fitting_problem(
434 {.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP});
435 std::unique_ptr<ceres::CostFunction> map_fitting_cost_function =
436 BuildMapFittingOptimizationProblem(&map_fitting_problem);
milind-u8f4e43e2023-04-09 17:11:19 -0700437 CHECK(SolveOptimizationProblem(&map_fitting_problem))
438 << "The map fitting solve was not successful, exiting.";
milind-u401de982023-04-14 17:32:03 -0700439 map_fitting_cost_function.release();
milind-u8f4e43e2023-04-09 17:11:19 -0700440
441 Eigen::Affine3d H_frozen_actual = T_frozen_actual_ * R_frozen_actual_;
442 LOG(INFO) << "H_frozen_actual: "
443 << PoseUtils::Affine3dToPose3d(H_frozen_actual);
444
445 auto H_world_frozen =
446 PoseUtils::Pose3dToAffine3d(target_poses_[FLAGS_frozen_target_id]);
447 auto H_world_frozenactual = H_world_frozen * H_frozen_actual;
448
449 // Offset the solved poses to become the actual ones
450 for (auto &[id, pose] : target_poses_) {
451 // Don't offset targets we didn't solve for
452 if (id < FLAGS_min_target_id || id > FLAGS_max_target_id) {
453 continue;
454 }
455
456 // Take the delta between the frozen target and the solved target, and put
457 // that on top of the actual pose of the frozen target
458 auto H_world_solved = PoseUtils::Pose3dToAffine3d(pose);
459 auto H_frozen_solved = H_world_frozen.inverse() * H_world_solved;
460 auto H_world_actual = H_world_frozenactual * H_frozen_solved;
461 pose = PoseUtils::Affine3dToPose3d(H_world_actual);
462 }
Milind Upadhyay7c205222022-11-16 18:20:58 -0800463
Milind Upadhyay05652cb2022-12-07 20:51:51 -0800464 auto map_json = MapToJson(field_name);
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800465 VLOG(1) << "Solved target poses: " << map_json;
Milind Upadhyay05652cb2022-12-07 20:51:51 -0800466
467 if (output_dir.has_value()) {
468 std::string output_path =
469 absl::StrCat(output_dir.value(), "/", field_name, ".json");
470 LOG(INFO) << "Writing map to file: " << output_path;
471 aos::util::WriteStringToFileOrDie(output_path, map_json);
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800472 }
milind-u401de982023-04-14 17:32:03 -0700473
474 for (TargetId id_start = FLAGS_min_target_id; id_start < FLAGS_max_target_id;
475 id_start++) {
476 for (TargetId id_end = id_start + 1; id_end <= FLAGS_max_target_id;
477 id_end++) {
478 auto H_start_end =
479 PoseUtils::Pose3dToAffine3d(target_poses_.at(id_start)).inverse() *
480 PoseUtils::Pose3dToAffine3d(target_poses_.at(id_end));
481 auto constraint = PoseUtils::Affine3dToPose3d(H_start_end);
482 LOG(INFO) << id_start << "->" << id_end << ": " << constraint.p.norm()
483 << " meters";
484 }
485 }
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800486}
487
Milind Upadhyay05652cb2022-12-07 20:51:51 -0800488std::string TargetMapper::MapToJson(std::string_view field_name) const {
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800489 flatbuffers::FlatBufferBuilder fbb;
490
491 // Convert poses to flatbuffers
492 std::vector<flatbuffers::Offset<TargetPoseFbs>> target_poses_fbs;
493 for (const auto &[id, pose] : target_poses_) {
milind-u3f5f83c2023-01-29 15:23:51 -0800494 target_poses_fbs.emplace_back(
495 PoseUtils::TargetPoseToFbs(TargetPose{.id = id, .pose = pose}, &fbb));
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800496 }
497
Milind Upadhyay05652cb2022-12-07 20:51:51 -0800498 const auto field_name_offset = fbb.CreateString(field_name);
499 flatbuffers::Offset<TargetMap> target_map_offset = CreateTargetMap(
500 fbb, fbb.CreateVector(target_poses_fbs), field_name_offset);
Milind Upadhyaycd677a32022-12-04 13:06:43 -0800501
502 return aos::FlatbufferToJson(
503 flatbuffers::GetMutableTemporaryPointer(fbb, target_map_offset),
504 {.multi_line = true});
Milind Upadhyay7c205222022-11-16 18:20:58 -0800505}
506
milind-u8f4e43e2023-04-09 17:11:19 -0700507namespace {
milind-u8f4e43e2023-04-09 17:11:19 -0700508// Hacks to extract a double from a scalar, which is either a ceres jet or a
509// double. Only used for debugging and displaying.
510template <typename S>
511double ScalarToDouble(S s) {
512 const double *ptr = reinterpret_cast<double *>(&s);
513 return *ptr;
514}
515
516template <typename S>
517Eigen::Affine3d ScalarAffineToDouble(Eigen::Transform<S, 3, Eigen::Affine> H) {
518 Eigen::Affine3d H_double;
519 for (size_t i = 0; i < H.rows(); i++) {
520 for (size_t j = 0; j < H.cols(); j++) {
521 H_double(i, j) = ScalarToDouble(H(i, j));
522 }
523 }
524 return H_double;
525}
526
527} // namespace
528
529template <typename S>
530bool TargetMapper::operator()(const S *const translation,
531 const S *const rotation, S *residual) const {
532 using Affine3s = Eigen::Transform<S, 3, Eigen::Affine>;
533 Eigen::Quaternion<S> R_frozen_actual(rotation[3], rotation[1], rotation[2],
534 rotation[0]);
535 Eigen::Translation<S, 3> T_frozen_actual(translation[0], translation[1],
536 translation[2]);
537 // Actual target pose in the frame of the fixed pose.
538 Affine3s H_frozen_actual = T_frozen_actual * R_frozen_actual;
539 VLOG(2) << "H_frozen_actual: "
540 << PoseUtils::Affine3dToPose3d(ScalarAffineToDouble(H_frozen_actual));
541
542 Affine3s H_world_frozen =
543 PoseUtils::Pose3dToAffine3d(target_poses_.at(FLAGS_frozen_target_id))
544 .cast<S>();
545 Affine3s H_world_frozenactual = H_world_frozen * H_frozen_actual;
546
547 size_t residual_index = 0;
548 if (FLAGS_visualize_solver) {
549 vis_robot_.ClearImage();
550 }
551
552 for (const auto &[id, solved_pose] : target_poses_) {
553 if (id < FLAGS_min_target_id || id > FLAGS_max_target_id) {
554 continue;
555 }
556
557 Affine3s H_world_ideal =
558 PoseUtils::Pose3dToAffine3d(ideal_target_poses_.at(id)).cast<S>();
559 Affine3s H_world_solved =
560 PoseUtils::Pose3dToAffine3d(solved_pose).cast<S>();
561 // Take the delta between the frozen target and the solved target, and put
562 // that on top of the actual pose of the frozen target
563 auto H_frozen_solved = H_world_frozen.inverse() * H_world_solved;
564 auto H_world_actual = H_world_frozenactual * H_frozen_solved;
565 VLOG(2) << id << ": " << H_world_actual.translation();
566 Affine3s H_ideal_actual = H_world_ideal.inverse() * H_world_actual;
567 auto T_ideal_actual = H_ideal_actual.translation();
568 VLOG(2) << "T_ideal_actual: " << T_ideal_actual;
569 VLOG(2);
570 auto R_ideal_actual = Eigen::AngleAxis<S>(H_ideal_actual.rotation());
571
milind-u401de982023-04-14 17:32:03 -0700572 // Weight translation errors higher than rotation.
573 // 1 m in position error = 0.01 radian (or ~0.573 degrees)
574 constexpr double kTranslationScalar = 1000.0;
575 constexpr double kRotationScalar = 100.0;
milind-u8f4e43e2023-04-09 17:11:19 -0700576
577 // Penalize based on how much our actual poses matches the ideal
578 // ones. We've already solved for the relative poses, now figure out
579 // where all of them fit in the world.
580 residual[residual_index++] = kTranslationScalar * T_ideal_actual(0);
581 residual[residual_index++] = kTranslationScalar * T_ideal_actual(1);
582 residual[residual_index++] = kTranslationScalar * T_ideal_actual(2);
583 residual[residual_index++] =
584 kRotationScalar * R_ideal_actual.angle() * R_ideal_actual.axis().x();
585 residual[residual_index++] =
586 kRotationScalar * R_ideal_actual.angle() * R_ideal_actual.axis().y();
587 residual[residual_index++] =
588 kRotationScalar * R_ideal_actual.angle() * R_ideal_actual.axis().z();
589
590 if (FLAGS_visualize_solver) {
Jim Ostrowski68e56172023-09-17 23:44:15 -0700591 LOG(INFO) << std::to_string(id) + std::string("-est") << " at "
592 << ScalarAffineToDouble(H_world_actual).matrix();
milind-u8f4e43e2023-04-09 17:11:19 -0700593 vis_robot_.DrawFrameAxes(ScalarAffineToDouble(H_world_actual),
Jim Ostrowski68e56172023-09-17 23:44:15 -0700594 std::to_string(id) + std::string("-est"),
595 cv::Scalar(0, 255, 0));
milind-u8f4e43e2023-04-09 17:11:19 -0700596 vis_robot_.DrawFrameAxes(ScalarAffineToDouble(H_world_ideal),
597 std::to_string(id), cv::Scalar(255, 255, 255));
598 }
599 }
600 if (FLAGS_visualize_solver) {
601 cv::imshow("Target maps", vis_robot_.image_);
602 cv::waitKey(0);
603 }
604
605 // Ceres can't handle residual values of exactly zero
606 for (size_t i = 0; i < residual_index; i++) {
607 if (residual[i] == S(0)) {
608 residual[i] = S(1e-9);
609 }
610 }
611
612 return true;
613}
614
milind-u401de982023-04-14 17:32:03 -0700615TargetMapper::PoseError TargetMapper::ComputeError(
616 const ceres::examples::Constraint3d &constraint) const {
617 // Compute the difference between the map-based transform of the end target
618 // in the start target frame, to the one from this constraint
619 auto H_start_end_map =
620 PoseUtils::Pose3dToAffine3d(target_poses_.at(constraint.id_begin))
621 .inverse() *
622 PoseUtils::Pose3dToAffine3d(target_poses_.at(constraint.id_end));
623 auto H_start_end_constraint = PoseUtils::Pose3dToAffine3d(constraint.t_be);
624 ceres::examples::Pose3d delta_pose = PoseUtils::Affine3dToPose3d(
625 H_start_end_map.inverse() * H_start_end_constraint);
626 double distance = delta_pose.p.norm();
627 Eigen::AngleAxisd err_angle(delta_pose.q);
628 double angle = std::abs(err_angle.angle());
629 return {.angle = angle, .distance = distance};
630}
631
632TargetMapper::Stats TargetMapper::ComputeStats() const {
633 Stats stats{.avg_err = {.angle = 0.0, .distance = 0.0},
634 .std_dev = {.angle = 0.0, .distance = 0.0},
635 .max_err = {.angle = 0.0, .distance = 0.0}};
636
637 for (const auto &constraint : target_constraints_) {
638 PoseError err = ComputeError(constraint);
639
640 // Update our statistics
641 stats.avg_err.distance += err.distance;
642 if (err.distance > stats.max_err.distance) {
643 stats.max_err.distance = err.distance;
644 }
645
646 stats.avg_err.angle += err.angle;
647 if (err.angle > stats.max_err.angle) {
648 stats.max_err.angle = err.angle;
649 }
650 }
651
652 stats.avg_err.distance /= static_cast<double>(target_constraints_.size());
653 stats.avg_err.angle /= static_cast<double>(target_constraints_.size());
654
655 for (const auto &constraint : target_constraints_) {
656 PoseError err = ComputeError(constraint);
657
658 // Update our statistics
659 stats.std_dev.distance +=
660 std::pow(err.distance - stats.avg_err.distance, 2);
661
662 stats.std_dev.angle += std::pow(err.angle - stats.avg_err.angle, 2);
663 }
664
665 stats.std_dev.distance = std::sqrt(
666 stats.std_dev.distance / static_cast<double>(target_constraints_.size()));
667 stats.std_dev.angle = std::sqrt(
668 stats.std_dev.angle / static_cast<double>(target_constraints_.size()));
669
670 return stats;
671}
672
673void TargetMapper::RemoveOutlierConstraints() {
674 stats_with_outliers_ = ComputeStats();
675 size_t original_size = target_constraints_.size();
676 target_constraints_.erase(
677 std::remove_if(
678 target_constraints_.begin(), target_constraints_.end(),
679 [&](const auto &constraint) {
680 PoseError err = ComputeError(constraint);
681 // Remove constraints with errors significantly above
682 // the average
683 if (err.distance > stats_with_outliers_.avg_err.distance +
684 FLAGS_outlier_std_devs *
685 stats_with_outliers_.std_dev.distance) {
686 return true;
687 }
688 if (err.angle > stats_with_outliers_.avg_err.angle +
689 FLAGS_outlier_std_devs *
690 stats_with_outliers_.std_dev.angle) {
691 return true;
692 }
693 return false;
694 }),
695 target_constraints_.end());
696
697 LOG(INFO) << "Removed " << (original_size - target_constraints_.size())
698 << " outlier constraints out of " << original_size << " total";
699}
700
701void TargetMapper::DumpStats(std::string_view path) const {
702 LOG(INFO) << "Dumping mapping stats to " << path;
703 Stats stats = ComputeStats();
704 std::ofstream fout(path.data());
705 fout << "Stats after outlier rejection: " << std::endl;
706 fout << "Average error - angle: " << stats.avg_err.angle
707 << ", distance: " << stats.avg_err.distance << std::endl
708 << std::endl;
709 fout << "Standard deviation - angle: " << stats.std_dev.angle
710 << ", distance: " << stats.std_dev.distance << std::endl
711 << std::endl;
712 fout << "Max error - angle: " << stats.max_err.angle
713 << ", distance: " << stats.max_err.distance << std::endl;
714
715 fout << std::endl << "Stats before outlier rejection:" << std::endl;
716 fout << "Average error - angle: " << stats_with_outliers_.avg_err.angle
717 << ", distance: " << stats_with_outliers_.avg_err.distance << std::endl
718 << std::endl;
719 fout << "Standard deviation - angle: " << stats_with_outliers_.std_dev.angle
720 << ", distance: " << stats_with_outliers_.std_dev.distance << std::endl
721 << std::endl;
722 fout << "Max error - angle: " << stats_with_outliers_.max_err.angle
723 << ", distance: " << stats_with_outliers_.max_err.distance << std::endl;
724
725 fout.flush();
726 fout.close();
727}
728
729void TargetMapper::DumpConstraints(std::string_view path) const {
730 LOG(INFO) << "Dumping target constraints to " << path;
731 std::ofstream fout(path.data());
732 for (const auto &constraint : target_constraints_) {
733 fout << constraint << std::endl;
734 }
735 fout.flush();
736 fout.close();
737}
738
milind-ufbc5c812023-04-06 21:24:29 -0700739} // namespace frc971::vision
740
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800741std::ostream &operator<<(std::ostream &os, ceres::examples::Pose3d pose) {
milind-ufbc5c812023-04-06 21:24:29 -0700742 auto rpy = frc971::vision::PoseUtils::QuaternionToEulerAngles(pose.q);
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800743 os << absl::StrFormat(
744 "{x: %.3f, y: %.3f, z: %.3f, roll: %.3f, pitch: "
745 "%.3f, yaw: %.3f}",
746 pose.p(0), pose.p(1), pose.p(2), rpy(0), rpy(1), rpy(2));
747 return os;
748}
749
750std::ostream &operator<<(std::ostream &os,
751 ceres::examples::Constraint3d constraint) {
752 os << absl::StrFormat("{id_begin: %d, id_end: %d, pose: ",
753 constraint.id_begin, constraint.id_end)
754 << constraint.t_be << "}";
755 return os;
756}