From 766a997de420925d4f5f5c1b9d8e6868d9623d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Thor=C3=A9n?= Date: Thu, 5 Nov 2026 14:34:16 +0100 Subject: [PATCH] Fix errors in the truck GUI --- .../inc/truckobjectcontrol.hpp | 2 + .../src/truckobjectcontrol.cpp | 33 ++++++++- atos_gui/atos_gui/main.py | 18 ++++- atos_gui/atos_gui/static/fleet_map.js | 74 ++++++------------- 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/atos/modules/TruckObjectControl/inc/truckobjectcontrol.hpp b/atos/modules/TruckObjectControl/inc/truckobjectcontrol.hpp index d525082be..bf3074617 100644 --- a/atos/modules/TruckObjectControl/inc/truckobjectcontrol.hpp +++ b/atos/modules/TruckObjectControl/inc/truckobjectcontrol.hpp @@ -50,6 +50,8 @@ class TruckObjectControl : public rclcpp::Node { std::string last_tcp_command = ""; std::string last_tcp_warning = ""; std::string last_cot_message = ""; + double ahead_gap_m = -1.0; + std::string ahead_uid = ""; rclcpp::Time last_cot_stamp = rclcpp::Time(0, 0, RCL_ROS_TIME); }; diff --git a/atos/modules/TruckObjectControl/src/truckobjectcontrol.cpp b/atos/modules/TruckObjectControl/src/truckobjectcontrol.cpp index 3fbea14d1..97ffa9a4f 100644 --- a/atos/modules/TruckObjectControl/src/truckobjectcontrol.cpp +++ b/atos/modules/TruckObjectControl/src/truckobjectcontrol.cpp @@ -50,6 +50,7 @@ constexpr int kTlsIoRetrySleepMs = 10; constexpr int kTlsWriteMaxTransientRetries = 100; constexpr std::string_view kDefaultGeoJsonName = "rural_road_centerline_ccw.geojson"; constexpr std::string_view kRuralRoadPrefix = "rural_road"; +constexpr double kMaxNearPathDistanceM = 15.0; // All rural_road* paths (case-insensitive) share one group under the default path name. void normalizePathName(std::string& path_name) { @@ -225,6 +226,10 @@ void TruckObjectControl::publishTruckState(const std::string& truck_id, const Tr payload["last_cot_message"] = state.last_cot_message; payload["last_tcp_command"] = state.last_tcp_command; payload["last_tcp_warning"] = state.last_tcp_warning; + payload["on_road"] = state.distance_to_path_m <= kMaxNearPathDistanceM; + payload["cot_fresh"] = isCotFresh(state, now()); + payload["ahead_gap_m"] = state.ahead_gap_m > 0.0 ? json(state.ahead_gap_m) : json(nullptr); + payload["ahead_uid"] = state.ahead_uid; payload["stamp_sec"] = now().seconds(); std_msgs::msg::String msg; @@ -1042,7 +1047,6 @@ void TruckObjectControl::handleTcpClient(const int client_fd, const std::string& void TruckObjectControl::evaluateAndPublishSpeedCommand() { constexpr double kMaxDistanceToPathM = 7.5; - constexpr double kMaxNearPathDistanceM = 15.0; struct ConnectedTruck { std::string id; @@ -1056,6 +1060,24 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() { }; std::vector connected; + // Ahead values exactly as sent in the TCP command, keyed by truck uid. + std::unordered_map> sent_ahead; + const auto publishAllTruckStates = [&]() { + std::vector> snapshot; + { + std::lock_guard lock(m_state_mutex); + for (auto& [id, state] : m_trucks) { + const auto it = sent_ahead.find(id); + state.ahead_gap_m = it != sent_ahead.end() ? it->second.first : -1.0; + state.ahead_uid = it != sent_ahead.end() ? it->second.second : ""; + snapshot.emplace_back(id, state); + } + } + for (const auto& [id, state] : snapshot) { + publishTruckState(id, state); + } + }; + const rclcpp::Time now_time = now(); { std::lock_guard lock(m_state_mutex); @@ -1106,6 +1128,7 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() { if (on_path_count == 0) { RCLCPP_WARN_THROTTLE( get_logger(), *get_clock(), 3000, "No TCP-connected trucks available for command evaluation."); + publishAllTruckStates(); return; } @@ -1278,6 +1301,10 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() { ahead_path_index = near_path_index; } + if (ahead_uid != "none") { + sent_ahead[group[i].id] = {ahead_gap, ahead_uid}; + } + std::string target_speed_mps_value = std::to_string(std::max(0.0, group[i].speed_mps)); if (control_command == "STOP") { target_speed_mps_value = std::to_string(m_stop_speed_kmh / 3.6); @@ -1324,6 +1351,8 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() { auto it = m_trucks.find(group[i].id); if (it != m_trucks.end()) { it->second.last_tcp_command = tcp_command; + it->second.ahead_gap_m = ahead_uid != "none" ? ahead_gap : -1.0; + it->second.ahead_uid = ahead_uid != "none" ? ahead_uid : ""; state_copy = it->second; has_state = true; } @@ -1337,6 +1366,8 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() { } } + publishAllTruckStates(); + if (!std::isfinite(min_gap_m)) { min_gap_m = -1.0; } diff --git a/atos_gui/atos_gui/main.py b/atos_gui/atos_gui/main.py index e78bde436..d176e4f4d 100644 --- a/atos_gui/atos_gui/main.py +++ b/atos_gui/atos_gui/main.py @@ -6,6 +6,7 @@ import json import sys import threading +import time from pathlib import Path import rclpy @@ -23,9 +24,10 @@ GEOJSON_NAME = "rural_road_centerline_ccw.geojson" FLEET_STATIC_ROUTE = "/atos_gui_static" FLEET_STATIC_DIR = Path(__file__).parent / "static" -FLEET_MAP_JS_VERSION = "20260424-3" +FLEET_MAP_JS_VERSION = "20261105-1" FLEET_STATE_LOCK = threading.Lock() FLEET_TRUCK_STATES: dict[str, dict] = {} +FLEET_STATE_MAX_AGE_S = 5.0 FLEET_GEOJSON_CACHE: dict[str, dict] = {} APP_CONFIGURED = False @@ -101,7 +103,18 @@ def _load_geojson_by_name(path_name: str) -> tuple[dict | None, Path | None]: def _fleet_map_payload_json() -> str: with FLEET_STATE_LOCK: - trucks = list(FLEET_TRUCK_STATES.values()) + now = time.monotonic() + for uid in [ + uid + for uid, truck in FLEET_TRUCK_STATES.items() + if now - truck["_rx_time"] > FLEET_STATE_MAX_AGE_S + ]: + del FLEET_TRUCK_STATES[uid] + trucks = [ + {k: v for k, v in truck.items() if k != "_rx_time"} + for truck in FLEET_TRUCK_STATES.values() + if truck.get("tcp_connected") and truck.get("cot_fresh", True) + ] path_names = {GEOJSON_NAME} for truck in trucks: @@ -147,6 +160,7 @@ def _on_state(self, msg: String) -> None: payload["path_name"] = Path(str(payload["path_name"])).name with FLEET_STATE_LOCK: + payload["_rx_time"] = time.monotonic() FLEET_TRUCK_STATES[str(uid)] = payload diff --git a/atos_gui/atos_gui/static/fleet_map.js b/atos_gui/atos_gui/static/fleet_map.js index 4e124dfee..41740ed92 100644 --- a/atos_gui/atos_gui/static/fleet_map.js +++ b/atos_gui/atos_gui/static/fleet_map.js @@ -182,42 +182,19 @@ return bestName; } - function computeAheadDistanceMap(trucks, pathLengthMeters, selectedPathName) { - const result = {}; - const candidates = trucks - .filter(function(item) { - const itemPath = item.path_name || selectedPathName; - return itemPath === selectedPathName && Number.isFinite(Number(item.distance_m)); - }) - .map(function(item) { - return { - uid: String(item.uid || "truck"), - distance_m: Number(item.distance_m), - }; - }) - .sort(function(a, b) { return a.distance_m - b.distance_m; }); - - if (candidates.length < 2) { - return result; + function aheadText(item) { + if (item.on_road === false) { + return "Not on Rural Road"; } - - for (let i = 0; i < candidates.length; i += 1) { - const current = candidates[i]; - const next = candidates[(i + 1) % candidates.length]; - let gap = next.distance_m - current.distance_m; - if (gap <= 0 && pathLengthMeters > 0) { - gap += pathLengthMeters; - } - if (gap > 0.01) { - result[current.uid] = gap; - } - } - return result; + const gap = Number(item.ahead_gap_m); + return item.ahead_gap_m !== null && item.ahead_gap_m !== undefined && Number.isFinite(gap) + ? gap.toFixed(1) + " m" + : "-"; } function renderSvg(container, coords, trucks, selectedPathName, pathsByName) { - const width = 600; - const height = 650; + const maxWidth = 600; + const maxHeight = 650; const padding = 30; let minLon = Infinity; @@ -234,12 +211,17 @@ if (lat > maxLat) maxLat = lat; }); - const lonSpan = Math.max(maxLon - minLon, 1e-9); - const latSpan = Math.max(maxLat - minLat, 1e-9); + // Equal metres per unit on both axes. + const lonScale = Math.cos(toRad((minLat + maxLat) / 2.0)); + const spanX = Math.max((maxLon - minLon) * lonScale, 1e-9); + const spanY = Math.max(maxLat - minLat, 1e-9); + const scale = Math.min((maxWidth - 2 * padding) / spanX, (maxHeight - 2 * padding) / spanY); + const width = Math.round(spanX * scale + 2 * padding); + const height = Math.round(spanY * scale + 2 * padding); function toSvgXY(lon, lat) { - const x = padding + ((lon - minLon) / lonSpan) * (width - 2 * padding); - const y = height - padding - ((lat - minLat) / latSpan) * (height - 2 * padding); + const x = padding + (lon - minLon) * lonScale * scale; + const y = height - padding - (lat - minLat) * scale; return {x: x, y: y}; } @@ -256,8 +238,6 @@ return itemPath === selectedPathName; }); - const aheadDistanceMap = computeAheadDistanceMap(filteredTrucks, totalMeters, selectedPathName); - const truckCircles = filteredTrucks.map(function(item) { const p = toSvgXY(Number(item.lon), Number(item.lat)); const uid = String(item.uid || "truck"); @@ -267,17 +247,13 @@ const courseDeg = Number(item.course_deg || 0); const pathIndex = Number(item.path_index || -1); const color = item.tcp_connected ? "#dc2626" : "#6b7280"; - const ahead = aheadDistanceMap[uid]; - const aheadText = - Number.isFinite(ahead) - ? (" next: " + ahead.toFixed(1) + " m") - : " next: -"; + const aheadLabel = " next: " + aheadText(item); return ( "" + "" + "" + - uid + " [idx " + pathIndex + "] " + speedKmh.toFixed(1) + " km/h @" + courseDeg.toFixed(0) + "°" + aheadText + + uid + " [idx " + pathIndex + "] " + speedKmh.toFixed(1) + " km/h @" + courseDeg.toFixed(0) + "°" + aheadLabel + "" + "" ); @@ -293,11 +269,7 @@ const lastCotMessage = String(item.last_cot_message || "-"); const lastTcpCommand = String(item.last_tcp_command || "-"); const tcpWarning = String(item.last_tcp_warning || ""); - const ahead = aheadDistanceMap[uid]; - const aheadCell = - Number.isFinite(ahead) - ? ahead.toFixed(1) + " m" - : "-"; + const aheadCell = aheadText(item); return ( "" + "" + uid + "" + @@ -327,9 +299,9 @@ container.innerHTML = "
" + - "
" + + "
" + "" + + "style='background:#f8fafc;border:1px solid #cbd5e1;border-radius:8px;max-width:100%;height:auto;overflow:visible;'>" + "" + truckCircles + "" +