0
$\begingroup$

Rosanswers logo

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.


Originally posted by Iftahnaf on ROS Answers with karma: 23 on 2021-04-17

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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/string/string/compare/ 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


Originally posted by ct2034 with karma: 862 on 2021-04-17

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2021-04-17:
And it's probably obvious, but this is not ROS-specific. It's C++.

Comment by Iftahnaf on 2021-04-17:
Thanks for the answer.

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

Comment by ct2034 on 2021-04-17:
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.

Comment by Iftahnaf on 2021-04-17:
Great, It works after a bit of modification.

Thanks again for your help.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.