This commit is contained in:
trung09122002
2026-09-19 18:35:09 +07:00
parent 3896886349
commit 62b611a3eb
9 changed files with 238 additions and 30 deletions

View File

@@ -28,4 +28,18 @@ if(VSLAM_BUILD_SLAM)
target_compile_definitions(vslam_node PRIVATE VSLAM_HAVE_SLAM=1)
endif()
# Hai driver phan cung can SDK ngoai. Cac macro nay PHAI dat lai o day chu
# khong the lay tu vslam_drivers: o do chung la PRIVATE, nen app khong thay.
# Dat PUBLIC ben kia thi moi nguoi dung vslam_drivers deu phai co SDK trong
# include path -- dung cai ma `option()` sinh ra de tranh.
#
# Hau qua neu quen: `#ifdef` trong runtime.cc lang le thanh false, va --live
# chay KHONG co camera ma khong ai hieu tai sao.
if(VSLAM_WITH_REALSENSE)
target_compile_definitions(vslam_node PRIVATE VSLAM_HAVE_REALSENSE=1)
endif()
if(VSLAM_WITH_CANOPEN)
target_compile_definitions(vslam_node PRIVATE VSLAM_HAVE_CANOPEN=1)
endif()
install(TARGETS vslam_node RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@@ -58,7 +58,8 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
const StatusResponse st = Open(dir, kSensorsConfigFile, &root);
if (!st.ok()) return st;
{
const StatusResponse r = Reject(root, "sensors.yaml", {"lidar", "camera", "mcu"});
const StatusResponse r =
Reject(root, "sensors.yaml", {"lidar", "camera", "mcu", "base"});
if (!r.ok()) return r;
}
@@ -74,6 +75,19 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
out->sanitizer.range_max = l.get<double>("range_max", 40.0);
out->sensors.lidar_hz = l.get<double>("expected_rate_hz", 25.0);
// Driver LakiBeam. `host` la dia chi de BIND, khong phai IP cua LiDAR --
// SDK chinh hang cung luon bind 0.0.0.0.
out->lidar_driver.host_ip = l.get<std::string>("host", "0.0.0.0");
out->lidar_driver.port = static_cast<uint16_t>(l.get<int>("port", 2368));
out->lidar_driver.scan_freq_hz = out->sensors.lidar_hz;
out->lidar_driver.frame_id = kLaserFrame;
out->lidar_driver.sweep_duration_tolerance_sec =
l.get<double>("stamp_mismatch_warn", 0.005);
// ⚠️ range_unit_m, angle_offset_deg va inverted CO Y khong doc tu YAML.
// Ca ba deu suy ra duoc (xem driver_config.h): don vi mm tu SDK, va goc
// tu manual §8.1 + REP-103 + tu the gan up nguoc. De chung trong YAML moi
// la de nguoi van hanh go bua vao mot thu da co dap an dung.
// Sector mask ghi bang DO trong YAML (doc duoc bang mat), dung bang RAD.
// Doi mot lan o day, khong de moi noi tu doi.
const ConfigNode masks = l["sector_masks"];
@@ -102,6 +116,10 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
out->sanitizer.sector_masks.push_back(SectorMask{a, b});
}
}
// Driver chi dung range_min/max cua cai nay de dien header LaserScan; no
// KHONG goi Apply(). Ve sinh scan la viec cua node (wiring.cc), dung mot
// doi tuong duy nhat -- xem lakibeam_driver.cc.
out->lidar_driver.sanitizer = out->sanitizer;
const ConfigNode c = root["camera"];
{
@@ -117,6 +135,15 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
out->sensors.camera_hz = c.get<double>("fps", 30.0);
out->sensors.camera_decimate = static_cast<uint32_t>(c.get<int>("decimate", 2));
out->camera_driver.depth_width = out->sensors.camera_width;
out->camera_driver.depth_height = out->sensors.camera_height;
out->camera_driver.fps = static_cast<uint32_t>(out->sensors.camera_hz);
out->camera_driver.decimate = out->sensors.camera_decimate;
out->camera_driver.enable_infra1 = c.get<bool>("enable_infra1", true);
out->camera_driver.enable_infra2 = c.get<bool>("enable_infra2", true);
out->camera_driver.use_global_time = c.get<bool>("prefer_global_time", true);
out->camera_driver.depth_frame_id = kCameraDepthOpticalFrame;
const ConfigNode mc = root["mcu"];
{
const StatusResponse r =
@@ -126,6 +153,38 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
if (!r.ok()) return r;
}
out->sensors.imu_hz = mc.get<double>("expected_imu_rate_hz", 200.0);
out->mcu_driver.port = mc.get<std::string>("port", "/dev/ttyACM0");
out->mcu_driver.baud = static_cast<uint32_t>(mc.get<int>("baud", 115200));
out->mcu_driver.imu_frame_id = kImuFrame;
// ---------- base ----------
// Muc nay MOI. Encoder va motor di qua CANopen tren can0, khong qua serial
// MCU nhu mcu_protocol.h gia dinh -- xem zlac_base.cc.
const ConfigNode b = root["base"];
{
const StatusResponse r =
Reject(b, "sensors.yaml base",
{"can_channel", "can_node_id", "dcf_path",
"encoder_counts_per_rev"});
if (!r.ok()) return r;
}
out->base_driver.protocol = BaseProtocol::kZlacCanOpen;
out->base_driver.can_channel = b.get<std::string>("can_channel", "can0");
out->base_driver.can_node_id =
static_cast<uint8_t>(b.get<int>("can_node_id", 1));
out->base_driver.dcf_path = b.get<std::string>("dcf_path", "");
out->base_driver.encoder_counts_per_rev =
static_cast<uint32_t>(b.get<int>("encoder_counts_per_rev", 0));
// Hinh hoc va gioi han SAO tu robot.yaml, khong doc rieng. robot.yaml la
// nguon duy nhat; mot ban sao trong sensors.yaml se troi khoi no ma khong
// ai phat hien -- va troi kieu im lang: robot van chay, chi sai ti le.
out->base_driver.wheel_radius = geom.wheel_radius;
out->base_driver.wheel_separation = geom.wheel_separation;
out->base_driver.max_linear = out->robot.base().max_linear;
out->base_driver.max_angular = out->robot.base().max_angular;
out->base_driver.cmd_timeout_sec = out->robot.base().cmd_timeout;
}
// ---------- pipeline.yaml ----------

View File

@@ -27,6 +27,7 @@
#include "vslam/ekf_se2.h"
#include "vslam/gravity_align.h"
#include "vslam/perception_pipeline.h"
#include "vslam/driver_config.h"
#include "vslam/robot_model.h"
#include "vslam/safety_monitor.h"
#include "vslam/scan_sanitizer.h"
@@ -80,6 +81,18 @@ struct AppConfig {
ScanSanitizerConfig sanitizer; // sensors.yaml
SensorRates sensors;
// Config cho driver phan cung that (--live). CHI dung o nhanh kLive:
// --sim va --replay cap cac interface nay tu SimSource/ReplaySource va
// khong doc gi o day.
//
// Hinh hoc trong `base` duoc SAO tu `robot` luc nap, khong doc rieng tu
// YAML: robot.yaml la nguon duy nhat cho hinh hoc, va mot ban sao doc lap
// trong sensors.yaml se troi khoi no ma khong ai phat hien.
LidarConfig lidar_driver; // sensors.yaml lidar
CameraConfig camera_driver; // sensors.yaml camera
McuConfig mcu_driver; // sensors.yaml mcu
BaseConfig base_driver; // sensors.yaml base + robot.yaml
WheelOdometryConfig wheel_odom; // robot.yaml + pipeline.yaml
EkfSe2Config ekf; // pipeline.yaml
PerceptionConfig perception; // pipeline.yaml + robot.yaml

View File

@@ -6,7 +6,7 @@
// vslam_node --sim chay voi bo mo phong
// vslam_node --sim --map nha.yaml mo phong tren ban do that
// vslam_node --replay X.vsr phat lai ban ghi
// vslam_node --live phan cung that (driver CHUA viet)
// vslam_node --live phan cung that (LiDAR + IMU + base CAN)
//
// Doc `src/wiring.cc` de hieu he thong: no la noi duy nhat noi nguon voi
// stage.
@@ -42,7 +42,7 @@ void Usage(const char* argv0) {
"\n"
" --sim chay voi bo mo phong (thay Gazebo)\n"
" --replay FILE phat lai ban ghi .vsr\n"
" --live phan cung that (driver CHUA viet)\n"
" --live phan cung that: LakiBeam + IMU + base ZLAC (CAN)\n"
"\n"
" --config DIR thu muc cau hinh (mac dinh: $VSLAM_CONFIG_DIR\n"
" hoac %s)\n"

View File

@@ -20,6 +20,15 @@ namespace vslam {
struct Runtime::SourceHolder {
std::unique_ptr<SimSource> sim;
std::unique_ptr<ReplaySource> replay;
// --live: bon driver roi, khong co object "LiveSource" bao chung. Co y --
// SimSource va ReplaySource ton tai vi chung phai DONG BO bon luong voi
// nhau tu mot dong ho ao. Phan cung that khong can dieu do: moi thiet bi
// tu chay tren luong rieng va tu dong dau theo dong ho he thong.
std::unique_ptr<LidarInterface> live_lidar;
std::unique_ptr<CameraInterface> live_camera;
std::unique_ptr<McuInterface> live_mcu;
std::unique_ptr<BaseInterface> live_base;
};
Runtime::Runtime(AppConfig* cfg, RuntimeOptions opts)
@@ -125,15 +134,74 @@ StatusResponse Runtime::OpenSource() {
return StatusResponse::Ok();
}
case SourceKind::kLive:
// Noi thang: driver LakiBeam / RealSense / base chua viet, va chung bi
// chan boi thong tin chua co (byte layout goi UDP, giao thuc encoder
// cua base), khong phai boi thoi gian.
case SourceKind::kLive: {
#ifndef VSLAM_HAVE_CANOPEN
// Tu choi o day thay vi de linker bao thieu CreateDiffDriveBase: thong
// bao nay noi duoc phai LAM GI, con loi link thi khong.
return StatusResponse::Error(
StatusCode::kUnavailable,
"che do --live chua chay duoc: driver LakiBeam, RealSense va base "
"chua viet. Chung bi chan boi thong tin phan cung chua co -- xem "
"README muc \"P2 lam duoc den dau\". Dung --sim hoac --replay.");
StatusCode::kFailedPrecondition,
"--live can base ZLAC, ma build nay khong co CANopen. Build lai voi "
"-DVSLAM_WITH_CANOPEN=ON (can liblely-coapp). Khong co base thi "
"khong co encoder va khong dieu khien duoc motor -- day khong phai "
"thu bo qua duoc nhu camera.");
#else
// Dong ho HE THONG, khong phai dong ho ao. Day la nhanh DUY NHAT dung
// no -- sim va replay deu cap dong ho cua rieng chung, va do la thu giu
// cho ca hai tat dinh.
clock_ = MakeSystemClock();
// Thu tu mo co chu dich: base TRUOC, va neu no hong thi dung han.
// Base la thu duy nhat lam robot DI CHUYEN. Mo cam bien truoc roi
// that bai o base nghia la co mot node dang chay, co du lieu, co web UI
// -- va khong co cach nao dung xe. Hong som, hong to.
source_->live_base = CreateDiffDriveBase(cfg_->base_driver, clock_);
if (StatusResponse st = source_->live_base->Open(); !st.ok()) {
return StatusResponse::Error(
st.code, "--live: base ZLAC (CANopen) khong mo duoc: " + st.message);
}
base_ = source_->live_base.get();
source_->live_lidar = CreateLakiBeamDriver(cfg_->lidar_driver, clock_);
if (StatusResponse st = source_->live_lidar->Open(); !st.ok()) {
return StatusResponse::Error(
st.code, "--live: LiDAR LakiBeam khong mo duoc: " + st.message);
}
lidar_ = source_->live_lidar.get();
source_->live_mcu = CreateMcuBridge(cfg_->mcu_driver, clock_);
if (StatusResponse st = source_->live_mcu->Open(); !st.ok()) {
return StatusResponse::Error(
st.code, "--live: MCU (IMU) khong mo duoc: " + st.message);
}
mcu_ = source_->live_mcu.get();
// Camera la thu DUY NHAT duoc phep vang mat: EKF chay bang encoder +
// gyro, va safety chay bang LiDAR. Mat camera la mat phat hien vuc va
// vat can thap -- kem di that, nhung van dung duoc va van lai duoc.
// Mat LiDAR hay mat base thi khong.
#ifdef VSLAM_HAVE_REALSENSE
if (cfg_->sensors.camera_enable) {
source_->live_camera = CreateRealSenseD410(cfg_->camera_driver, clock_);
if (StatusResponse st = source_->live_camera->Open(); !st.ok()) {
VSLAM_WARN("--live: camera khong mo duoc, chay KHONG co camera: "
<< st.message
<< " -- cliff_detect va vat can thap se KHONG hoat dong");
source_->live_camera.reset();
} else {
camera_ = source_->live_camera.get();
}
} else {
VSLAM_WARN("--live: camera bi tat trong sensors.yaml -- cliff_detect "
"va vat can thap se KHONG hoat dong");
}
#else
VSLAM_WARN("--live: build khong co -DVSLAM_WITH_REALSENSE=ON, chay KHONG "
"co camera -- cliff_detect va vat can thap se KHONG hoat dong");
#endif
return StatusResponse::Ok();
#endif // VSLAM_HAVE_CANOPEN
}
}
return StatusResponse::Error(StatusCode::kInvalidArgument, "nguon la");
}