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

How to set the queue size in the arduino publisher

asked 2018-01-16 18:22:44 -0500

JollyRogers gravatar image

updated 2018-01-17 09:39:07 -0500

gvdhoorn gravatar image

I wrote the following code in Arduino:

#include <ros.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Int64.h>
std_msgs::Int64 int_msg;
ros::NodeHandle nh;
int cont = 0;
ros::Publisher pub("I_know", &int_msg, 10);
void messageCb(const std_msgs::Float64& msg)
{
  if(msg.data > 1.0)
  {
    digitalWrite(13, HIGH);   // blink the led
    int_msg.data = cont++;
    pub.publish(&int_msg);
  }
  else
  { 
    digitalWrite(13, LOW);    // Turn off led
  }
}

ros::Subscriber<std_msgs::Float64> sub("your_topic", &messageCb);


void setup()
{
  pinMode(13, OUTPUT);
  nh.initNode();
  nh.subscribe(sub);
  nh.advertise(pub);
}
void loop()
{

 nh.spinOnce();
}

The code takes a Float64 signal from topic "your_topic" and, dependent on its value, turn on/off a LED. Also, everytime that the LED blinks the algorithm sends an integer value using topic "I_know". This integer value is used to count how many times the LED blinks. The program does not show any error, but every time that I try to print the values from the counter I receive the following:

0 0 1 1 1 2 2 2 2 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7

I am not sure why is this happening. I expected to receive just one value for each integer. I consider that this occurs because I am not setting the publisher queue in Arduino. If this is the case please explain to me how to it, if not, how can I avoid it?

On the other hand, I share the python's code.

#!/usr/bin/env python
import rospy
from std_msgs.msg import  Float64
from random import randint
from time import sleep
from std_msgs.msg import Int64

def counter(msg):

        print msg.data

rospy.init_node('Arduino_hear_my_whispers')
pub = rospy.Publisher('your_topic', Float64, queue_size=10)
while not rospy.is_shutdown():
        rnd = 1.0
        #print "the number is " + str(rnd)
        message=Float64(rnd)
        pub.publish(message)
        rnd = 2.0
        sleep(0.1)
        #print "the number is " + str(rnd)
        message=Float64(rnd)
        pub.publish(message)
        sub = rospy.Subscriber('I_know', Int64, counter, queue_size=2)
rospy.spin()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-01-17 09:32:57 -0500

JollyRogers gravatar image

This situation was avoided placing the python subscriber before the while loop.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-16 18:22:44 -0500

Seen: 1,076 times

Last updated: Jan 17 '18