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

ROS2 plugin example #2

Open
wants to merge 3 commits into
base: ros2-devel-node
Choose a base branch
from
Open
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
430 changes: 0 additions & 430 deletions .ros1_unported/gazebo_plugins/CMakeLists.txt

This file was deleted.

55 changes: 0 additions & 55 deletions .ros1_unported/gazebo_plugins/package.xml

This file was deleted.

31 changes: 31 additions & 0 deletions gazebo_plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.5)
project(gazebo_plugins)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# we dont use add_compile_options with pedantic in message packages
# because the Python C extensions dont comply with it
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
endif()

find_package(ament_cmake REQUIRED)
find_package(gazebo_ros REQUIRED)
find_package(std_msgs REQUIRED)

include_directories(include)

add_library(gazebo_ros_node_example SHARED src/node_example.cpp)
ament_target_dependencies(gazebo_ros_node_example gazebo_ros std_msgs)

ament_package()

install(DIRECTORY include/
DESTINATION include)

install(TARGETS gazebo_ros_node_example
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
40 changes: 40 additions & 0 deletions gazebo_plugins/include/gazebo_plugins/node_example.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 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.

#ifndef GAZEBO_PLUGINS_NODE_EXAMPLE_HPP
#define GAZEBO_PLUGINS_NODE_EXAMPLE_HPP

#include <gazebo/common/common.hh>
#include <std_msgs/msg/string.hpp>
#include <gazebo_ros/node.hpp>


/// Simple example of a gazebo plugin which uses a ROS2 node with gazebo_ros::Node
class GazeboRosNodeExample : public gazebo::SystemPlugin
{
public:
GazeboRosNodeExample();
virtual ~GazeboRosNodeExample();
/// Called by gazebo to load plugin. Creates #node_, #timer_, and #pub_
void Load(int argc, char** argv);
private:
/// ROS node used for publisher and timer
gazebo_ros::Node::SharedPtr node_;
/// Timer called to publish a message every second
std::shared_ptr<rclcpp::TimerBase> timer_;
/// Example publisher
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::String> > pub_;
};

#endif
31 changes: 31 additions & 0 deletions gazebo_plugins/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>gazebo_plugins</name>
<version>2.8.4</version>
<description>
Robot-independent Gazebo plugins for sensors, motors and dynamic reconfigurable components.
</description>

<maintainer email="jrivero@osrfoundation.org">Jose Luis Rivero</maintainer>

<license>BSD, Apache 2.0</license>

<url type="website">http://gazebosim.org/tutorials?cat=connect_ros</url>
<url type="bugtracker">https://github.com/ros-simulation/gazebo_ros_pkgs/issues</url>
<url type="repository">https://github.com/ros-simulation/gazebo_ros_pkgs</url>

<author>John Hsu</author>

<buildtool_depend>ament_cmake</buildtool_depend>

<build_depend>gazebo_ros</build_depend>
<build_depend>std_msgs</build_depend>

<exec_depend>gazebo_ros</exec_depend>
<exec_depend>std_msgs</exec_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
51 changes: 51 additions & 0 deletions gazebo_plugins/src/node_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018 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 <gazebo_plugins/node_example.hpp>

GazeboRosNodeExample::GazeboRosNodeExample()
{
}

GazeboRosNodeExample::~GazeboRosNodeExample()
{
}

void GazeboRosNodeExample::Load(int argc, char** argv)
{
// Initialize ROS with arguments
gazebo_ros::Node::InitROS(argc, argv);

// Create the ROS node
node_ = gazebo_ros::Node::Create("gazebo_ros_node_example");

// Create a publisher
pub_ = node_->create_publisher<std_msgs::msg::String>("test");

// Run lambda every 1 second
using namespace std::chrono_literals;
timer_ = node_->create_wall_timer(1s, [this] () {
// Create string message
auto msg = std_msgs::msg::String();
msg.data = "Hello world";

// Warn with this node's name (to test logging)
RCLCPP_WARN(node_->get_logger(), "Publishing");

// Publish message
this->pub_->publish(msg);
});
}

GZ_REGISTER_SYSTEM_PLUGIN(GazeboRosNodeExample)
145 changes: 25 additions & 120 deletions gazebo_ros/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,132 +1,37 @@
cmake_minimum_required(VERSION 3.6.3)
cmake_minimum_required(VERSION 3.5)
project(gazebo_ros)

find_package(catkin REQUIRED COMPONENTS
gazebo_dev
cmake_modules
roslib
roscpp
geometry_msgs
std_srvs
tf
rosgraph_msgs
dynamic_reconfigure
std_msgs
gazebo_msgs
)

# Through transitive dependencies in the packages above, gazebo_ros depends
# on Simbody. There is a bug in the Ubuntu Artful (17.10) version of the
# Simbody package where it includes /usr/lib/libblas.so and
# /usr/lib/liblapack.so in the CMake list of libraries even though neither of
# those two paths exist (they both really live in /usr/lib/<arch>-linux-gnu).
# We remove these two during build-time on artful below; this works because
# they both will get resolved to the proper paths during runtime linking.
find_program(LSB_RELEASE_EXEC lsb_release)
if(NOT LSB_RELEASE_EXEC STREQUAL "LSB_RELEASE_EXEC-NOTFOUND")
execute_process(COMMAND ${LSB_RELEASE_EXEC} -cs
OUTPUT_VARIABLE OS_CODENAME
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(OS_CODENAME STREQUAL "artful")
list(FILTER catkin_LIBRARIES EXCLUDE REGEX "/usr/lib/libblas.so")
list(FILTER catkin_LIBRARIES EXCLUDE REGEX "/usr/lib/liblapack.so")
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

include (FindPkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(XML libxml-2.0)
else()
message(FATAL_ERROR "pkg-config is required; please install it")
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# we dont use add_compile_options with pedantic in message packages
# because the Python C extensions dont comply with it
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
endif()

find_package(Boost REQUIRED COMPONENTS thread)

find_package(TinyXML REQUIRED)

catkin_python_setup()

generate_dynamic_reconfigure_options(cfg/Physics.cfg)

catkin_package(
LIBRARIES
gazebo_ros_api_plugin
gazebo_ros_paths_plugin

CATKIN_DEPENDS
roslib
roscpp
geometry_msgs
std_srvs
tf
rosgraph_msgs
dynamic_reconfigure
std_msgs
gazebo_msgs

DEPENDS
TinyXML
)

include_directories(
include
${Boost_INCLUDE_DIRS}
${catkin_INCLUDE_DIRS}
${TinyXML_INCLUDE_DIRS})

link_directories(${catkin_LIBRARY_DIRS})

set(cxx_flags)
foreach (item ${GAZEBO_CFLAGS})
set(cxx_flags "${cxx_flags} ${item}")
endforeach ()

set(ld_flags)
foreach (item ${GAZEBO_LDFLAGS})
set(ld_flags "${ld_flags} ${item}")
endforeach ()

## Plugins
add_library(gazebo_ros_api_plugin src/gazebo_ros_api_plugin.cpp)
add_dependencies(gazebo_ros_api_plugin ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
set_target_properties(gazebo_ros_api_plugin PROPERTIES LINK_FLAGS "${ld_flags}")
set_target_properties(gazebo_ros_api_plugin PROPERTIES COMPILE_FLAGS "${cxx_flags}")
target_link_libraries(gazebo_ros_api_plugin ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${TinyXML_LIBRARIES})

add_library(gazebo_ros_paths_plugin src/gazebo_ros_paths_plugin.cpp)
add_dependencies(gazebo_ros_paths_plugin ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
set_target_properties(gazebo_ros_paths_plugin PROPERTIES COMPILE_FLAGS "${cxx_flags}")
set_target_properties(gazebo_ros_paths_plugin PROPERTIES LINK_FLAGS "${ld_flags}")
target_link_libraries(gazebo_ros_paths_plugin ${catkin_LIBRARIES} ${Boost_LIBRARIES})
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(gazebo_dev REQUIRED)

## Tests
include_directories(include)

add_subdirectory(test)
add_library(gazebo_ros_node SHARED src/node.cpp src/executor.cpp)
ament_target_dependencies(gazebo_ros_node rclcpp gazebo_dev)

# Install Gazebo System Plugins
install(TARGETS gazebo_ros_api_plugin gazebo_ros_paths_plugin
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)
ament_export_include_directories(include)
ament_export_libraries(gazebo_ros_node)
ament_export_dependencies(rclcpp)
ament_export_dependencies(gazebo_dev)

# Install Gazebo Scripts
install(PROGRAMS scripts/gazebo
scripts/debug
scripts/gzclient
scripts/gzserver
scripts/gdbrun
scripts/perf
scripts/libcommon.sh
DESTINATION
${CATKIN_PACKAGE_BIN_DESTINATION}
)
ament_package()

# This one is a Python program, not a shell script, so install it separately
catkin_install_python(PROGRAMS scripts/spawn_model
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
install(DIRECTORY include/
DESTINATION include)

# Install Gazebo launch files
install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
)
install(TARGETS gazebo_ros_node
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
Loading