Ask Your Question

Need help in subscribing from one topic and publishing to another.

asked 2017-06-29 08:24:10 -0500

SajiK gravatar image

updated 2017-06-29 22:05:38 -0500

ahendrix gravatar image

I'm trying to subscribe the values from a potentiometer which is connected to an Arduino and publish it to /cmd_vel topic in order to move my robot. So far i've tried to subscribe the values from the potentiometer using rosserial. The code is as follows.

#if (ARDUINO >= 100)
 #include <Arduino.h>
 #include <WProgram.h>
#include <ros.h>
#include <rosserial_arduino/Adc.h>

ros::NodeHandle nh;

rosserial_arduino::Adc adc_msg;
ros::Publisher p("adc", &adc_msg);

void setup()
  pinMode(13, OUTPUT);


//We average the analog reading to elminate some of the noise
int averageAnalog(int pin){
  int v=0;
  for(int i=0; i<4; i++) v+= analogRead(pin);
  return v/4;

long adc_timer;

void loop()
  adc_msg.adc0 = averageAnalog(0);
  adc_msg.adc1 = averageAnalog(1);
  adc_msg.adc2 = averageAnalog(2);
  adc_msg.adc3 = averageAnalog(3);
  adc_msg.adc4 = averageAnalog(4);
  adc_msg.adc5 = averageAnalog(5);



I tried to get these values by writing a subscriber node program ,

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + 'adc_value %d',

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber('/adc', String, callback)

    # spin() simply keeps python from exiting until this node is stopped

if __name__ == '__main__':

But there is no output coming onto the terminal.

Kindly help in subscribing to the /adc topic and publish its values to /cmd_vel topic.

Thank you.

edit retag flag offensive close merge delete


Have you checked if your topic is being published by running rostopic list | grep /adc and rostopic echo /adc?

Ruben Alves gravatar image Ruben Alves  ( 2017-06-29 12:13:15 -0500 )edit

Hi Ruben, I ran the command "rostopic echo /adc" and i'm able to see the values from the potentiometer and the changes in values when i rotate the potentiometer.

SajiK gravatar image SajiK  ( 2017-06-30 00:40:25 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-06-29 22:09:26 -0500

ahendrix gravatar image

updated 2017-06-30 15:09:48 -0500

Your publisher and subscriber types do not match. You're publishing a rosserial_arduino/Adc message, but you're subscribing to a std_msgs/String. ROS cannot convert messages between different types, so instead it prints an error and drops the messages. You should have seen an error message about MD5 sum mismatch from the publisher or the subscriber.

You should change your subscriber to import the rosserial_arduino/Adc message with from rosserial_arduino.msg import Adc and then you should subscribe to the Adc message type instead of String.

As @ruben-alves says, you should also be able to confirm that your arduino is publishing by using rostopic list and rostopic echo /adc

edit flag offensive delete link more


Hi ahendrix, As per your suggestion i edited the subscriber by importing rosserial_msgs.msgs import Adc and this is the following error which i'm getting

line 41, in <module> from rosserial_msgs.msg import Adc ImportError: cannot import name Adc

SajiK gravatar image SajiK  ( 2017-06-30 00:42:43 -0500 )edit

Oops; typo in my answer; that should've been rosserial_arduino instead of rosserial_msgs . Edited my answer to have to correct package name.

ahendrix gravatar image ahendrix  ( 2017-06-30 15:10:27 -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



Asked: 2017-06-29 08:24:10 -0500

Seen: 927 times

Last updated: Jun 30 '17