diff --git a/ros_gz_example_gazebo/CMakeLists.txt b/ros_gz_example_gazebo/CMakeLists.txt index 0b568d9..5601332 100644 --- a/ros_gz_example_gazebo/CMakeLists.txt +++ b/ros_gz_example_gazebo/CMakeLists.txt @@ -14,9 +14,30 @@ find_package(gz-plugin2 REQUIRED COMPONENTS register) set(GZ_PLUGIN_VER ${gz-plugin2_VERSION_MAJOR}) find_package(gz-common5 REQUIRED COMPONENTS profiler) set(GZ_COMMON_VER ${gz-common5_VERSION_MAJOR}) -find_package(gz-sim7 REQUIRED) -set(GZ_SIM_VER ${gz-sim7_VERSION_MAJOR}) +find_package(gz-sim8 REQUIRED) +set(GZ_SIM_VER ${gz-sim8_VERSION_MAJOR}) + +find_package(gz-msgs10 REQUIRED) +set(GZ_MSGS_VER ${gz-msgs10_VERSION_MAJOR}) + +# Example of custom messages that depend on gz.msgs +set(MSGS_PROTOS + ${CMAKE_CURRENT_SOURCE_DIR}/proto/ros_gz_example_gazebo/msgs/foobar.proto +) + +gz_msgs_generate_messages( + # The cmake target to be generated for libraries/executables to link + TARGET msgs + # The protobuf package to generate (Typically based on the path) + PROTO_PACKAGE "ros_gz_example_gazebo.msgs" + # The path to the base directory of the proto files + # All import paths should be relative to this (eg gz/custom_msgs/vector3d.proto) + MSGS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/proto + # List of proto files to generate + MSGS_PROTOS ${MSGS_PROTOS} + DEPENDENCIES gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER} +) # Following 'add_library' directive defines a library target named 'BasicSystem'. # The 'SHARED' keyword indicates that a shared library should be compiled, and @@ -34,11 +55,11 @@ target_include_directories( BasicSystem PRIVATE include ) -# Following 'target_link_libraries' directive specifies to use the +# Following 'target_link_libraries' directive specifies to use the # gz-sim library when linking the 'BasicSystem' target. # The 'PRIVATE' keyword stipulates that the gz-sim library will not # automatically be included if the 'BasicSystem' target were to -# be linked to anoter target. +# be linked to anoter target. # ${GZ_SIM_VER} is substituted by the value that is was set to above. target_link_libraries(BasicSystem PRIVATE gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}) @@ -55,7 +76,10 @@ target_include_directories( ) target_link_libraries(FullSystem PRIVATE - gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}) + gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER} + ros_gz_example_gazebo-msgs +) + diff --git a/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.dsv.in b/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.dsv.in index 20259a8..cd6e400 100644 --- a/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.dsv.in +++ b/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.dsv.in @@ -1,2 +1,3 @@ prepend-non-duplicate;GZ_SIM_RESOURCE_PATH;share/@PROJECT_NAME@/worlds prepend-non-duplicate;GZ_SIM_SYSTEM_PLUGIN_PATH;lib/@PROJECT_NAME@/ +prepend-non-duplicate;GZ_DESCRIPTOR_PATH;share/gz/protos/ diff --git a/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.sh.in b/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.sh.in index 28e9c54..4e16880 100644 --- a/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.sh.in +++ b/ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.sh.in @@ -1,2 +1,3 @@ ament_prepend_unique_value GZ_SIM_RESOURCE_PATH "$AMENT_CURRENT_PREFIX/share/@PROJECT_NAME@/worlds" ament_prepend_unique_value GZ_SIM_PLUGIn_PATH "$AMENT_CURRENT_PREFIX/lib/@PROJECT_NAME@" +ament_prepend_unique_value GZ_DESCRIPTOR_PATH "$AMENT_CURRENT_PREFIX/share/gz/protos/" diff --git a/ros_gz_example_gazebo/include/ros_gz_example_gazebo/FullSystem.hh b/ros_gz_example_gazebo/include/ros_gz_example_gazebo/FullSystem.hh index 0612899..624eda7 100644 --- a/ros_gz_example_gazebo/include/ros_gz_example_gazebo/FullSystem.hh +++ b/ros_gz_example_gazebo/include/ros_gz_example_gazebo/FullSystem.hh @@ -23,6 +23,8 @@ #include #include +#include + namespace ros_gz_example_gazebo { // This is the main plugin's class. It must inherit from System and at least @@ -38,8 +40,11 @@ namespace ros_gz_example_gazebo public gz::sim::ISystemPostUpdate, public gz::sim::ISystemReset { - // Plugins inheriting ISystemConfigure must implement the Configure - // callback. This is called when a system is initially loaded. + // Construtor + public: FullSystem(); + + // Plugins inheriting ISystemConfigure must implement the Configure + // callback. This is called when a system is initially loaded. // The _entity variable contains the entity that the system is attached to // The _element variable contains the sdf Element with custom configuration // The _ecm provides an interface to all entities and components @@ -74,10 +79,12 @@ namespace ros_gz_example_gazebo public: void PostUpdate(const gz::sim::UpdateInfo &_info, const gz::sim::EntityComponentManager &_ecm) override; - // Plugins inheriting ISystemReset must implement the Reset callback. + // Plugins inheriting ISystemReset must implement the Reset callback. // This is called when simulation is reset/rewound to initial conditions. public: void Reset(const gz::sim::UpdateInfo &_info, gz::sim::EntityComponentManager &_ecm) override; + + GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr) }; } #endif diff --git a/ros_gz_example_gazebo/proto/ros_gz_example_gazebo/msgs/foobar.proto b/ros_gz_example_gazebo/proto/ros_gz_example_gazebo/msgs/foobar.proto new file mode 100644 index 0000000..dc17067 --- /dev/null +++ b/ros_gz_example_gazebo/proto/ros_gz_example_gazebo/msgs/foobar.proto @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 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. + * +*/ + +syntax = "proto3"; + +package ros_gz_example_gazebo.msgs; + +import "gz/msgs/header.proto"; + +message Foobar +{ + double value = 1; +} + +message FoobarStamped +{ + gz.msgs.Header header = 1; + Foobar foobar = 2; +} diff --git a/ros_gz_example_gazebo/src/FullSystem.cc b/ros_gz_example_gazebo/src/FullSystem.cc index 56afde6..f375465 100644 --- a/ros_gz_example_gazebo/src/FullSystem.cc +++ b/ros_gz_example_gazebo/src/FullSystem.cc @@ -24,6 +24,10 @@ // in the cc file, like it's done here. #include +#include + +#include + // Don't forget to include the plugin's header. #include "ros_gz_example_gazebo/FullSystem.hh" @@ -39,9 +43,23 @@ GZ_ADD_PLUGIN( ros_gz_example_gazebo::FullSystem::ISystemReset ) -namespace ros_gz_example_gazebo +namespace ros_gz_example_gazebo { +class FullSystem::Implementation +{ + public: gz::transport::Node node; + + public: gz::transport::Node::Publisher pub; +}; + +FullSystem::FullSystem() +: dataPtr(gz::utils::MakeUniqueImpl()) +{ + this->dataPtr->pub = + this->dataPtr->node.Advertise("foobar"); +} + void FullSystem::Configure(const gz::sim::Entity &_entity, const std::shared_ptr &_element, gz::sim::EntityComponentManager &_ecm, @@ -56,6 +74,14 @@ void FullSystem::PreUpdate(const gz::sim::UpdateInfo &_info, if (!_info.paused && _info.iterations % 1000 == 0) { gzdbg << "ros_gz_example_gazebo::FullSystem::PreUpdate" << std::endl; + + ros_gz_example_gazebo::msgs::FoobarStamped msg; + auto *header = msg.mutable_header(); + auto simTime = gz::math::durationToSecNsec(_info.simTime); + header->mutable_stamp()->set_sec(simTime.first); + header->mutable_stamp()->set_nsec(simTime.second); + msg.mutable_foobar()->set_value(_info.iterations % 1000); + this->dataPtr->pub.Publish(msg); } } @@ -69,7 +95,7 @@ void FullSystem::Update(const gz::sim::UpdateInfo &_info, } void FullSystem::PostUpdate(const gz::sim::UpdateInfo &_info, - const gz::sim::EntityComponentManager &_ecm) + const gz::sim::EntityComponentManager &_ecm) { if (!_info.paused && _info.iterations % 1000 == 0) {