# Ultrasonic Board v0.9: ultrasonic bumpers

The “Ultrasonic Board” is the electronic board that allows to control up to four HC-SR04 ultrasonic sensors providing a simple and reliable obstacle detection system.

The optical spatial sensors mounted on MyzharBot v3 and MyzharBot v4 has a intrinsic limitation that make it unable to perceive obstacle that are too near along its forward motion direction. Asus Xtion Pro Live does not see obstacles nearest than 50cm, Stereolabs ZED does not see obstacles nearest than 1 meter.

This limitation is not important in a static environment, but is really dangerous in a really dynamic workplace where objects (other robots, human, CHILDRENS, …) can suddenly appear in front of the robot in its blind zone.

Furthermore optical 3D sensors cannot see transparent obstacles like glass.

To overcome this limitation MyzharBot v4 will be equipped with four Ultrasonic sensors, that despite their low precision (related to optical sensors), they will allow the robot to move with high security in a highly dynamic environment. Even more, Ultrasonic technology is based on sound reflection, so it will allow the robot to sense also obstacles invisible to optical sensors.

## The sensors

The theory behind HC-SR04 sensors is really simple: an ultrasonic emitter sends a $40 khz$ sound pulse, the sound wave bumps into an obstacle and goes back to the sensor. An ultrasonic listener get it and a microcontroller calculates the time $T$ that the sound wave took in making its “voyage”. Assuming that the sound wave has a speed of about $v_s=341 m/sec$, we can easily calculate the distance $D$ of the obstacle using the formula:

$D = \frac{v_s * T}{2}$

The time $T$ is emitted from the sensor on the “Echo” pin as a pulse with a width as long as the duration of the “voyage”. So the microcontroller needs to acquire the pulse to finally get the distance of the obstacle.

It would be very easy if the sensor worked as expected. According to its user guide to activate the sensor a trigger pulse longer than $10 \mu sec$ must be emitted on the “Trig” pin, the sensor than emits the$40 khz$ sound wave and it should have to emit an “Echo” pulse in the range $150 \mu sec-25 msec$ as reply, $38 msec$ if no obstacle is detected. But when there is no obstacles, or when the sensor is obstructed, the echo duration is really aleatory and sometimes its width is longer than $200 msec$, as reported also on this well done analysis: Making a better HC-SR04 Echo Locator.

## The board

The sensors are managed using anST Nucleo32 board based on the powerful STm32F303 microcontroller. The board communicates with host using USART with a simple binary protocol.

A simple prototype on a soldered breadboard has been realized, the board can be powered using the microUSB connector, or using an external 5V stabilized power supply using the dedicated 2 PINs connector or the 4 PINs USART connector.

Ultrasonic Board prototype

## The Firmware

The firmware is based on the STm32CubeF3 library and is configured using the powerful STM32CubeMX tool.

### USART binary protocol

The Ultrasonic Bumper board emits data on USART 1 at 10 Hz. Serial communication has the following parameters:

• Baudrate: 115200
• Bits: 8
• Parity: none
• Stop Bits: 1
• Hardware handshaking: not available

The communication protocol is really simple. The byte order is LSB.

• 16 bit unsigned intSynchronization word: 0xa55a – used to synchronize the communication
• 16 bit unsigned intByte count: 30 – the number of bytes after Synchronization word
• 32 bit unsigned intTicks: milliseconds elapsed since the power on of the board
• 32 bit floatNot valid value: value assigned to ranges not valid. A range is not valid if obstacle is beyond 4 m or nearest than 2 cm
• 32 bit floatRange #0: range measured by the sensor connected to input #0
• 32 bit floatRange #1: range measured by the sensor connected to input #1
• 32 bit floatRange #2: range measured by the sensor connected to input #2
• 32 bit floatRange #3: range measured by the sensor connected to input #3
• 16 bit unsigned intSonar active: the number of sonar active on the board
• 16 bit unsigned intTerminator: Line feed and Carriage Return to terminate the data stream

The C/C++ structure that can be mapped on the data stream using a “simple” memcpy command is the following:

#define MAX_SONAR 4

typedef struct _data_out
{
uint16_t ctrl_frame_0;   // 0xA55A
uint16_t byte_count;     // number of bytes following
uint32_t ticks;          // ticks since system start
float not_valid_val;     // value for not valid distances
float distances[MAX_SONAR];   // distances in meters
uint16_t sonar_active;   // Number of sonar connected;
uint16_t ctrl_frame_1;   // 0x0D0A
} DataOut;

Main functions

Here you will find a simple description of the main functions of the firmware

Recent commits:

## ROS integration

The code of the node to acquire data from the Ultrasonic bumper board is available on Github:

Recent commits: