arduino超声波控制舵机避障小车
时间: 2024-07-29 12:01:01 浏览: 149
Arduino超声波控制舵机避障小车是一种常见的机器人项目,通常用于教学和初级电子制作。它结合了Arduino Uno或类似微控制器、超声波传感器以及舵机技术。以下是基本步骤:
1. **硬件组成**:
- Arduino主板作为控制系统:负责接收信号并处理数据。
- 超声波传感器:检测小车前方障碍物的距离。
- 舵机:用于控制小车的方向,使其能够避开障碍。
2. **软件部分**:
- 使用Arduino IDE编写程序,设置超声波传感器读取频率,并处理接收到的数据(如计算最短距离)。
- 当检测到前方有障碍物时,通过控制舵机的角度改变小车的方向,通常是向后或侧边移动,避免碰撞。
3. **流程示例**:
a. 初始化超声波传感器和舵机。
b. 循环测量距离,如果小于预设的安全阈值,则转动舵机,调整行驶方向。
c. 若无障碍,继续前进。
相关问题
arduino超声波控制舵机小车
### Arduino 超声波传感器控制舵机驱动小车的实现
#### 硬件需求
构建一个基于Arduino的超声波传感器控制的小车需要以下硬件组件[^1]:
- **Arduino UNO开发板**: 主控芯片,用于处理数据和逻辑运算。
- **L298N电机驱动模块**: 用于驱动直流电机。
- **超声波传感器(HC-SR04)**: 测量与障碍物之间的距离。
- **两个直流电机**: 提供动力使小车移动。
- **四个轮子**: 支撑小车并提供运动能力。
- **电池盒和电池**: 给整个系统供电。
- **面包板和杜邦线**: 连接各个电子元件。
#### 工作原理
该系统的运作依赖于超声波测距技术来检测周围环境中的障碍物位置。具体来说,在正常状态下,小车沿着预定轨迹向前行驶;一旦发现正前方存在低于预设阈值的距离内的物体,则触发避障机制。此时,舵机会带动安装在其上的超声波探头分别向右侧45度角以及左侧135度角扫描,并将获取的数据反馈至Arduino控制器上进行分析比较,从而决定最佳绕行路径[^3]。
值得注意的是,在每次改变方向之前都需要给予舵机足够的停顿时间(大约半秒钟),以便其完全旋转到位后再执行测量操作,这样可以获得更加精确的结果。
以下是完整的程序清单,您可以将其复制粘贴到官方推荐使用的集成开发环境中去测试您的设备性能:
```cpp
#include <Servo.h>
// 定义引脚编号
const int trigPin = 9; // Trig Pin of Ultrasonic Sensor connected to Digital pin 9 on the board.
const int echoPin = 10; // Echo Pin of Ultrasonic Sensor connected to Digital pin 10 on the board.
int leftMotorSpeedControlPin = 5;
int rightMotorSpeedControlPin = 6;
int motorLeftForward = 7;
int motorRightForward = 8;
long duration;
float distanceFront, distanceLeft, distanceRight;
Servo servoTilt;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(motorLeftForward, OUTPUT);
pinMode(motorRightForward, OUTPUT);
pinMode(leftMotorSpeedControlPin, OUTPUT);
pinMode(rightMotorSpeedControlPin, OUTPUT);
servoTilt.attach(3); // Attaches Servo Object to digital pin 3
}
void loop(){
checkDistance();
if (distanceFront >= 20){
moveStraight(); // If no obstacle detected within safe range then go straight ahead.
}
else{
stopMotors(); // Stop all motors first before deciding next action.
delay(500); // Wait half a second after stopping so that servos can settle down properly
scanAndDecidePath(); // Function call which scans surroundings and decides safest path forward based upon readings obtained from ultrasonicsensor mounted over rotating base provided by attachedservomechanism.
}
}
void checkDistance(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceFront = duration * 0.034 / 2 ;
}
void moveStraight(){
analogWrite(leftMotorSpeedControlPin , 200 );
analogWrite(rightMotorSpeedControlPin , 200 );
digitalWrite(motorLeftForward,HIGH);
digitalWrite(motorRightForward,HIGH);
}
void turnRight(){
analogWrite(leftMotorSpeedControlPin , 200 );
analogWrite(rightMotorSpeedControlPin , 100 );
digitalWrite(motorLeftForward,HIGH);
digitalWrite(motorRightForward,LOW);
}
void turnLeft(){
analogWrite(leftMotorSpeedControlPin , 100 );
analogWrite(rightMotorSpeedControlPin , 200 );
digitalWrite(motorLeftForward,LOW);
digitalWrite(motorRightForward,HIGH);
}
void reverseDirection(){
analogWrite(leftMotorSpeedControlPin , 200 );
analogWrite(rightMotorSpeedControlPin , 200 );
digitalWrite(motorLeftForward,LOW);
digitalWrite(motorRightForward,LOW);
}
void stopMotors(){
digitalWrite(motorLeftForward,LOW);
digitalWrite(motorRightForward,LOW);
}
void scanAndDecidePath(){
float distances[3];
servoTilt.write(90); // Center Position For Front Measurement
delay(500);
checkDistance();
distances[0]=distanceFront;
servoTilt.write(45); // Right Side Scan At Angle Of Forty Five Degrees From The Central Line
delay(500);
checkDistance();
distances[1]=distanceFront;
servoTilt.write(135); // Left Side Scan At An Angle One Hundred Thirty-Five Degree Relative To Straight Ahead Direction
delay(500);
checkDistance();
distances[2]=distanceFront;
if(distances[1]>distances[2]){
turnRight(); // Choose turning towards side having greater clearance space available according to measured values returned earlier during scanning phase conducted via servo-driven ultrasound emitter/receiver unit combination arrangement set up previously inside this function itself already explained above under respective sections dedicated thereto accordingly likewise also mentioned somewhere else too probably but anyway yeah sure why not let's just keep going shall we?
}
else if(distances[2]>=distances[1]){
turnLeft();
}
}
```
此代码实现了基本的功能描述,即利用HC-SR04型号的超音波感测器搭配伺服马达共同作用下达成自主导航功能的目的[^2].
arduino无舵机避障小车
以下是一个基于Arduino的无舵机避障小车的代码示例:
```arduino
// 引入LCD库
#include <LiquidCrystal.h>
// 定义LCD引脚
LiquidCrystal lcd(11, 2, 3, 4, 7, 8);
// 定义超声波引脚
#define trigPin 12
#define echoPin 13
// 定义电机引脚
#define L_motor_go 5
#define L_motor_back 6
#define R_motor_go 9
#define R_motor_back 10
// 定义超声波测距函数
int Distance_test()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int distance = pulseIn(echoPin, HIGH) / 58.00;
return distance;
}
// 定义小车前进函数
void Forward()
{
digitalWrite(L_motor_go, HIGH);
digitalWrite(L_motor_back, LOW);
digitalWrite(R_motor_go, HIGH);
digitalWrite(R_motor_back, LOW);
}
// 定义小车后退函数
void Backward()
{
digitalWrite(L_motor_go, LOW);
digitalWrite(L_motor_back, HIGH);
digitalWrite(R_motor_go, LOW);
digitalWrite(R_motor_back, HIGH);
}
// 定义小车左转函数
void Left()
{
digitalWrite(L_motor_go, LOW);
digitalWrite(L_motor_back, HIGH);
digitalWrite(R_motor_go, HIGH);
digitalWrite(R_motor_back, LOW);
}
// 定义小车右转函数
void Right()
{
digitalWrite(L_motor_go, HIGH);
digitalWrite(L_motor_back, LOW);
digitalWrite(R_motor_go, LOW);
digitalWrite(R_motor_back, HIGH);
}
// 定义小车停止函数
void Stop()
{
digitalWrite(L_motor_go, LOW);
digitalWrite(L_motor_back, LOW);
digitalWrite(R_motor_go, LOW);
digitalWrite(R_motor_back, LOW);
}
void setup()
{
// 初始化LCD
lcd.begin(16, 2);
// 初始化电机引脚
pinMode(L_motor_go, OUTPUT);
pinMode(L_motor_back, OUTPUT);
pinMode(R_motor_go, OUTPUT);
pinMode(R_motor_back, OUTPUT);
// 初始化超声波引脚
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// 获取当前距离
int distance = Distance_test();
// 显示当前距离
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
// 如果距离小于10cm,后退
if (distance < 10)
{
Backward();
delay(500);
Left();
delay(1000);
}
// 如果距离小于20cm,左转
else if (distance < 20)
{
Left();
delay(1000);
}
// 如果距离小于30cm,前进
else if (distance < 30)
{
Forward();
}
// 如果距离大于30cm,右转
else
{
Right();
delay(1000);
}
}
```
阅读全文
相关推荐
















