Ask Your Question
0

std_msgs::string compare in a callback cpp node

asked 2021-04-17 04:55:37 -0500

Iftahnaf gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-17 08:56:46 -0500

ct2034 gravatar image

your code compares the pointers of the strings. These are different, because those are two unique strings. If you want to compare the content of the string, use https://www.cplusplus.com/reference/s... This would be in your code ...

if (commands.data.compare(scan.data) == 0)
{
   scanning(pos, flag, minAngle, maxAngle, myservo, servoPos, pub_pos, scansNum, range_msg, pub_range);
   nh.loginfo("Scanning...");
}

etc

edit flag offensive delete link more

Comments

And it's probably obvious, but this is not ROS-specific. It's C++.

gvdhoorn gravatar image gvdhoorn  ( 2021-04-17 09:04:00 -0500 )edit

Thanks for the answer.

I used std_msgs::String and not std::String, can I still access the compare method?

Iftahnaf gravatar image Iftahnaf  ( 2021-04-17 09:06:52 -0500 )edit

yeah, that is probably a little confusing: An object of std_msgs::String has one member, thats called data and is of type std::String. This is the same for all std_msgs types http://wiki.ros.org/std_msgs . Thats why you have two additional .data in the answer above.

ct2034 gravatar image ct2034  ( 2021-04-17 09:16:30 -0500 )edit

Great, It works after a bit of modification.

Thanks again for your help.

Iftahnaf gravatar image Iftahnaf  ( 2021-04-17 12:00:03 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2021-04-17 04:55:37 -0500

Seen: 113 times

Last updated: Apr 17