Files
robot_sensor_ports/CMakeLists.txt
duongtd c60173d320 One place raw sensor readings live, shared by everything that needs them
Sensor data entered the navigation runtime and disappeared into the costmap.
At 0.05 m with inflation the costmap has destroyed ray order and object shape,
so a dock detector fitting lines to find a V plate cannot reconstruct what the
laser saw; two 0.2 m wings on that grid are one blob. A reinforcement-learning
local planner wants the raw scan for the same reason. Neither had a route to
it: ActionContext held only TF, and LocalPlanner::initialize takes only a
costmap.

The store keeps readings raw. Anything that wants filtering filters before
publishing here; the store does not know a filter exists. That split is
deliberate - a costmap wants outliers removed, a detector wants the true
shape, and a store deciding for both would be wrong for one of them.

Sharing is by shared_ptr to an immutable reading. Producers build a new one
and swap the pointer rather than writing in place, so a reader copies the
pointer under the mutex in tens of nanoseconds and then walks a thousand
ranges with nobody able to change them underneath. Two consumers reading one
topic share one copy rather than taking one each, which is what makes a
megabyte depth cloud affordable to share. A bare pointer into the store would
instead force every reader to hold the lock for the whole read, putting the
sensor input path behind however long a detector takes. The store keeps its
own mutex for the same reason: NavigationServer's already covers five kinds of
data, and readers running at control-loop rate do not belong in that queue.

Readers order on an arrival counter, not on the stamp. A stamp can go
backwards - host restart, a switch to simulated time, a replayed bag - and a
reader filtering on stamp then treats every later reading as old and freezes
for good, silently. The stamp is still kept, for TF lookups and for age, which
are different jobs. A consumer faster than its sensor gets null on the ticks
with nothing new rather than the same reading handed back to be counted twice.

The interface is type-erased because boost::dll-loaded plugins cross it. One
virtual per message type would mean a new virtual per sensor, reshuffling the
vtable and silently breaking every .so not rebuilt in the same pass - dlsym
plus reinterpret_cast catches none of that at compile time. So identity
travels as a string, payload as shared_ptr<const void>, and five virtuals
cover every message type that exists or will. The typed sugar is a header-only
template on the caller's side. typeid is avoided deliberately: comparing
type_index across a .so depends on visibility and on how the linker merges
typeinfo, and it fails quietly. Adding a message type is one line of a list
that generates both the trait and the YAML short-name table, so a type can
never be castable without being nameable, or the reverse.

A read with the wrong identity is refused and logged once, never cast.
static_pointer_cast to the wrong type reads garbage instead of crashing and
surfaces somewhere unrelated; that check is the only thing standing between
the erasure and it.

22 of the 24 robot_sensor_msgs types are registered. Imu and TimeReference are
not, because their headers do not compile here at all - Imu.h still carries
the unported ROS tail specialising ros::message_operations::Printer, and
TimeReference.h declares its field as ros::Time. Both are faults in
robot_sensor_msgs, and fixing them makes each one line here.

Config is refused at boot rather than at first use, and an undeclared topic is
dropped rather than added on the fly: an undeclared depth cloud would
otherwise cost megabytes a frame and the config would stop describing what the
process holds. A store with nothing declared stays silent - warning once per
topic about a feature nobody switched on is how a working system starts
printing errors after an upgrade.

SensorConsumer is the side channel for a local planner: widening
LocalPlanner::initialize would break every planner in the workspace at once
for the sake of the one that wants a scan, so a planner that wants sensor data
inherits this as well and the runtime finds out with a dynamic_cast.
2026-08-26 11:37:28 +07:00

308 lines
7.8 KiB
CMake

cmake_minimum_required(VERSION 3.0.2)
project(robot_sensor_ports VERSION 0.1.0 LANGUAGES CXX)
# ========================================================
# Build mode detection
# ========================================================
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building robot_sensor_ports with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building robot_sensor_ports with Standalone CMake")
endif()
# ========================================================
# C++ Standard
# ========================================================
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# ========================================================
# Common dependencies
# ========================================================
# Boost chỉ dùng phần header (shared_ptr, make_shared, pointer_cast) — không link component nào.
find_package(Boost REQUIRED)
find_package(Threads REQUIRED)
find_package(yaml-cpp REQUIRED)
# ========================================================
# Standalone configuration
# ========================================================
if(NOT BUILDING_WITH_CATKIN)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_BUILD_RPATH "${CMAKE_BINARY_DIR}")
# Test/robot_sensor_ports -> Test -> AMR_T800. Cây nguồn của nav core nằm ngay dưới đó.
set(PNKX_NAV_CORE_SRC_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/../../.."
)
set(WORKSPACE_DEVEL_LIB_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/../../../../devel/lib"
)
file(GLOB STANDALONE_PACKAGE_INCLUDE_DIRS
LIST_DIRECTORIES true
${PNKX_NAV_CORE_SRC_DIR}/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/*/*/include
)
set(STANDALONE_INCLUDE_DIRS
${STANDALONE_PACKAGE_INCLUDE_DIRS}
/opt/ros/noetic/include
/usr/local/include
)
if(EXISTS ${WORKSPACE_DEVEL_LIB_DIR})
link_directories(${WORKSPACE_DEVEL_LIB_DIR})
endif()
link_directories(
/usr/local/lib
)
# Gói này KHÔNG kéo costmap, tf3 hay boost::dll: nó chỉ cất message và trả con trỏ. Đây là lý do
# mọi tầng khác phụ thuộc được vào nó mà không tạo vòng.
set(STANDALONE_LIBRARIES
robot_cpp
robot_time
robot_xmlrpcpp
)
include_directories(
include
)
include_directories(SYSTEM
${STANDALONE_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
# ========================================================
# Catkin configuration
# ========================================================
else()
find_package(catkin REQUIRED COMPONENTS
robot_cpp
robot_time
robot_sensor_msgs
robot_xmlrpcpp
)
catkin_package(
INCLUDE_DIRS
include
LIBRARIES
robot_sensor_ports
CATKIN_DEPENDS
robot_cpp
robot_time
robot_sensor_msgs
robot_xmlrpcpp
DEPENDS
Boost
)
include_directories(
include
)
# Header của dependency vào dạng SYSTEM: gói này build với -Wall -Wextra, mà robot_cpp sinh hàng
# trăm warning riêng của nó — để lẫn vào thì warning của chính gói này vô hình.
include_directories(SYSTEM
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
endif()
# ========================================================
# Library
# ========================================================
add_library(robot_sensor_ports SHARED
src/sensor_type.cpp
src/sensor_store.cpp
src/sensor_store_config.cpp
)
target_compile_options(robot_sensor_ports PRIVATE -Wall -Wextra)
if(BUILDING_WITH_CATKIN)
add_dependencies(robot_sensor_ports
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)
target_include_directories(robot_sensor_ports
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${Boost_INCLUDE_DIRS}
)
target_link_libraries(robot_sensor_ports
PUBLIC
${catkin_LIBRARIES}
PRIVATE
Boost::boost
yaml-cpp
Threads::Threads
)
else()
target_include_directories(robot_sensor_ports
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# SYSTEM phải đứng ngay sau tên target. Include trong cây nguồn phải bọc BUILD_INTERFACE,
# nếu không install(EXPORT) fail "prefixed in the source directory" khi package nằm in-tree.
target_include_directories(robot_sensor_ports SYSTEM
PUBLIC
"$<BUILD_INTERFACE:${STANDALONE_INCLUDE_DIRS}>"
${Boost_INCLUDE_DIRS}
)
target_link_libraries(robot_sensor_ports
PUBLIC
${STANDALONE_LIBRARIES}
PRIVATE
Boost::boost
yaml-cpp
Threads::Threads
)
# .so ra root build dir như action_core/mission_adapters, để runtime tìm thấy ở cùng một chỗ.
set_target_properties(robot_sensor_ports PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
endif()
# ========================================================
# Install
# ========================================================
if(BUILDING_WITH_CATKIN)
install(TARGETS robot_sensor_ports
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
)
else()
include(GNUInstallDirs)
install(TARGETS robot_sensor_ports
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
FILES_MATCHING PATTERN "*.h"
)
endif()
# ========================================================
# Tests
# ========================================================
if(CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
if(NOT COMMAND catkin_add_gtest)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif()
set(ROBOT_SENSOR_PORTS_TESTS
sensor_type_test
sensor_store_test
sensor_store_config_test
)
foreach(test_name ${ROBOT_SENSOR_PORTS_TESTS})
catkin_add_gtest(${test_name} test/${test_name}.cpp)
if(TARGET ${test_name})
# catkin_add_gtest đặt target EXCLUDE_FROM_ALL; mở ra để binary có mặt sau `catkin_make`
# thường, đúng quy trình verify của repo (chạy thẳng ./devel/lib/<pkg>/<test>).
set_target_properties(${test_name} PROPERTIES EXCLUDE_FROM_ALL FALSE)
target_compile_options(${test_name} PRIVATE -Wall -Wextra)
target_include_directories(${test_name}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(${test_name} SYSTEM
PRIVATE
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
# catkin_add_gtest dùng target_link_libraries dạng plain, nên phần bổ sung cũng phải plain.
target_link_libraries(${test_name}
robot_sensor_ports
${catkin_LIBRARIES}
yaml-cpp
pthread
)
# Đường tới cây config test — ctest không mang theo PNKX_NAV_CORE_CONFIG_DIR của shell.
target_compile_definitions(${test_name} PRIVATE
ROBOT_SENSOR_PORTS_TEST_CONFIG_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/config"
)
endif()
endforeach()
endif()