548 lines
21 KiB
C++
548 lines
21 KiB
C++
// vslam_sim_run -- chay mo phong + web UI trong mot tien trinh.
|
|
//
|
|
// Thay cho `roslaunch robot_bringup sim.launch` + Gazebo GUI + RViz.
|
|
//
|
|
// ./vslam_sim_run [--map <map.yaml>] [--port 8080] [--token <chuoi>]
|
|
// [--slip 0.05] [--speed 1.0]
|
|
//
|
|
// Roi mo http://<may>:8080 va lai robot bang joypad tren trang.
|
|
//
|
|
// ================== DUONG DI CUA DU LIEU ==================
|
|
// SimSource -> LidarInterface -> ScanSanitizer -> web
|
|
// -> McuInterface -> (P3: EKF)
|
|
// -> BaseInterface <- lenh teleop tu web
|
|
//
|
|
// Chu y SimSource noi vao dung nhung interface ma phan cung that se noi vao.
|
|
// Khi cam LiDAR va base vao, chi doi hai dong o wiring, khong doi gi khac.
|
|
|
|
#include <atomic>
|
|
#include <cmath>
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "vslam/geometry.h"
|
|
#include "vslam/json_parse.h"
|
|
#include "vslam/log.h"
|
|
#include "vslam/node_constants.h"
|
|
#include "vslam/robot_model.h"
|
|
#include "vslam/occupancy_map_io.h"
|
|
#include "vslam/sim_source.h"
|
|
#include "vslam/ekf_se2.h"
|
|
#include "vslam/perception_pipeline.h"
|
|
#include "vslam/safety_monitor.h"
|
|
#include "vslam/teleop_model.h"
|
|
#include "vslam/wheel_odometry.h"
|
|
#include "vslam/web_server.h"
|
|
|
|
namespace {
|
|
|
|
std::atomic<bool> g_stop{false};
|
|
void OnSignal(int) { g_stop.store(true); }
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
vslam::SimConfig sim_cfg;
|
|
vslam::WebConfig web_cfg;
|
|
|
|
// Mo hinh teleop port tu robot_bringup/scripts/teleop_model.py (BSD-3-Clause,
|
|
// goc: virtual_joystick_teleop cua duongtd). Trang web chi gui truc joystick
|
|
// da chuan hoa; MOI phep quy ra van toc nam o day.
|
|
vslam::TeleopConfig tcfg;
|
|
tcfg.linear_speed = 0.30;
|
|
tcfg.angular_speed = 0.70;
|
|
tcfg.max_linear_speed = 0.35; // khop gioi han cua base
|
|
tcfg.max_angular_speed = 0.80;
|
|
|
|
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 == "--map") sim_cfg.map_yaml = next();
|
|
else if (a == "--port") web_cfg.port = static_cast<uint16_t>(std::atoi(next().c_str()));
|
|
else if (a == "--token") web_cfg.auth_token = next();
|
|
else if (a == "--assets") web_cfg.asset_dir = next();
|
|
else if (a == "--slip") sim_cfg.slip_factor = std::atof(next().c_str());
|
|
else if (a == "--speed") sim_cfg.speed = std::atof(next().c_str());
|
|
else if (a == "--x") sim_cfg.start_x = std::atof(next().c_str());
|
|
else if (a == "--y") sim_cfg.start_y = std::atof(next().c_str());
|
|
else if (a == "--yaw") sim_cfg.start_yaw = std::atof(next().c_str());
|
|
else if (a == "--help") {
|
|
std::printf(
|
|
"cach dung: %s [--map map.yaml] [--port N] [--token S]\n"
|
|
" [--x X] [--y Y] [--yaw RAD] [--slip F] [--speed F]\n\n"
|
|
"Khong co --map thi dung the gioi tong hop (hanh lang + phong).\n",
|
|
argv[0]);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
std::signal(SIGINT, OnSignal);
|
|
std::signal(SIGTERM, OnSignal);
|
|
|
|
// --- Hinh hoc robot ------------------------------------------------------
|
|
// MOT nguon duy nhat cho hinh hoc: robot.yaml. Bo mo phong cung lay tu day,
|
|
// neu khong thi "sim dung mot bo so, duong ong dung bo khac" va moi phep do
|
|
// deu do nham.
|
|
vslam::RobotModel robot;
|
|
{
|
|
const vslam::StatusResponse rs =
|
|
robot.LoadFile(vslam::ConfigPath(vslam::kRobotConfigFile));
|
|
if (!rs.ok()) {
|
|
std::fprintf(stderr, "robot.yaml: %s\n", rs.message.c_str());
|
|
return 1;
|
|
}
|
|
}
|
|
sim_cfg.wheel_radius = robot.geometry().wheel_radius;
|
|
sim_cfg.wheel_separation = robot.geometry().wheel_separation;
|
|
sim_cfg.robot_radius = robot.geometry().robot_radius;
|
|
sim_cfg.camera_z = robot.camera_height_above_floor();
|
|
sim_cfg.camera_pitch = robot.camera_pitch();
|
|
sim_cfg.max_linear = robot.base().max_linear;
|
|
sim_cfg.max_angular = robot.base().max_angular;
|
|
sim_cfg.accel_limit = robot.base().accel_limit;
|
|
{
|
|
vslam::Transform laser;
|
|
if (robot.StaticTransform(vslam::kBaseLinkFrame, vslam::kLaserFrame, &laser)) {
|
|
sim_cfg.lidar_x = laser.translation.x;
|
|
}
|
|
vslam::Transform cam;
|
|
if (robot.StaticTransform(vslam::kBaseLinkFrame, vslam::kCameraFrame, &cam)) {
|
|
sim_cfg.camera_x = cam.translation.x;
|
|
}
|
|
}
|
|
|
|
// --- Mo phong ------------------------------------------------------------
|
|
vslam::SimSource sim(sim_cfg);
|
|
const vslam::StatusResponse st = sim.Open();
|
|
if (!st.ok()) {
|
|
std::fprintf(stderr, "sim: %s\n", st.message.c_str());
|
|
return 1;
|
|
}
|
|
|
|
// --- Ve sinh scan --------------------------------------------------------
|
|
// Dung CHINH ScanSanitizer ma phan cung that se dung. Sector mask +-118 do
|
|
// khop cau hinh cu: laser o x=0.24, than robot nua rong 0.17.
|
|
constexpr double kPi = 3.14159265358979323846;
|
|
vslam::ScanSanitizerConfig san_cfg;
|
|
san_cfg.range_min = 0.05;
|
|
san_cfg.range_max = 40.0;
|
|
san_cfg.sector_masks.push_back(vslam::SectorMask{118.0 * kPi / 180.0, kPi});
|
|
san_cfg.sector_masks.push_back(vslam::SectorMask{-kPi, -118.0 * kPi / 180.0});
|
|
vslam::ScanSanitizer sanitizer(san_cfg);
|
|
|
|
// --- Perception: depth -> cloud -> loc -> can trong luc -> phat hien vuc --
|
|
//
|
|
// Chay INLINE trong callback camera, dung mot luong. Do la cai thay cho ba
|
|
// nodelet chung mot manager cua ban ROS, va tot hon: thu tu buoc la thu tu
|
|
// dong lenh chu khong phu thuoc thu tu remap trong launch file.
|
|
vslam::PerceptionConfig perc_cfg;
|
|
perc_cfg.gravity.wheel_radius = robot.geometry().wheel_radius;
|
|
perc_cfg.cliff.wheel_radius = robot.geometry().wheel_radius;
|
|
// `nominal_tz` la HE QUA cua camera_link.z cong wheel_radius, khong phai
|
|
// mot so go tay. Go tay lan thu hai la cho no duoc phep lech -- va no DA
|
|
// lech that: 0.803 trong cliff_detect.h so voi 0.80 cua sim.
|
|
perc_cfg.cliff.nominal_tz = robot.camera_height_above_floor();
|
|
|
|
// camera_depth_optical_frame -> base_link, lay tu CAY FRAME cua robot.yaml.
|
|
//
|
|
// Truoc day phep dung nay duoc go tay o day, va o ba file khac nua. Bon ban
|
|
// chep cua cung mot chuoi QuaternionMultiply, va khong co gi bat duoc khi
|
|
// mot trong bon lech di.
|
|
vslam::Transform cam_to_base;
|
|
if (!robot.StaticTransform(vslam::kBaseLinkFrame,
|
|
vslam::kCameraDepthOpticalFrame, &cam_to_base)) {
|
|
std::fprintf(stderr, "robot.yaml: khong co duong camera -> base_link\n");
|
|
return 1;
|
|
}
|
|
|
|
vslam::PerceptionPipeline perception(perc_cfg, *sim.clock());
|
|
perception.SetCameraExtrinsic(cam_to_base);
|
|
|
|
std::mutex perc_mu;
|
|
vslam::PointCloudXYZPtr latest_cloud;
|
|
bool perc_cliff = false;
|
|
vslam::UnixTime perc_cliff_stamp;
|
|
bool have_perc = false;
|
|
std::string perc_detail;
|
|
|
|
// --- Trang thai chia se giua luong sim va luong web ----------------------
|
|
// --- Duong odometry that: encoder -> wheel_odom -> EKF ------------------
|
|
// Dung CHINH cac lop se chay tren robot that. SimSource day encoder va IMU
|
|
// vao dung nhung interface ma phan cung day vao.
|
|
vslam::WheelOdometryConfig wo_cfg;
|
|
wo_cfg.wheel_radius = robot.geometry().wheel_radius;
|
|
wo_cfg.wheel_separation = robot.geometry().wheel_separation;
|
|
vslam::WheelOdometry wheel_odom(wo_cfg);
|
|
vslam::EkfSe2 ekf;
|
|
|
|
std::mutex odom_mu;
|
|
vslam::Odometry latest_odom;
|
|
bool have_odom = false;
|
|
|
|
// --- Lop an toan that ---------------------------------------------------
|
|
// Thay cho chot e-stop tam thoi. FSM 10 nhanh, dung lop se chay tren robot
|
|
// that. Lenh teleop di QUA no truoc khi toi base.
|
|
vslam::SafetyMonitor safety;
|
|
std::mutex safety_mu;
|
|
vslam::SafetyOutput last_safety;
|
|
|
|
// Lenh teleop THO, chua qua lop an toan. Luong safety doc bien nay o 20 Hz.
|
|
std::mutex cmd_mu;
|
|
vslam::Twist raw_cmd;
|
|
vslam::UnixTime raw_cmd_stamp;
|
|
bool have_raw_cmd = false;
|
|
|
|
std::mutex mu;
|
|
vslam::TeleopModel teleop(tcfg);
|
|
std::mutex teleop_mu;
|
|
vslam::LaserScanPtr latest_scan;
|
|
std::vector<vslam::Pose2D> trajectory;
|
|
std::atomic<bool> estop{false};
|
|
|
|
sim.lidar()->SetCallback([&](vslam::LaserScanPtr s) {
|
|
auto copy = std::make_shared<vslam::LaserScan>(*s);
|
|
sanitizer.Apply(copy.get());
|
|
std::lock_guard<std::mutex> lk(mu);
|
|
latest_scan = copy;
|
|
});
|
|
// IMU -> gyro cho phat hien truot, VA -> EKF.
|
|
sim.mcu()->SetImuCallback([&](vslam::ImuPtr m) {
|
|
// Bo loc bu cua gravity_align an o day: 200 Hz, truoc moi thu khac.
|
|
perception.FeedImu(*m);
|
|
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);
|
|
});
|
|
|
|
// Encoder -> wheel_odom -> EKF.
|
|
//
|
|
// Chu y truyen THANG twist.covariance tu wheel_odom 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.
|
|
sim.base()->SetEncoderCallback([&](const vslam::EncoderReading& e) {
|
|
if (!e.valid) return;
|
|
std::lock_guard<std::mutex> lk(odom_mu);
|
|
vslam::Odometry o;
|
|
if (!wheel_odom.Update(e.stamp, e.left_rad, e.right_rad, &o)) return;
|
|
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 -- dung loi da lam cliff_detect bao vuc gia 1625/1832
|
|
// mau trong repo cu.
|
|
perception.FeedOdom(e.stamp, o.twist.twist.linear.x, o.twist.twist.angular.z);
|
|
});
|
|
|
|
// Camera -> perception. SimSource tra nullptr khi enable_depth_camera=false,
|
|
// khong tra mot camera khong co anh -- nen kiem nullptr o day la du.
|
|
if (sim.camera() != nullptr) {
|
|
sim.camera()->SetCallback([&](const vslam::CameraFrame& f) {
|
|
if (!f.depth) return;
|
|
vslam::PerceptionOutput po = perception.Process(*f.depth, f.depth_info);
|
|
std::lock_guard<std::mutex> lk(perc_mu);
|
|
latest_cloud = po.cloud_aligned;
|
|
perc_cliff = po.cliff_detected;
|
|
perc_cliff_stamp = po.stamp;
|
|
perc_detail = po.cliff_detail;
|
|
have_perc = true;
|
|
});
|
|
}
|
|
|
|
sim.lidar()->Open();
|
|
sim.mcu()->Open();
|
|
sim.base()->Open();
|
|
if (sim.camera() != nullptr) sim.camera()->Open();
|
|
|
|
// --- Web -----------------------------------------------------------------
|
|
vslam::WebServer web(
|
|
web_cfg,
|
|
[&](const std::string& cmd, const std::string& body) -> vslam::StatusResponse {
|
|
if (cmd == "safety/estop") {
|
|
bool on = false;
|
|
if (!vslam::JsonGetBool(body, "on", &on)) {
|
|
return vslam::StatusResponse::Error(vslam::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 se thay estop va zero lenh
|
|
// trong vong 50 ms. Mot duong duy nhat toi base.
|
|
return vslam::StatusResponse::Ok();
|
|
}
|
|
if (cmd == "safety/reset") {
|
|
estop.store(false);
|
|
std::lock_guard<std::mutex> lk(safety_mu);
|
|
safety.SetEstop(false);
|
|
safety.Reset();
|
|
return vslam::StatusResponse::Ok();
|
|
}
|
|
if (cmd == "teleop/adjust") {
|
|
// Doi scale van toc NGAY KHI DANG CHAY -- tinh nang cua
|
|
// teleop_model.py ma ban joystick viet voi khong co.
|
|
std::string axis;
|
|
double dir = 0.0;
|
|
if (!vslam::JsonGetString(body, "axis", &axis) ||
|
|
!vslam::JsonGetNumber(body, "dir", &dir)) {
|
|
return vslam::StatusResponse::Error(vslam::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 vslam::StatusResponse::Error(vslam::StatusCode::kInvalidArgument,
|
|
"axis phai la \"lin\" hoac \"ang\"");
|
|
}
|
|
return vslam::StatusResponse::Ok();
|
|
}
|
|
return vslam::StatusResponse::Error(vslam::StatusCode::kNotFound, "lenh la");
|
|
},
|
|
[&](double vx_norm, double wz_norm) {
|
|
// E-stop chan lenh o day. Lop safety that (P4) se lam viec nay tu te
|
|
// hon -- co FSM, co timeout, co dem tia. Day chi la chan tam.
|
|
if (estop.load()) {
|
|
std::lock_guard<std::mutex> lk(teleop_mu);
|
|
teleop.ClearMotion();
|
|
sim.base()->SetVelocity(0.0, 0.0);
|
|
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 vx/wz 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> cmd;
|
|
{
|
|
std::lock_guard<std::mutex> lk(teleop_mu);
|
|
teleop.SetJoystick(-wz_norm, -vx_norm);
|
|
cmd = 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 teleop
|
|
// 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 = cmd.first;
|
|
raw_cmd.angular.z = cmd.second;
|
|
raw_cmd_stamp = sim.clock()->Now();
|
|
have_raw_cmd = true;
|
|
});
|
|
|
|
const vslam::StatusResponse wst = web.Start();
|
|
if (!wst.ok()) {
|
|
std::fprintf(stderr, "web: %s\n", wst.message.c_str());
|
|
return 1;
|
|
}
|
|
|
|
const vslam::OccupancyGrid& grid = sim.map();
|
|
auto map_ptr = std::make_shared<const vslam::OccupancyGrid>(grid);
|
|
|
|
std::printf("\n mo phong dang chay\n");
|
|
std::printf(" ban do : %ux%u @ %.3f m/o\n", grid.width, grid.height,
|
|
static_cast<double>(grid.resolution));
|
|
std::printf(" web UI : http://localhost:%u%s\n", web.port(),
|
|
web_cfg.auth_token.empty() ? "" : "/?token=<token>");
|
|
std::printf(" dieu khien: joypad tren trang web\n");
|
|
std::printf(" Ctrl-C de dung\n\n");
|
|
|
|
// --- Luong mo phong ------------------------------------------------------
|
|
std::thread sim_thread([&] { sim.Run(); });
|
|
|
|
// --- Luong an toan: 20 Hz, doc lap voi moi thu khac ----------------------
|
|
//
|
|
// Tren robot that luong nay chay SCHED_FIFO cao nhat va KHONG chia se mutex
|
|
// nao voi luong slam. O day chua co slam, nhung giu dung cau truc de khi
|
|
// them vao thi khong phai sua lai.
|
|
//
|
|
// No chay DEU DAN bat ke co lenh teleop hay khong -- do la dieu kien de
|
|
// NO_CMD, SCAN_TIMEOUT va cliff_hold co y nghia.
|
|
std::thread safety_thread([&] {
|
|
while (!g_stop.load()) {
|
|
vslam::SafetyInputs si;
|
|
si.now = sim.clock()->Now();
|
|
|
|
vslam::Twist cmd_copy;
|
|
{
|
|
std::lock_guard<std::mutex> lk(cmd_mu);
|
|
if (have_raw_cmd) {
|
|
cmd_copy = raw_cmd;
|
|
si.cmd = &cmd_copy;
|
|
si.cmd_stamp = raw_cmd_stamp;
|
|
}
|
|
}
|
|
|
|
vslam::LaserScanPtr scan_copy;
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu);
|
|
scan_copy = latest_scan;
|
|
}
|
|
if (scan_copy) {
|
|
si.scan = scan_copy.get();
|
|
si.scan_stamp = scan_copy->header.stamp;
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(perc_mu);
|
|
if (have_perc) {
|
|
si.have_cliff = true;
|
|
si.cliff = perc_cliff;
|
|
si.cliff_stamp = perc_cliff_stamp;
|
|
}
|
|
}
|
|
|
|
vslam::SafetyOutput so;
|
|
{
|
|
std::lock_guard<std::mutex> lk(safety_mu);
|
|
so = safety.Step(si);
|
|
last_safety = so;
|
|
}
|
|
// Day la duong DUY NHAT lenh toi duoc base. Khong co nhanh nao cho
|
|
// lenh di thang xuong, ke ca teleop.
|
|
sim.base()->SetVelocity(so.cmd.linear.x, so.cmd.angular.z);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 20 Hz
|
|
}
|
|
});
|
|
|
|
// --- Vong phat len web ---------------------------------------------------
|
|
vslam::UnixTime last_traj;
|
|
while (!g_stop.load()) {
|
|
vslam::WebSnapshot snap;
|
|
snap.stamp = sim.clock()->Now();
|
|
|
|
const vslam::Pose2D gt = sim.ground_truth();
|
|
|
|
// Pose hien thi la dau ra cua ESTIMATOR, khong phai ground truth.
|
|
// Ground truth chi dung de DOI CHUNG -- dung vai tro cua
|
|
// /ground_truth/odom trong repo cu, noi ghi ro "khong estimator nao duoc
|
|
// subscribe".
|
|
vslam::Pose2D ekf_pose = ekf.pose();
|
|
vslam::Pose2D wheel_pose;
|
|
{
|
|
std::lock_guard<std::mutex> lk(odom_mu);
|
|
wheel_pose = wheel_odom.pose();
|
|
snap.slip_metric = wheel_odom.slip_metric();
|
|
if (have_odom) {
|
|
snap.vx = latest_odom.twist.twist.linear.x;
|
|
snap.vyaw = latest_odom.twist.twist.angular.z;
|
|
}
|
|
}
|
|
snap.pose_map = ekf_pose;
|
|
snap.pose_odom = wheel_pose;
|
|
|
|
// Sai so tich luy so voi ground truth -- day la con so ma bai L2 cua repo
|
|
// cu do duoc 1.04% trong sim va 18% tren robot that.
|
|
const double ate = vslam::Distance2D(ekf_pose, gt);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu);
|
|
snap.scan = latest_scan;
|
|
if (trajectory.empty() ||
|
|
vslam::Distance2D(trajectory.back(), gt) > 0.1) {
|
|
trajectory.push_back(gt);
|
|
if (trajectory.size() > 2000) {
|
|
trajectory.erase(trajectory.begin(), trajectory.begin() + 500);
|
|
}
|
|
}
|
|
snap.trajectory = trajectory;
|
|
}
|
|
|
|
snap.map = map_ptr;
|
|
snap.slam.map_version = 1;
|
|
snap.slam.mode = "sim";
|
|
snap.slam.localized = true;
|
|
snap.slam.score = 1.0;
|
|
|
|
const vslam::SimSource::Stats ss = sim.stats();
|
|
|
|
// So lieu an toan lay THANG tu FSM, khong tinh lai o day. Tinh lai la mo
|
|
// duong cho hai ban logic lech nhau -- dung loai bug da gap o lop xac
|
|
// thuc e-stop cua web.
|
|
{
|
|
std::lock_guard<std::mutex> lk(safety_mu);
|
|
snap.safety.state = vslam::SafetyStateName(last_safety.state);
|
|
snap.safety.detail = last_safety.detail;
|
|
snap.safety.min_front = last_safety.min_front;
|
|
snap.safety.front_rays = last_safety.front_rays;
|
|
snap.safety.front_blanked = last_safety.front_blanked;
|
|
snap.safety.front_usable = last_safety.front_usable;
|
|
snap.safety.estop = safety.estop();
|
|
snap.safety.bumper_latched = safety.bumper_latched();
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(teleop_mu);
|
|
snap.teleop.linear_speed = teleop.linear_speed();
|
|
snap.teleop.angular_speed = teleop.angular_speed();
|
|
snap.teleop.max_linear_speed = teleop.max_linear_speed();
|
|
snap.teleop.max_angular_speed = teleop.max_angular_speed();
|
|
}
|
|
|
|
snap.health.lidar_hz = sim_cfg.lidar_rate_hz;
|
|
snap.health.imu_hz = sim_cfg.imu_rate_hz;
|
|
snap.health.ekf_hz = sim_cfg.encoder_rate_hz;
|
|
{
|
|
std::lock_guard<std::mutex> lk(perc_mu);
|
|
snap.cloud = latest_cloud;
|
|
snap.safety.cliff = perc_cliff;
|
|
if (!perc_detail.empty() && perc_detail != "judged") {
|
|
// "khong biet" khac han "khong co vuc" -- phai hien duoc tren web.
|
|
snap.safety.detail = "cliff:" + perc_detail;
|
|
}
|
|
}
|
|
snap.roll = perception.gravity().roll();
|
|
snap.pitch = perception.gravity().pitch();
|
|
snap.accel_ok = perception.gravity().accel_ok();
|
|
snap.health.camera_hz =
|
|
sim.camera() != nullptr ? sim_cfg.camera_rate_hz : 0.0;
|
|
snap.health.clock_skew = 1.0;
|
|
snap.health.drops = {{"collisions", ss.collisions},
|
|
{"slip_events", wheel_odom.stats().slip_events}};
|
|
|
|
// Hien sai so EKF vs ground truth ngay tren bang trang thai: con so nay
|
|
// la thu duy nhat noi duoc odometry co dung khong.
|
|
snap.slam.mode = "sim";
|
|
snap.slam.score = ss.distance_travelled > 0.1
|
|
? 1.0 - ate / ss.distance_travelled
|
|
: 1.0;
|
|
|
|
web.Publish(snap);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
}
|
|
|
|
sim.RequestStop();
|
|
if (safety_thread.joinable()) safety_thread.join();
|
|
if (sim_thread.joinable()) sim_thread.join();
|
|
web.Stop();
|
|
|
|
const vslam::SimSource::Stats s = sim.stats();
|
|
std::printf("\n thoi gian mo phong : %.1f s\n", s.sim_time_sec);
|
|
std::printf(" quang duong : %.2f m\n", s.distance_travelled);
|
|
std::printf(" scan / imu / enc : %lu / %lu / %lu\n",
|
|
static_cast<unsigned long>(s.scans), static_cast<unsigned long>(s.imu),
|
|
static_cast<unsigned long>(s.encoder));
|
|
std::printf(" va cham : %lu\n",
|
|
static_cast<unsigned long>(s.collisions));
|
|
return 0;
|
|
}
|