0
$\begingroup$

Rosanswers logo

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


Originally posted by Brolin on ROS Answers with karma: 35 on 2018-12-22

Post score: 0


Original comments

Comment by jayess on 2018-12-22:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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;
}

Originally posted by Hamid Didari with karma: 1769 on 2018-12-22

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Brolin on 2018-12-22:
I tried the command, I throws an error saying that the 5th charachter(:) is not allowed ?

Comment by Hamid Didari on 2018-12-22:
sorry some syntax was wrong i updated the answer

Comment by Brolin on 2018-12-22:
Thank you @hamid. The solution is working

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.