This commit is contained in:
trung09122002
2026-09-18 18:14:42 +07:00
commit 48bd5377dc
224 changed files with 37685 additions and 0 deletions

234
app/src/wiring.cc Normal file
View File

@@ -0,0 +1,234 @@
// NOI DUY NHAT noi nguon voi stage.
//
// Day la tuong duong cua toan bo 20 file `.launch` cua ban ROS, gom lai thanh
// mot ham doc duoc tu tren xuong. Muon biet du lieu di dau thi doc file nay,
// khong phai grep remap qua 20 file XML.
//
// ================== BON DUONG ==================
// LiDAR -> ScanSanitizer -> {safety, web}
// IMU -> {gravity_align, wheel_odom (gyro), ekf}
// Encoder -> wheel_odom -> {ekf, gravity_align (tru gia toc rieng)}
// Camera -> perception (INLINE) -> {safety (co vuc), web (cloud)}
//
// Lenh di NGUOC lai va chi mot duong:
// web -> teleop -> raw_cmd -> [luong safety] -> base
// Khong nhanh nao cho lenh di thang xuong base. Ke ca e-stop: no dat co, roi
// luong safety thay co va zero lenh trong vong mot chu ky.
#include "runtime.h"
#include "vslam/geometry.h"
#include "vslam/json_parse.h"
#include "vslam/log.h"
#include <cmath>
namespace vslam {
StatusResponse Runtime::Wire() {
// ---------------- LiDAR ----------------
if (lidar_ != nullptr) {
lidar_->SetCallback([this](LaserScanPtr s) {
// Ve sinh MOT lan, ngay tai bien. Moi nguoi doc phia sau nhan cung mot
// ban -- khong ai duoc ve sinh lai, va khong ai thay ban tho.
//
// ScanSanitizer gop header-fix va sector mask lam MOT ham: hai bo loc
// rieng thi "cai nao chay truoc" la mot bay co that.
auto copy = std::make_shared<LaserScan>(*s);
sanitizer_->Apply(copy.get());
std::lock_guard<std::mutex> lk(scan_mu_);
latest_scan_ = copy;
});
}
// ---------------- IMU ----------------
if (mcu_ != nullptr) {
mcu_->SetImuCallback([this](ImuPtr m) {
// gravity_align chay TRUOC: no o 200 Hz va re, va cliff_detect can
// nghieng moi nhat.
perception_->FeedImu(*m);
// Cong bo nghieng cho luong web. Phai qua atomic: `GravityAlign` thuoc
// ve luong nay va khong tu khoa -- doc thang tu luong khac la data
// race, va ThreadSanitizer da bat dung no.
tilt_roll_.store(perception_->gravity().roll(), std::memory_order_relaxed);
tilt_pitch_.store(perception_->gravity().pitch(), std::memory_order_relaxed);
tilt_accel_ok_.store(perception_->gravity().accel_ok(),
std::memory_order_relaxed);
std::lock_guard<std::mutex> lk(odom_mu_);
wheel_odom_->FeedGyroZ(m->header.stamp, m->angular_velocity.z);
ekf_->UpdateImuYawRate(m->header.stamp, m->angular_velocity.z,
m->angular_velocity_covariance[8] > 0.0
? m->angular_velocity_covariance[8]
: 2.4e-7);
});
}
// ---------------- Sieu am + bumper ----------------
if (mcu_ != nullptr) {
mcu_->SetUltrasonicCallback([this](const UltrasonicReading& u) {
std::lock_guard<std::mutex> lk(mcu_mu_);
ultrasonic_.clear();
for (size_t i = 0; i < u.count; ++i) {
// GIU CA NaN. Truoc day o day loc NaN di, va do la sai: map rong
// trong y het "khong lap sieu am", nen mot day cam bien DUT hoan
// toan vo hinh voi FSM. Chinh FSM phan biet ba ngu nghia (huu han /
// inf / NaN) -- day chi truyen nguyen ven.
ultrasonic_["us" + std::to_string(i)] = u.range[i];
}
us_stamp_ = u.stamp;
// `have_us_` van true ke ca khi moi dau do deu NaN: co cam bien lap
// dat, chi la chung khong tra loi -> dung la truong hop US_TIMEOUT.
have_us_ = true;
});
mcu_->SetBumperCallback([this](const BumperReading& b) {
std::lock_guard<std::mutex> lk(mcu_mu_);
bumper_ = b.any();
bumper_stamp_ = b.stamp;
have_bumper_ = true;
});
}
// ---------------- Encoder ----------------
if (base_ != nullptr) {
base_->SetEncoderCallback([this](const EncoderReading& e) {
if (!e.valid) return;
Odometry o;
{
std::lock_guard<std::mutex> lk(odom_mu_);
if (!wheel_odom_->Update(e.stamp, e.left_rad, e.right_rad, &o)) return;
// Truyen THANG twist.covariance vao EKF.
//
// Do chinh la cach wheel_odom bao "dang truot, dung tin toi". Thay
// bang hang so o day la vut bo toan bo co che phat hien truot -- va
// no im lang, vi EKF van chay va van cho ra mot pose trong nhu that.
ekf_->UpdateOdomTwist(e.stamp, o.twist.twist.linear.x,
o.twist.twist.angular.z, o.twist.covariance[0],
o.twist.covariance[35]);
latest_odom_ = o;
have_odom_ = true;
}
// gravity_align phai TRU gia toc rieng cua robot truoc khi coi phan con
// lai la trong luc. Khong co duong nay, moi lan tang toc bi hieu thanh
// mot cu nghieng -- da do tren robot that: bien do pitch 11.23 do thay
// vi 1.32 do, va cliff_detect bao vuc gia 1625/1832 mau.
//
// Goi NGOAI odom_mu_: PerceptionPipeline thuoc ve luong nay, khong can
// mutex, va giu lock khi goi code khac la cach tao ra deadlock.
perception_->FeedOdom(e.stamp, o.twist.twist.linear.x,
o.twist.twist.angular.z);
});
}
// ---------------- Camera ----------------
if (camera_ != nullptr) {
camera_->SetCallback([this](const CameraFrame& f) {
if (!f.depth) return;
// Ca chuoi perception chay INLINE o day: depth -> cloud -> loc ->
// can trong luc -> phat hien vuc. Mot loi goi ham, tren chinh luong
// camera.
const PerceptionOutput po = perception_->Process(*f.depth, f.depth_info);
std::lock_guard<std::mutex> lk(perc_mu_);
latest_cloud_ = po.cloud_aligned;
latest_cliff_cloud_ = po.cloud_cliff;
cliff_flag_ = po.cliff_detected;
cliff_stamp_ = po.stamp;
cliff_detail_ = po.cliff_detail;
have_cliff_ = true;
});
}
// ---------------- Web ----------------
WebConfig wc = cfg_->web;
if (opts_.port_override != 0) wc.port = opts_.port_override;
if (!opts_.token_override.empty()) wc.auth_token = opts_.token_override;
web_.reset(new WebServer(
wc,
// --- Lenh POST /api/* ---
[this](const std::string& cmd, const std::string& body) -> StatusResponse {
if (cmd == "safety/estop") {
bool on = false;
if (!JsonGetBool(body, "on", &on)) {
return StatusResponse::Error(StatusCode::kInvalidArgument,
"can truong bool \"on\"");
}
estop_.store(on);
{
std::lock_guard<std::mutex> lk(safety_mu_);
safety_->SetEstop(on);
}
// KHONG tu goi SetVelocity: luong safety thay co va zero lenh trong
// vong mot chu ky (50 ms). Mot duong duy nhat toi base, ke ca o
// duong khan cap -- hai duong nghia la hai cho de sai.
return StatusResponse::Ok();
}
if (cmd == "safety/reset") {
estop_.store(false);
std::lock_guard<std::mutex> lk(safety_mu_);
safety_->SetEstop(false);
safety_->Reset();
return StatusResponse::Ok();
}
if (cmd == "teleop/adjust") {
std::string axis;
double dir = 0.0;
if (!JsonGetString(body, "axis", &axis) ||
!JsonGetNumber(body, "dir", &dir)) {
return StatusResponse::Error(StatusCode::kInvalidArgument,
"can \"axis\" va \"dir\"");
}
std::lock_guard<std::mutex> lk(teleop_mu_);
if (axis == "lin") teleop_->AdjustLinearSpeed(dir);
else if (axis == "ang") teleop_->AdjustAngularSpeed(dir);
else {
return StatusResponse::Error(StatusCode::kInvalidArgument,
"axis phai la \"lin\" hoac \"ang\"");
}
return StatusResponse::Ok();
}
return StatusResponse::Error(StatusCode::kNotFound, "lenh la");
},
// --- Joypad qua WebSocket ---
[this](double vx_norm, double wz_norm) {
if (estop_.load()) {
std::lock_guard<std::mutex> lk(teleop_mu_);
teleop_->ClearMotion();
return;
}
// Truc joystick cua trang web dung quy uoc cua TeleopModel:
// vertical duong = keo XUONG = lui
// horizontal duong = keo PHAI = quay phai
// app.js gui theo chieu NGUOI LAI (duong = tien/trai), nen doi dau o
// day. Doi dau o MOT cho, khong rai rac hai phia.
std::pair<double, double> c;
{
std::lock_guard<std::mutex> lk(teleop_mu_);
teleop_->SetJoystick(-wz_norm, -vx_norm);
c = teleop_->Command();
}
// CHI ghi lai lenh THO. Viec danh gia an toan do LUONG SAFETY lam, o
// 20 Hz tren dong ho cua no.
//
// Goi Step() ngay o day la sai: FSM se chi chay khi CO lenh toi, ma
// NO_CMD / SCAN_TIMEOUT / cliff_hold ton tai chinh xac cho truong hop
// lenh NGUNG toi. Da mac loi nay mot lan.
std::lock_guard<std::mutex> lk(cmd_mu_);
raw_cmd_.linear.x = c.first;
raw_cmd_.angular.z = c.second;
raw_cmd_stamp_ = clock_->Now();
have_raw_cmd_ = true;
}));
return StatusResponse::Ok();
}
} // namespace vslam