#include "json_writer.h" #include "vslam/geometry.h" #include #include namespace vslam { namespace { void Key(const char* k, std::string* out) { out->push_back('"'); out->append(k); out->append("\":"); } void Comma(std::string* out) { out->push_back(','); } void Str(const char* k, const std::string& v, std::string* out) { Key(k, out); out->push_back('"'); JsonEscape(v, out); out->push_back('"'); } void Num(const char* k, double v, int dec, std::string* out) { Key(k, out); JsonNumber(v, dec, out); } void Int(const char* k, long long v, std::string* out) { Key(k, out); char buf[24]; std::snprintf(buf, sizeof(buf), "%lld", v); out->append(buf); } void Bool(const char* k, bool v, std::string* out) { Key(k, out); out->append(v ? "true" : "false"); } } // namespace void JsonEscape(const std::string& in, std::string* out) { for (const char c : in) { switch (c) { case '"': out->append("\\\""); break; case '\\': out->append("\\\\"); break; case '\n': out->append("\\n"); break; case '\r': out->append("\\r"); break; case '\t': out->append("\\t"); break; default: if (static_cast(c) < 0x20) { char buf[8]; std::snprintf(buf, sizeof(buf), "\\u%04x", static_cast(c)); out->append(buf); } else { out->push_back(c); } } } } void JsonNumber(double v, int decimals, std::string* out) { // JSON khong co NaN/Infinity. De nguyen thi JSON.parse() nem loi va browser // mat CA khung chu khong chi mot truong -- mot min_front = inf (hop le, co // nghia "khong thay gi") se lam ca bang trang thai bien mat. if (!std::isfinite(v)) { out->append("null"); return; } char buf[40]; std::snprintf(buf, sizeof(buf), "%.*f", decimals, v); out->append(buf); } void WriteStateJson(const WebSnapshot& s, std::string* out) { out->append("{\"t\":\"state\","); Num("stamp", s.stamp.toSec(), 3, out); Comma(out); // Hinh hoc robot -- trinh duyet KHONG duoc tu doan kich thuoc. // // Chi xuat khi `valid`: mot trinh ve nhan so 0 se dung ban kinh 0 va ve ra // mot cham, con nhan KHONG GI thi biet la chua co du lieu va bao ro. if (s.robot.valid) { Key("robot", out); out->push_back('{'); Num("radius", s.robot.radius, 4, out); Comma(out); Num("wheel_radius", s.robot.wheel_radius, 4, out); Comma(out); Num("wheel_sep", s.robot.wheel_separation, 4, out); Comma(out); Num("half_width", s.robot.body_half_width, 4, out); Comma(out); Num("body_l", s.robot.body_length, 4, out); Comma(out); Num("body_w", s.robot.body_width, 4, out); Comma(out); Num("body_h", s.robot.body_height, 4, out); Comma(out); Num("body_off_x", s.robot.body_offset_x, 4, out); Comma(out); Num("clearance", s.robot.ground_clearance, 4, out); Comma(out); Num("laser_x", s.robot.laser_x, 4, out); Comma(out); Num("laser_y", s.robot.laser_y, 4, out); Comma(out); Num("laser_z", s.robot.laser_z, 4, out); Comma(out); Num("cam_x", s.robot.camera_x, 4, out); Comma(out); Num("cam_y", s.robot.camera_y, 4, out); Comma(out); Num("cam_z", s.robot.camera_z, 4, out); Comma(out); Num("cam_pitch", s.robot.camera_pitch, 4, out); Comma(out); Num("cam_hfov", s.robot.camera_hfov, 4, out); Comma(out); Num("cam_vfov", s.robot.camera_vfov, 4, out); Comma(out); Num("cliff_min", s.robot.cliff_min, 3, out); Comma(out); Num("cliff_max", s.robot.cliff_max, 3, out); out->push_back('}'); Comma(out); } Key("pose", out); out->push_back('{'); Num("x", s.pose_map.x, 3, out); Comma(out); Num("y", s.pose_map.y, 3, out); Comma(out); Num("yaw", s.pose_map.theta, 4, out); out->push_back('}'); Comma(out); // Bu do tre cua scan: quy tia tu pose HIEN TAI ve pose LUC QUET. Cloud // duoc bu san o server (EncodeCloud), scan thi khong -- no la du lieu cuc. // // Chi xuat khi CO. Khong co thi trinh duyet ve bang pose hien tai nhu truoc, // chu khong phai ve bang mot delta 0 gia mao la "da bu". if (s.have_scan_delta) { Key("scan_delta", out); out->push_back('{'); Num("x", s.scan_delta.x, 4, out); Comma(out); Num("y", s.scan_delta.y, 4, out); Comma(out); Num("yaw", s.scan_delta.theta, 5, out); out->push_back('}'); Comma(out); } // Ground truth chi xuat khi CO. Xuat 0 khi khong co la noi doi: client // khong phan biet duoc "dung o goc" voi "khong biet". if (s.have_ground_truth) { Key("gt", out); out->push_back('{'); Num("x", s.ground_truth.x, 3, out); Comma(out); Num("y", s.ground_truth.y, 3, out); Comma(out); Num("yaw", s.ground_truth.theta, 4, out); Comma(out); Num("roll", s.gt_roll, 4, out); Comma(out); Num("pitch", s.gt_pitch, 4, out); Comma(out); // Sai so vi tri cua uoc luong so voi su that. Tinh o day thay vi de // client tu tru: neu sau nay map va gt khac goc toa do thi phep tru // ngay tren client se sai am tham. Num("err", Distance2D(s.pose_map, s.ground_truth), 4, out); out->push_back('}'); Comma(out); } Key("odom", out); out->push_back('{'); Num("x", s.pose_odom.x, 3, out); Comma(out); Num("y", s.pose_odom.y, 3, out); Comma(out); Num("yaw", s.pose_odom.theta, 4, out); Comma(out); Num("vx", s.vx, 3, out); Comma(out); Num("vyaw", s.vyaw, 3, out); Comma(out); Num("slip", s.slip_metric, 3, out); out->push_back('}'); Comma(out); Key("tilt", out); out->push_back('{'); Num("roll", s.roll, 4, out); Comma(out); Num("pitch", s.pitch, 4, out); Comma(out); Bool("accel_ok", s.accel_ok, out); out->push_back('}'); Comma(out); Key("safety", out); out->push_back('{'); Str("state", s.safety.state, out); Comma(out); Str("detail", s.safety.detail, out); Comma(out); Num("min_front", s.safety.min_front, 3, out); Comma(out); Int("front_rays", s.safety.front_rays, out); Comma(out); Int("front_blanked", s.safety.front_blanked, out); Comma(out); Int("front_usable", s.safety.front_usable, out); Comma(out); Bool("estop", s.safety.estop, out); Comma(out); Bool("bumper_latched", s.safety.bumper_latched, out); Comma(out); Bool("cliff", s.safety.cliff, out); out->push_back('}'); Comma(out); Key("slam", out); out->push_back('{'); Str("mode", s.slam.mode, out); Comma(out); Bool("localized", s.slam.localized, out); Comma(out); Num("score", s.slam.score, 3, out); Comma(out); Int("loop_id", s.slam.loop_closure_id, out); Comma(out); Int("prox_id", s.slam.proximity_id, out); Comma(out); Int("prox_total", s.slam.proximity_total, out); Comma(out); Int("map_version", s.slam.map_version, out); Comma(out); Num("process_ms", s.slam.process_ms, 1, out); Comma(out); // Ban do 3D: nguon va so o. Mot ban do voxel RONG phai doc duoc tu day, chu // khong phai bang cach mo khung 3D len roi doan. Str("voxels_source", s.slam.voxels_source, out); Comma(out); Int("voxel_cells", static_cast(s.slam.voxel_cells), out); Comma(out); Int("voxel_epoch", static_cast(s.slam.voxel_epoch), out); out->push_back('}'); Comma(out); Key("teleop", out); out->push_back('{'); Num("linear_speed", s.teleop.linear_speed, 3, out); Comma(out); Num("angular_speed", s.teleop.angular_speed, 3, out); Comma(out); Num("max_linear_speed", s.teleop.max_linear_speed, 3, out); Comma(out); Num("max_angular_speed", s.teleop.max_angular_speed, 3, out); out->push_back('}'); Comma(out); Key("health", out); out->push_back('{'); Num("lidar_hz", s.health.lidar_hz, 1, out); Comma(out); Num("camera_hz", s.health.camera_hz, 1, out); Comma(out); Num("imu_hz", s.health.imu_hz, 1, out); Comma(out); Num("ekf_hz", s.health.ekf_hz, 1, out); Comma(out); Num("clock_skew", s.health.clock_skew, 7, out); Comma(out); Num("cpu_pct", s.health.cpu_pct, 1, out); Comma(out); Num("rss_mb", s.health.rss_mb, 0, out); Comma(out); Key("drops", out); out->push_back('{'); bool first = true; for (const auto& kv : s.health.drops) { if (!first) Comma(out); first = false; Int(kv.first.c_str(), static_cast(kv.second), out); } out->push_back('}'); out->push_back('}'); out->push_back('}'); } void WriteStatusJson(const StatusResponse& st, std::string* out) { out->push_back('{'); Int("code", static_cast(st.code), out); Comma(out); Str("message", st.message, out); out->push_back('}'); } } // namespace vslam