-
Notifications
You must be signed in to change notification settings - Fork 148
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
Bridge as a gz sim system #501
Changes from 17 commits
5d3d406
2882c87
4921a07
9ddd652
758aa21
f1f5fa0
be153b6
ac76877
3bc36bd
6f05a12
ebb6cab
bdcc825
42aa826
8d767e9
49c8811
e2a5db0
a195207
fe132a1
8c5b845
0ff8633
161f8ee
ddf084c
62b212c
15cbccb
93a0836
f232b4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ endif() | |
|
||
find_package(ament_cmake REQUIRED) | ||
find_package(rclcpp REQUIRED) | ||
find_package(rclcpp_components REQUIRED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need to include this dependency, with rclcpp should be enough, you are not adding any component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I don't add it, the code doesn't compile. Maybe a dependency isn't exporting it correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we determined that ros_gz_bridge isn't correctly exporting the dependency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed! |
||
find_package(ros_gz_bridge REQUIRED) | ||
find_package(std_msgs REQUIRED) | ||
|
||
# TODO(CH3): Deprecated. Remove on tock. | ||
|
@@ -20,6 +22,8 @@ if("$ENV{GZ_VERSION}" STREQUAL "" AND NOT "$ENV{IGNITION_VERSION}" STREQUAL "") | |
set(ENV{GZ_VERSION} $ENV{IGNITION_VERSION}) | ||
endif() | ||
|
||
set(GZ_TARGET_SIM gz-sim) | ||
|
||
# Edifice | ||
if("$ENV{GZ_VERSION}" STREQUAL "edifice") | ||
find_package(ignition-math6 REQUIRED) | ||
|
@@ -35,6 +39,7 @@ if("$ENV{GZ_VERSION}" STREQUAL "edifice") | |
set(GZ_SIM_VER ${ignition-gazebo5_VERSION_MAJOR}) | ||
|
||
set(GZ_TARGET_PREFIX ignition) | ||
set(GZ_TARGET_SIM ignition-gazebo) | ||
macro(gz_find_package) | ||
ign_find_package(${ARGV}) | ||
endmacro() | ||
|
@@ -88,6 +93,7 @@ else() | |
set(GZ_SIM_VER ${ignition-gazebo6_VERSION_MAJOR}) | ||
|
||
set(GZ_TARGET_PREFIX ignition) | ||
set(GZ_TARGET_SIM ignition-gazebo) | ||
|
||
message(STATUS "Compiling against Gazebo Fortress") | ||
macro(gz_find_package) | ||
|
@@ -127,6 +133,21 @@ install(DIRECTORY include/${PROJECT_NAME}/ | |
DESTINATION include/${PROJECT_NAME} | ||
) | ||
|
||
# ROS Gz bridge | ||
add_library(ROSGzPlugin SHARED src/ros_gz.cpp) | ||
target_link_libraries(ROSGzPlugin PUBLIC | ||
${GZ_TARGET_SIM}${GZ_SIM_VER}::core | ||
rclcpp::rclcpp | ||
ros_gz_bridge::ros_gz_bridge_lib | ||
) | ||
target_include_directories(ROSGzPlugin PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/ros_gz_sim> | ||
$<INSTALL_INTERFACE:include/ros_gz_sim> | ||
) | ||
install( | ||
TARGETS ROSGzPlugin | ||
DESTINATION lib) | ||
|
||
configure_file( | ||
launch/gz_sim.launch.py.in | ||
launch/gz_sim.launch.py.configured | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||||||||||||||||
// Copyright 2024 Open Source Robotics Foundation | ||||||||||||||||||||
// | ||||||||||||||||||||
// 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 ROS_GZ_SIM__ROS_GZ_HPP_ | ||||||||||||||||||||
#define ROS_GZ_SIM__ROS_GZ_HPP_ | ||||||||||||||||||||
|
||||||||||||||||||||
#include <memory> | ||||||||||||||||||||
#include <gz/sim/System.hh> | ||||||||||||||||||||
#include <sdf/sdf.hh> | ||||||||||||||||||||
|
||||||||||||||||||||
namespace ros_gz_sim | ||||||||||||||||||||
{ | ||||||||||||||||||||
// Private class. | ||||||||||||||||||||
class ROSGzPluginPrivate; | ||||||||||||||||||||
|
||||||||||||||||||||
/// \brief ToDo | ||||||||||||||||||||
class ROSGzPlugin | ||||||||||||||||||||
: public gz::sim::System, | ||||||||||||||||||||
public gz::sim::ISystemConfigure | ||||||||||||||||||||
{ | ||||||||||||||||||||
public: | ||||||||||||||||||||
// \brief Constructor. | ||||||||||||||||||||
ROSGzPlugin(); | ||||||||||||||||||||
|
||||||||||||||||||||
/// \brief Destructor. | ||||||||||||||||||||
~ROSGzPlugin() override; | ||||||||||||||||||||
|
||||||||||||||||||||
// Documentation inherited. | ||||||||||||||||||||
void Configure( | ||||||||||||||||||||
const gz::sim::Entity & _entity, | ||||||||||||||||||||
const std::shared_ptr<const sdf::Element> & _sdf, | ||||||||||||||||||||
gz::sim::EntityComponentManager & _ecm, | ||||||||||||||||||||
gz::sim::EventManager & _eventMgr) override; | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
/// \brief Private data pointer. | ||||||||||||||||||||
std::unique_ptr<ROSGzPluginPrivate> dataPtr; | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||
}; | ||||||||||||||||||||
} // namespace ros_gz_sim | ||||||||||||||||||||
#endif // ROS_GZ_SIM__ROS_GZ_HPP_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2024 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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. | ||
|
||
#include <filesystem> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include <gz/plugin/Register.hh> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <ros_gz_bridge/ros_gz_bridge.hpp> | ||
#include <ros_gz_bridge/bridge_config.hpp> | ||
|
||
#include "ros_gz.hpp" | ||
|
||
namespace ros_gz_sim | ||
{ | ||
/// \brief Private ROSGzPlugin data class. | ||
class ROSGzPluginPrivate | ||
{ | ||
public: | ||
/// \brief The ROS 2 <--> Gz bridge. | ||
std::shared_ptr<ros_gz_bridge::RosGzBridge> bridge; | ||
|
||
/// \brief The ROS 2 executor. | ||
std::shared_ptr<rclcpp::executors::MultiThreadedExecutor> exec; | ||
|
||
/// \brief A thread to call spin and not block the Gazebo thread. | ||
std::thread thread; | ||
ahcorde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
////////////////////////////////////////////////// | ||
ROSGzPlugin::ROSGzPlugin() | ||
: System(), dataPtr(new ROSGzPluginPrivate()) | ||
{ | ||
} | ||
|
||
////////////////////////////////////////////////// | ||
ROSGzPlugin::~ROSGzPlugin() | ||
{ | ||
this->dataPtr->exec->cancel(); | ||
this->dataPtr->thread.join(); | ||
} | ||
|
||
////////////////////////////////////////////////// | ||
void ROSGzPlugin::Configure( | ||
const gz::sim::Entity & /*_entity*/, | ||
const std::shared_ptr<const sdf::Element> & _sdf, | ||
gz::sim::EntityComponentManager & /*_ecm*/, | ||
gz::sim::EventManager & /*_eventMgr*/) | ||
{ | ||
// Ensure that ROS is setup. | ||
if (!rclcpp::ok()) { | ||
rclcpp::init(0, nullptr); | ||
} | ||
|
||
if (!_sdf->HasElement("config_file")) { | ||
std::cerr << "No <config_file> found. Plugin disabled." << std::endl; | ||
return; | ||
} | ||
|
||
// Sanity check: Make sure that the config file exists and it's a file. | ||
std::filesystem::path configFile = _sdf->Get<std::string>("config_file"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was already there :) |
||
if (!std::filesystem::is_regular_file(configFile)) { | ||
std::cerr << "[" << configFile << "] is not a regular file. Plugin disabled" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<< std::endl; | ||
return; | ||
} | ||
|
||
// Create the bridge passing the parameters as rclcpp::NodeOptions(). | ||
this->dataPtr->bridge = std::make_shared<ros_gz_bridge::RosGzBridge>( | ||
rclcpp::NodeOptions().append_parameter_override("config_file", configFile)); | ||
|
||
// Create the executor. | ||
this->dataPtr->exec = | ||
std::make_shared<rclcpp::executors::MultiThreadedExecutor>(); | ||
this->dataPtr->exec->add_node(this->dataPtr->bridge); | ||
|
||
// Spin in a separate thread to not block Gazebo. | ||
this->dataPtr->thread = std::thread([this]() {this->dataPtr->exec->spin();}); | ||
} | ||
} // namespace ros_gz_sim | ||
|
||
#if (IGNITION_GAZEBO_MAJOR_VERSION == 6) | ||
IGNITION_ADD_PLUGIN( | ||
ros_gz_sim::ROSGzPlugin, | ||
gz::sim::System, | ||
ros_gz_sim::ROSGzPlugin::ISystemConfigure) | ||
#else | ||
GZ_ADD_PLUGIN( | ||
ros_gz_sim::ROSGzPlugin, | ||
gz::sim::System, | ||
ros_gz_sim::ROSGzPlugin::ISystemConfigure) | ||
#endif | ||
|
||
#if (IGNITION_GAZEBO_MAJOR_VERSION == 6) | ||
IGNITION_ADD_PLUGIN_ALIAS( | ||
ros_gz_sim::ROSGzPlugin, | ||
"ros_gz_sim::ROSGzPlugin") | ||
#else | ||
GZ_ADD_PLUGIN_ALIAS( | ||
ros_gz_sim::ROSGzPlugin, | ||
"ros_gz_sim::ROSGzPlugin") | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Full set of configurations | ||
- ros_topic_name: "ros_chatter" | ||
gz_topic_name: "gz_chatter" | ||
ros_type_name: "std_msgs/msg/String" | ||
gz_type_name: "ignition.msgs.StringMsg" | ||
subscriber_queue: 5 | ||
publisher_queue: 6 | ||
lazy: true | ||
direction: ROS_TO_GZ | ||
|
||
- ros_topic_name: "ros_chatter" | ||
gz_topic_name: "gz_chatter" | ||
ros_type_name: "std_msgs/msg/String" | ||
gz_type_name: "ignition.msgs.StringMsg" | ||
Comment on lines
+5
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in #511. |
||
subscriber_queue: 10 | ||
publisher_queue: 20 | ||
lazy: false | ||
direction: GZ_TO_ROS |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2024 Open Source Robotics Foundation, Inc. | ||
# | ||
# 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. | ||
|
||
import os | ||
|
||
from ament_index_python.packages import get_package_share_directory | ||
from launch import LaunchDescription | ||
from launch.actions import IncludeLaunchDescription | ||
from launch.launch_description_sources import PythonLaunchDescriptionSource | ||
from launch.substitutions import PathJoinSubstitution | ||
|
||
|
||
def generate_launch_description(): | ||
|
||
pkg_ros_gz_sim_demos = get_package_share_directory('ros_gz_sim_demos') | ||
pkg_ros_gz_sim = get_package_share_directory('ros_gz_sim') | ||
|
||
gazebo = IncludeLaunchDescription( | ||
PythonLaunchDescriptionSource( | ||
os.path.join(pkg_ros_gz_sim, 'launch', 'gz_sim.launch.py'), | ||
), | ||
launch_arguments={'gz_args': PathJoinSubstitution([ | ||
pkg_ros_gz_sim_demos, | ||
'worlds', | ||
'ros_gz.sdf' | ||
])}.items(), | ||
) | ||
|
||
return LaunchDescription([ | ||
gazebo, | ||
]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR should fix this issue #501
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed!