Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 1 | #include <opencv2/aruco/charuco.hpp> |
| 2 | #include <opencv2/calib3d.hpp> |
| 3 | #include <opencv2/features2d.hpp> |
| 4 | #include <opencv2/highgui/highgui.hpp> |
| 5 | #include <opencv2/imgproc.hpp> |
| 6 | #include "Eigen/Dense" |
| 7 | #include "Eigen/Geometry" |
| 8 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 9 | #include "absl/strings/str_format.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 10 | #include "aos/events/logging/log_reader.h" |
| 11 | #include "aos/events/shm_event_loop.h" |
| 12 | #include "aos/init.h" |
| 13 | #include "aos/network/team_number.h" |
| 14 | #include "aos/time/time.h" |
| 15 | #include "aos/util/file.h" |
| 16 | #include "frc971/control_loops/drivetrain/improved_down_estimator.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 17 | #include "frc971/control_loops/quaternion_utils.h" |
| 18 | #include "frc971/wpilib/imu_batch_generated.h" |
| 19 | #include "y2020/vision/calibration_accumulator.h" |
| 20 | #include "y2020/vision/charuco_lib.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 21 | #include "y2020/vision/sift/sift_generated.h" |
| 22 | #include "y2020/vision/sift/sift_training_generated.h" |
| 23 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
| 24 | #include "y2020/vision/vision_generated.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 25 | |
| 26 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
| 27 | DEFINE_string(pi, "pi-7971-2", "Pi name to calibrate."); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 28 | |
| 29 | namespace frc971 { |
| 30 | namespace vision { |
| 31 | namespace chrono = std::chrono; |
| 32 | using aos::distributed_clock; |
| 33 | using aos::monotonic_clock; |
| 34 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 35 | class PoseFilter : public CalibrationDataObserver { |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 36 | public: |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 37 | PoseFilter() |
| 38 | : accel_(Eigen::Matrix<double, 3, 1>::Zero()), |
| 39 | omega_(Eigen::Matrix<double, 3, 1>::Zero()), |
| 40 | x_hat_(Eigen::Matrix<double, 9, 1>::Zero()), |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 41 | q_(Eigen::Matrix<double, 9, 9>::Zero()) {} |
| 42 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 43 | void UpdateCamera(distributed_clock::time_point t, |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 44 | std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) override { |
| 45 | Integrate(t); |
| 46 | // TODO(austin): take the sample. |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 47 | LOG(INFO) << t << " Camera " << rt.second.transpose(); |
| 48 | } |
| 49 | |
| 50 | void UpdateIMU(distributed_clock::time_point t, |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 51 | std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) override { |
| 52 | Integrate(t); |
| 53 | omega_ = wa.first; |
| 54 | accel_ = wa.second; |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 55 | LOG(INFO) << t << " IMU " << wa.first.transpose(); |
| 56 | } |
| 57 | |
| 58 | private: |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 59 | void Integrate(distributed_clock::time_point t) { LOG(INFO) << t; } |
| 60 | |
| 61 | Eigen::Matrix<double, 3, 1> accel_; |
| 62 | Eigen::Matrix<double, 3, 1> omega_; |
| 63 | |
| 64 | // TODO(austin): Actually use these. Or make a new "callback" object which |
| 65 | // has these. |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 66 | Eigen::Matrix<double, 9, 1> x_hat_; |
| 67 | Eigen::Matrix<double, 9, 9> q_; |
| 68 | |
| 69 | // Proposed filter states: |
| 70 | // States: |
| 71 | // xyz position |
| 72 | // xyz velocity |
| 73 | // orientation rotation vector |
| 74 | // |
| 75 | // Inputs |
| 76 | // xyz accel |
| 77 | // angular rates |
| 78 | // |
| 79 | // Measurement: |
| 80 | // xyz position |
| 81 | // orientation rotation vector |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | void Main(int argc, char **argv) { |
| 85 | CalibrationData data; |
| 86 | |
| 87 | { |
| 88 | // Now, accumulate all the data into the data object. |
| 89 | aos::logger::LogReader reader( |
| 90 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
| 91 | |
| 92 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 93 | reader.Register(&factory); |
| 94 | |
| 95 | CHECK(aos::configuration::MultiNode(reader.configuration())); |
| 96 | |
| 97 | // Find the nodes we care about. |
| 98 | const aos::Node *const roborio_node = |
| 99 | aos::configuration::GetNode(factory.configuration(), "roborio"); |
| 100 | |
| 101 | std::optional<uint16_t> pi_number = aos::network::ParsePiNumber(FLAGS_pi); |
| 102 | CHECK(pi_number); |
| 103 | LOG(INFO) << "Pi " << *pi_number; |
| 104 | const aos::Node *const pi_node = aos::configuration::GetNode( |
| 105 | factory.configuration(), absl::StrCat("pi", *pi_number)); |
| 106 | |
| 107 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 108 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 109 | |
| 110 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 111 | factory.MakeEventLoop("calibration", roborio_node); |
| 112 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 113 | factory.MakeEventLoop("calibration", pi_node); |
| 114 | |
| 115 | // Now, hook Calibration up to everything. |
| 116 | Calibration extractor(&factory, pi_event_loop.get(), |
| 117 | roborio_event_loop.get(), FLAGS_pi, &data); |
| 118 | |
| 119 | factory.Run(); |
| 120 | |
| 121 | reader.Deregister(); |
| 122 | } |
| 123 | |
| 124 | LOG(INFO) << "Done with event_loop running"; |
| 125 | // And now we have it, we can start processing it. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 126 | |
| 127 | { |
| 128 | PoseFilter filter; |
| 129 | data.ReviewData(&filter); |
| 130 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } // namespace vision |
| 134 | } // namespace frc971 |
| 135 | |
| 136 | int main(int argc, char **argv) { |
| 137 | aos::InitGoogle(&argc, &argv); |
| 138 | |
| 139 | frc971::vision::Main(argc, argv); |
| 140 | } |