std_msgs::string compare in a callback cpp node
Hi all,
I'm trying to create a node with rosserial on Arduino. It should publish some sensor information and acts when I publish a command through a command topic. I wrote a cpp node with a callback function:
void commandsCB (const std_msgs::String& commands)
{
nh.loginfo("Reciving new message... \n");
nh.loginfo(commands.data);
nh.loginfo(scan.data);
if (commands.data == scan.data)
{
scanning(pos, flag, minAngle, maxAngle, myservo, servoPos, pub_pos, scansNum, range_msg, pub_range);
nh.loginfo("Scanning...");
}
if (commands.data == forward.data)
{
cart.forwardDrive();
getRange(range_msg, pub_range);
nh.loginfo("Driving forward...");
}
if (commands.data == backward.data)
{
cart.backwardDrive();
getRange(range_msg, pub_range);
nh.loginfo("Driving backward...");
}
if (commands.data == rightTurn.data)
{
cart.rightTurn();
getRange(range_msg, pub_range);
nh.loginfo("Turning right...");
}
if (commands.data == leftTurn.data)
{
cart.leftTurn();
getRange(range_msg, pub_range);
nh.loginfo("Turning left...");
}
}
ros::Subscriber<std_msgs::String> sub_commands ("/commands", &commandsCB);
Where scan.data, forward.data etc all std_msgs::String types.
When I try this node and publish command, for example,
scan
, I get from both
nh.loginfo(commands.data)
and
nh.loginfo(scan.data)
"scan", but it doesn't go into the if condition in the following lines in the callback.
I wonder if that's the right way to write this callback.
I'll appreciate any help, Thanks in advance, Iftach.