src/main.cpp:
Код: Выделить всё
#include
#include
#include
#include
class MyNode : public rclcpp_lifecycle::LifecycleNode
{
public:
MyNode(rclcpp::NodeOptions &options)
: rclcpp_lifecycle::LifecycleNode("test_node", options.allow_undeclared_parameters(true).automatically_declare_parameters_from_overrides(true))
{
rt_pub1_ = std::make_shared(this->create_publisher("/test_topic1", 10));
rt_pub2_ = std::make_shared(this->create_publisher("/test_topic2", 10));
rt_pub3_ = std::make_shared(this->create_publisher("/test_topic3", 10));
rt_pub4_ = std::make_shared(this->create_publisher("/test_topic4", 10));
timer_ = this->create_wall_timer(
std::chrono::milliseconds(1), [this]()
{
publishMessage(rt_pub1_, "Hello from topic 1!");
publishMessage(rt_pub2_, "Hello from topic 2!");
publishMessage(rt_pub3_, "Hello from topic 3!");
publishMessage(rt_pub4_, "Hello from topic 4!"); });
}
~MyNode() {}
private:
void publishMessage(realtime_tools::RealtimePublisherSharedPtr &pub, const std::string &message)
{
if (pub->trylock())
{
pub->msg_.data = message;
pub->unlockAndPublish();
}
else
{
RCLCPP_WARN(this->get_logger(), "Failed to lock publisher");
}
}
rclcpp::TimerBase::SharedPtr timer_;
realtime_tools::RealtimePublisherSharedPtr rt_pub1_;
realtime_tools::RealtimePublisherSharedPtr rt_pub2_;
realtime_tools::RealtimePublisherSharedPtr rt_pub3_;
realtime_tools::RealtimePublisherSharedPtr rt_pub4_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
rclcpp::NodeOptions options;
auto node = std::make_shared(options);
rclcpp::spin(node->get_node_base_interface());
rclcpp::shutdown();
return 0;
}
< /code>
package.xml:
rt_test
0.0.0
A simple ROS2 lifecycle node using realtime_tools.
Your Name
Apache License 2.0
ament_cmake
rclcpp
rclcpp_lifecycle
realtime_tools
std_msgs
ament_lint_auto
ament_lint_common
ament_cmake
< /code>
cmakelists.txt:
cmake_minimum_required(VERSION 3.8)
project(rt_test)
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(realtime_tools REQUIRED)
find_package(std_msgs REQUIRED)
include_directories(
include
${rclcpp_INCLUDE_DIRS}
${rclcpp_lifecycle_INCLUDE_DIRS}
${realtime_tools_INCLUDE_DIRS}
${std_msgs_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME} src/main.cpp)
ament_target_dependencies(${PROJECT_NAME}
rclcpp
rclcpp_lifecycle
realtime_tools
std_msgs
)
install(TARGETS ${PROJECT_NAME}
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
< /code>
Структура каталога: < /p>
❯ tree realtime_pub_test/
realtime_pub_test/
├── CMakeLists.txt
├── package.xml
└── src
└── main.cpp
1 directory, 3 files
< /code>
Запуск результата: < /p>
exr@li9htning:~/zxm/ros_ws$ ros2 run rt_test rt_test
[WARN] [1750988390.588930275] [test_node]: Failed to lock publisher
[WARN] [1750988390.859900980] [test_node]: Failed to lock publisher
[WARN] [1750988391.261064491] [test_node]: Failed to lock publisher
[WARN] [1750988391.429071660] [test_node]: Failed to lock publisher
< /code>
Другая информация: < /p>
exr@li9htning:~$ ros2 topic hz /test_topic1
average rate: 999.633
min: 0.000s max: 0.002s std dev: 0.00030s window: 1002
average rate: 998.661
min: 0.000s max: 0.002s std dev: 0.00031s window: 2001
average rate: 998.817
min: 0.000s max: 0.002s std dev: 0.00032s window: 3001
exr@li9htning:~$ ros2 topic hz /test_topic2
average rate: 999.575
min: 0.000s max: 0.002s std dev: 0.00030s window: 1001
average rate: 999.179
min: 0.000s max: 0.002s std dev: 0.00030s window: 2000
average rate: 999.018
min: 0.000s max: 0.002s std dev: 0.00030s window: 2999
< /code>
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 20
On-line CPU(s) list: 0-19
Vendor ID: GenuineIntel
Model name: 12th Gen Intel(R) Core(TM) i9-12900HK
CPU family: 6
Model: 154
Thread(s) per core: 2
Core(s) per socket: 14
Socket(s): 1
Stepping: 3
CPU max MHz: 5000.0000
CPU min MHz: 400.0000
BogoMIPS: 3763.20
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni p
clmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept
vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi v
nmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization features:
Virtualization: VT-x
Caches (sum of all):
L1d: 544 KiB (14 instances)
L1i: 704 KiB (14 instances)
L2: 11.5 MiB (8 instances)
L3: 24 MiB (1 instance)
NUMA:
NUMA node(s): 1
NUMA node0 CPU(s): 0-19
Vulnerabilities:
Gather data sampling: Not affected
Itlb multihit: Not affected
L1tf: Not affected
Mds: Not affected
Meltdown: Not affected
Mmio stale data: Not affected
Retbleed: Not affected
Spec rstack overflow: Not affected
Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Spectre v2: Mitigation; Enhanced / Automatic IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Srbds: Not affected
Tsx async abort: Not affected
< /code>
The thread runs at a frequency of approximately 1000HZ, and the program frequently prints "Publish failed", which means that a large number of trylock()Код: Выделить всё
trylock()Подробнее здесь: https://stackoverflow.com/questions/796 ... e-threaded
Мобильная версия