// vslam_web_demo -- tieu chi nghiem thu P1. // // Chay web UI bang DU LIEU GIA: khong can LiDAR, khong can camera, khong can // RTAB-Map. Muc dich la tach viec kiem chung tang hien thi ra khoi viec kiem // chung tang cam bien -- neu doi ca hai cung luc thi khi co gi sai se khong // biet loi nam o dau. // // ./vslam_web_demo [--port 8080] [--token abc] [--assets ] // [--map-w 2000] [--map-h 2000] // // Roi mo http://:8080 tu laptop. #include #include #include #include #include #include #include #include #include #include #include #include "vslam/clock.h" #include "vslam/geometry.h" #include "vslam/log.h" #include "vslam/json_parse.h" #include "vslam/web_server.h" namespace { std::atomic g_stop{false}; void OnSignal(int) { g_stop.store(true); } constexpr double kPi = 3.14159265358979323846; // Ban do gia: mot hanh lang co phong hai ben. Du de kiem RLE va kiem render. vslam::OccupancyGridPtr MakeFakeMap(uint32_t w, uint32_t h, float res) { auto map = std::make_shared(); map->width = w; map->height = h; map->resolution = res; map->origin.position.x = -static_cast(w) * res / 2.0; map->origin.position.y = -static_cast(h) * res / 2.0; map->data.assign(static_cast(w) * h, -1); auto put = [&](uint32_t x, uint32_t y, int8_t v) { if (x < w && y < h) map->data[static_cast(y) * w + x] = v; }; const uint32_t cy = h / 2; const uint32_t half = std::max(6, h / 24); // Hanh lang chinh chay theo truc x. for (uint32_t x = w / 10; x < w - w / 10; ++x) { for (uint32_t y = cy - half; y <= cy + half; ++y) put(x, y, 0); put(x, cy - half - 1, 100); put(x, cy + half + 1, 100); } // Phong hai ben, cach deu. const uint32_t room = half * 3; for (uint32_t rx = w / 6; rx + room < w - w / 10; rx += room + room / 2) { for (int side = 0; side < 2; ++side) { const uint32_t y0 = side == 0 ? cy + half + 2 : cy - half - 2 - room; for (uint32_t x = rx; x < rx + room; ++x) { for (uint32_t y = y0; y < y0 + room; ++y) put(x, y, 0); } for (uint32_t x = rx - 1; x <= rx + room; ++x) { put(x, y0 - 1, 100); put(x, y0 + room, 100); } for (uint32_t y = y0 - 1; y <= y0 + room; ++y) { put(rx - 1, y, 100); put(rx + room, y, 100); } // Cua thong ra hanh lang. const uint32_t door = rx + room / 2; for (uint32_t x = door - 2; x <= door + 2; ++x) { put(x, side == 0 ? y0 - 1 : y0 + room, 0); } } } return map; } vslam::LaserScanPtr MakeFakeScan(double t) { auto scan = std::make_shared(); // Khop cau hinh LakiBeam 1L that: 270 do, 1081 tia, 25 Hz. scan->angle_min = -2.356194f; scan->angle_max = 2.356194f; scan->angle_increment = static_cast(2.0 * 2.356194 / 1080.0); scan->range_min = 0.05f; scan->range_max = 40.0f; scan->scan_time = 0.04f; scan->time_increment = scan->scan_time / 1081.0f; scan->ranges.resize(1081); static std::mt19937 rng(42); std::normal_distribution noise(0.0f, 0.01f); for (size_t i = 0; i < scan->ranges.size(); ++i) { const double a = scan->angle_min + static_cast(i) * scan->angle_increment; // Than robot che +-118 do: ScanSanitizer that se dat sentinel range_max+1. // Tai lap o day de kiem client phan biet dung ba ngu nghia. if (std::abs(a) > 118.0 * kPi / 180.0) { scan->ranges[i] = scan->range_max + 1.0f; continue; } double r = 3.0 + 1.5 * std::sin(a * 3.0 + t * 0.5); if (i % 97 == 0) { scan->ranges[i] = std::numeric_limits::infinity(); // khong phan xa continue; } if (i % 131 == 0) { scan->ranges[i] = std::numeric_limits::quiet_NaN(); // khong do duoc continue; } scan->ranges[i] = static_cast(r) + noise(rng); } return scan; } vslam::PointCloudXYZPtr MakeFakeCloud(double t) { auto cloud = std::make_shared(); cloud->points.reserve(4000); static std::mt19937 rng(7); std::uniform_real_distribution u(-1.0f, 1.0f); for (int i = 0; i < 4000; ++i) { const double a = static_cast(i) / 4000.0 * 2.0 * kPi; const double r = 1.5 + 0.8 * std::sin(a * 5.0 + t); vslam::PointXYZ p; p.x = static_cast(r * std::cos(a)) + u(rng) * 0.05f; p.y = static_cast(r * std::sin(a)) + u(rng) * 0.05f; p.z = static_cast(0.3 + 0.5 * std::sin(a * 2.0)) + u(rng) * 0.03f; cloud->points.push_back(p); } return cloud; } } // namespace int main(int argc, char** argv) { vslam::WebConfig cfg; uint32_t map_w = 600, map_h = 400; 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 == "--port") cfg.port = static_cast(std::atoi(next().c_str())); else if (a == "--token") cfg.auth_token = next(); else if (a == "--assets") cfg.asset_dir = next(); else if (a == "--map-w") map_w = static_cast(std::atoi(next().c_str())); else if (a == "--map-h") map_h = static_cast(std::atoi(next().c_str())); else if (a == "--help") { std::printf("cach dung: %s [--port N] [--token S] [--assets DIR] " "[--map-w N] [--map-h N]\n", argv[0]); return 0; } } std::signal(SIGINT, OnSignal); std::signal(SIGTERM, OnSignal); std::atomic estop{false}; vslam::WebServer server( cfg, [&estop](const std::string& cmd, const std::string& body) -> vslam::StatusResponse { VSLAM_INFO("lenh: " << cmd << " body=" << body); if (cmd == "safety/estop") { // Dung CHINH ham parse ma lop xac thuc dung. Neu handler va lop xac // thuc doc body theo hai cach khac nhau thi khoang cach giua hai // cach do la mot lo hong -- da tung nhu vay o ban truoc, khi handler // tim chuoi "true" con lop xac thuc goi JsonGetBool. bool on = false; if (!vslam::JsonGetBool(body, "on", &on)) { return vslam::StatusResponse::Error(vslam::StatusCode::kInvalidArgument, "can truong bool \"on\""); } estop.store(on); return vslam::StatusResponse::Ok(); } if (cmd == "safety/reset") { estop.store(false); return vslam::StatusResponse::Ok(); } return vslam::StatusResponse::Error(vslam::StatusCode::kNotFound, "lenh la"); }, [](double vx, double wz) { VSLAM_DEBUG_THROTTLE(1.0, "teleop vx=" << vx << " wz=" << wz); }); const vslam::StatusResponse st = server.Start(); if (!st.ok()) { std::fprintf(stderr, "khong khoi dong duoc web: %s\n", st.message.c_str()); return 1; } std::printf("mo http://localhost:%u (Ctrl-C de dung)\n", server.port()); const auto map = MakeFakeMap(map_w, map_h, 0.05f); auto clock = vslam::MakeSystemClock(); const auto t0 = std::chrono::steady_clock::now(); int tick = 0; while (!g_stop.load()) { const double t = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); vslam::WebSnapshot snap; snap.stamp = clock->Now(); // Robot chay vong quanh hanh lang. const double R = static_cast(map_w) * 0.05 * 0.25; snap.pose_map.x = R * std::cos(t * 0.15); snap.pose_map.y = R * 0.35 * std::sin(t * 0.30); snap.pose_map.theta = vslam::NormalizeAngle( std::atan2(R * 0.35 * 0.30 * std::cos(t * 0.30), -R * 0.15 * std::sin(t * 0.15))); snap.pose_odom = snap.pose_map; snap.vx = 0.32; snap.vyaw = 0.10 * std::sin(t * 0.3); snap.slip_metric = 0.02 + 0.01 * std::sin(t * 2.0); snap.roll = 0.01 * std::sin(t); snap.pitch = -0.02 * std::cos(t * 0.7); snap.accel_ok = true; const double min_front = 0.5 + 0.45 * (1.0 + std::sin(t * 0.4)); snap.safety.min_front = min_front; snap.safety.front_rays = 141; snap.safety.front_blanked = 4; snap.safety.front_usable = 137; snap.safety.estop = estop.load(); if (estop.load()) { snap.safety.state = "ESTOP"; snap.safety.detail = "dung khan cap tu web"; } else if (min_front < 0.40) { snap.safety.state = "SCAN_STOP"; snap.safety.detail = "vat can " + std::to_string(min_front).substr(0, 4) + " m"; } else if (min_front < 0.90) { snap.safety.state = "SCAN_SLOW"; snap.safety.detail = "vat can " + std::to_string(min_front).substr(0, 4) + " m"; } else { snap.safety.state = "OK"; } snap.slam.mode = "localization"; snap.slam.localized = true; snap.slam.score = 0.65 + 0.2 * std::sin(t * 0.5); snap.slam.map_version = 1; snap.slam.process_ms = 120.0 + 40.0 * std::sin(t); snap.health.lidar_hz = 24.9; snap.health.camera_hz = 29.7; snap.health.imu_hz = 199.4; snap.health.ekf_hz = 50.0; snap.health.clock_skew = 1.0000031; snap.health.cpu_pct = 38.0 + 8.0 * std::sin(t * 0.2); snap.health.rss_mb = 812.0; snap.health.drops = {{"scan", 0}, {"cloud", 3}, {"camera", 1}}; snap.map = map; // Ton trong tan so trong WebConfig. Day la viec ma luong web_tx that se // lam: mot Snapshot khong dien truong nao thi truong do khong duoc gui. // De tat ca cung 10 Hz la lang phi bang thong -- do chinh la loi lan do // dau tien, PATH chay 9.8 Hz thay vi 1 Hz va an het 30 kB/s. const int scan_every = std::max(1, static_cast(10.0 / cfg.scan_hz)); const int cloud_every = std::max(1, static_cast(10.0 / cfg.cloud_hz)); const int path_every = std::max(1, static_cast(10.0 / cfg.path_hz)); if (tick % scan_every == 0) snap.scan = MakeFakeScan(t); if (tick % cloud_every == 0) snap.cloud = MakeFakeCloud(t); if (tick % path_every == 0) { snap.trajectory.reserve(400); for (int k = 0; k < 400; ++k) { const double tk = t - static_cast(400 - k) * 0.1; snap.trajectory.push_back( vslam::Pose2D{R * std::cos(tk * 0.15), R * 0.35 * std::sin(tk * 0.30), 0.0}); } } server.Publish(snap); ++tick; std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 10 Hz } const vslam::WebServer::Stats s = server.stats(); std::printf("\nket noi=%lu khung_gui=%lu khung_drop=%lu byte=%lu lenh=%lu\n", static_cast(s.connections_total), static_cast(s.frames_sent), static_cast(s.frames_dropped), static_cast(s.bytes_sent), static_cast(s.commands_handled)); server.Stop(); return 0; }