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

Example of using custom messages in the ros project #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
34 changes: 29 additions & 5 deletions ros_gz_example_gazebo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
Expand All @@ -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
)




Expand Down
1 change: 1 addition & 0 deletions ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.dsv.in
Original file line number Diff line number Diff line change
@@ -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/
1 change: 1 addition & 0 deletions ros_gz_example_gazebo/hooks/ros_gz_example_gazebo.sh.in
Original file line number Diff line number Diff line change
@@ -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/"
13 changes: 10 additions & 3 deletions ros_gz_example_gazebo/include/ros_gz_example_gazebo/FullSystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <gz/sim/EventManager.hh>
#include <gz/sim/System.hh>

#include <gz/utils/ImplPtr.hh>

namespace ros_gz_example_gazebo
{
// This is the main plugin's class. It must inherit from System and at least
Expand All @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 28 additions & 2 deletions ros_gz_example_gazebo/src/FullSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
// in the cc file, like it's done here.
#include <gz/plugin/Register.hh>

#include <ros_gz_example_gazebo/msgs/foobar.pb.h>

#include <gz/transport/Node.hh>

// Don't forget to include the plugin's header.
#include "ros_gz_example_gazebo/FullSystem.hh"

Expand All @@ -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<Implementation>())
{
this->dataPtr->pub =
this->dataPtr->node.Advertise<ros_gz_example_gazebo::msgs::FoobarStamped>("foobar");
}

void FullSystem::Configure(const gz::sim::Entity &_entity,
const std::shared_ptr<const sdf::Element> &_element,
gz::sim::EntityComponentManager &_ecm,
Expand All @@ -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);
}
}

Expand All @@ -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)
{
Expand Down