Ultrasonic sensor is displayed incorrectly in RViz
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:
Expected Behavior:
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();
}
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, I have updated my question.