写一个arduino程序,控制车(四个电机驱动轮)实现循迹避障功能,要求如下: 1.两个红外传感器(TCRT5000)辅助车实现沿着单一黑色线行驶; 2.超声波传感器(HC-SR04)判断前方是否有障碍,如果有障碍,车子优先避障,然后绕过障碍物之后再次回到黑色线轨迹上(红外传感器辅助判断)。 3.配备光敏传感器控制LED灯,外界环境亮度不够时点亮LED。 备注:1.左边两个轮子同步运转,右边两个轮子运动同步,利用左右的差速实现车身偏转,四个轮子由电机驱动模块L9110驱动。 2.车身为30cm长,10cm宽,黑色轨迹为宽度为2cm的圆环,轨迹中心线的半径约1M
时间: 2023-05-24 18:04:00 浏览: 125
基于arduino的循迹避障小车
以下是示例arduino程序,可供参考:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Ultrasonic.h>
#include <QTRSensors.h>
// 配置传感器引脚
#define QTR_NUM_SENSORS 2
#define QTR_SENSOR_PIN_LEFT A0
#define QTR_SENSOR_PIN_RIGHT A1
#define ULTRASONIC_TRIGGER_PIN 2
#define ULTRASONIC_ECHO_PIN 3
#define LIGHT_SENSOR_PIN A2
// 定义电机驱动模块
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motor_left_1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor_left_2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor_right_1 = AFMS.getMotor(3);
Adafruit_DCMotor *motor_right_2 = AFMS.getMotor(4);
// 定义传感器
Ultrasonic ultrasonic_sensor(ULTRASONIC_TRIGGER_PIN, ULTRASONIC_ECHO_PIN);
QTRSensors qtr_sensors((unsigned char[]) {QTR_SENSOR_PIN_LEFT, QTR_SENSOR_PIN_RIGHT}, QTR_NUM_SENSORS);
// 光敏传感器
int light_sensor_value;
// 车速(0-255)
const int MOTOR_SPEED = 200;
void setup() {
Serial.begin(9600);
AFMS.begin();
motor_left_1->setSpeed(MOTOR_SPEED);
motor_left_2->setSpeed(MOTOR_SPEED);
motor_right_1->setSpeed(MOTOR_SPEED);
motor_right_2->setSpeed(MOTOR_SPEED);
}
void loop() {
// 读取传感器数据
unsigned int sensor_values[QTR_NUM_SENSORS];
qtr_sensors.read(sensor_values);
int ultrasonic_distance = ultrasonic_sensor.MeasureInCentimeters();
light_sensor_value = analogRead(LIGHT_SENSOR_PIN);
// 判断前方是否有障碍物
if (ultrasonic_distance <= 10) { // 超声波传感器所能探测到的最短距离为2cm,为了避免误差可以设定为10cm
Serial.println("Obstacle detected, avoiding...");
motor_left_1->run(BACKWARD);
motor_left_2->run(BACKWARD);
motor_right_1->run(BACKWARD);
motor_right_2->run(BACKWARD);
delay(1000);
motor_left_1->run(BACKWARD);
motor_left_2->run(FORWARD);
motor_right_1->run(FORWARD);
motor_right_2->run(BACKWARD);
delay(1000);
motor_left_1->run(FORWARD);
motor_left_2->run(FORWARD);
motor_right_1->run(FORWARD);
motor_right_2->run(FORWARD);
} else { // 如果没有障碍物,则遵循黑线轨迹行驶
int left_speed = 0;
int right_speed = 0;
// 根据传感器读数确定左右轮子的车速
if (sensor_values[0] > 500 && sensor_values[1] > 500) {
// 黑线轨迹已经找到,电机前进
left_speed = MOTOR_SPEED;
right_speed = MOTOR_SPEED;
} else if (sensor_values[0] > sensor_values[1]) {
// 车向左偏,右轮加速,左轮减速
left_speed = MOTOR_SPEED * 0.7;
right_speed = MOTOR_SPEED * 1.3;
} else {
// 车向右偏,左轮加速,右轮减速
left_speed = MOTOR_SPEED * 1.3;
right_speed = MOTOR_SPEED * 0.7;
}
// 控制电机速度
if (left_speed > 0) {
motor_left_1->run(FORWARD);
motor_left_2->run(FORWARD);
motor_left_1->setSpeed(left_speed);
motor_left_2->setSpeed(left_speed);
} else {
motor_left_1->run(BACKWARD);
motor_left_2->run(BACKWARD);
motor_left_1->setSpeed(-left_speed);
motor_left_2->setSpeed(-left_speed);
}
if (right_speed > 0) {
motor_right_1->run(FORWARD);
motor_right_2->run(FORWARD);
motor_right_1->setSpeed(right_speed);
motor_right_2->setSpeed(right_speed);
} else {
motor_right_1->run(BACKWARD);
motor_right_2->run(BACKWARD);
motor_right_1->setSpeed(-right_speed);
motor_right_2->setSpeed(-right_speed);
}
}
// 光敏传感器控制LED灯
if (light_sensor_value < 100) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(50); // 等待50毫秒再重新读取传感器数据
}
阅读全文