0
$\begingroup$

Rosanswers logo

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!!!


Originally posted by atomoclast on ROS Answers with karma: 151 on 2016-09-07

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Mark Rose with karma: 1563 on 2016-09-07

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

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