Skip to content
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

Closed
wants to merge 26 commits into from
Closed
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
21 changes: 21 additions & 0 deletions ros_gz_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(ros_gz_bridge REQUIRED)
find_package(std_msgs REQUIRED)

# TODO(CH3): Deprecated. Remove on tock.
Expand All @@ -20,6 +21,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)
Expand All @@ -35,6 +38,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()
Expand Down Expand Up @@ -88,6 +92,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)
Expand Down Expand Up @@ -127,6 +132,22 @@ 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
)
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
Expand Down
50 changes: 50 additions & 0 deletions ros_gz_sim/include/ros_gz_sim/ros_gz.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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;
};
} // namespace ros_gz_sim
#endif // ROS_GZ_SIM__ROS_GZ_HPP_
1 change: 1 addition & 0 deletions ros_gz_sim/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<depend>ament_index_python</depend>
<depend>libgflags-dev</depend>
<depend>rclcpp</depend>
<depend>ros_gz_bridge</depend>
<depend>std_msgs</depend>

<!-- Harmonic -->
Expand Down
119 changes: 119 additions & 0 deletions ros_gz_sim/src/ros_gz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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 <iostream>
#include <memory>
#include <string>
#include <thread>

#include <gz/common/Util.hh>
#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()
{
if (this->dataPtr->exec) {
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::string filename = _sdf->Get<std::string>("config_file");
std::string path = gz::common::findFile(filename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use package:// URIs for the file name since this is all happening in the ROS ecosystem. Then, instead of calling gz::common::findFile, we can pass the file directly to RosGzBridge (so no need to call findFile here) and update RosGzBridge so it can use resource_retriever to find the file.

Copy link
Contributor Author

@caguero caguero Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource_retriever is functional but the API is slightly different because as far as I know it can't resolve the path only. Instead, it resolves the path and read the content of the file, returning it as a byte array.

However I figured that we still can use the package:// approach. I guess findFile() is solving it for us?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the API is different. That's why I was suggesting to pass the filename directly to RosGzBridge and use resource_retriever there where we are currently opening the file:

std::ifstream in(filename);

I don't know how common::findFile is handling package://. My guess is it ignores the package:// part and tries to find the path based on Gazebo environment variables, which is not what we want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case I can tackle it in a separate PR as it affects mainly ros_gz_bridge. I already played with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!gz::common::isFile(path)) {
std::cerr << "Unable to open YAML file [" << filename
<< "], check your GAZEBO_RESOURCE_PATH settings." << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use GAZEBO_RESOURCE_PATH? I thought that was for Gazebo-classic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was a typo. Changed in #511.

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", path));

// 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
6 changes: 6 additions & 0 deletions ros_gz_sim_demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ install(
DESTINATION share/${PROJECT_NAME}/worlds
)

install(
DIRECTORY
config/
DESTINATION share/${PROJECT_NAME}/config
)

ament_environment_hooks("${CMAKE_CURRENT_SOURCE_DIR}/hooks/${PROJECT_NAME}.dsv.in")

ament_package()
18 changes: 18 additions & 0 deletions ros_gz_sim_demos/config/full.yaml
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can use gz.msgs for the gz_type_name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
42 changes: 42 additions & 0 deletions ros_gz_sim_demos/launch/ros_gz.launch.py
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,
])
77 changes: 77 additions & 0 deletions ros_gz_sim_demos/worlds/ros_gz.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" ?>
<!--
ROS 2 <-> Gazebo Sim plugin demo.

-->
<sdf version="1.8">
<world name="demo">

<physics name="1ms" type="ignored">
<max_step_size>0.001</max_step_size>
<real_time_factor>1.0</real_time_factor>
</physics>
<plugin
filename="ignition-gazebo-physics-system"
name="gz::sim::systems::Physics">
</plugin>
<plugin
filename="ignition-gazebo-user-commands-system"
name="gz::sim::systems::UserCommands">
</plugin>
<plugin
filename="ignition-gazebo-scene-broadcaster-system"
name="gz::sim::systems::SceneBroadcaster">
</plugin>
<plugin
filename="ignition-gazebo-contact-system"
name="gz::sim::systems::Contact">
Comment on lines +14 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use gz-sim instead of ignition-gazebo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in #511

</plugin>

<light name="sun" type="directional">
<cast_shadows>true</cast_shadows>
<pose>0 0 10 0 0 0</pose>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
<attenuation>
<range>1000</range>
<constant>0.9</constant>
<linear>0.01</linear>
<quadratic>0.001</quadratic>
</attenuation>
<direction>-0.5 0.1 -0.9</direction>
</light>

<model name="ground_plane">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
</collision>
<visual name="visual">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.8 0.8 0.8 1</specular>
</material>
</visual>
</link>
</model>

<plugin name="ros_gz_sim::ROSGzPlugin"
filename="libROSGzPlugin.so">
Comment on lines +71 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to be name=ros_gz_sim::ROSGzBridge and filename=ros-gz-bridge-system to have bridge in the name and to be more consistent with our other systems (eg. we haven't used Plugin in any of our existing systems). For the filename, the shared library would need to be libros-gz-bridge-system.so. In the <plugin> tag the lib and .so can be dropped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in #511.

<config_file>ros_gz_sim_demos/config/full.yaml</config_file>
</plugin>

</world>
</sdf>
Loading