05 obstacle avoidance
05 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
1
Formula: measurement distance=(high voltage level time*sound speed
(340m/s))/2.
3. Program Download
Note
Remove the Bluetooth module before downloading the program. Or the program will
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
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
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.
6
5.5 Ultrasonic Detection
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.
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