sim: dia hinh 3D -- doc va san cao tu model vslam_terrain_*, robot di tren mat san, LiDAR tia 3D, ground truth 6-DoF, world slope_test (5 doc 5-30 do)

This commit is contained in:
trung09122002
2026-10-03 18:31:13 +07:00
parent be2aa17e3e
commit 26ee1a3b72
27 changed files with 2373 additions and 37 deletions

View File

@@ -1,10 +1,12 @@
#include "vslam/sim_source.h"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <mutex>
#include <random>
#include <thread>
#include <vector>
#include "vslam/geometry.h"
#include "vslam/log.h"
@@ -16,6 +18,33 @@
namespace vslam {
namespace {
// Raycast dia hinh 3D: chi noi suy san khi tia da xuong trong dai nay tren
// mat san cua o. Lon hon nhieu sai khac giua gia tri o va gia tri noi suy
// (o doc 30 do: < 1.5 cm), nho hon do cao LiDAR (0.2165 m).
constexpr double kSmoothFloorBand = 0.15;
// Ma tran xoay R = Rz(yaw) * Ry(pitch) * Rx(roll), REP-103: than -> the gioi.
// pitch DUONG = mui chuc xuong, roll DUONG = ben trai nhac len. Tinh MOT lan
// cho moi scan / moi khung anh, khong goi sin/cos cho tung tia.
struct Rot3 {
double m[9];
static Rot3 FromRPY(double roll, double pitch, double yaw) {
const double cr = std::cos(roll), sr = std::sin(roll);
const double cp = std::cos(pitch), sp = std::sin(pitch);
const double cy = std::cos(yaw), sy = std::sin(yaw);
Rot3 r;
r.m[0] = cy * cp; r.m[1] = cy * sp * sr - sy * cr; r.m[2] = cy * sp * cr + sy * sr;
r.m[3] = sy * cp; r.m[4] = sy * sp * sr + cy * cr; r.m[5] = sy * sp * cr - cy * sr;
r.m[6] = -sp; r.m[7] = cp * sr; r.m[8] = cp * cr;
return r;
}
void Apply(double x, double y, double z, double* ox, double* oy, double* oz) const {
*ox = m[0] * x + m[1] * y + m[2] * z;
*oy = m[3] * x + m[4] * y + m[5] * z;
*oz = m[6] * x + m[7] * y + m[8] * z;
}
};
// Bon lop nay la interface driver THAT, y het ReplaySource. vslam_node khong
// phan biet duoc phan cung / file .vsr / mo phong.
@@ -204,6 +233,21 @@ struct SimSource::Impl {
double terrain_pitch = 0.0; // nghieng do DIA HINH, cong vao than
double last_accel = 0.0;
// --- Dia hinh 3D (SimConfig::terrain). Tat thi khong truong nao duoi day
// duoc doc, va moi duong cu giu nguyen tung byte. ---
bool terrain_on = false; // chot o Open()
double ground_z = 0.0; // san duoi tam robot (base_footprint)
double terrain_roll = 0.0; // nghieng ngang do dia hinh
double terrain_pitch_rate = 0.0, terrain_roll_rate = 0.0;
// Goc dia hinh da co gia tri dau chua. Chua thi lan tinh dau KHONG sinh toc
// do goc -- neu khong, robot dat san tren doc se "quay" 10 do trong 5 ms.
bool terrain_ready = false;
bool steps_dirty = true; // san vua doi -> tinh lai mat na bac
std::vector<uint8_t> step_block; // o sat mep bac (chenh > max_step)
// Dinh vat can cao nhat moi o (he world, tu mesh). 0 = khong biet -> dung
// `wall_height` nhu cu.
std::vector<float> wall_top;
std::atomic<bool> stop{false};
mutable std::mutex state_mu;
Stats stats;
@@ -354,6 +398,82 @@ struct SimSource::Impl {
return inf;
}
// Do cao san NOI SUY SONG TUYEN giua tam cac o (chi dia hinh 3D).
//
// Gia tri tung o la bac thang 5 cm: o doc 30 do moi o nhay 2.9 cm, va
// robot / tia doc san se thay rang cua thay vi mat doc. Ngoai ban do kep vao
// o bien -- robot khong ra duoc ngoai (va cham), con tia ra ngoai thi raycast
// da dung tu truoc.
double FloorAtSmooth(double wx, double wy) const {
if (floor_z.empty() || !std::isfinite(wx) || !std::isfinite(wy)) return 0.0;
const int W = static_cast<int>(map.width), H = static_cast<int>(map.height);
const double fx = std::min(
std::max((wx - map.origin.position.x) / map.resolution - 0.5, 0.0), W - 1.0);
const double fy = std::min(
std::max((wy - map.origin.position.y) / map.resolution - 0.5, 0.0), H - 1.0);
const int x0 = static_cast<int>(fx), y0 = static_cast<int>(fy);
const int x1 = std::min(x0 + 1, W - 1), y1 = std::min(y0 + 1, H - 1);
const double tx = fx - x0, ty = fy - y0;
auto at = [&](int cx, int cy) {
return static_cast<double>(
floor_z[static_cast<size_t>(cy) * map.width + static_cast<size_t>(cx)]);
};
const double a = at(x0, y0) * (1.0 - tx) + at(x1, y0) * tx;
const double b = at(x0, y1) * (1.0 - tx) + at(x1, y1) * tx;
return a * (1.0 - ty) + b * ty;
}
// O nao sat mep bac -- chenh san voi mot o ke > max_step. Danh dau CA HAI
// phia mep, va CellBlocked() coi chung nhu tuong: robot khong leo duoc
// len bac, cung khong roi khoi mep san cao.
void RebuildStepMask() {
const uint32_t W = map.width, H = map.height;
step_block.assign(static_cast<size_t>(W) * H, 0);
const float lim = static_cast<float>(cfg.max_step);
size_t n = 0;
for (uint32_t cy = 0; cy < H; ++cy) {
for (uint32_t cx = 0; cx < W; ++cx) {
const size_t i = static_cast<size_t>(cy) * W + cx;
const float f = floor_z[i];
const bool step = (cx + 1 < W && std::fabs(floor_z[i + 1] - f) > lim) ||
(cx > 0 && std::fabs(floor_z[i - 1] - f) > lim) ||
(cy + 1 < H && std::fabs(floor_z[i + W] - f) > lim) ||
(cy > 0 && std::fabs(floor_z[i - W] - f) > lim);
if (step) { step_block[i] = 1; n++; }
}
}
steps_dirty = false;
VSLAM_DEBUG("sim: mat na bac: " << n << " o chenh > " << cfg.max_step << " m");
}
// z, pitch, roll do DIA HINH tai tu the hien tai, va toc do doi cua chung
// (cho gyro). Bon diem do: truoc/sau cach nhau `wheelbase_for_slope`,
// trai/phai cach nhau `wheel_separation`.
void UpdateTerrainPose(double dt) {
const double hl = cfg.wheelbase_for_slope / 2.0;
const double hw = cfg.wheel_separation / 2.0;
const double c = std::cos(yaw), s = std::sin(yaw);
const double ff = FloorAtSmooth(x + hl * c, y + hl * s);
const double fb = FloorAtSmooth(x - hl * c, y - hl * s);
const double fl = FloorAtSmooth(x - hw * s, y + hw * c);
const double fr = FloorAtSmooth(x + hw * s, y - hw * c);
// Doc len phia truoc -> mui ngoc len -> pitch AM (quy uoc cu). Ben TRAI
// cao hon -> roll DUONG (quay quanh +X dua +Y len).
const double np = -std::atan2(ff - fb, 2.0 * hl);
const double nr = std::atan2(fl - fr, 2.0 * hw);
if (terrain_ready && dt > 0.0) {
terrain_pitch_rate = (np - terrain_pitch) / dt;
terrain_roll_rate = (nr - terrain_roll) / dt;
} else {
terrain_pitch_rate = 0.0;
terrain_roll_rate = 0.0;
terrain_ready = true;
}
terrain_pitch = np;
terrain_roll = nr;
ground_z = FloorAtSmooth(x, y);
}
// Do cao san tai mot diem the gioi. Ngoai ban do coi nhu phang.
float FloorAt(double wx, double wy) const {
const int cx = static_cast<int>(
@@ -567,7 +687,13 @@ struct SimSource::Impl {
// depth camera. Mot robot dich vu di vao cua kinh la truong hop that,
// va no la ly do nhanh US_BLOCKED cua FSM ton tai.
if (map.data[idx] >= 50 && !(idx < glass.size() && glass[idx])) {
const double lo = fz, hi = fz + cfg.wall_height;
const double lo = fz;
double hi = fz + cfg.wall_height;
// Dia hinh 3D: vat can cao dung bang mesh cua no. Tuong 4 m ben doc
// khong duoc thanh tuong 2 m chi vi san o day bang 0.
if (terrain_on && idx < wall_top.size() && wall_top[idx] > lo) {
hi = wall_top[idx];
}
if ((z_enter >= lo && z_enter <= hi) || (z_exit >= lo && z_exit <= hi) ||
(z_enter > hi && z_exit < lo) || (z_enter < lo && z_exit > hi)) {
// Chi tinh chinh theo mat phang XY: tuong trong dai z la thang dung,
@@ -581,8 +707,18 @@ struct SimSource::Impl {
// Cham san: tia tut xuong duoi mat san trong o nay. Noi suy tuyen tinh
// giua hai dau o de khong luong tu hoa khoang cach theo kich thuoc o.
const double gap_enter = z_enter - fz;
const double gap_exit = z_exit - fz;
//
// Dia hinh 3D: san tai hai dau o lay NOI SUY, de mat doc la mat phang
// chu khong phai bac thang. Chi tinh khi tia da xuong gan san -- tia
// LiDAR cach san 0.22 m bay qua hang tram o, va noi suy o moi o la
// phi.
double f_in = fz, f_out = fz;
if (terrain_on && std::min(z_enter, z_exit) < fz + kSmoothFloorBand) {
f_in = FloorAtSmooth(ox + dx * t_enter * res, oy + dy * t_enter * res);
f_out = FloorAtSmooth(ox + dx * t_exit * res, oy + dy * t_exit * res);
}
const double gap_enter = z_enter - f_in;
const double gap_exit = z_exit - f_out;
if (gap_enter > 0.0 && gap_exit <= 0.0) {
const double frac = gap_enter / (gap_enter - gap_exit);
return (t_enter + frac * (t_exit - t_enter)) * res;
@@ -614,8 +750,10 @@ struct SimSource::Impl {
cy >= static_cast<int>(map.height)) {
return true;
}
return map.data[static_cast<size_t>(cy) * map.width +
static_cast<size_t>(cx)] >= 50;
const size_t idx = static_cast<size_t>(cy) * map.width + static_cast<size_t>(cx);
if (map.data[idx] >= 50) return true;
// Dia hinh 3D: mep bac / mep san cao cung chan nhu tuong.
return terrain_on && idx < step_block.size() && step_block[idx] != 0;
}
// Kiem va cham: lay 8 diem tren vanh robot.
@@ -649,11 +787,24 @@ struct SimSource::Impl {
prev_v = v;
// Tich phan pose THAT.
const double nx = x + v * std::cos(yaw) * dt;
const double ny = y + v * std::sin(yaw) * dt;
const double nyaw = NormalizeAngle(yaw + w * dt);
double nx, ny, nyaw;
if (terrain_on) {
// Lenh v la toc do BANH, doc mat san: di NGANG v*cos(pitch). Than quay
// quanh truc z CUA NO (dang nghieng), nen huong tren mat bang doi
// w*cos(roll)/cos(pitch) -- bo qua thi sai 15% o doc 30 do.
const double ct = std::cos(terrain_pitch);
const double cr = std::cos(terrain_roll);
nx = x + v * ct * std::cos(yaw) * dt;
ny = y + v * ct * std::sin(yaw) * dt;
nyaw = NormalizeAngle(yaw + w * cr / std::max(ct, 0.1) * dt);
} else {
nx = x + v * std::cos(yaw) * dt;
ny = y + v * std::sin(yaw) * dt;
nyaw = NormalizeAngle(yaw + w * dt);
}
std::lock_guard<std::mutex> lk(state_mu);
if (terrain_on && steps_dirty) RebuildStepMask();
if (Collides(nx, ny)) {
// Dung tai cho. Robot that se truot va quay, nhung "dung han" la mo
// hinh trung thuc hon voi mot robot dich vu co bumper cat nguon.
@@ -690,7 +841,10 @@ struct SimSource::Impl {
roll += roll_rate * dt;
// Nghieng do DIA HINH: do chenh lech do cao san giua truoc va sau robot.
if (cfg.follow_terrain_slope) {
if (terrain_on) {
// Ca z, pitch va roll, voi toc do doi cho gyro.
UpdateTerrainPose(dt);
} else if (cfg.follow_terrain_slope) {
const double half = cfg.wheelbase_for_slope / 2.0;
const double fz = FloorAt(x + half * std::cos(yaw), y + half * std::sin(yaw));
const double bz = FloorAt(x - half * std::cos(yaw), y - half * std::sin(yaw));
@@ -728,6 +882,45 @@ struct SimSource::Impl {
right_rad += vr / cfg.wheel_radius * dt;
}
// LiDAR tren dia hinh 3D: MOT mat phang quet gan cung vao than, nghieng
// theo dia hinh (va theo lo xo neu `lidar_follow_suspension`). Moi tia la
// tia 3D, cham tuong / vat can / MAT SAN that.
//
// Day la thu cho thay loi cua SLAM 2D tren doc: dung o san phang nhin vao
// chan doc goc t, tia ngang cham mat doc o 0.2165 / tan(t) -- mot "tuong"
// khong co that, va no lui dan khi robot tien len.
void FillScan3D(LaserScan* s) {
double rx, ry, rz, ryaw, rroll, rpitch;
{
std::lock_guard<std::mutex> lk(state_mu);
rx = x; ry = y; rz = ground_z; ryaw = yaw;
rroll = terrain_roll;
rpitch = terrain_pitch;
if (cfg.lidar_follow_suspension) { rroll += roll; rpitch += pitch; }
}
const Rot3 R = Rot3::FromRPY(rroll, rpitch, ryaw);
// Goc tia: base_footprint (tren san) + R * (lidar_x, 0, r_banh + lidar_z).
// `lidar_z` do tu TAM TRUC banh, nhu laser_link trong robot.yaml.
double ox, oy, oz;
R.Apply(cfg.lidar_x, 0.0, cfg.wheel_radius + cfg.lidar_z, &ox, &oy, &oz);
ox += rx; oy += ry; oz += rz;
std::normal_distribution<double> noise(0.0, cfg.lidar_noise_stddev);
for (uint32_t i = 0; i < cfg.lidar_samples; ++i) {
const double a = s->angle_min + i * s->angle_increment;
double dx, dy, dz;
R.Apply(std::cos(a), std::sin(a), 0.0, &dx, &dy, &dz);
const double r = RaycastDepth(ox, oy, oz, dx, dy, dz, cfg.lidar_range_max);
if (std::isinf(r)) {
s->ranges[i] = std::numeric_limits<float>::infinity();
} else {
const double rn = r + noise(rng);
s->ranges[i] = static_cast<float>(
rn < cfg.lidar_range_min ? cfg.lidar_range_min : rn);
}
}
}
LaserScanPtr MakeScan(UnixTime stamp) {
auto s = std::make_shared<LaserScan>();
s->header.stamp = stamp;
@@ -742,6 +935,11 @@ struct SimSource::Impl {
s->range_max = static_cast<float>(cfg.lidar_range_max);
s->ranges.resize(cfg.lidar_samples);
if (terrain_on) {
FillScan3D(s.get());
return s;
}
double rx, ry, ryaw;
{
std::lock_guard<std::mutex> lk(state_mu);
@@ -782,18 +980,20 @@ struct SimSource::Impl {
depth_buf.assign(static_cast<size_t>(w) * h * 2, 0); // 16UC1
}
double rx, ry, ryaw;
double rx, ry, rz, ryaw;
{
std::lock_guard<std::mutex> lk(state_mu);
rx = x;
ry = y;
rz = ground_z;
ryaw = yaw;
}
// Vi tri camera trong the gioi.
const double cx_w = rx + cfg.camera_x * std::cos(ryaw);
const double cy_w = ry + cfg.camera_x * std::sin(ryaw);
const double cz_w = cfg.camera_z;
// Vi tri camera trong the gioi. Dia hinh 3D tinh lai ben duoi, theo tu the
// 6-DoF cua than.
double cx_w = rx + cfg.camera_x * std::cos(ryaw);
double cy_w = ry + cfg.camera_x * std::sin(ryaw);
double cz_w = cfg.camera_z;
// Noi suy tu HFOV. Anh da decimate nen fx/fy tinh theo kich thuoc DA
// giam; binning trong CameraInfo bao cho consumer biet dieu do.
@@ -810,12 +1010,25 @@ struct SimSource::Impl {
std::lock_guard<std::mutex> lk(state_mu);
body_roll = roll;
body_pitch = pitch + terrain_pitch;
if (terrain_on) body_roll += terrain_roll;
}
const double total_pitch = cfg.camera_pitch + body_pitch;
const double cp = std::cos(total_pitch), sp = std::sin(total_pitch);
const double cr_b = std::cos(body_roll), sr_b = std::sin(body_roll);
const double cy_r = std::cos(ryaw), sy_r = std::sin(ryaw);
// Dia hinh 3D: than xoay du ba goc, camera chuc `camera_pitch` TREN than.
// (Duong cu cong camera_pitch vao pitch cua than va xoay roll truoc --
// chi dung khi roll = 0, du cho san phang.)
const Rot3 R_body = Rot3::FromRPY(body_roll, body_pitch, ryaw);
const double ccp = std::cos(cfg.camera_pitch), csp = std::sin(cfg.camera_pitch);
if (terrain_on) {
R_body.Apply(cfg.camera_x, 0.0, cfg.camera_z, &cx_w, &cy_w, &cz_w);
cx_w += rx;
cy_w += ry;
cz_w += rz;
}
// Khong dung mot `normal_distribution` chung: sigma phu thuoc KHOANG
// CACH cua tung diem anh, nen phan phoi duoc dung ngay trong vong lap.
std::uniform_real_distribution<double> uni01(0.0, 1.0);
@@ -831,15 +1044,23 @@ struct SimSource::Impl {
// Quang hoc -> than robot: +Z_quang = +X_than, +X_quang = -Y_than,
// +Y_quang = -Z_than. Roi chuc xuong `camera_pitch`, roi xoay theo yaw.
const double bx0 = oz * inv, by0 = -ox * inv, bz0 = -oy * inv;
// Nghieng ngang cua than (roll) truoc, roi chuc xuong (pitch).
const double by1 = by0 * cr_b - bz0 * sr_b;
const double bz1 = by0 * sr_b + bz0 * cr_b;
const double px = bx0 * cp + bz1 * sp;
const double pz = -bx0 * sp + bz1 * cp;
const double py = by1;
double wx, wy, pz;
if (terrain_on) {
// Chuc camera tren than (quanh y than), roi xoay theo than.
const double mx = bx0 * ccp + bz0 * csp;
const double mz = -bx0 * csp + bz0 * ccp;
R_body.Apply(mx, by0, mz, &wx, &wy, &pz);
} else {
// Nghieng ngang cua than (roll) truoc, roi chuc xuong (pitch).
const double by1 = by0 * cr_b - bz0 * sr_b;
const double bz1 = by0 * sr_b + bz0 * cr_b;
const double px = bx0 * cp + bz1 * sp;
pz = -bx0 * sp + bz1 * cp;
const double py = by1;
const double wx = px * cy_r - py * sy_r;
const double wy = px * sy_r + py * cy_r;
wx = px * cy_r - py * sy_r;
wy = px * sy_r + py * cy_r;
}
double d = RaycastDepth(cx_w, cy_w, cz_w, wx, wy, pz, cfg.depth_far);
uint16_t mm = 0; // 0 = khong do duoc, dung quy uoc cua RealSense
@@ -966,6 +1187,14 @@ struct SimSource::Impl {
cur_rr = roll_rate;
cur_pr = pitch_rate;
cur_accel = last_accel;
if (terrain_on) {
// Than nghieng theo dia hinh thi gyro PHAI thay luc no nghieng --
// khong thi gia toc ke bao "dang tren doc" con gyro bao "chua he xoay",
// va moi bo loc (gravity_align, VIO) bi hai cam bien cai nhau.
cur_roll += terrain_roll;
cur_rr += terrain_roll_rate;
cur_pr += terrain_pitch_rate;
}
}
// Con quay do van toc goc THAT tren ca ba truc.
@@ -1017,18 +1246,21 @@ StatusResponse SimSource::Open() {
// chuoi do phai giong nhau giua hai lan ghi cung mot kich ban.
impl_->rng.seed(impl_->cfg.seed);
WorldTerrain terr;
if (!impl_->cfg.world_path.empty()) {
const StatusResponse st = LoadGazeboWorld(
impl_->cfg.world_path, impl_->cfg.world_model_paths, &impl_->world);
if (!st.ok()) return st;
WorldRasterOptions wo;
const StatusResponse rs = RasterizeGazeboWorld(impl_->world, wo, &impl_->map);
const StatusResponse rs =
RasterizeGazeboWorld(impl_->world, wo, &impl_->map, &terr);
if (!rs.ok()) return rs;
// Chi muc doan 2D, de khoang cach tra ve trung MAT TUONG chu khong trung
// bien o. Khong dung duoc thi van chay -- chi la moi tia dung som toi mot
// o, y nhu truoc day.
const StatusResponse ss =
BuildWorldSegments(impl_->world, wo, impl_->map, &impl_->segments);
const StatusResponse ss = BuildWorldSegments(
impl_->world, wo, impl_->map, &impl_->segments,
terr.terrain_cells > 0 ? &terr.floor_z : nullptr);
if (!ss.ok()) {
VSLAM_WARN("sim: khong dung duoc chi muc doan (" << ss.message
<< ") -- raycast se luong tu hoa theo o "
@@ -1048,13 +1280,37 @@ StatusResponse SimSource::Open() {
}
// San phang mac dinh. SetFloorHeight() tao bac hut xuong.
impl_->floor_z.assign(static_cast<size_t>(impl_->map.width) * impl_->map.height, 0.0f);
impl_->glass.assign(static_cast<size_t>(impl_->map.width) * impl_->map.height, 0);
const size_t n_cells = static_cast<size_t>(impl_->map.width) * impl_->map.height;
impl_->floor_z.assign(n_cells, 0.0f);
impl_->glass.assign(n_cells, 0);
impl_->wall_top.clear();
impl_->step_block.clear();
// World co dia hinh (san cao, doc): san lay tu mesh dia hinh.
if (terr.terrain_cells > 0 && terr.floor_z.size() == n_cells) {
impl_->floor_z = terr.floor_z;
}
if (terr.wall_top.size() == n_cells) impl_->wall_top = terr.wall_top;
const SimConfig::TerrainMode tm = impl_->cfg.terrain;
impl_->terrain_on = tm == SimConfig::TerrainMode::kOn ||
(tm == SimConfig::TerrainMode::kAuto && terr.terrain_cells > 0);
if (impl_->terrain_on) {
VSLAM_INFO("sim: dia hinh 3D BAT -- " << terr.terrain_cells << " o dia hinh, san tu "
<< terr.floor_min << " den " << terr.floor_max
<< " m. Robot di tren mat san, LiDAR quet tia 3D, chenh san > "
<< impl_->cfg.max_step << " m la vat can.");
}
impl_->x = impl_->cfg.start_x;
impl_->y = impl_->cfg.start_y;
impl_->yaw = impl_->cfg.start_yaw;
if (impl_->terrain_on) {
impl_->RebuildStepMask();
impl_->terrain_ready = false;
impl_->UpdateTerrainPose(0.0);
}
if (impl_->Collides(impl_->x, impl_->y)) {
// Bao NGAY thay vi de robot ket ngay tu khung dau tien va nguoi dung ngoi
// doan tai sao no khong nhuc nhich.
@@ -1093,6 +1349,10 @@ void SimSource::SetMapForTest(const OccupancyGrid& m) {
impl_->map = m;
impl_->floor_z.assign(static_cast<size_t>(m.width) * m.height, 0.0f);
impl_->glass.assign(static_cast<size_t>(m.width) * m.height, 0);
impl_->wall_top.clear();
impl_->step_block.clear();
impl_->steps_dirty = true;
impl_->terrain_ready = false;
}
bool SimSource::true_bumper() const { return impl_->bumper_now; }
@@ -1143,16 +1403,67 @@ void SimSource::SetFloorHeight(double x0, double y0, double x1, double y1, float
impl_->floor_z[static_cast<size_t>(cy) * m.width + static_cast<size_t>(cx)] = z;
}
}
// Dia hinh 3D: mat na bac tinh lai o buoc vat ly sau, va goc dia hinh lay
// lai tu dau (khong sinh toc do goc gia).
impl_->steps_dirty = true;
impl_->terrain_ready = false;
VSLAM_INFO("sim: dat do cao san " << z << " m cho vung (" << x0 << ", " << y0
<< ") - (" << x1 << ", " << y1 << ")");
}
void SimSource::SetFloorRamp(double x0, double y0, double x1, double y1,
float z_at_x0, float z_at_x1) {
const OccupancyGrid& m = impl_->map;
if (x1 == x0) return;
auto to_cell = [&](double w, double origin) {
return static_cast<int>(std::floor((w - origin) / m.resolution));
};
const int cx0 = std::max(0, to_cell(std::min(x0, x1), m.origin.position.x));
const int cy0 = std::max(0, to_cell(std::min(y0, y1), m.origin.position.y));
const int cx1 = std::min<int>(static_cast<int>(m.width) - 1,
to_cell(std::max(x0, x1), m.origin.position.x));
const int cy1 = std::min<int>(static_cast<int>(m.height) - 1,
to_cell(std::max(y0, y1), m.origin.position.y));
std::lock_guard<std::mutex> lk(impl_->state_mu);
for (int cy = cy0; cy <= cy1; ++cy) {
for (int cx = cx0; cx <= cx1; ++cx) {
// Do cao tai TAM o, cung quy uoc voi raster dia hinh tu world.
const double wx = m.origin.position.x + (cx + 0.5) * m.resolution;
const double t = std::min(1.0, std::max(0.0, (wx - x0) / (x1 - x0)));
impl_->floor_z[static_cast<size_t>(cy) * m.width + static_cast<size_t>(cx)] =
static_cast<float>(z_at_x0 + t * (z_at_x1 - z_at_x0));
}
}
impl_->steps_dirty = true;
impl_->terrain_ready = false;
VSLAM_INFO("sim: doc tu x = " << x0 << " (" << z_at_x0 << " m) toi x = " << x1
<< " (" << z_at_x1 << " m)");
}
bool SimSource::terrain_active() const { return impl_->terrain_on; }
void SimSource::true_tilt(double* roll_out, double* pitch_out) const {
std::lock_guard<std::mutex> lk(impl_->state_mu);
*roll_out = impl_->roll;
// Tat dia hinh 3D thi KHONG cong (ke ca 0.0): giu dung tung bit nhu cu.
*roll_out = impl_->terrain_on ? impl_->roll + impl_->terrain_roll : impl_->roll;
*pitch_out = impl_->pitch + impl_->terrain_pitch;
}
SimPose3 SimSource::ground_truth_3d() const {
std::lock_guard<std::mutex> lk(impl_->state_mu);
SimPose3 p;
p.x = impl_->x;
p.y = impl_->y;
p.yaw = impl_->yaw;
// Tat dia hinh 3D thi robot luon o z = 0, ke ca khi SetFloorHeight tao vuc:
// do cao san luc do chi de camera NHIN thay, robot khong di len no.
p.z = impl_->terrain_on ? impl_->ground_z : 0.0;
p.roll = impl_->terrain_on ? impl_->roll + impl_->terrain_roll : impl_->roll;
p.pitch = impl_->pitch + impl_->terrain_pitch;
return p;
}
Pose2D SimSource::ground_truth() const {
std::lock_guard<std::mutex> lk(impl_->state_mu);
return Pose2D{impl_->x, impl_->y, impl_->yaw};