首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于超声波传感器的机器人导航

基于超声波传感器的机器人导航
EN

Stack Overflow用户
提问于 2022-09-17 19:12:36
回答 1查看 48关注 0票数 -1

我正在建造一个简单的机器人,使用Arduino Uno,Arduino电机屏蔽,2台直流电机(这是我使用的https://www.aliexpress.com/item/33026775429.html)和2台12V电池。我有机器人控制两个电机,但我也希望它导航使用SR04超声波传感器。我已经把两个场景放入了空环,如果超声波传感器测量的距离大于40厘米,它就会向前推进,否则它应该反转并改变方向。然而,当我的代码运行时,所发生的一切都是我的一个马达完全静止的,而另一个马达则定期地来回跳动。我试过对代码做很多修改,但结果总是一样的。请看一看我写的代码,并解释我哪里出错了。当我编写代码来控制马达时,它们都能工作,当连接到超声波传感器时,两个马达都起作用了,但是一旦传感器发生变化,所有的东西都会停止运转。这是我的密码:

代码语言:javascript
复制
const int brakeleft = 9; //identifies pin 9
const int speedleft = 3; //identifies pin 3
const int directionright = 13; //identifies pin 13
const int brakeright = 8; //identifies pin 8
const int speedright = 11; //identifies pin 11
const int trigPin = 7; //identifies pin 7
const int echoPin = 6; //identifies pin 6
long duration; //creates a variable under which the duration between emission and reception of sound can be stored
int distance;

void setup() {
pinMode(trigPin, OUTPUT); //assigns a role to the pin we will be emitting sound on
pinMode(echoPin, INPUT); //assigns a role to the pin that will be reporting back the return of that sound wave 
pinMode(brakeleft, OUTPUT); //assigns a role to the pin that will be stopping motor A
pinMode(directionleft, OUTPUT); //assigns a role to the pin that will be deciding motor A's direction
pinMode(speedleft, OUTPUT); //assigns a role to the pin that will be deciding motor A's speed
pinMode(brakeright, OUTPUT); //assigns a role to the pin that will be stopping motor B
pinMode(directionright, OUTPUT); //assigns a role to the pin that will be deciding motor B's direction
pinMode(speedright, OUTPUT); //assigns a role to the pin that will be deciding motor B's speed
}

void loop() {
  
digitalWrite(trigPin, LOW); 
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); //emits ultrasonic pulse
delayMicroseconds(10); 
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH); //recieves the echo of the ultrasonic pulse
distance = duration * 0.01715; //converts duration for sound to echo into a distance in cm, based on the speed of light, half of 0.0343 cm per second

if (distance > 40) {
  delay(10);
  digitalWrite(directionleft, HIGH); //Make motor A spin forwards
  digitalWrite(directionright, HIGH); //Make motor B spin forwards
  digitalWrite(brakeleft, LOW); //Disengage the brake for motor A
  digitalWrite(brakeright, LOW); //Disengage the brake for motor B
  analogWrite(speedleft, 50); //Spins the motor A at a low speed
  analogWrite(speedright, 50); //Spins motor B at a low speed
  delay(3000); 
} 
else {
  delay(10);
  digitalWrite(directionleft, LOW);
  digitalWrite(directionright, LOW);
  digitalWrite(brakeleft, LOW);
  digitalWrite(brakeright, LOW);
  analogWrite(speedleft, 50);
  analogWrite(speedright, 50);
  delay(2000);
  analogWrite(brakeright, HIGH);
  analogWrite(speedright, 0);
  digitalWrite(directionleft, LOW);
  digitalWrite(brakeleft, LOW);
  analogWrite(speedleft, 50);
  delay(2000);
}
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-17 19:26:24

我认为这是由于这些延迟(10)而发生的。试着把那些评论掉,然后再试一次,事实上,这些都是不必要的。我希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73757792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档