0% found this document useful (0 votes)
11 views10 pages

08 Intersection Judgment

The document outlines the process for programming a miniAuto to follow a black line using a 4-channel line follower sensor. It includes details on the hardware setup, program flow, and specific functions for detecting intersections and controlling the car's movement. Additionally, it provides troubleshooting tips and a method for modifying the car's behavior at intersections.

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)
11 views10 pages

08 Intersection Judgment

The document outlines the process for programming a miniAuto to follow a black line using a 4-channel line follower sensor. It includes details on the hardware setup, program flow, and specific functions for detecting intersections and controlling the car's movement. Additionally, it provides troubleshooting tips and a method for modifying the car's behavior at intersections.

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/ 10

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

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 “tracking_crossroads” program file in the same directory


of this section.

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.

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

will keep moving backward.

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.

5.2 Define Pin and Create Objects


1) The I2C communication addresses for the 4-ch line follower is defined.
Then, the onboard RGB LED pins, onboard button pins, and motor pins are
defined.

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.3 Initial Settings


1) In the “setup()” function, set the serial port’s communication baud rate to
9600. Configure the button pin as an input mode. Initialize the RGB LED on the
expansion board via the “FastLED” library. Connect it to the “ledPin”. Then, call
the “Rgb_Show(255,255,255)” function to set RGB LED to white.

2) Call “Wire.begin()” to initialize the I2C communication. Use “Motor_Init()” to


bind and initialize the motor pins.

5.4 Main Function


When a button press is detected in the main function, the “Sensor_Receive()”
function will be called in a loop to obtain the values of the 4-ch line follower.
They are used for detecting the black line. After the values are determined,
control the car to move accordingly. Then, the “Task_Dispatcher()” function is
called to execute corresponding task.

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.

5.6 Task Dispatcher Function


In the “task_dispatcher ()” function, the value of the variable “modestate” will
execute the set task accordingly. The car is set to the line-following mode by
default. If the crossroad is detected, the car will perform the intersection
detection task.

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.

5.9 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

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.

After modifying, follow the “3.Program Download” to download the program


again.

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

You might also like