386 lines
16 KiB
C++
386 lines
16 KiB
C++
// vslam_vsr_export -- doi ban ghi .vsr cua bo mo phong (anh IR + IMU + su
|
|
// that) sang thu muc dataset cho DM-VIO, dinh dang TUM-VI "dso".
|
|
//
|
|
// vslam_vsr_export --in X.vsr --out DIR [--robot robot.yaml]
|
|
// [--imu-hz 0] [--imu-offset-ms 0]
|
|
// [--gyro-noise 0] [--accel-noise 0] [--seed 1]
|
|
//
|
|
// Ra (DM-VIO doc theo src/dso/util/DatasetReader.h):
|
|
// DIR/cam0/images/<ns>.png anh IR 8 bit. DM-VIO ghep anh voi times.txt
|
|
// theo THU TU ten file da sap -- ten 19 chu so.
|
|
// DIR/cam0/times.txt "ns giay" moi anh.
|
|
// DIR/imu.txt "#..." roi "ns wx wy wz ax ay az". CO mau dung
|
|
// gio MOI anh: DM-VIO bo khung thieu mau trung gio.
|
|
// DIR/camera.txt pinhole kieu DSO, khong meo.
|
|
// DIR/camchain.yaml cam0/T_cam_imu (Kalibr) + noi suy.
|
|
// DIR/imu_noise.yaml mat do nhieu / bias (Kalibr imu.yaml).
|
|
// DIR/gt_imu.csv su that diem dat IMU, "ns,px,py,pz,qw,qx,qy,qz"
|
|
// (TUM-VI) -- dung cho gtFile cua DM-VIO.
|
|
// DIR/gt_cam_tum.txt su that camera (frame quang hoc), TUM.
|
|
// DIR/gt_base_tum.txt su that base_footprint, TUM.
|
|
// DIR/export.json thong so da dung.
|
|
//
|
|
// ================== LAM XAU DU LIEU O DAY, KHONG GHI LAI ==================
|
|
// Ban ghi mang IMU SACH 200 Hz. Muon thu "giong robot that" (WT61PC 10 Hz,
|
|
// lech gio camera-IMU, nhieu lon hon) thi lam o buoc xuat nay -- khong phai
|
|
// render lai anh:
|
|
// --imu-hz N giu moi k mau de con ~N Hz (0 = giu nguyen)
|
|
// --imu-offset-ms D CONG D ms vao stamp IMU: lech dong ho camera-IMU ma
|
|
// DM-VIO khong tu uoc luong
|
|
// --gyro-noise S, --accel-noise S them nhieu trang (sigma moi mau)
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <algorithm>
|
|
#include <cerrno>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <opencv2/core.hpp>
|
|
#include <opencv2/imgcodecs.hpp>
|
|
|
|
#include "vslam/geometry.h"
|
|
#include "vslam/node_constants.h"
|
|
#include "vslam/record_format.h"
|
|
#include "vslam/record_io.h"
|
|
#include "vslam/robot_model.h"
|
|
|
|
namespace {
|
|
|
|
using vslam::Transform;
|
|
|
|
struct ImuS {
|
|
int64_t t = 0;
|
|
double w[3] = {0, 0, 0};
|
|
double a[3] = {0, 0, 0};
|
|
};
|
|
|
|
struct GtS {
|
|
int64_t t = 0;
|
|
double p[6] = {0, 0, 0, 0, 0, 0}; // x y z roll pitch yaw (base_footprint)
|
|
};
|
|
|
|
struct Frame {
|
|
int64_t t = 0;
|
|
int w = 0, h = 0;
|
|
std::vector<uint8_t> px;
|
|
};
|
|
|
|
bool MakeDir(const std::string& p) {
|
|
std::string cur;
|
|
for (size_t i = 0; i <= p.size(); ++i) {
|
|
if (i == p.size() || p[i] == '/') {
|
|
if (!cur.empty() && ::mkdir(cur.c_str(), 0775) != 0 && errno != EEXIST) return false;
|
|
}
|
|
if (i < p.size()) cur.push_back(p[i]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Transform PoseOf(const GtS& g) {
|
|
Transform t;
|
|
t.translation.x = g.p[0];
|
|
t.translation.y = g.p[1];
|
|
t.translation.z = g.p[2];
|
|
t.rotation = vslam::QuaternionFromRPY(g.p[3], g.p[4], g.p[5]);
|
|
return t;
|
|
}
|
|
|
|
void WriteTum(FILE* f, int64_t t, const Transform& T) {
|
|
std::fprintf(f, "%.9f %.6f %.6f %.6f %.9f %.9f %.9f %.9f\n", t * 1e-9, T.translation.x,
|
|
T.translation.y, T.translation.z, T.rotation.x, T.rotation.y, T.rotation.z,
|
|
T.rotation.w);
|
|
}
|
|
|
|
// So theo khoa trong JSON phang cua header ("key":so). Khong co thi `def`.
|
|
double JsonNumber(const std::string& json, const std::string& key, double def) {
|
|
const std::string k = "\"" + key + "\":";
|
|
const size_t p = json.find(k);
|
|
if (p == std::string::npos) return def;
|
|
return std::strtod(json.c_str() + p + k.size(), nullptr);
|
|
}
|
|
|
|
ImuS Lerp(const ImuS& a, const ImuS& b, int64_t t) {
|
|
const double u = static_cast<double>(t - a.t) / static_cast<double>(b.t - a.t);
|
|
ImuS o;
|
|
o.t = t;
|
|
for (int i = 0; i < 3; ++i) {
|
|
o.w[i] = a.w[i] + u * (b.w[i] - a.w[i]);
|
|
o.a[i] = a.a[i] + u * (b.a[i] - a.a[i]);
|
|
}
|
|
return o;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
std::string in, out, robot_yaml;
|
|
double imu_hz = 0.0, offset_ms = 0.0, gyro_noise = 0.0, accel_noise = 0.0;
|
|
// Nhieu SAN co trong ban ghi (sigma moi mau) -- de ghi mat do nhieu dung vao
|
|
// imu_noise.yaml. vslam_record ghi chung vao header cua kich ban doc; am =
|
|
// chua biet, lay tu header.
|
|
double sim_gyro_sigma = -1.0, sim_accel_sigma = -1.0;
|
|
uint32_t seed = 1;
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : ""; };
|
|
if (a == "--in") in = next();
|
|
else if (a == "--out") out = next();
|
|
else if (a == "--robot") robot_yaml = next();
|
|
else if (a == "--imu-hz") imu_hz = std::atof(next().c_str());
|
|
else if (a == "--imu-offset-ms") offset_ms = std::atof(next().c_str());
|
|
else if (a == "--gyro-noise") gyro_noise = std::atof(next().c_str());
|
|
else if (a == "--accel-noise") accel_noise = std::atof(next().c_str());
|
|
else if (a == "--sim-gyro-sigma") sim_gyro_sigma = std::atof(next().c_str());
|
|
else if (a == "--sim-accel-sigma") sim_accel_sigma = std::atof(next().c_str());
|
|
else if (a == "--seed") seed = static_cast<uint32_t>(std::atoi(next().c_str()));
|
|
else {
|
|
std::fprintf(stderr, "tham so la: %s (xem dau file)\n", a.c_str());
|
|
return 2;
|
|
}
|
|
}
|
|
if (in.empty() || out.empty()) {
|
|
std::fprintf(stderr, "can --in va --out\n");
|
|
return 2;
|
|
}
|
|
if (robot_yaml.empty()) {
|
|
const char* cd = std::getenv("VSLAM_CONFIG_DIR");
|
|
robot_yaml = (cd != nullptr && *cd != '\0' ? std::string(cd) : std::string("configuration_files")) +
|
|
"/robot.yaml";
|
|
}
|
|
|
|
// --- Ngoai suy cam bien tu robot.yaml (CUNG nguon voi bo mo phong) ---
|
|
vslam::RobotModel robot;
|
|
const vslam::StatusResponse rs = robot.LoadFile(robot_yaml);
|
|
if (!rs.ok()) {
|
|
std::fprintf(stderr, "%s: %s\n", robot_yaml.c_str(), rs.message.c_str());
|
|
return 1;
|
|
}
|
|
Transform T_cam_imu, T_bl_cam, T_bl_imu;
|
|
if (!robot.StaticTransform(vslam::kCameraDepthOpticalFrame, vslam::kImuFrame, &T_cam_imu) ||
|
|
!robot.StaticTransform(vslam::kBaseLinkFrame, vslam::kCameraDepthOpticalFrame, &T_bl_cam) ||
|
|
!robot.StaticTransform(vslam::kBaseLinkFrame, vslam::kImuFrame, &T_bl_imu)) {
|
|
std::fprintf(stderr, "robot.yaml thieu frame camera/IMU\n");
|
|
return 1;
|
|
}
|
|
Transform T_fp_bl = vslam::TransformIdentity();
|
|
T_fp_bl.translation.z = robot.geometry().wheel_radius;
|
|
|
|
// --- Doc ban ghi ---
|
|
vslam::RecordReader r;
|
|
vslam::StatusResponse st = r.Open(in);
|
|
if (!st.ok()) {
|
|
std::fprintf(stderr, "%s: %s\n", in.c_str(), st.message.c_str());
|
|
return 1;
|
|
}
|
|
const std::string& cj = r.info().config_json;
|
|
if (sim_gyro_sigma < 0.0) sim_gyro_sigma = JsonNumber(cj, "gyro_noise_stddev", 0.0);
|
|
if (sim_accel_sigma < 0.0) sim_accel_sigma = JsonNumber(cj, "accel_noise_stddev", 0.0);
|
|
if (sim_gyro_sigma <= 0.0 || sim_accel_sigma <= 0.0) {
|
|
std::fprintf(stderr, "canh bao: header khong ghi nhieu IMU -- imu_noise.yaml chi tinh nhieu "
|
|
"them luc xuat (dung --sim-gyro-sigma / --sim-accel-sigma)\n");
|
|
}
|
|
std::vector<ImuS> imu;
|
|
std::vector<GtS> gt;
|
|
std::vector<Frame> frames;
|
|
vslam::CameraInfo info;
|
|
bool have_info = false;
|
|
vslam::VsrChunkHeader h;
|
|
std::string payload;
|
|
std::vector<uint8_t> storage;
|
|
while (r.Next(&h, &payload)) {
|
|
const int64_t t = static_cast<int64_t>(h.stamp_ns);
|
|
switch (h.stream) {
|
|
case vslam::VsrStream::kImu: {
|
|
vslam::Imu m;
|
|
if (!vslam::DecodeImuPayload(payload, &m)) break;
|
|
ImuS s;
|
|
s.t = t;
|
|
s.w[0] = m.angular_velocity.x; s.w[1] = m.angular_velocity.y; s.w[2] = m.angular_velocity.z;
|
|
s.a[0] = m.linear_acceleration.x; s.a[1] = m.linear_acceleration.y;
|
|
s.a[2] = m.linear_acceleration.z;
|
|
imu.push_back(s);
|
|
break;
|
|
}
|
|
case vslam::VsrStream::kGroundTruth: {
|
|
GtS g;
|
|
g.t = t;
|
|
if (vslam::DecodeGroundTruthPayload(payload, g.p)) gt.push_back(g);
|
|
break;
|
|
}
|
|
case vslam::VsrStream::kCameraInfo:
|
|
if (vslam::DecodeCameraInfoPayload(payload, &info)) have_info = true;
|
|
break;
|
|
case vslam::VsrStream::kInfra1: {
|
|
vslam::Image img;
|
|
if (!vslam::DecodeImagePayload(payload, &img, &storage)) break;
|
|
if (img.format != vslam::PixelFormat::kMono8) break;
|
|
Frame f;
|
|
f.t = t;
|
|
f.w = static_cast<int>(img.width);
|
|
f.h = static_cast<int>(img.height);
|
|
f.px.resize(static_cast<size_t>(f.w) * f.h);
|
|
for (int y = 0; y < f.h; ++y) {
|
|
std::copy(img.data + static_cast<size_t>(y) * img.step,
|
|
img.data + static_cast<size_t>(y) * img.step + f.w,
|
|
f.px.begin() + static_cast<long>(y) * f.w);
|
|
}
|
|
frames.push_back(std::move(f));
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
if (imu.size() < 10 || frames.empty() || !have_info) {
|
|
std::fprintf(stderr, "ban ghi thieu IMU (%zu), anh IR (%zu) hoac camera_info -- ghi bang "
|
|
"vslam_record --gl va kich ban doc\n", imu.size(), frames.size());
|
|
return 1;
|
|
}
|
|
|
|
// --- Lam xau IMU: ha tan so, lech gio, them nhieu ---
|
|
std::vector<int64_t> dts;
|
|
for (size_t i = 1; i < imu.size(); ++i) dts.push_back(imu[i].t - imu[i - 1].t);
|
|
std::nth_element(dts.begin(), dts.begin() + dts.size() / 2, dts.end());
|
|
const double src_hz = 1e9 / static_cast<double>(dts[dts.size() / 2]);
|
|
const int keep_every = imu_hz > 0.0 ? std::max(1, static_cast<int>(std::lround(src_hz / imu_hz))) : 1;
|
|
const int64_t offset_ns = static_cast<int64_t>(std::llround(offset_ms * 1e6));
|
|
std::mt19937 rng(seed);
|
|
std::normal_distribution<double> n01(0.0, 1.0);
|
|
std::vector<ImuS> q;
|
|
for (size_t i = 0; i < imu.size(); i += static_cast<size_t>(keep_every)) {
|
|
ImuS s = imu[i];
|
|
s.t += offset_ns;
|
|
for (int k = 0; k < 3; ++k) {
|
|
if (gyro_noise > 0.0) s.w[k] += gyro_noise * n01(rng);
|
|
if (accel_noise > 0.0) s.a[k] += accel_noise * n01(rng);
|
|
}
|
|
q.push_back(s);
|
|
}
|
|
const double out_hz = src_hz / keep_every;
|
|
|
|
// --- Mau IMU dung gio MOI anh (noi suy tuyen tinh) ---
|
|
std::vector<Frame*> used;
|
|
std::vector<ImuS> merged = q;
|
|
for (Frame& f : frames) {
|
|
if (f.t < q.front().t || f.t > q.back().t) continue; // ngoai khoang IMU
|
|
used.push_back(&f);
|
|
auto it = std::lower_bound(q.begin(), q.end(), f.t,
|
|
[](const ImuS& s, int64_t t) { return s.t < t; });
|
|
if (it != q.end() && it->t == f.t) continue; // da co mau trung gio
|
|
merged.push_back(Lerp(*(it - 1), *it, f.t));
|
|
}
|
|
std::sort(merged.begin(), merged.end(), [](const ImuS& a, const ImuS& b) { return a.t < b.t; });
|
|
|
|
// --- Ghi ---
|
|
if (!MakeDir(out + "/cam0/images")) {
|
|
std::fprintf(stderr, "khong tao duoc %s\n", out.c_str());
|
|
return 1;
|
|
}
|
|
FILE* ft = std::fopen((out + "/cam0/times.txt").c_str(), "w");
|
|
for (const Frame* f : used) {
|
|
char name[64];
|
|
std::snprintf(name, sizeof(name), "/cam0/images/%019lld.png", static_cast<long long>(f->t));
|
|
const cv::Mat m(f->h, f->w, CV_8UC1, const_cast<uint8_t*>(f->px.data()));
|
|
cv::imwrite(out + name, m);
|
|
std::fprintf(ft, "%lld %.9f\n", static_cast<long long>(f->t), f->t * 1e-9);
|
|
}
|
|
std::fclose(ft);
|
|
|
|
FILE* fi = std::fopen((out + "/imu.txt").c_str(), "w");
|
|
std::fprintf(fi, "#timestamp [ns] w_x w_y w_z [rad/s] a_x a_y a_z [m/s^2]\n");
|
|
for (const ImuS& s : merged) {
|
|
std::fprintf(fi, "%lld %.9f %.9f %.9f %.9f %.9f %.9f\n", static_cast<long long>(s.t), s.w[0],
|
|
s.w[1], s.w[2], s.a[0], s.a[1], s.a[2]);
|
|
}
|
|
std::fclose(fi);
|
|
|
|
// Noi suy o DO PHAN GIAI ANH: K cua camera_info la anh goc, binning bao da
|
|
// decimate bao nhieu (giong image_proc/crop_decimate).
|
|
const double bx = std::max<uint32_t>(1, info.binning_x), by = std::max<uint32_t>(1, info.binning_y);
|
|
const double fx = info.k[0] / bx, fy = info.k[4] / by, cx = info.k[2] / bx, cy = info.k[5] / by;
|
|
const int W = used.front()->w, H = used.front()->h;
|
|
FILE* fc = std::fopen((out + "/camera.txt").c_str(), "w");
|
|
std::fprintf(fc, "Pinhole %.6f %.6f %.6f %.6f 0\n%d %d\nnone\n%d %d\n", fx, fy, cx, cy, W, H, W, H);
|
|
std::fclose(fc);
|
|
|
|
// T_cam_imu dang ma tran 4x4 (Kalibr).
|
|
const vslam::Vector3 ex = vslam::RotateVector(T_cam_imu.rotation, vslam::Vector3{1, 0, 0});
|
|
const vslam::Vector3 ey = vslam::RotateVector(T_cam_imu.rotation, vslam::Vector3{0, 1, 0});
|
|
const vslam::Vector3 ez = vslam::RotateVector(T_cam_imu.rotation, vslam::Vector3{0, 0, 1});
|
|
FILE* fk = std::fopen((out + "/camchain.yaml").c_str(), "w");
|
|
std::fprintf(fk,
|
|
"# Sinh boi vslam_vsr_export tu robot.yaml (%s): camera = "
|
|
"camera_depth_optical_frame, imu = imu_link.\n"
|
|
"cam0:\n T_cam_imu:\n"
|
|
" - [%.9f, %.9f, %.9f, %.9f]\n - [%.9f, %.9f, %.9f, %.9f]\n"
|
|
" - [%.9f, %.9f, %.9f, %.9f]\n - [0.0, 0.0, 0.0, 1.0]\n"
|
|
" camera_model: pinhole\n intrinsics: [%.6f, %.6f, %.6f, %.6f]\n"
|
|
" distortion_model: radtan\n distortion_coeffs: [0.0, 0.0, 0.0, 0.0]\n"
|
|
" resolution: [%d, %d]\n timeshift_cam_imu: 0.0\n",
|
|
robot_yaml.c_str(), ex.x, ey.x, ez.x, T_cam_imu.translation.x, ex.y, ey.y, ez.y,
|
|
T_cam_imu.translation.y, ex.z, ey.z, ez.z, T_cam_imu.translation.z, fx, fy, cx, cy,
|
|
W, H);
|
|
std::fclose(fk);
|
|
|
|
// Mat do nhieu = sigma moi mau / sqrt(tan so RA): ha tan so bang cach bo mau
|
|
// (khong lay trung binh) giu nguyen sigma moi mau, nen mat do TANG. Bias: sim
|
|
// khong troi bias, ghi gia tri NHO nhung khac 0 -- bo loc can no de khong tin
|
|
// IMU tuyet doi.
|
|
const double g_sig = std::sqrt(sim_gyro_sigma * sim_gyro_sigma + gyro_noise * gyro_noise);
|
|
const double a_sig = std::sqrt(sim_accel_sigma * sim_accel_sigma + accel_noise * accel_noise);
|
|
FILE* fn = std::fopen((out + "/imu_noise.yaml").c_str(), "w");
|
|
std::fprintf(fn,
|
|
"# Kalibr imu.yaml. Nhieu tu bo mo phong (sigma moi mau %.3g rad/s, %.3g m/s^2 o "
|
|
"%.0f Hz)%s.\n"
|
|
"gyroscope_noise_density: %.6e\naccelerometer_noise_density: %.6e\n"
|
|
"gyroscope_random_walk: 1.0e-05\naccelerometer_random_walk: 1.0e-04\n"
|
|
"update_rate: %.1f\n",
|
|
sim_gyro_sigma, sim_accel_sigma, src_hz,
|
|
(gyro_noise > 0 || accel_noise > 0) ? " + nhieu them luc xuat" : "",
|
|
g_sig / std::sqrt(out_hz), a_sig / std::sqrt(out_hz), out_hz);
|
|
std::fclose(fn);
|
|
|
|
// Su that: IMU (TUM-VI csv, cho DM-VIO), camera va base (TUM, cho cham diem).
|
|
FILE* fg = std::fopen((out + "/gt_imu.csv").c_str(), "w");
|
|
FILE* fgc = std::fopen((out + "/gt_cam_tum.txt").c_str(), "w");
|
|
FILE* fgb = std::fopen((out + "/gt_base_tum.txt").c_str(), "w");
|
|
std::fprintf(fg, "#timestamp [ns],p_x,p_y,p_z,q_w,q_x,q_y,q_z\n");
|
|
std::fprintf(fgc, "# timestamp x y z qx qy qz qw -- camera_depth_optical_frame trong world\n");
|
|
std::fprintf(fgb, "# timestamp x y z qx qy qz qw -- base_footprint trong world\n");
|
|
for (const GtS& g : gt) {
|
|
const Transform T_w_fp = PoseOf(g);
|
|
const Transform T_w_bl = vslam::TransformCompose(T_w_fp, T_fp_bl);
|
|
const Transform T_w_imu = vslam::TransformCompose(T_w_bl, T_bl_imu);
|
|
const Transform T_w_cam = vslam::TransformCompose(T_w_bl, T_bl_cam);
|
|
std::fprintf(fg, "%lld,%.6f,%.6f,%.6f,%.9f,%.9f,%.9f,%.9f\n", static_cast<long long>(g.t),
|
|
T_w_imu.translation.x, T_w_imu.translation.y, T_w_imu.translation.z,
|
|
T_w_imu.rotation.w, T_w_imu.rotation.x, T_w_imu.rotation.y, T_w_imu.rotation.z);
|
|
WriteTum(fgc, g.t, T_w_cam);
|
|
WriteTum(fgb, g.t, T_w_fp);
|
|
}
|
|
std::fclose(fg);
|
|
std::fclose(fgc);
|
|
std::fclose(fgb);
|
|
|
|
FILE* fj = std::fopen((out + "/export.json").c_str(), "w");
|
|
std::fprintf(fj,
|
|
"{\"in\":\"%s\",\"robot\":\"%s\",\"images\":%zu,\"imu_src_hz\":%.3f,"
|
|
"\"imu_out_hz\":%.3f,\"imu_offset_ms\":%.3f,\"gyro_noise\":%.3g,"
|
|
"\"accel_noise\":%.3g,\"seed\":%u,\"fx\":%.4f,\"fy\":%.4f,\"cx\":%.4f,"
|
|
"\"cy\":%.4f,\"width\":%d,\"height\":%d}\n",
|
|
in.c_str(), robot_yaml.c_str(), used.size(), src_hz, out_hz, offset_ms,
|
|
gyro_noise, accel_noise, seed, fx, fy, cx, cy, W, H);
|
|
std::fclose(fj);
|
|
|
|
std::printf("xuat %zu anh %dx%d, %zu mau IMU (%.1f Hz -> %.1f Hz, lech %.1f ms), %zu su that "
|
|
"-> %s\n",
|
|
used.size(), W, H, merged.size(), src_hz, out_hz, offset_ms, gt.size(), out.c_str());
|
|
return 0;
|
|
}
|