How to set the queue size in the arduino publisher
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()