blob: be64efbcdc7d2fef7d845b0a250abe65f944d10f [file] [log] [blame]
Austin Schuhbb4aae72021-10-08 22:12:25 -07001#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 Schuhbb4aae72021-10-08 22:12:25 -07009#include "absl/strings/str_format.h"
Austin Schuhbb4aae72021-10-08 22:12:25 -070010#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-u8c72d532021-12-11 15:02:42 -080017#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 Schuhbb4aae72021-10-08 22:12:25 -070021#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 Schuhbb4aae72021-10-08 22:12:25 -070025
26DEFINE_string(config, "config.json", "Path to the config file to use.");
27DEFINE_string(pi, "pi-7971-2", "Pi name to calibrate.");
Austin Schuhbb4aae72021-10-08 22:12:25 -070028
29namespace frc971 {
30namespace vision {
31namespace chrono = std::chrono;
32using aos::distributed_clock;
33using aos::monotonic_clock;
34
milind-u8c72d532021-12-11 15:02:42 -080035class PoseFilter : public CalibrationDataObserver {
Austin Schuhbb4aae72021-10-08 22:12:25 -070036 public:
milind-u8c72d532021-12-11 15:02:42 -080037 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 Schuhbb4aae72021-10-08 22:12:25 -070041 q_(Eigen::Matrix<double, 9, 9>::Zero()) {}
42
Austin Schuhbb4aae72021-10-08 22:12:25 -070043 void UpdateCamera(distributed_clock::time_point t,
milind-u8c72d532021-12-11 15:02:42 -080044 std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) override {
45 Integrate(t);
46 // TODO(austin): take the sample.
Austin Schuhbb4aae72021-10-08 22:12:25 -070047 LOG(INFO) << t << " Camera " << rt.second.transpose();
48 }
49
50 void UpdateIMU(distributed_clock::time_point t,
milind-u8c72d532021-12-11 15:02:42 -080051 std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) override {
52 Integrate(t);
53 omega_ = wa.first;
54 accel_ = wa.second;
Austin Schuhbb4aae72021-10-08 22:12:25 -070055 LOG(INFO) << t << " IMU " << wa.first.transpose();
56 }
57
58 private:
milind-u8c72d532021-12-11 15:02:42 -080059 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 Schuhbb4aae72021-10-08 22:12:25 -070066 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 Schuhbb4aae72021-10-08 22:12:25 -070082};
83
84void 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-u8c72d532021-12-11 15:02:42 -0800126
127 {
128 PoseFilter filter;
129 data.ReviewData(&filter);
130 }
Austin Schuhbb4aae72021-10-08 22:12:25 -0700131}
132
133} // namespace vision
134} // namespace frc971
135
136int main(int argc, char **argv) {
137 aos::InitGoogle(&argc, &argv);
138
139 frc971::vision::Main(argc, argv);
140}