An I/O board such as Arduino can be connected to multiple sensors and actuators. In this section, we will program Arduino to control HC-SR04, an ultrasonic range sensor to be used in ROS. HC-SR04 sensors are low-cost and are commonly employed to measure distances between robots and obstacles.
This device is a Printed Circuit Board (PCB) with two piezoelectric transducers that are similar to microphones and piezoelectric headphones, but for the fact that they work in the ultrasonic range. One of the transducers is used as an emitter and the other as a receiver. The principle for measuring distance with this sensor is based on the echolocation system of animals such as bats. The sensor will emit ultrasound. If the ultrasound bounces against an obstacle, it will return to the sensor. Knowing the speed of sound in the air, we will measure the time between the emission and the reception of the sound to calculate the distance to the obstacle.
The HC-SR04 sensor has four male pins, VCC will be connected to 5V, GND will be connected to GND, the trigger will be connected to pin number 7, and ECHO will be connected to pin number 3. The trigger is used to activate the emission of ultrasound; when we write in pin number 7, a logical, high-level sensor will emit ultrasound waves. The echo pin will be at a high level if the sensor receives an ultrasound, as shown in the following diagram:
We will open c4_examples5_2.ino
with the Arduino IDE and upload it to the board. The code is given here:
#include <ros.h> #include <std_msgs/Int16.h> ros::NodeHandle nh; std_msgs::Int16 range; ros::Publisher range_pub("range", &range); const int trigpin = 7; const int echopin = 3; long duration = 0; void setup() { nh.initNode(); nh.advertise(range_pub); } void loop() { range.data = ping(); range_pub.publish(&range); nh.spinOnce(); delay(100); } long ping() { // Send out PING))) signal pulse pinMode(trigpin, OUTPUT); pinMode(echopin,INPUT); digitalWrite(trigpin, LOW); delayMicroseconds(2); digitalWrite(trigpin, HIGH); delayMicroseconds(10); digitalWrite(trigpin, LOW); //Get duration it takes to receive echo duration = pulseIn(echopin, HIGH); //Convert duration into distance return duration /29/2; }
We will include the ros_lib
and std_msgs/Int16
libraries, declare the ros
handler and std_msgs::Int16
named "range"
. In this topic, Arduino will send the distance to the closest obstacle:
std_msgs::Int16 range; ros::Publisher range_pub("range", &range);
In the setup()
function, ROS is initialized and the message to be published is advertised as follows:
nh.initNode(); nh.advertise(range_pub);
In Arduino's loop()
function, the ping()
function is invoked to manage the ultrasonic sensor, the distance is calculated, and then the distance is published in centimeters. A delay of 100 ms is introduced to generate data at almost 10 hz:
range.data = ping(); range_pub.publish(&range); nh.spinOnce(); delay(100);
The ping()
function declares pin 7
as a digital output and pin 3
as a digital input. We will put pin number 7
to a low level to ensure that no sound is emitted and keep the channel clean. Then, it writes a digital high level during 10 microseconds.
After that, the pulseIn()
Arduino function measures the time until the echo pin is at the high level, indicating that the ultrasound is received. This function returns the time in microseconds. Multiplying the duration of the pulse by the speed of sound, the distance is calculated. It's important to divide the distance by two because the sound pulse will go over two times the distance. First, it goes to the obstacle, and then it comes back to the sensor.
To run this node, we will execute roscore
in a terminal. In a new one, we will execute the rosserial
node to communicate with Arduino:
$ rosrun rosserial_python serial_node.py /dev/ttyACM0
Now, we should be prepared to use an ultrasound sensor in ROS.
As a curiosity, in the underwater robotics field, sonar technology is really fundamental; it plays the same role as a laser range sensor in ground robotics. As sound has better propagation features in water than it does in light, the sonar sensor is used to detect obstacles, to map, and to determine location.
18.227.46.227