08 Intersection Judgment
08 Intersection Judgment
In this section, let’s learn how to detect a black line via the 4-ch line follower,
and control miniAuto to follow the line. If encountering an intersection during
the line following, the miniAuto will stop.
1. Program Flowchart
2. Module Introduction
1) 4-ch Line Follower
The 4-ch line follower adopts an I2C communication interface, which can read
the data of the probes on the module through I2C communication.
It has four probes. Each with an infrared emitter and an infrared receiver. White
reflects infrared strongly, while black reflects infrared weakly. This allows
determining whether the line has been recognized.
1
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
3) Click “Select Board”, and the software will automatically detect the current
Arduino serial port. Next, click to connect.
2
4) Click to download the program into Arduino. Then just wait for it to
complete.
4. Program Outcome
Place the car on the black line, and turn on the miniAuto. When the onboard
button is detected to be pressed, it will enter the line-following program.
If the black line is recognized, it will follow the black line; if the black line is not
recognized, it will move backward until the black line is recognized again.
Note: Before starting the line following, please place the car on the black line. Otherwise, it
5. Program Analysis
The program for this game is called “tracking_crossroads.ino”. You can refer to
the following flowchart for the implementation logic of this program.
3
1.1 Import Library File
Import the required library files of I2C communication and RGB LED for the
game.
2) An RGB LED control object is created. The “modestate” variable sets the
4
car to line-following mode, which is the default mode.
5
5.5 Obtain Sensor Data
Call the “WireReadDataByte()” function to obtain the current line data from the
sensor. The values of the probes 1 to 4 on the line follower are stored in the
0th to 3rd bits of “rec_data” array. If the obtained value is 0, no black line is
detected; if the obtained value is 1, the black line is detected. Then, the value
is saved in the “rec_data” array.
6
5.7 Line Following Task
1) Call the “Rgb_Show(255,0,0)” function to set the onboard RGB LED to red.
The received sensor data is then checked. If both sensor2 and sensor3 detect
a black line, the car moves forward.
2) If sensor2 detects a black line while Sensor3 does not, the car turns left by
65 degrees. If sensor3 detects a black line while sensor2 does not, the car
turns right by 65 degrees.
3) When neither sensor2 nor sensor3 detect a black line, the car moves
backward.
4) When both sensor2 and sensor3 detect a black line, indicating it is at the
crossroad, the car will perform intersection detection.
7
5.8 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
values output to each motor.
6. Function Extension
Let’s modify the car to turn left after the intersection is detected.
Locate the intersection detection function in the code. Modify the code to make
the car turn left for 2 seconds after detecting a crossroad, instead of stopping
for 2 seconds.
7. FAQ
Q1:since the code is uploaded, the car keeps moving backward.
A:please place the car on the black line before running the program.
Q2:the line detection of the 4-ch line follower is inaccurate.
9
A:please adjust the potentiometer on the 4-ch line follower to the proper
position.
10