0% found this document useful (0 votes)
7 views11 pages

05 obstacle avoidance

This document explains how to implement obstacle avoidance using an ultrasonic sensor in a robot car. It details the working principle of the ultrasonic sensor, the program flow, and how to control the robot's movement based on detected distances. Additionally, it provides troubleshooting tips and instructions for modifying the robot's turning direction.

Uploaded by

Ywhite Eric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

05 obstacle avoidance

This document explains how to implement obstacle avoidance using an ultrasonic sensor in a robot car. It details the working principle of the ultrasonic sensor, the program flow, and how to control the robot's movement based on detected distances. Additionally, it provides troubleshooting tips and instructions for modifying the robot's turning direction.

Uploaded by

Ywhite Eric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Obstacle Avoidance

In this section, you can learn to detect the distance of obstacle through glowing
ultrasonic module. The robot car can be simultaneously controlled to turn right
to avoid obstacle.

1. Program Flowchart

2. Ultrasonic Sensor

This is a glowing ultrasonic ranging module. It adopts an I2C communication


interface, which can read the range measured by an ultrasonic sensor through
I2C communication.

2.1 Working Principle


The ultrasonic sensor will automatically transmit 8 square waves at 40khz
during ranging, and then detect whether there is a signal return. If there is a
signal return, it outputs a high voltage level, whose continuing time is the time
of the ultrasonic from transmitting to returning.

1
Formula: measurement distance=(high voltage level time*sound speed
(340m/s))/2.

2.2 Schematic Diagram


The sensor is controlled by the “CS100” chip. The chip’s pins (TP, TN) transmit
8 square waves at 40Khz, and the chip’s pins (RP, RN) detect echo signals.
Then the distance value can be calculated by the formula “measurement
distance=(high voltage level time*sound speed (340m/s))/2”.

 Supply Voltage :5V

 Working Current: 2mA

 Effective Ranging: 20mm - 4000mm

3. Program Download
Note

 Remove the Bluetooth module before downloading the program. Or the program will

fail to download because of the serial port conflict.

 Please switch the battery box to the ‘OFF’ when connecting the Type-B download

cable. This action prevents the download cable from accidentally touching the power pins

of the expansion board, which may cause a short circuit.

1) Locate and open “ultrasonic_avoid” program file in the same directory of


this section.

2
2) Connect Arduino to the computer with the Type-B cable.

3) Click “Select Board”, and the software will automatically detect the current
Arduino serial port. Next, click to connect.

4) Click to download the program into Arduino. Then just wait for it to
complete.

3
4. Program Outcome
When miniAuto is powered on and the onboard button is detected to be
pressed, it will move forward at a speed of 100. The color of the ultrasonic
sensor will be simultaneously changed, based on the distance measured by
the ultrasonic sensor. The car will be controlled to turn.
Put the obstacle directly in front of the ultrasonic module and move it slowly
towards the module.
1) The color change outcome of the ultrasonic sensor:

① When the distance is less than 80mm, the RGB light on the ultrasonic
module is in a red breathing light mode.
② When the distance is between 80mm and 180mm, the RGB light on the
ultrasonic module is in red gradient mode.
③ When the distance is between 180mm and 320mm, the RGB light on the
ultrasonic module is in blue gradient mode.
④ When the distance is between 320mm and 500mm, the RGB light on the
ultrasonic module is in green gradient mode.
⑤ When the distance is greater than 500mm, the green RGB LED on the
ultrasonic module will remain on.
2) The car control outcome:
① When the distance is greater than 300mm, the car moves forward.
② When the distance is less than 300mm, the car turns right.

5. Program Analysis
The program for this game is called “ultrasonic_avoid.ino”. You can refer to the
following flowchart for the implementation logic of this program.

4
1.1 Import Library File

Import the required library file of controlling the glowing ultrasonic sensor for
the game.

5.2 Define Pin and Create Objects


Create a ultrasonic class to obtain distance data. Define a variable named
“pwm_min” to store the minimum mapping value of PWM. An array is also
created to define the digital motor pins. Then, create a variable “dis” to store
the distance value obtained from the ultrasonic sensor.

5
5.3 Initial Settings
Initialize related hardware equipment in “setup()” function. The first is serial
port. Set its baud rate of communication to 9600. Then, call the “Motor_Init()”
function to bind and initialize motors.

5.4 Main Function


When the button is pressed in the “loop()” function, enter the ultrasonic
obstacle avoidance game. Call the ultrasonic task function in a loop. It is used
to obtain distance data and set the color of the glowing ultrasonic sensor.
Determine the detected data to control the robot.

6
5.5 Ultrasonic Detection

1) Call the ultrasonic task function “ultrasonic_distance()” in the main “loop”


function. It measures the distance and changes the color of the RGB light.

2) When the distance is 0 to 80mm, the glowing ultrasonic sensor will be a


breathing light mode, showing red at a speed of 0.1s.

3) When the distance is 80 to 180mm, the glowing ultrasonic sensor will


display a red light, with the color getting lighter as the distance increases.

4) When the distance is 180 to 320mm, the glowing ultrasonic sensor will
display a blue light, with the color getting bluer as the distance increases.

5) When the distance is 320 to 500mm, the glowing ultrasonic sensor will
display a green light, with the color getting greener as the distance increases.

7
6) When the distance is greater than 500mm, the green LED on the glowing
ultrasonic sensor will be steady on.

5.6 Velocity Control Function

In the velocity control function, the control values of each motor are calculated
based on the kinematic analysis of the mecanum wheel. The “angle”
parameter sets the motion direction of the car. 0 degrees is the direction of the
car's front, and counterclockwise is the positive direction. The “velocity”
controls the speed of the car. The “rot” controls the direction of the car's
self-rotation. Counterclockwise is for values greater than 0, and clockwise is
for values less than 0. The “drift” controls whether the car's drift function is
enabled.

8
5.7 Motor Control Function
The control values of each motor calculated in the velocity control function are
used to control each motor with PWM. The “motors” array maps the speed
values of each motor to PWM values. The “pwm_set” array controls the PWM
values output to each motor.

6. Function Extension
How to modify the robot’s turning direction from right to left when encountering
the obstacle?
Please refer to the following steps:
1) Locate the code that controls the robot’s turning in the program. The
“Velocity_Controller()” function controls it to turn to right clockwise at a speed
of 100. If the value is less than 0, it is clockwise rotation; if the value is greater
than 0, it is counterclockwise rotation.

9
2) Change the value -100 to 100. The car will turn to left counterclockwise at a
speed of 100.

7. FAQ
Q1:since the code was upload, the distance measured by the ultrasound is
always 0.
A:please check that you have connected the 4-pin cable to the I2C interface.
Q2:the distance measured by the ultrasonic sensor is sometimes accurate,
and sometimes inaccurate.
A:please use a smooth and flat object for distance measurement, and avoid

10
prolonged close-range detection of the obstacle.

11

You might also like