Robot car's code analysis of patrol line escape mode

First. Functional principle:

Install two vertical downward infrared obstacle avoidance modules on the front of the car as sensors for detecting black lines. When the sensor returns a level of 0, it indicates that it is a normal ground, and when the return level is 1, it indicates that it is a black line. Therefore, according to the state of the two detectors, determine the position of the current head of the roobot car body relative to the black line, and then correct the direction. Every round of endless loop will be corrected once, until the head is parallel to the black line and the detector is not triggered.

Second. Hardware connection

Install two vertical downward infrared obstacle avoidance modules on the front of the car. The transmitter head of the module is about 2cm away from the ground. Adjust the sensitivity of the infrared obstacle avoidance module so that when it is facing the black line, the indicator light at the rear of the sensor is off and facing Lights up when the ground is normal.

Install two vertical downward infrared obstacle avoidance modules on the front of the car. The transmitter head of the module is about 2cm away from the ground. Adjust the sensitivity of the infrared obstacle avoidance module so that when it is facing the black line, the indicator light at the rear of the sensor is off and facing Lights up when the ground is normal.

(The rear of the car faces the user) We connect the infrared sensor on the left side of the robot car to the P20 interface of the drive board, and the right side to the P23 interface.

 

Third. software writing

Open the lower computer program project with keil C V4, find the config.h configuration file, and add the IO pin configuration of the line tracking sensor in the configuration file:

1. /* Obstacle avoidance pin configuration */

2. sbit Input_Detect_LEFT=P2^0;          //Right probe

3. sbit Input_Detect_RIGHT=P2^3;         //Left prob

Copy code

Then open the motor.c file and add the line-following code to the file. First, define a variable to save the motion state of the robot, so that after deviating from the course, the robot will always make direction correction actions before returning to the correct direction.

1. uchar  Robots_Run_Status;

Copy code

Then add a line tracking logic function to realize the line tracking logic:

  1. //Line-followingmode
  2. void FollowLine(void)
  3. {
  4.                  switch(Robots_Run_Status)
  5.                {
  6.                       case 0x01:MOTOR_GO_RIGHT;     break;   
  7.                       case 0x02:MOTOR_GO_LEFT;     break;           
  8.                       case 0x03:MOTOR_GO_FORWARD;  break;           
  9.                       case 0x04:MOTOR_GO_STOP;  break;        
  10.                }
  11.                          if((Input_Detect_LEFT == 0)&& (Input_Detect_RIGHT == 0))//The detectors on both sides return to low level, indicating that the infrared sensors on the left and right sides have detected the ground and the robot is moving straight.
  12.                       {
  13.                                 Robots_Run_Status=0x03;//straight
  14.                       }
  15.                       
  16.                       if((Input_Detect_LEFT == 1)&& (Input_Detect_RIGHT == 0))//The left sensor returns to a high level, indicating that the left sensor has detected a black line. Compared with the black line, the overall direction of the robot has shifted to the right and needs to be corrected to the left.
  17.                       {
  18.                                Robots_Run_Status=0x02;//Turn left
  19.                       }
  20.              
  21.                       if((Input_Detect_LEFT == 0)&& (Input_Detect_RIGHT == 1))//The sensor on the right returns to a high level, indicating that the sensor on the right has detected a black line. Compared with the black line, the overall direction of the robot has shifted to the left and needs to be corrected to the right.
  22.                       {
  23.                                Robots_Run_Status=0x01;// turn right
  24.                       }
  25.                       
  26.                       if((Input_Detect_LEFT == 1)&& (Input_Detect_RIGHT == 1))//A black line is detected on both the left and right at the same time, indicating that there is a horizontal black line, and the robot stops at the end of the line inspection.

 

  1.                       {
  2.                                Robots_Run_Status=0x04;//停止stop
  3.                       }
  4. }

 

Copy code

Call the written line tracking function:

In the main function main.c, find the while(1) infinite loop, and add the line tracking function just written

  1. while(1)
  2.       {
  3.                   FollowLine();//Perform line inspection
  4.       }

 

Copy code

This is a relatively rudimentary way of patrolling. There is no problem with the black line that meets the general conditions. If you need a safety point, you can add several infrared obstacle avoidance modules to make multiple judgments more accurate.

DiySmart robot car

Lascia un commento

Tutti i commenti vengono moderati prima della pubblicazione