After connecting the signal pin of the sensor to (physical) GPIO 11 of the Raspberry Pi 4 B and power it with 5V and ground, we can test its functionality with the available
python script ultrasonic.py from Seed Studio.
The following shows the truncated output of the ultrasonic.py script when moving an obstacle in front of the sensor. We see that the distance value changes as expected.
To use the ultrasonic ranger as a ROS node it is convenient to wrap the sensor functionality in a class.
This provides an easy to extend interface for the ultrasonic ranger (API)
Therefore I copied the core functionality of the ultrasonic.py script from Seeed Studio in a class named GroveUltrasonicRanger.
Executing the grove_ultrasonic_ranger.py will result in the following output:
ROS provides the Range Message in the sensor_msgs header. This message type can be used to write a wrapper that will act as a ROS node for the Grove ultrasonic sensor.
To design this node we will send out measurements periodically over a topic of type sensor_msgs/Range.
The code for this node is in ranger.py.
After writing the node we need to build the packages in the workspace with catkin build.
This lets the node publish range messages which we can capture in another terminal window using rostopic.
First we use rostopic list to find the name of the topic we are interested in:
To provide additional information when an obstacle is too close or the robot has no obstacle in front of it we use
REP-117 as guideline.
To successfully implement this REP, checks for valid measurements should use floating-point standards and follow this form:
1 2 3 4 5 6 7 8 91011
if(minimum_range<=value&&value<=maximum_range){// Represents expected pre-REP logic and is the only necessary condition for most applications.// This is a valid measurement.}elseif(!isfinite(value)&&value<0){// Object too close to measure.}elseif(!isfinite(value)&&value>0){// No objects detected in range.}elseif(isnan(value)){// This is an erroneous, invalid, or missing measurement.}else{// The sensor reported these measurements as valid, but they are discarded per the limits defined by minimum_range and maximum_range.}
Comments
Comments are GitHub Discussions,
shown with giscus. Loading them connects to giscus.app (hosted by Vercel, USA) and GitHub (USA); you need a GitHub account to comment.