Welcome to Roboduino V2 repository
01.Car tracking
Car tracking
Car trackingDevice connectionHardware connectionSoftware connectionImplementation ideasCode analysisExperimental resultsNotes
The K1 button is used to control the on and off of the car's tracking (line patrol) function. The environment for debugging the car code is to follow the black line under a white background.
xxxxxxxxxxSince the previous tutorial has explained the basic knowledge of all car modules, the car control tutorial will not be repeated, and the main focus is on the car function implementation ideas!
Device connection
Hardware connection
Use Type-B data cable to connect Arduino Uno and computer.
Software connection
Open the "Arduino IDE" software and select the model and serial port number corresponding to the development board.
Implementation ideas
Use the three-way line patrol module to obtain the position of the car relative to the black route, and control the movement of the car according to this position.
Button K1: turn on or off the car tracking function;
Three-way patrol module: get the position of the car head relative to the black line;
Car control:
The three-way patrol module is located on the left side of the black line, and the car turns left;
The three-way patrol module is located in the middle of the black line, and the car goes straight;
The three-way patrol module is located on the right side of the black line, and the car turns right.
xxxxxxxxxxThe actual code also analyzes the situation where the three-way patrol module detects all black lines, and the code controls the forward movement in this situation.
Code analysis
Here is only a brief introduction to the code content. For detailed code, please refer to the corresponding code file, which is provided in the download area!
Include
Wire、Adafruit_PWMServoDriverlibrary
x#include <Wire.h> // 包含Wire(I2C)通讯库 Include Wire library#include <Adafruit_PWMServoDriver.h> // 包含Adafruit PWMServoDriver库 Include Adafruit PWMServoDriver library
Define the motor, button, tracking control pin, I2C address, motor frequency and initial speed
x// 定义电机控制引脚 Define motor control pins#define Motor_L1_F_PIN 11 // 控制小车左前方电机前进 Control the motor on the left front of the car#define Motor_L1_B_PIN 10 // 控制小车左前方电机后退 Control the motor back on the left front of the car#define Motor_L2_F_PIN 8 // 控制小车左后方电机前进 Control car left rear motor forward#define Motor_L2_B_PIN 9 // 控制小车左后方电机后退 Control the car left rear motor back#define Motor_R1_F_PIN 13 // 控制小车右前方电机前进 Control the right front motor of the car to move forward#define Motor_R1_B_PIN 12 // 控制小车右前方电机后退 Control the motor back on the right front of the car#define Motor_R2_F_PIN 14 // 控制小车右后方电机前进 Control car right rear motor forward#define Motor_R2_B_PIN 15 // 控制小车右后方电机后退 Control car right rear motor back// 定义底层驱动芯片参数 Bottom-layer driver chip related parameters#define Bottom_Layer_Driver_ADDR 0x40// 定义PWM频率 Define PWM frequency#define PWM_FREQUENCY 50// 定义按键引脚和控制状态 Define pin and key(button) states#define KEY_PIN 7#define Press_KEY 0#define Release_KEY 1// 定义三路循迹模块引脚 Define the three-way line patrol module pins#define L_TRACK_PIN A2#define M_TRACK_PIN A1#define R_TRACK_PIN A0// 定义巡线阈值 Define the patrol thresholdconst int Threshold = 500;bool bCar_Switch = false;int iCarSpeed = 50;unsigned int uTimeOut = 0;
Enumerate the common movement modes of omnidirectional cars
x// 枚举全向小车的常见运动方式 Enumerate the common movement modes of omnidirectional carsenum OmniDirectionalCar {STOP,FORWARD,BACKWARD,LEFT,RIGHT,LEFT_ROTATE,RIGHT_ROTATE,LEFT_FORWARD,RIGHT_BACKWARD,RIGHT_FORWARD,LEFT_BACKWARD,};
Create an instance of the Adafruit_PWMServoDriver class
x// 创建Adafruit_PWMServoDriver类的实例 Create an instance of the Adafruit_PWMServoDriver classAdafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(Bottom_Layer_Driver_ADDR);
Setting the Motor Speed
x/*** @brief 设置单个电机速度 Setting the Motor Speed* @param motor_forward_pin: 控制电机前进引脚 Control the motor forward pin* @param motor_backward_pin: 控制电机后退引脚 Control the motor backward pin* @param motor_speed: 设置电机速度 Setting the Motor Speed* @retval 无 None*/void setMotorSpeed(uint16_t motor_forward_pin, uint16_t motor_backward_pin, int motor_speed) {motor_speed = map(motor_speed, -255, 255, -4095, 4095);if (motor_speed >= 0) {pwm.setPWM(motor_forward_pin, 0, motor_speed);pwm.setPWM(motor_backward_pin, 0, 0);} else if (motor_speed < 0) {pwm.setPWM(motor_forward_pin, 0, 0);pwm.setPWM(motor_backward_pin, 0, -(motor_speed));}}
Set the car movement mode and speed
x/*** @brief 设置小车运动方式和速度 Set the car movement mode and speed* @param Movement: 小车运动方式 Car movement* @param Speed: 小车运动速度 Car speed* @retval 无 None*/void setCarMove(uint8_t Movement, int Speed) {switch (Movement) {case STOP:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, 0);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, 0);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, 0);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, 0);break;case FORWARD:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, Speed);break;case BACKWARD:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, -Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, -Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, -Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, -Speed);break;case LEFT:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, -Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, -Speed);break;case RIGHT:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, -Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, -Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, Speed);break;case LEFT_ROTATE:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, -Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, -Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, Speed);break;case RIGHT_ROTATE:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, -Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, -Speed);break;case LEFT_FORWARD:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, 0);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, 0);break;case RIGHT_BACKWARD:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, 0);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, -Speed);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, -Speed);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, 0);break;case RIGHT_FORWARD:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, 0);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, 0);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, Speed);break;case LEFT_BACKWARD:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, -Speed);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, 0);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, 0);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, -Speed);break;default:setMotorSpeed(Motor_L1_F_PIN, Motor_L1_B_PIN, 0);setMotorSpeed(Motor_L2_F_PIN, Motor_L2_B_PIN, 0);setMotorSpeed(Motor_R1_F_PIN, Motor_R1_B_PIN, 0);setMotorSpeed(Motor_R2_F_PIN, Motor_R2_B_PIN, 0);break;}}
Get key(button) status
x/*** @brief 获取按键状态 Get key(button) status* @param pin: 按键控制引脚 Control key(button) pins* @retval 按键状态 Key(button) Status*/int getKeyState(uint8_t pin) {if (digitalRead(pin) == LOW) {delay(20);if (digitalRead(pin) == LOW) {while (digitalRead(pin) == LOW);return Press_KEY;}