Ask Your Question

How to correctly publish data from an analog sensor

asked 2018-10-20 15:20:25 -0500

rosuseruser gravatar image

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
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'))


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-20 21:47:26 -0500

KinWah gravatar image


I am doing something similar and this is how I solved this problem.

The arduino nano is responsible to read analog signal and then, from arduino, I can directly publish the data to a specific topic. What you need is the following library in both arduino and raspberry >>> rosserial. I am using it and successfully implement it.

Just follow its tutorial and hopefully, you can do it ;)

Hope you can fight your own way out !!!!

here is another resource that definitely can help you out !

May God bless you ;)

edit flag offensive delete link more


Thank you! Works nice :)

rosuseruser gravatar image rosuseruser  ( 2018-10-21 04:57:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-10-20 15:14:41 -0500

Seen: 708 times

Last updated: Oct 20 '18