Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions atos/modules/TruckObjectControl/inc/truckobjectcontrol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
33 changes: 32 additions & 1 deletion atos/modules/TruckObjectControl/src/truckobjectcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -1056,6 +1060,24 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() {
};
std::vector<ConnectedTruck> connected;

// Ahead values exactly as sent in the TCP command, keyed by truck uid.
std::unordered_map<std::string, std::pair<double, std::string>> sent_ahead;
const auto publishAllTruckStates = [&]() {
std::vector<std::pair<std::string, TruckState>> snapshot;
{
std::lock_guard<std::mutex> 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<std::mutex> lock(m_state_mutex);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -1337,6 +1366,8 @@ void TruckObjectControl::evaluateAndPublishSpeedCommand() {
}
}

publishAllTruckStates();

if (!std::isfinite(min_gap_m)) {
min_gap_m = -1.0;
}
Expand Down
18 changes: 16 additions & 2 deletions atos_gui/atos_gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import sys
import threading
import time
from pathlib import Path

import rclpy
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down
74 changes: 23 additions & 51 deletions atos_gui/atos_gui/static/fleet_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
}

Expand All @@ -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");
Expand All @@ -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 (
"<g>" +
"<circle cx='" + p.x.toFixed(2) + "' cy='" + p.y.toFixed(2) + "' r='5' fill='" + color + "' />" +
"<text x='" + (p.x + 8).toFixed(2) + "' y='" + (p.y - 8).toFixed(2) + "' " +
"font-size='12' font-family='sans-serif' fill='#111827'>" +
uid + " [idx " + pathIndex + "] " + speedKmh.toFixed(1) + " km/h @" + courseDeg.toFixed(0) + "°" + aheadText +
uid + " [idx " + pathIndex + "] " + speedKmh.toFixed(1) + " km/h @" + courseDeg.toFixed(0) + "°" + aheadLabel +
"</text>" +
"</g>"
);
Expand All @@ -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 (
"<tr>" +
"<td style='padding:2px 8px 2px 0;'>" + uid + "</td>" +
Expand Down Expand Up @@ -327,9 +299,9 @@

container.innerHTML =
"<div style='display:flex;gap:12px;align-items:flex-start;flex-wrap:wrap;'>" +
"<div style='flex:0 0 auto;width:600px;'>" +
"<div style='flex:0 0 auto;max-width:100%;'>" +
"<svg width='" + width + "' height='" + height + "' viewBox='0 0 " + width + " " + height + "' " +
"style='background:#f8fafc;border:1px solid #cbd5e1;border-radius:8px;'>" +
"style='background:#f8fafc;border:1px solid #cbd5e1;border-radius:8px;max-width:100%;height:auto;overflow:visible;'>" +
"<polyline fill='none' stroke='#0ea5e9' stroke-width='3' points='" + linePoints + "'/>" +
truckCircles +
"</svg>" +
Expand Down
Loading