ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Subscribing to a Bool msg via rosserial_arduino.

asked 2016-09-07 14:51:53 -0500

Hello All!

I'm in the process of building a tool for a robot that I'm working with and we are trying to use an Arduino to communicate with ROS to control the end effector. We are right now broadcasting a Boolean topic via a separate node, and we would like the Arduino node to subscribe to the topic and then do an operation based off of the true/false state of the topic.

In my callback I have:

void messageCB(const std_msgs::Bool::ConstPtr& state)
{
  if (state.data = true)
  {
     //do stuff
  }

But I keep getting many errors:

rotational_table_node.ino:17:22: error: ‘ConstPtr’ in ‘class std_msgs::Bool’ does not name a type
rotational_table_node.ino:17:48: error: ISO C++ forbids declaration of ‘state’ with no type [-fpermissive]
rotational_table_node.ino: In function ‘void messageCB(const int&)’:
rotational_table_node.ino:21:13: error: request for member ‘data’ in ‘state’, which is of non-class type ‘const int’
rotational_table_node.ino: At global scope:
rotational_table_node.ino:54:76: error: invalid conversion from ‘void (*)(const int&)’ to ‘ros::Subscriber<std_msgs::Bool>::CallbackT {aka void (*)(const std_msgs::Bool&)}’ [-fpermissive]
In file included from /home/motherbrain/sketchbook/libraries/ros_lib/ros/node_handle.h:84:0,
                 from /home/motherbrain/sketchbook/libraries/ros_lib/ros.h:38,
                 from rotational_table_node.ino:5:
/home/motherbrain/sketchbook/libraries/ros_lib/ros/subscriber.h:97:7: error:   initializing argument 2 of ‘ros::Subscriber<MsgT, void>::Subscriber(const char*, ros::Subscriber<MsgT, void>::CallbackT, int) [with MsgT = std_msgs::Bool; ros::Subscriber<MsgT, void>::CallbackT = void (*)(const std_msgs::Bool&)]’ [-fpermissive]
       Subscriber(const char * topic_name, CallbackT cb, int endpoint=rosserial_msgs::TopicInfo::ID_SUBSCRIBER) :
       ^

Any idea what I'm doing wrong or could do differently?

Thanks!!!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-09-07 18:30:38 -0500

Mark Rose gravatar image

updated 2016-09-07 18:32:35 -0500

You should use the message types directly. For example:

void myCallback(const std_msgs::Bool& msg);
ros::Subscriber<std_msgs::Bool> mySubscriber("~some_topic", &myCallback);
...
void myCallback(const std_msgs::Bool& msg) {
    if (msg.data) {
        ... do something ...
    } else {
        ... do something else ...
    }
}

See the rosserial_arduino subscriber tutorial. There, they use the Empty message type. The callback parameter type is just const std_msgs::Empty&. All the rosserial_arduino tutorials are worth a look.

A few caveats about rosserial_arduino. It uses quite a bit of memory, not a problem if you are using a Mega or a Teensy, for example, but can be limiting on an Uno or Leonardo. You can limit memory by declaring the node handle in a way slightly different than the tutorials:

ros::NodeHandle_<ArduinoHardware, 5, 14, 125, 125> nh;

Using the NodeHandle_ template type directly allows you to control the buffer sizes and the number of subscribers and publishers in the declaration, rather than having to modify <ros.h>. The parameters 5, 14, 125, and 125 above are, respectively, the number of subscribers you will declare, the number of publishers, the size of the input buffer in bytes, and the size of the output buffer in bytes. Those buffer sizes are very skimpy, because I was running out of memory (leading to my abandonment of rosserial_arduino). You need to have enough buffer size to hold the largest message you will send or receive, and you have to call nh.spinOnce() often enough that the output buffer doesn't fill up.

ROS messages can be large. Also, because the Atmel chip is a non-Von Neumann architecture, static strings also take up RAM, so long topic names will start to eat up precious memory.

As I said, you don't have to worry much about this on a Mega or Teensy. I had a lot of stability issues on a Leonardo-class board which housed my power supply and motor controller. I eventually gave up and used ros_arduino_bridge instead.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-09-07 14:51:53 -0500

Seen: 1,994 times

Last updated: Sep 07 '16