ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

SajiK's profile - activity

2018-08-08 01:01:47 -0500 marked best answer Need help in subscribing from one topic and publishing to another.

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.

2017-07-03 06:54:14 -0500 received badge  Famous Question (source)
2017-06-30 00:51:04 -0500 received badge  Notable Question (source)
2017-06-30 00:45:00 -0500 commented answer Need help in subscribing from one topic and publishing to another.

Hi ahendrix, As per your suggestion i edited the subscriber by importing rosserial_msgs.msgs import

2017-06-30 00:44:27 -0500 commented answer Need help in subscribing from one topic and publishing to another.

Hi ahendrix, As per your suggestion i edited the subscriber by importing "rosserial_msgs.msgs impor

2017-06-30 00:42:43 -0500 commented answer Need help in subscribing from one topic and publishing to another.

Hi ahendrix,

2017-06-30 00:40:25 -0500 commented question Need help in subscribing from one topic and publishing to another.

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

2017-06-29 22:04:08 -0500 received badge  Popular Question (source)
2017-06-29 09:04:37 -0500 asked a question Need help in subscribing from one topic and publishing to another.

Need help in subscribing from one topic and publishing to another. I'm trying to subscribe the values from a potentiomet