ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Just to complement, I think your original code is a bit confusing. You are expecting to have a parameter with the type "string", instead of expecting a specific value for a given parameter.
It's important to pay attention to the return of nh.getParam()
. For example, if you run with _blue:=true
or _blue:=1
, you have false
return because it expects to fill a string variable, not a boolean or an integer. But if you run _blue:=some_text
, you have true
return, because the type matches.
Just to complement, you can remove the parameter param
or you can use it and remove check
, it gets cleaner. I tried in my local environment and it worked like that:
#include<ros/ros.h>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
std::string param;
ros::init(argc, argv, "node_name");
ros::NodeHandle nh("~");
nh.getParam("param", param);
ROS_INFO("Got parameter : %s", param.c_str());
if(param.compare("blue") == 0)
{
cout << "blue " << endl;
}
else if(param.compare("green") == 0)
{
cout << "green " << endl;
}
else
{
cout << "Don't run anything !! " << endl;
}
return 0;
}