ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Parrameter server in C++ publisher?

asked 2018-12-22 00:58:51 -0600

Brolin gravatar image

updated 2018-12-22 01:13:34 -0600

jayess gravatar image

How do i provide command line parameter for publisher written in c++. I tried similar to what i did with python publisher but it doesn't work. I used getparam in c++.

#include "ros/ros.h"
#include "performance_tests/SuperAwesome.h"  
#include <sstream>

int main(int argc, char **argv)
{
ros::init(argc, argv, "PublisherNode");
ros::NodeHandle nh;
std::string ra; //I want to accept an Integer value
if (nh.getParam("/ra", ra))
    {

      ROS_INFO("Got param: %s", ra.c_str());
    }
else
    {
      ROS_INFO("Failed to get param ");

    }
ros::Publisher msg_pub = nh.advertise<performance_tests::SuperAwesome>("publisher", 1000);
// I want to change the rate value through the value I receive in through ra.
ros::Rate rate(100);

int count = 0;

while (ros::ok())
{
    performance_tests::SuperAwesome msg;
    std::stringstream s;

    s <<"Hello World " << count; 
    msg.data = s.str();

    ROS_INFO("%s",msg.data.c_str());
    msg_pub.publish(msg);
    ros::spinOnce();

    rate.sleep();

    count++;
}

return 0;
}

I would like to change the publisher rate through command line parameter similar to what I do in python publisher. i.e,

rosrun publisher_package publisher.py _rate:=50

How can I do the same in c++ publisher

edit retag flag offensive close merge delete

Comments

1

Can you please update your question so that it's a minimum working example? If I were to copy and paste your code and try to compile it (which I tried), it wouldn't work due to a custom class that's not defined.

jayess gravatar image jayess  ( 2018-12-22 01:41:22 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-12-22 01:56:33 -0600

Hamid Didari gravatar image

updated 2018-12-22 02:19:10 -0600

use this command and code:

rosrun publisher_package PublisherNode _my_param:=1

#include "ros/ros.h"
#include "performance_tests/SuperAwesome.h"  
#include <sstream>

int main(int argc, char **argv)
{
ros::init(argc, argv, "PublisherNode");
ros::NodeHandle nh("~");
int ra;
nh.param("my_param", ra,100);

ros::Publisher msg_pub = nh.advertise<performance_tests::SuperAwesome>("publisher", 1000);
// I want to change the rate value through the value I receive in through ra.
ros::Rate rate(ra);

int count = 0;

while (ros::ok())
{
    performance_tests::SuperAwesome msg;
    std::stringstream s;

    s <<"Hello World " << count; 
    msg.data = s.str();

    ROS_INFO("%s",msg.data.c_str());
    msg_pub.publish(msg);
    ros::spinOnce();

    rate.sleep();

    count++;
}

return 0;
}
edit flag offensive delete link more

Comments

I tried the command, I throws an error saying that the 5th charachter(:) is not allowed ?

Brolin gravatar image Brolin  ( 2018-12-22 02:06:13 -0600 )edit

sorry some syntax was wrong i updated the answer

Hamid Didari gravatar image Hamid Didari  ( 2018-12-22 02:19:45 -0600 )edit

Thank you @hamid. The solution is working

Brolin gravatar image Brolin  ( 2018-12-22 06:56:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-22 00:58:51 -0600

Seen: 204 times

Last updated: Dec 22 '18