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

Ultrasonic sensor is displayed incorrectly in RViz

asked 2022-05-04 15:49:07 -0500

ixtora gravatar image

updated 2022-05-05 05:25:32 -0500

Hello,

I have an ultrasonic sensor publishing in a topic. I have a distance visualization in RViz. I am getting incorrect display. What could be wrong?

My result: RViz

Expected Behavior: expected

This is the topic echo:

ubuntu@ubuntu:/$ rostopic echo /ultrasound
---
header: 
  seq: 18316
  stamp: 
    secs: 1651696499
    nsecs: 773573032
  frame_id: "/ultrasound"
radiation_type: 0
field_of_view: 0.5235990285873413
min_range: 0.05000000074505806
max_range: 2.0
range: 0.05999999865889549
---

This is the topic info:

Type: sensor_msgs/Range

Publishers:
   * /serial_node (http://ubuntu:35161/)

Subscribers:
   * /rviz_1651695997117575758 (http://ubuntu:38335/)

Code:

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

ros::NodeHandle  nh;

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

const int adc_pin = 0;

char frameid[] = "/ultrasound";

NewPing u_sonar = NewPing(D6, D7, 200);

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.523599f;
  range_msg.min_range = 0.05;
  range_msg.max_range = 2;

}

long range_time;

void loop() {
  if ( millis() >= range_time ) {
    range_msg.range = (float)u_sonar.ping_cm() / 100;
    range_msg.header.stamp = nh.now();
    pub_range.publish(&range_msg);
    range_time =  millis() + 50;
  }
  nh.spinOnce();
}
edit retag flag offensive close merge delete

Comments

Just for clarity. You said it's being displayed 'incorrectly'. It would make it easier for us if you would also include your 'correct' or more specifically 'desired' outcome.

Joe28965 gravatar image Joe28965  ( 2022-05-05 02:46:47 -0500 )edit

@Joe28965, I have updated my question.

ixtora gravatar image ixtora  ( 2022-05-05 05:29:22 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2022-05-10 08:46:38 -0500

ixtora gravatar image

I found the soultion, the problem was in this line:

range_msg.header.frame_id =  frameid; // "/ultrasound"

I changed it to:

range_msg.header.frame_id =  "ultrasound";
edit flag offensive delete link more
0

answered 2022-05-05 09:45:29 -0500

Alex-SSoM gravatar image

It looks like you're not publishing a TF. Look at your TF tree (using the rqt plugin), your frames are most likely unconnected.

edit flag offensive delete link more

Comments

I see in expected behavior they don't get published either. But the sensor works correctly.

ixtora gravatar image ixtora  ( 2022-05-05 10:42:18 -0500 )edit

Your error doesn't come from the sensor it self. It's simply not localised in the world. RViz doesn;t have the information to localise it properly. The most simple way to fix this is to add a static_transform. This open course explains it quite well: https://ocw.tudelft.nl/course-lecture...

Alex-SSoM gravatar image Alex-SSoM  ( 2022-05-05 10:50:54 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-05-04 15:49:07 -0500

Seen: 121 times

Last updated: May 10 '22