0
$\begingroup$

Rosanswers logo

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>
#else
 #include <WProgram.h>
#endif
#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);
  nh.initNode();

  nh.advertise(p);
}

//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);
    
  p.publish(&adc_msg);

  nh.spinOnce();
}

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', data.data)

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
    rospy.spin()

if __name__ == '__main__':
    listener()

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.


Originally posted by SajiK on ROS Answers with karma: 1 on 2017-06-29

Post score: 0


Original comments

Comment by Ruben Alves on 2017-06-29:
Have you checked if your topic is being published by running rostopic list | grep /adc and rostopic echo /adc?

Comment by SajiK on 2017-06-30:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by ahendrix with karma: 47576 on 2017-06-29

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by SajiK on 2017-06-30:
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 from rosserial_msgs.msg import Adc ImportError: cannot import name Adc

Comment by ahendrix on 2017-06-30:
Oops; typo in my answer; that should've been rosserial_arduino instead of rosserial_msgs . Edited my answer to have to correct package name.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.