How to correctly publish data from an analog sensor
Hi. I have an ultrasonic sensor (SRF 06 - current loop - this sensor has no other interface) which outputs analog signal (1V for 3-4cm - 5V for 510cm), Raspberry Pi 3 with Ubuntu Mate 16.04 installed and Arduino Nano
I want to publish this data to a ROS topic. Currently I'm using Arduino Nano to receive data from the sensor (because RPi has no analog inputs) and I connect Arduino to RPi with USB cable. Arduino sends ints from range 3 to 510. I wrote a small Python script to check if the transmission works - I receive correct data so looks like it's fine. Now, i created a ROS package with custom message to publish output of "/dev/ttyUSB0" to the topic. The problem is that data dont seem to be updated - it always oscillates nearby one value (the initial value of the output).
Can anyone tell me what might be wrong? Btw. Is there any package that deals with this kind of sensor? Maybe I should use rosserial with Arduino?
My publisher:
#!/usr/bin/env python
import rospy
import serial
from ultrasonic_sensor.msg import distance_cm
def main():
port = "/dev/ttyUSB0"
ser = serial.Serial(port, 9600)
ser.baudrate = 9600
rospy.init_node("ultrasonic_distance")
pub = rospy.Publisher("ultrasonic_data", distance_cm, queue_size=1)
r = rospy.Rate(5)
msg = distance_cm()
while not rospy.is_shutdown():
#print(ser.readline()) - also not working properly, no update
msg.distance = int(ser.readline().decode("utf-8").strip('\n').strip('\r'))
pub.publish(msg)
r.sleep()
main()