Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 1 | #include "Eigen/Dense" |
| 2 | #include "Eigen/Geometry" |
| 3 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 4 | #include "absl/strings/str_format.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 5 | #include "aos/events/logging/log_reader.h" |
| 6 | #include "aos/events/shm_event_loop.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/network/team_number.h" |
| 9 | #include "aos/time/time.h" |
| 10 | #include "aos/util/file.h" |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 11 | #include "ceres/ceres.h" |
| 12 | #include "frc971/analysis/in_process_plotter.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/improved_down_estimator.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/quaternion_utils.h" |
| 15 | #include "frc971/wpilib/imu_batch_generated.h" |
| 16 | #include "y2020/vision/calibration_accumulator.h" |
| 17 | #include "y2020/vision/charuco_lib.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 18 | #include "y2020/vision/sift/sift_generated.h" |
| 19 | #include "y2020/vision/sift/sift_training_generated.h" |
| 20 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
| 21 | #include "y2020/vision/vision_generated.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 22 | |
| 23 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
| 24 | DEFINE_string(pi, "pi-7971-2", "Pi name to calibrate."); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 25 | |
| 26 | namespace frc971 { |
| 27 | namespace vision { |
| 28 | namespace chrono = std::chrono; |
| 29 | using aos::distributed_clock; |
| 30 | using aos::monotonic_clock; |
| 31 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 32 | // The basic ideas here are taken from Kalibr. |
| 33 | // (https://github.com/ethz-asl/kalibr), but adapted to work with AOS, and to be |
| 34 | // simpler. |
| 35 | // |
| 36 | // Camera readings and IMU readings come in at different times, on different |
| 37 | // time scales. Our first problem is to align them in time so we can actually |
| 38 | // compute an error. This is done in the calibration accumulator code. The |
| 39 | // kalibr paper uses splines, while this uses kalman filters to solve the same |
| 40 | // interpolation problem so we can get the expected vs actual pose at the time |
| 41 | // each image arrives. |
| 42 | // |
| 43 | // The cost function is then fed the computed angular and positional error for |
| 44 | // each camera sample before the kalman filter update. Intuitively, the smaller |
| 45 | // the corrections to the kalman filter each step, the better the estimate |
| 46 | // should be. |
| 47 | // |
| 48 | // We don't actually implement the angular kalman filter because the IMU is so |
| 49 | // good. We give the solver an initial position and bias, and let it solve from |
| 50 | // there. This lets us represent drift that is linear in time, which should be |
| 51 | // good enough for ~1 minute calibration. |
| 52 | // |
| 53 | // TODO(austin): Kalman smoother ala |
| 54 | // https://stanford.edu/~boyd/papers/pdf/auto_ks.pdf should allow for better |
| 55 | // parallelism, and since we aren't causal, will take that into account a lot |
| 56 | // better. |
| 57 | |
| 58 | // This class takes the initial parameters and biases, and computes the error |
| 59 | // between the measured and expected camera readings. When optimized, this |
| 60 | // gives us a cost function to minimize. |
| 61 | template <typename Scalar> |
| 62 | class CeresPoseFilter : public CalibrationDataObserver { |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 63 | public: |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 64 | CeresPoseFilter(Eigen::Quaternion<Scalar> initial_orientation, |
| 65 | Eigen::Quaternion<Scalar> imu_to_camera, |
| 66 | Eigen::Matrix<Scalar, 3, 1> imu_bias) |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 67 | : accel_(Eigen::Matrix<double, 3, 1>::Zero()), |
| 68 | omega_(Eigen::Matrix<double, 3, 1>::Zero()), |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 69 | imu_bias_(imu_bias), |
| 70 | orientation_(initial_orientation), |
| 71 | x_hat_(Eigen::Matrix<Scalar, 6, 1>::Zero()), |
| 72 | p_(Eigen::Matrix<Scalar, 6, 6>::Zero()), |
| 73 | imu_to_camera_(imu_to_camera) {} |
| 74 | |
| 75 | virtual void ObserveCameraUpdate(distributed_clock::time_point /*t*/, |
| 76 | Eigen::Vector3d /*board_to_camera_rotation*/, |
| 77 | Eigen::Quaternion<Scalar> /*imu_to_world*/) { |
| 78 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 79 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 80 | void UpdateCamera(distributed_clock::time_point t, |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 81 | std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) override { |
| 82 | Integrate(t); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 83 | |
| 84 | Eigen::Quaternion<Scalar> board_to_camera( |
| 85 | frc971::controls::ToQuaternionFromRotationVector(rt.first) |
| 86 | .cast<Scalar>()); |
| 87 | |
| 88 | // This converts us from (facing the board), |
| 89 | // x right, y up, z towards us -> x right, y away, z up. |
| 90 | // Confirmed to be right. |
| 91 | Eigen::Quaternion<Scalar> board_to_world( |
| 92 | Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitX()).cast<Scalar>()); |
| 93 | |
| 94 | // Want world -> imu rotation. |
| 95 | // world <- board <- camera <- imu. |
| 96 | const Eigen::Quaternion<Scalar> imu_to_world = |
| 97 | board_to_world * board_to_camera.inverse() * imu_to_camera_; |
| 98 | |
| 99 | const Eigen::Quaternion<Scalar> error(imu_to_world.inverse() * |
| 100 | orientation()); |
| 101 | |
| 102 | errors_.emplace_back( |
| 103 | Eigen::Matrix<Scalar, 3, 1>(error.x(), error.y(), error.z())); |
| 104 | |
| 105 | ObserveCameraUpdate(t, rt.first, imu_to_world); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 106 | } |
| 107 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 108 | virtual void ObserveIMUUpdate( |
| 109 | distributed_clock::time_point /*t*/, |
| 110 | std::pair<Eigen::Vector3d, Eigen::Vector3d> /*wa*/) {} |
| 111 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 112 | void UpdateIMU(distributed_clock::time_point t, |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 113 | std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) override { |
| 114 | Integrate(t); |
| 115 | omega_ = wa.first; |
| 116 | accel_ = wa.second; |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 117 | |
| 118 | ObserveIMUUpdate(t, wa); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 119 | } |
| 120 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 121 | const Eigen::Quaternion<Scalar> &orientation() const { return orientation_; } |
| 122 | |
| 123 | std::vector<Eigen::Matrix<Scalar, 3, 1> > errors_; |
| 124 | |
| 125 | // Returns the angular errors for each camera sample. |
| 126 | size_t num_errors() const { return errors_.size(); } |
| 127 | Scalar errorx(size_t i) const { return errors_[i].x(); } |
| 128 | Scalar errory(size_t i) const { return errors_[i].y(); } |
| 129 | Scalar errorz(size_t i) const { return errors_[i].z(); } |
| 130 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 131 | private: |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 132 | Eigen::Matrix<Scalar, 46, 1> Pack(Eigen::Quaternion<Scalar> q, |
| 133 | Eigen::Matrix<Scalar, 6, 1> x_hat, |
| 134 | Eigen::Matrix<Scalar, 6, 6> p) { |
| 135 | Eigen::Matrix<Scalar, 46, 1> result = Eigen::Matrix<Scalar, 46, 1>::Zero(); |
| 136 | result.template block<4, 1>(0, 0) = q.coeffs(); |
| 137 | result.template block<6, 1>(4, 0) = x_hat; |
| 138 | result.template block<36, 1>(10, 0) = |
| 139 | Eigen::Map<Eigen::Matrix<Scalar, 36, 1> >(p.data(), p.size()); |
| 140 | |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | std::tuple<Eigen::Quaternion<Scalar>, Eigen::Matrix<Scalar, 6, 1>, |
| 145 | Eigen::Matrix<Scalar, 6, 6> > |
| 146 | UnPack(Eigen::Matrix<Scalar, 46, 1> input) { |
| 147 | Eigen::Quaternion<Scalar> q(input.template block<4, 1>(0, 0)); |
| 148 | Eigen::Matrix<Scalar, 6, 1> x_hat(input.template block<6, 1>(4, 0)); |
| 149 | Eigen::Matrix<Scalar, 6, 6> p = |
| 150 | Eigen::Map<Eigen::Matrix<Scalar, 6, 6> >(input.data() + 10, 6, 6); |
| 151 | return std::make_tuple(q, x_hat, p); |
| 152 | } |
| 153 | |
| 154 | Eigen::Matrix<Scalar, 46, 1> Derivitive( |
| 155 | const Eigen::Matrix<Scalar, 46, 1> &input) { |
| 156 | auto [q, x_hat, p] = UnPack(input); |
| 157 | |
| 158 | Eigen::Quaternion<Scalar> omega_q; |
| 159 | omega_q.w() = Scalar(0.0); |
| 160 | omega_q.vec() = 0.5 * (omega_.cast<Scalar>() - imu_bias_); |
| 161 | Eigen::Matrix<Scalar, 4, 1> q_dot = (q * omega_q).coeffs(); |
| 162 | |
| 163 | Eigen::Matrix<Scalar, 6, 1> x_hat_dot = Eigen::Matrix<Scalar, 6, 1>::Zero(); |
| 164 | x_hat_dot(0, 0) = x_hat(3, 0); |
| 165 | x_hat_dot(1, 0) = x_hat(4, 0); |
| 166 | x_hat_dot(2, 0) = x_hat(5, 0); |
| 167 | x_hat_dot.template block<3, 1>(3, 0) = accel_.cast<Scalar>(); |
| 168 | |
| 169 | Eigen::Matrix<Scalar, 6, 6> p_dot = Eigen::Matrix<Scalar, 6, 6>::Zero(); |
| 170 | |
| 171 | return Pack(Eigen::Quaternion<Scalar>(q_dot), x_hat_dot, p_dot); |
| 172 | } |
| 173 | |
| 174 | virtual void ObserveIntegrated(distributed_clock::time_point /*t*/, |
| 175 | Eigen::Matrix<Scalar, 6, 1> /*x_hat*/, |
| 176 | Eigen::Quaternion<Scalar> /*orientation*/) {} |
| 177 | |
| 178 | void Integrate(distributed_clock::time_point t) { |
| 179 | if (last_time_ != distributed_clock::min_time) { |
| 180 | Eigen::Matrix<Scalar, 46, 1> next = control_loops::RungeKutta( |
| 181 | [this](auto r) { return Derivitive(r); }, |
| 182 | Pack(orientation_, x_hat_, p_), |
| 183 | aos::time::DurationInSeconds(t - last_time_)); |
| 184 | |
| 185 | std::tie(orientation_, x_hat_, p_) = UnPack(next); |
| 186 | |
| 187 | // Normalize q so it doesn't drift. |
| 188 | orientation_.normalize(); |
| 189 | } |
| 190 | |
| 191 | last_time_ = t; |
| 192 | ObserveIntegrated(t, x_hat_, orientation_); |
| 193 | } |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 194 | |
| 195 | Eigen::Matrix<double, 3, 1> accel_; |
| 196 | Eigen::Matrix<double, 3, 1> omega_; |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 197 | Eigen::Matrix<Scalar, 3, 1> imu_bias_; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 198 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 199 | Eigen::Quaternion<Scalar> orientation_; |
| 200 | Eigen::Matrix<Scalar, 6, 1> x_hat_; |
| 201 | Eigen::Matrix<Scalar, 6, 6> p_; |
| 202 | distributed_clock::time_point last_time_ = distributed_clock::min_time; |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 203 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 204 | Eigen::Quaternion<Scalar> imu_to_camera_; |
| 205 | |
| 206 | // States outside the KF: |
| 207 | // orientation quaternion |
| 208 | // |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 209 | // States: |
| 210 | // xyz position |
| 211 | // xyz velocity |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 212 | // |
| 213 | // Inputs |
| 214 | // xyz accel |
| 215 | // angular rates |
| 216 | // |
| 217 | // Measurement: |
| 218 | // xyz position |
| 219 | // orientation rotation vector |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 222 | // Subclass of the filter above which has plotting. This keeps debug code and |
| 223 | // actual code separate. |
| 224 | class PoseFilter : public CeresPoseFilter<double> { |
| 225 | public: |
| 226 | PoseFilter(Eigen::Quaternion<double> initial_orientation, |
| 227 | Eigen::Quaternion<double> imu_to_camera, |
| 228 | Eigen::Matrix<double, 3, 1> imu_bias) |
| 229 | : CeresPoseFilter<double>(initial_orientation, imu_to_camera, imu_bias) {} |
| 230 | |
| 231 | void Plot() { |
| 232 | std::vector<double> x; |
| 233 | std::vector<double> y; |
| 234 | std::vector<double> z; |
| 235 | for (const Eigen::Quaternion<double> &q : orientations_) { |
| 236 | Eigen::Matrix<double, 3, 1> rotation_vector = |
| 237 | frc971::controls::ToRotationVectorFromQuaternion(q); |
| 238 | x.emplace_back(rotation_vector(0, 0)); |
| 239 | y.emplace_back(rotation_vector(1, 0)); |
| 240 | z.emplace_back(rotation_vector(2, 0)); |
| 241 | } |
| 242 | frc971::analysis::Plotter plotter; |
| 243 | plotter.AddFigure("position"); |
| 244 | plotter.AddLine(times_, x, "x_hat(0)"); |
| 245 | plotter.AddLine(times_, y, "x_hat(1)"); |
| 246 | plotter.AddLine(times_, z, "x_hat(2)"); |
| 247 | plotter.AddLine(ct, cx, "Camera x"); |
| 248 | plotter.AddLine(ct, cy, "Camera y"); |
| 249 | plotter.AddLine(ct, cz, "Camera z"); |
| 250 | plotter.AddLine(ct, cerrx, "Camera error x"); |
| 251 | plotter.AddLine(ct, cerry, "Camera error y"); |
| 252 | plotter.AddLine(ct, cerrz, "Camera error z"); |
| 253 | plotter.Publish(); |
| 254 | |
| 255 | plotter.AddFigure("error"); |
| 256 | plotter.AddLine(times_, x, "x_hat(0)"); |
| 257 | plotter.AddLine(times_, y, "x_hat(1)"); |
| 258 | plotter.AddLine(times_, z, "x_hat(2)"); |
| 259 | plotter.AddLine(ct, cerrx, "Camera error x"); |
| 260 | plotter.AddLine(ct, cerry, "Camera error y"); |
| 261 | plotter.AddLine(ct, cerrz, "Camera error z"); |
| 262 | plotter.Publish(); |
| 263 | |
| 264 | plotter.AddFigure("imu"); |
| 265 | plotter.AddLine(ct, world_gravity_x, "world_gravity(0)"); |
| 266 | plotter.AddLine(ct, world_gravity_y, "world_gravity(1)"); |
| 267 | plotter.AddLine(ct, world_gravity_z, "world_gravity(2)"); |
| 268 | plotter.AddLine(imut, imu_x, "imu x"); |
| 269 | plotter.AddLine(imut, imu_y, "imu y"); |
| 270 | plotter.AddLine(imut, imu_z, "imu z"); |
| 271 | plotter.Publish(); |
| 272 | |
| 273 | plotter.AddFigure("raw"); |
| 274 | plotter.AddLine(imut, imu_x, "imu x"); |
| 275 | plotter.AddLine(imut, imu_y, "imu y"); |
| 276 | plotter.AddLine(imut, imu_z, "imu z"); |
| 277 | plotter.AddLine(imut, imu_ratex, "omega x"); |
| 278 | plotter.AddLine(imut, imu_ratey, "omega y"); |
| 279 | plotter.AddLine(imut, imu_ratez, "omega z"); |
| 280 | plotter.AddLine(ct, raw_cx, "Camera x"); |
| 281 | plotter.AddLine(ct, raw_cy, "Camera y"); |
| 282 | plotter.AddLine(ct, raw_cz, "Camera z"); |
| 283 | plotter.Publish(); |
| 284 | |
| 285 | plotter.Spin(); |
| 286 | } |
| 287 | |
| 288 | void ObserveIntegrated(distributed_clock::time_point t, |
| 289 | Eigen::Matrix<double, 6, 1> x_hat, |
| 290 | Eigen::Quaternion<double> orientation) override { |
| 291 | times_.emplace_back(chrono::duration<double>(t.time_since_epoch()).count()); |
| 292 | x_hats_.emplace_back(x_hat); |
| 293 | orientations_.emplace_back(orientation); |
| 294 | } |
| 295 | |
| 296 | void ObserveIMUUpdate( |
| 297 | distributed_clock::time_point t, |
| 298 | std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) override { |
| 299 | imut.emplace_back(chrono::duration<double>(t.time_since_epoch()).count()); |
| 300 | imu_ratex.emplace_back(wa.first.x()); |
| 301 | imu_ratey.emplace_back(wa.first.y()); |
| 302 | imu_ratez.emplace_back(wa.first.z()); |
| 303 | imu_x.emplace_back(wa.second.x()); |
| 304 | imu_y.emplace_back(wa.second.y()); |
| 305 | imu_z.emplace_back(wa.second.z()); |
| 306 | |
| 307 | last_accel_ = wa.second; |
| 308 | } |
| 309 | |
| 310 | void ObserveCameraUpdate(distributed_clock::time_point t, |
| 311 | Eigen::Vector3d board_to_camera_rotation, |
| 312 | Eigen::Quaternion<double> imu_to_world) override { |
| 313 | raw_cx.emplace_back(board_to_camera_rotation(0, 0)); |
| 314 | raw_cy.emplace_back(board_to_camera_rotation(1, 0)); |
| 315 | raw_cz.emplace_back(board_to_camera_rotation(2, 0)); |
| 316 | |
| 317 | Eigen::Matrix<double, 3, 1> rotation_vector = |
| 318 | frc971::controls::ToRotationVectorFromQuaternion(imu_to_world); |
| 319 | ct.emplace_back(chrono::duration<double>(t.time_since_epoch()).count()); |
| 320 | |
| 321 | Eigen::Matrix<double, 3, 1> cerr = |
| 322 | frc971::controls::ToRotationVectorFromQuaternion( |
| 323 | imu_to_world.inverse() * orientation()); |
| 324 | |
| 325 | cx.emplace_back(rotation_vector(0, 0)); |
| 326 | cy.emplace_back(rotation_vector(1, 0)); |
| 327 | cz.emplace_back(rotation_vector(2, 0)); |
| 328 | |
| 329 | cerrx.emplace_back(cerr(0, 0)); |
| 330 | cerry.emplace_back(cerr(1, 0)); |
| 331 | cerrz.emplace_back(cerr(2, 0)); |
| 332 | |
| 333 | const Eigen::Vector3d world_gravity = imu_to_world * last_accel_; |
| 334 | |
| 335 | world_gravity_x.emplace_back(world_gravity.x()); |
| 336 | world_gravity_y.emplace_back(world_gravity.y()); |
| 337 | world_gravity_z.emplace_back(world_gravity.z()); |
| 338 | } |
| 339 | |
| 340 | std::vector<double> ct; |
| 341 | std::vector<double> cx; |
| 342 | std::vector<double> cy; |
| 343 | std::vector<double> cz; |
| 344 | std::vector<double> raw_cx; |
| 345 | std::vector<double> raw_cy; |
| 346 | std::vector<double> raw_cz; |
| 347 | std::vector<double> cerrx; |
| 348 | std::vector<double> cerry; |
| 349 | std::vector<double> cerrz; |
| 350 | |
| 351 | std::vector<double> world_gravity_x; |
| 352 | std::vector<double> world_gravity_y; |
| 353 | std::vector<double> world_gravity_z; |
| 354 | std::vector<double> imu_x; |
| 355 | std::vector<double> imu_y; |
| 356 | std::vector<double> imu_z; |
| 357 | |
| 358 | std::vector<double> imut; |
| 359 | std::vector<double> imu_ratex; |
| 360 | std::vector<double> imu_ratey; |
| 361 | std::vector<double> imu_ratez; |
| 362 | |
| 363 | std::vector<double> times_; |
| 364 | std::vector<Eigen::Matrix<double, 6, 1> > x_hats_; |
| 365 | std::vector<Eigen::Quaternion<double> > orientations_; |
| 366 | |
| 367 | Eigen::Matrix<double, 3, 1> last_accel_ = Eigen::Matrix<double, 3, 1>::Zero(); |
| 368 | }; |
| 369 | |
| 370 | // Adapter class from the KF above to a Ceres cost function. |
| 371 | struct CostFunctor { |
| 372 | CostFunctor(CalibrationData *d) : data(d) {} |
| 373 | |
| 374 | CalibrationData *data; |
| 375 | |
| 376 | template <typename S> |
| 377 | bool operator()(const S *const q1, const S *const q2, const S *const v, |
| 378 | S *residual) const { |
| 379 | Eigen::Quaternion<S> initial_orientation(q1[3], q1[0], q1[1], q1[2]); |
| 380 | Eigen::Quaternion<S> mounting_orientation(q2[3], q2[0], q2[1], q2[2]); |
| 381 | Eigen::Matrix<S, 3, 1> imu_bias(v[0], v[1], v[2]); |
| 382 | |
| 383 | CeresPoseFilter<S> filter(initial_orientation, mounting_orientation, |
| 384 | imu_bias); |
| 385 | data->ReviewData(&filter); |
| 386 | |
| 387 | for (size_t i = 0; i < filter.num_errors(); ++i) { |
| 388 | residual[3 * i + 0] = filter.errorx(i); |
| 389 | residual[3 * i + 1] = filter.errory(i); |
| 390 | residual[3 * i + 2] = filter.errorz(i); |
| 391 | } |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | }; |
| 396 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 397 | void Main(int argc, char **argv) { |
| 398 | CalibrationData data; |
| 399 | |
| 400 | { |
| 401 | // Now, accumulate all the data into the data object. |
| 402 | aos::logger::LogReader reader( |
| 403 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
| 404 | |
| 405 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 406 | reader.Register(&factory); |
| 407 | |
| 408 | CHECK(aos::configuration::MultiNode(reader.configuration())); |
| 409 | |
| 410 | // Find the nodes we care about. |
| 411 | const aos::Node *const roborio_node = |
| 412 | aos::configuration::GetNode(factory.configuration(), "roborio"); |
| 413 | |
| 414 | std::optional<uint16_t> pi_number = aos::network::ParsePiNumber(FLAGS_pi); |
| 415 | CHECK(pi_number); |
| 416 | LOG(INFO) << "Pi " << *pi_number; |
| 417 | const aos::Node *const pi_node = aos::configuration::GetNode( |
| 418 | factory.configuration(), absl::StrCat("pi", *pi_number)); |
| 419 | |
| 420 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 421 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 422 | |
| 423 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 424 | factory.MakeEventLoop("calibration", roborio_node); |
| 425 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 426 | factory.MakeEventLoop("calibration", pi_node); |
| 427 | |
| 428 | // Now, hook Calibration up to everything. |
| 429 | Calibration extractor(&factory, pi_event_loop.get(), |
| 430 | roborio_event_loop.get(), FLAGS_pi, &data); |
| 431 | |
| 432 | factory.Run(); |
| 433 | |
| 434 | reader.Deregister(); |
| 435 | } |
| 436 | |
| 437 | LOG(INFO) << "Done with event_loop running"; |
| 438 | // And now we have it, we can start processing it. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 439 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 440 | Eigen::Quaternion<double> nominal_initial_orientation( |
| 441 | frc971::controls::ToQuaternionFromRotationVector( |
| 442 | Eigen::Vector3d(0.0, 0.0, M_PI))); |
| 443 | Eigen::Quaternion<double> nominal_imu_to_camera( |
| 444 | Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX())); |
| 445 | |
| 446 | Eigen::Quaternion<double> initial_orientation = |
| 447 | Eigen::Quaternion<double>::Identity(); |
| 448 | Eigen::Quaternion<double> imu_to_camera = |
| 449 | Eigen::Quaternion<double>::Identity(); |
| 450 | Eigen::Vector3d imu_bias = Eigen::Vector3d::Zero(); |
| 451 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 452 | { |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame^] | 453 | ceres::Problem problem; |
| 454 | |
| 455 | ceres::EigenQuaternionParameterization *quaternion_local_parameterization = |
| 456 | new ceres::EigenQuaternionParameterization(); |
| 457 | // Set up the only cost function (also known as residual). This uses |
| 458 | // auto-differentiation to obtain the derivative (jacobian). |
| 459 | |
| 460 | ceres::CostFunction *cost_function = |
| 461 | new ceres::AutoDiffCostFunction<CostFunctor, ceres::DYNAMIC, 4, 4, 3>( |
| 462 | new CostFunctor(&data), data.camera_samples_size() * 3); |
| 463 | problem.AddResidualBlock(cost_function, nullptr, |
| 464 | initial_orientation.coeffs().data(), |
| 465 | imu_to_camera.coeffs().data(), imu_bias.data()); |
| 466 | problem.SetParameterization(initial_orientation.coeffs().data(), |
| 467 | quaternion_local_parameterization); |
| 468 | problem.SetParameterization(imu_to_camera.coeffs().data(), |
| 469 | quaternion_local_parameterization); |
| 470 | for (int i = 0; i < 3; ++i) { |
| 471 | problem.SetParameterLowerBound(imu_bias.data(), i, -0.05); |
| 472 | problem.SetParameterUpperBound(imu_bias.data(), i, 0.05); |
| 473 | } |
| 474 | |
| 475 | // Run the solver! |
| 476 | ceres::Solver::Options options; |
| 477 | options.minimizer_progress_to_stdout = true; |
| 478 | options.gradient_tolerance = 1e-12; |
| 479 | options.function_tolerance = 1e-16; |
| 480 | options.parameter_tolerance = 1e-12; |
| 481 | ceres::Solver::Summary summary; |
| 482 | Solve(options, &problem, &summary); |
| 483 | LOG(INFO) << summary.FullReport(); |
| 484 | |
| 485 | LOG(INFO) << "Nominal initial_orientation " |
| 486 | << nominal_initial_orientation.coeffs().transpose(); |
| 487 | LOG(INFO) << "Nominal imu_to_camera " |
| 488 | << nominal_imu_to_camera.coeffs().transpose(); |
| 489 | |
| 490 | LOG(INFO) << "initial_orientation " |
| 491 | << initial_orientation.coeffs().transpose(); |
| 492 | LOG(INFO) << "imu_to_camera " << imu_to_camera.coeffs().transpose(); |
| 493 | LOG(INFO) << "imu_to_camera(rotation) " |
| 494 | << frc971::controls::ToRotationVectorFromQuaternion(imu_to_camera) |
| 495 | .transpose(); |
| 496 | LOG(INFO) << "imu_to_camera delta " |
| 497 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 498 | imu_to_camera * nominal_imu_to_camera.inverse()) |
| 499 | .transpose(); |
| 500 | LOG(INFO) << "imu_bias " << imu_bias.transpose(); |
| 501 | } |
| 502 | |
| 503 | { |
| 504 | PoseFilter filter(initial_orientation, imu_to_camera, imu_bias); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 505 | data.ReviewData(&filter); |
| 506 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | } // namespace vision |
| 510 | } // namespace frc971 |
| 511 | |
| 512 | int main(int argc, char **argv) { |
| 513 | aos::InitGoogle(&argc, &argv); |
| 514 | |
| 515 | frc971::vision::Main(argc, argv); |
| 516 | } |