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.