Skip to content

Commit

Permalink
Configurable joint state publisher's topic (#1076)
Browse files Browse the repository at this point in the history
Signed-off-by: Jasmeet Singh <[email protected]>
  • Loading branch information
jasmeet0915 authored Nov 15, 2021
1 parent ab2ef6c commit adadda7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/systems/joint_state_publisher/JointStatePublisher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ignition/gazebo/components/JointVelocity.hh"
#include "ignition/gazebo/components/ParentEntity.hh"
#include "ignition/gazebo/components/Pose.hh"
#include "ignition/gazebo/Util.hh"

using namespace ignition;
using namespace gazebo;
Expand Down Expand Up @@ -89,6 +90,14 @@ void JointStatePublisher::Configure(
this->CreateComponents(_ecm, joint);
}
}

// Advertise the state topic
// Sets to provided topic if available
if (_sdf->HasElement("topic"))
{
this->topic = _sdf->Get<std::string>("topic");
}

}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -142,11 +151,26 @@ void JointStatePublisher::PostUpdate(const UpdateInfo &_info,
worldName = _ecm.Component<components::Name>(
parentEntity->Data())->Data();

// Advertise the state topic
std::string topic = std::string("/world/") + worldName + "/model/"
+ this->model.Name(_ecm) + "/joint_state";
// if topic not set it will be empty
std::vector<std::string> topics;
// this helps avoid unecesarry invalid topic error
if (!this->topic.empty())
{
topics.push_back(this->topic);
}
topics.push_back(std::string("/world/") + worldName + "/model/"
+ this->model.Name(_ecm) + "/joint_state");

this->topic = validTopic(topics);
if (this->topic.empty())
{
ignerr << "No valid topics for JointStatePublisher could be found."
<< "Make sure World/Model name does'nt contain invalid characters.\n";
return;
}

this->modelPub = std::make_unique<transport::Node::Publisher>(
this->node.Advertise<msgs::Model>(topic));
this->node.Advertise<msgs::Model>(this->topic));
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/systems/joint_state_publisher/JointStatePublisher.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <memory>
#include <set>
#include <string>
#include <ignition/gazebo/Model.hh>
#include <ignition/transport/Node.hh>
#include <ignition/gazebo/System.hh>
Expand Down Expand Up @@ -82,6 +83,9 @@ namespace systems

/// \brief The joints that will be published.
private: std::set<Entity> joints;

/// \brief The topic
private: std::string topic;
};
}
}
Expand Down

0 comments on commit adadda7

Please sign in to comment.