-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Convert nav2_loopback_sim to C++ #6062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SteveMacenski
merged 27 commits into
ros-navigation:main
from
botsandus:loopback-simulation-cpp
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a2ac1d0
Refactor nav2_loopback_sim package
tonynajjar f6db2be
fix copyright date
tonynajjar 658325d
fixes
tonynajjar 5523df6
more minor fixes
tonynajjar 4cc3635
simplify getlaserscan
tonynajjar 8297e8f
Add Gaussian noise to laser scan measurements
tonynajjar 909e720
lint
tonynajjar 46b9139
Refactor ClockPublisher to use resetTimer for timer management and re…
tonynajjar 7220047
Refactor publishLaserScan and publishOdometry to use unique_ptr for m…
tonynajjar ae49aa9
Refactor ClockPublisher integration into LoopbackSimulator and remove…
tonynajjar 6d7377d
add tests
tonynajjar 34e3518
Update copyright notice to include "Dexory" in all relevant files
tonynajjar 0eb647c
fix readme
tonynajjar 3fa1195
Merge remote-tracking branch 'upstream/main' into loopback-simulation…
tonynajjar 8856eba
fix typo in README for standalone simulator description
tonynajjar 9153c75
Add validation for map response and ensure map is fetched before publ…
tonynajjar 559c186
Fix formatting of package selection and skip regex in CircleCI config
tonynajjar 755aeb0
Pr review
tonynajjar b05b996
PR review
tonynajjar 17ca4df
Add autostart parameter to loopback simulator launch and remove unuse…
tonynajjar b0426c0
PR review
tonynajjar efc8627
remove initial publishing of t_map_to_odom_
tonynajjar fb0fde0
fix CI
tonynajjar ce356da
add nav2_core to algorithm_build
tonynajjar 6d392a9
disable coverage-gcc for cache warm up
tonynajjar 8ac7894
Revert "disable coverage-gcc for cache warm up"
tonynajjar fd3084b
add nav2_loopback_sim as an execution dependency in package.xml
tonynajjar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| cmake_minimum_required(VERSION 3.8) | ||
| project(nav2_loopback_sim) | ||
|
|
||
| find_package(ament_cmake REQUIRED) | ||
| find_package(backward_ros REQUIRED) | ||
| find_package(geometry_msgs REQUIRED) | ||
| find_package(nav2_common REQUIRED) | ||
| find_package(nav2_ros_common REQUIRED) | ||
| find_package(nav2_util REQUIRED) | ||
| find_package(nav_msgs REQUIRED) | ||
| find_package(rcl_interfaces REQUIRED) | ||
| find_package(rclcpp REQUIRED) | ||
| find_package(rclcpp_components REQUIRED) | ||
| find_package(rclcpp_lifecycle REQUIRED) | ||
| find_package(rosgraph_msgs REQUIRED) | ||
| find_package(sensor_msgs REQUIRED) | ||
| find_package(tf2 REQUIRED) | ||
| find_package(tf2_geometry_msgs REQUIRED) | ||
| find_package(tf2_msgs REQUIRED) | ||
| find_package(tf2_ros REQUIRED) | ||
|
|
||
| nav2_package() | ||
|
|
||
| set(executable_name loopback_simulator) | ||
| set(library_name ${executable_name}_core) | ||
|
|
||
| # Main library | ||
| add_library(${library_name} SHARED | ||
| src/loopback_simulator.cpp | ||
| src/clock_publisher.cpp | ||
| ) | ||
| target_include_directories(${library_name} | ||
| PUBLIC | ||
| "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>" | ||
| "$<INSTALL_INTERFACE:include/${PROJECT_NAME}>" | ||
| ) | ||
| target_link_libraries(${library_name} PUBLIC | ||
| ${geometry_msgs_TARGETS} | ||
| ${nav_msgs_TARGETS} | ||
| nav2_ros_common::nav2_ros_common | ||
| nav2_util::nav2_util_core | ||
| ${rcl_interfaces_TARGETS} | ||
| rclcpp::rclcpp | ||
| rclcpp_lifecycle::rclcpp_lifecycle | ||
| ${rosgraph_msgs_TARGETS} | ||
| ${sensor_msgs_TARGETS} | ||
| tf2::tf2 | ||
| tf2_geometry_msgs::tf2_geometry_msgs | ||
| tf2_ros::tf2_ros | ||
| ) | ||
| target_link_libraries(${library_name} PRIVATE | ||
| rclcpp_components::component | ||
| ) | ||
|
|
||
| # Main executable | ||
| add_executable(${executable_name} | ||
| src/main.cpp | ||
| ) | ||
| target_link_libraries(${executable_name} PRIVATE ${library_name} rclcpp::rclcpp) | ||
|
|
||
| rclcpp_components_register_nodes(${library_name} "nav2_loopback_sim::LoopbackSimulator") | ||
|
|
||
| install( | ||
| TARGETS ${library_name} | ||
| ARCHIVE DESTINATION lib | ||
| LIBRARY DESTINATION lib | ||
| RUNTIME DESTINATION bin | ||
| ) | ||
|
|
||
| install(TARGETS ${executable_name} | ||
| RUNTIME DESTINATION lib/${PROJECT_NAME} | ||
| ) | ||
|
|
||
| install(DIRECTORY include/ | ||
| DESTINATION include/${PROJECT_NAME} | ||
| ) | ||
|
|
||
| install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}) | ||
|
|
||
| if(BUILD_TESTING) | ||
| find_package(ament_lint_auto REQUIRED) | ||
| set(ament_cmake_copyright_FOUND TRUE) | ||
| ament_lint_auto_find_test_dependencies() | ||
|
|
||
| find_package(ament_cmake_gtest REQUIRED) | ||
| ament_find_gtest() | ||
|
|
||
| nav2_add_gtest(test_clock_publisher test/test_clock_publisher.cpp) | ||
| target_link_libraries(test_clock_publisher | ||
| ${library_name} | ||
| rclcpp::rclcpp | ||
| rclcpp_lifecycle::rclcpp_lifecycle | ||
| nav2_ros_common::nav2_ros_common | ||
| ${rosgraph_msgs_TARGETS} | ||
| ) | ||
|
|
||
| nav2_add_gtest(test_loopback_simulator test/test_loopback_simulator.cpp) | ||
| target_link_libraries(test_loopback_simulator | ||
| ${library_name} | ||
| rclcpp::rclcpp | ||
| rclcpp_lifecycle::rclcpp_lifecycle | ||
| ${geometry_msgs_TARGETS} | ||
| ${nav_msgs_TARGETS} | ||
| tf2::tf2 | ||
| ${tf2_msgs_TARGETS} | ||
| ) | ||
| endif() | ||
|
|
||
| ament_export_include_directories(include/${PROJECT_NAME}) | ||
| ament_export_libraries(${library_name}) | ||
| ament_export_dependencies( | ||
| geometry_msgs | ||
| nav2_ros_common | ||
| rcl_interfaces | ||
| nav2_util | ||
| nav_msgs | ||
| rclcpp | ||
| rclcpp_lifecycle | ||
| rosgraph_msgs | ||
| sensor_msgs | ||
| tf2 | ||
| tf2_geometry_msgs | ||
| tf2_ros | ||
| ) | ||
| ament_package() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
nav2_loopback_sim/include/nav2_loopback_sim/clock_publisher.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright (c) 2024, Open Navigation LLC | ||
| // Copyright (c) 2026, Dexory (Tony Najjar) | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef NAV2_LOOPBACK_SIM__CLOCK_PUBLISHER_HPP_ | ||
| #define NAV2_LOOPBACK_SIM__CLOCK_PUBLISHER_HPP_ | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
|
|
||
| #include "nav2_ros_common/lifecycle_node.hpp" | ||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "rosgraph_msgs/msg/clock.hpp" | ||
|
|
||
| namespace nav2_loopback_sim | ||
| { | ||
|
|
||
| /** | ||
| * @brief Publishes simulated clock to /clock using wall time. | ||
| * Uses wall timers so that it works correctly even when owned by a | ||
| * node with use_sim_time=true. Supports speed_factor to run sim | ||
| * time faster or slower than real time. | ||
| */ | ||
| class ClockPublisher | ||
tonynajjar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public: | ||
| /** | ||
| * @brief Construct a ClockPublisher. | ||
| * @param node Weak pointer to the owning lifecycle node | ||
| * @param speed_factor Sim-time speed relative to wall time | ||
| */ | ||
| ClockPublisher( | ||
| nav2::LifecycleNode::WeakPtr node, | ||
| double speed_factor = 1.0); | ||
|
|
||
| /** | ||
| * @brief Start publishing /clock | ||
| */ | ||
| void start(); | ||
| /** | ||
| * @brief Stop publishing /clock | ||
| */ | ||
| void stop(); | ||
| /** | ||
| * @brief Update the simulation speed factor | ||
| * @param speed_factor New speed multiplier (must be positive) | ||
| */ | ||
| void setSpeedFactor(double speed_factor); | ||
|
|
||
| protected: | ||
| /** | ||
| * @brief Wall-timer callback that advances sim time and publishes /clock | ||
| */ | ||
| void timerCallback(); | ||
| /** | ||
| * @brief (Re)create the wall timer based on current speed_factor | ||
| */ | ||
| void resetTimer(); | ||
|
|
||
| nav2::LifecycleNode::WeakPtr node_; | ||
| rclcpp::Logger logger_; | ||
|
|
||
| rclcpp::Publisher<rosgraph_msgs::msg::Clock>::SharedPtr clock_pub_; | ||
| rclcpp::TimerBase::SharedPtr timer_; | ||
|
|
||
| static constexpr double kResolution = 0.01; // 10ms sim-time step | ||
| static constexpr double kMinWallPeriod = 0.001; // 1ms / 1000 Hz max | ||
| double speed_factor_; | ||
| rclcpp::Time sim_time_; | ||
| std::chrono::steady_clock::time_point last_wall_time_; | ||
| }; | ||
tonynajjar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } // namespace nav2_loopback_sim | ||
|
|
||
| #endif // NAV2_LOOPBACK_SIM__CLOCK_PUBLISHER_HPP_ | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.