Ask Your Question
0

sensor_msgs/Range

asked 2014-01-03 08:09:28 -0600

richkappler gravatar image

updated 2014-01-28 17:07:24 -0600

ngrennan gravatar image

I have a simple sonar sensor running to an Arduino Mega ADK. On the Arduino I have the following code patched together from rosserial_arduino tutorials and the code specific to my sensor:

#include <ros.h>
#include <ros/time.h>
#include <sensor_msgs/Range.h>
#include <Ultrasonic.h>

ros::NodeHandle nh;

sensor_msgs::Range range_msg;
ros::Publisher pub_range( "/ultrasound", & range_msg);

char frameid[] = "/ultrasound";
int inMsec;

#define TRIGGER_PIN  48
#define ECHO_PIN     49

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()
  {
  nh.initNode();
  nh.advertise(pub_range);

  range_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
  range_msg.header.frame_id =  frameid;
  range_msg.field_of_view = 0.1;  // fake
  range_msg.min_range = 0.0;
  range_msg.max_range = 6.47;
  }

long range_time;

void loop()
  {
  long microsec = ultrasonic.timing();

  inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);

  //publish the adc value every 50 milliseconds
  //since it takes that long for the sensor to stablize
  if ( millis() >= range_time ){
    int r =0;

    range_msg.range = inMsec;
    range_msg.header.stamp = nh.now();
    pub_range.publish(&range_msg);
    range_time =  millis() + 50;
  }

  nh.spinOnce();
}

I know it is publishing as rostopic list shows .ultrasound as one of the running topics. As I expect based on the line range_msg.radiation_type = sensor_msgs::Range::ULTRASOUND; it is publishing a Range and that is confirmed by running rostopic type ultrasound.

My subscriber, however, refuses to read this because of the Range type. Here's the code:

#!/usr/bin/env python

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_name() + ": I heard %s" % data.data)

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("ultrasound", Range, callback)
    rospy.spin()

if __name__=='__main__':
    listener()

based on the simple publisher and subscriber tutorials.

However, when I run it, the error message tells me

Traceback (most recent call last):
     rospy.Subscriber("ultrasound", Range, callback)
NameError: global name 'Range' is not defined

I tried changing it to rospy.Range and got

rospy.Subscriber("ultrasound", rospy.Range, callback)
AttributeError: 'module' object has no attribute 'Range'

I understand the error, but have no idea how to fix it. Can I get a gentle-ish shove in the right direction?

edit retag flag offensive close merge delete

Comments

Hello mate, I am thinking of doing the same but as you have included the file Ultrasound.h, I am asking that this file contains the code that gets the raw data from the sensor?

mukut_noob gravatar imagemukut_noob ( 2016-04-08 07:35:45 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-01-03 08:54:38 -0600

Thomas D gravatar image

Try adding from sensor_msgs.msg import Range to your Python node with the subscriber.

edit flag offensive delete link more

Comments

That did the trick, thanks for the answer!

richkappler gravatar imagerichkappler ( 2014-01-03 09:06:11 -0600 )edit

It work for me (y)

Kellya gravatar imageKellya ( 2014-01-04 11:12:10 -0600 )edit
1

Hello mate, I am thinking of doing the same but as you have included the file Ultrasound.h, I am asking that this file contains the code that gets the raw data from the sensor?

mukut_noob gravatar imagemukut_noob ( 2016-04-09 11:35:12 -0600 )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

Stats

Asked: 2014-01-03 08:09:28 -0600

Seen: 3,475 times

Last updated: Jan 03 '14