diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 481b56e..e21ad2f 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -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}) diff --git a/app/src/app_config.cc b/app/src/app_config.cc index de62765..3e9e05d 100644 --- a/app/src/app_config.cc +++ b/app/src/app_config.cc @@ -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("range_max", 40.0); out->sensors.lidar_hz = l.get("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("host", "0.0.0.0"); + out->lidar_driver.port = static_cast(l.get("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("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("fps", 30.0); out->sensors.camera_decimate = static_cast(c.get("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(out->sensors.camera_hz); + out->camera_driver.decimate = out->sensors.camera_decimate; + out->camera_driver.enable_infra1 = c.get("enable_infra1", true); + out->camera_driver.enable_infra2 = c.get("enable_infra2", true); + out->camera_driver.use_global_time = c.get("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("expected_imu_rate_hz", 200.0); + + out->mcu_driver.port = mc.get("port", "/dev/ttyACM0"); + out->mcu_driver.baud = static_cast(mc.get("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("can_channel", "can0"); + out->base_driver.can_node_id = + static_cast(b.get("can_node_id", 1)); + out->base_driver.dcf_path = b.get("dcf_path", ""); + out->base_driver.encoder_counts_per_rev = + static_cast(b.get("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 ---------- diff --git a/app/src/app_config.h b/app/src/app_config.h index 748c5c2..15307cd 100644 --- a/app/src/app_config.h +++ b/app/src/app_config.h @@ -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 diff --git a/app/src/main.cc b/app/src/main.cc index ffc9666..f0fbcf0 100644 --- a/app/src/main.cc +++ b/app/src/main.cc @@ -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" diff --git a/app/src/runtime.cc b/app/src/runtime.cc index cb0666f..0165645 100644 --- a/app/src/runtime.cc +++ b/app/src/runtime.cc @@ -20,6 +20,15 @@ namespace vslam { struct Runtime::SourceHolder { std::unique_ptr sim; std::unique_ptr 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 live_lidar; + std::unique_ptr live_camera; + std::unique_ptr live_mcu; + std::unique_ptr 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"); } diff --git a/configuration_files/sensors.yaml b/configuration_files/sensors.yaml index b725399..33ddca4 100644 --- a/configuration_files/sensors.yaml +++ b/configuration_files/sensors.yaml @@ -71,7 +71,9 @@ camera: # ============ MCU ============ mcu: port: /dev/ttyACM0 - baud: 921600 + # 115200 theo firmware that (ESP32-S3 + WT61PC). Gia tri cu la 921600 -- doc + # sai baud cho ra RAC chu khong cho ra loi, nen no hong im lang. + baud: 115200 # ClockSync khop micros() cua MCU voi dong ho host bang fit tuyen tinh. # @@ -84,5 +86,29 @@ mcu: expected_imu_rate_hz: 200.0 # Ban ghi encoder `E,,,` la MO RONG moi -- giao - # thuc cu chi co I/B/P. Firmware that chua xac nhan co ban ghi nay. + # thuc cu chi co I/B/P. + # + # ⚠️ KHONG DUNG TOI tren robot nay. Firmware that khong phat ban ghi E, va + # encoder khong di qua MCU ma qua CANopen -- xem muc `base` ben duoi. Giu + # lai vi neu sau nay firmware phat dung mcu_protocol.h thi day la duong duy + # nhat dong bo duoc dong ho encoder voi dong ho IMU. ticks_per_rev: 1024 + +# ============ Base: encoder + motor ============ +# ⚠️ KHONG phai serial. ZLAC8015D noi qua CANopen tren can0. +# +# Dieu nay khac han gia dinh cua mcu_protocol.h ("tat ca tren mot serial MCU"). +# Hau qua that: khong co duong nao dong bo dong ho base voi dong ho host -- +# ClockSync chi bac cau duoc cho MCU serial. Stamp cua EncoderReading la dong +# ho host luc doc xong SDO. +base: + can_channel: can0 + can_node_id: 1 + + # File DCF sinh bang `dcfgen` tu master.yml. lely KHONG chay duoc neu thieu. + # Duong dan TUYET DOI, va phai ton tai luc khoi dong. + dcf_path: /opt/vslam/share/vslam/master.dcf + + # Increment/vong cua encoder, doc qua 0x6064. 0 = hoi thiet bi luc Open() + # qua 0x608F:1 thay vi chep tay mot hang so. + encoder_counts_per_rev: 0 diff --git a/libs/vslam_common/include/vslam/robot_model.h b/libs/vslam_common/include/vslam/robot_model.h index b8fd8f2..c4ebbdd 100644 --- a/libs/vslam_common/include/vslam/robot_model.h +++ b/libs/vslam_common/include/vslam/robot_model.h @@ -27,8 +27,11 @@ namespace vslam { struct RobotGeometry { - double wheel_radius = 0.075; - double wheel_separation = 0.36; + // Do tren robot that: banh Ø173 mm. Phai TRUNG voi BaseConfig trong + // driver_config.h -- hai default khac nhau cho cung mot dai luong vat ly la + // dung cai bay ma file nay ton tai de tranh. + double wheel_radius = 0.0865; + double wheel_separation = 0.400; double robot_radius = 0.24; double body_half_width = 0.17; diff --git a/libs/vslam_drivers/src/lakibeam_driver.cc b/libs/vslam_drivers/src/lakibeam_driver.cc index 4ccee1a..5db1545 100644 --- a/libs/vslam_drivers/src/lakibeam_driver.cc +++ b/libs/vslam_drivers/src/lakibeam_driver.cc @@ -32,11 +32,16 @@ // - block cuoi cua goi phai lay azimuth tu goi SAU, nen driver luon giu lai // mot goi va giai ma tre mot nhip. Do la ly do co `pending_`. // -// ================== HAI QUY UOC PHAI DOI ================== -// Manual §8.1: LakiBeam tra ve X = R*sin(a), Y = R*cos(a) -// REP-103: he nay dung x = R*cos(t), y = R*sin(t) -// => t = 90do - a. Trong config la angle_offset_deg = 90 VA inverted = true. -// Thieu mot trong hai thi ban do lat guong. +// ================== QUY UOC GOC + TU THE GAN ================== +// Manual §8.1: LakiBeam tra ve X_dev = R*sin(a), Y_dev = R*cos(a) +// REP-103: he nay dung x = R*cos(t), y = R*sin(t) +// +// LiDAR tren robot nay GAN UP NGUOC, tuc xoay 180do quanh truc X (Y doi dau): +// gan xuoi -> t = 90do - a (offset +90, inverted = true) +// gan up nguoc -> t = a - 90do (offset -90, inverted = FALSE) +// +// Hai phep dao dau -- cua quy uoc va cua tu the gan -- triet tieu nhau. Chi +// tiet va canh bao "dung bu lan thu hai trong TF" nam o driver_config.h. // // Don vi range la MILIMET (scan_conversion.cpp:36 chia 1000), KHONG phai 2 mm // nhu Velodyne -- suy tu layout goi se ra sai dung gap doi. @@ -107,7 +112,7 @@ struct Point { class LakiBeamDriver : public LidarInterface { public: LakiBeamDriver(const LidarConfig& cfg, ClockPtr clock) - : cfg_(cfg), clock_(std::move(clock)), sanitizer_(cfg.sanitizer) {} + : cfg_(cfg), clock_(std::move(clock)) {} ~LakiBeamDriver() override { Close(); } @@ -363,8 +368,18 @@ class LakiBeamDriver : public LidarInterface { } scan->header.stamp = stamp; - sanitizer_.Apply(scan.get()); - + // ⚠️ KHONG goi ScanSanitizer o day, du LidarConfig co mang mot + // ScanSanitizerConfig. Driver tra ve scan THO. + // + // Hai ly do, ca hai deu tu kien truc co san: + // 1. Node da ve sinh roi (wiring.cc goi sanitizer_->Apply), va no dung + // DUNG MOT doi tuong ScanSanitizer cho moi nguon. Ve sinh hai lan + // la lam thua, va lam lech thong ke cua chinh sanitizer do. + // 2. Ban ghi .vsr luu du lieu THO co y -- replay_source.h noi ro "de doi + // nguong roi test lai duoc". Neu driver ve sinh truoc khi ghi thi + // nguong bi dong cung vao file va khong bao gio chinh lai duoc. + // + // cfg_.sanitizer o day CHI dung de dien range_min/range_max cua header. { std::lock_guard lk(stats_mu_); ++stats_.frames; @@ -406,7 +421,6 @@ class LakiBeamDriver : public LidarInterface { LidarConfig cfg_; ClockPtr clock_; - ScanSanitizer sanitizer_; ScanCallback cb_; int fd_ = -1; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 956dfe2..649edd7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -162,12 +162,23 @@ if(VSLAM_BUILD_APP) PASS_REGULAR_EXPRESSION "phai chon mot nguon" ENVIRONMENT "${VSLAM_NODE_TEST_ENV}") - # `--live` phai noi RO la chua chay duoc, khong duoc im lang chay thu gi do. - add_test(NAME node_live_bao_chua_co_driver - COMMAND vslam_node --live --config ${CMAKE_SOURCE_DIR}/configuration_files) - set_tests_properties(node_live_bao_chua_co_driver PROPERTIES - PASS_REGULAR_EXPRESSION "driver LakiBeam" - ENVIRONMENT "${VSLAM_NODE_TEST_ENV}") + # `--live` phai noi RO vi sao khong chay duoc, khong duoc im lang chay thu + # gi do. + # + # ⚠️ CHI dang ky khi build KHONG co CANopen. Voi -DVSLAM_WITH_CANOPEN=ON va + # phan cung that cam vao, `--live` se KHOI DONG THAT va chay mai -- test se + # TREO chu khong fail. Mot test treo trong CI te hon mot test thieu. + # + # Cai duoc kiem o day la: mot build thieu CANopen phai TU CHOI --live va noi + # ro thieu gi. Duong `--live` chay duoc thi phai kiem tren phan cung that, + # khong kiem bang ctest duoc. + if(NOT VSLAM_WITH_CANOPEN) + add_test(NAME node_live_tu_choi_khi_thieu_canopen + COMMAND vslam_node --live --config ${CMAKE_SOURCE_DIR}/configuration_files) + set_tests_properties(node_live_tu_choi_khi_thieu_canopen PROPERTIES + PASS_REGULAR_EXPRESSION "VSLAM_WITH_CANOPEN" + ENVIRONMENT "${VSLAM_NODE_TEST_ENV}") + endif() endif() # ---------------------------------------------------------------------------