极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10935|回复: 4

平衡球與超音波

[复制链接]
发表于 2017-7-25 09:57:09 | 显示全部楼层 |阅读模式
最近做了一個平衡球的實驗,但超音波讀取的值不是很正確。當超音波打到物體時,實際是1cm但超音波會讀到30,想問這是什麼問題?
如果想讓誤判30cm的值變成1cm,程式該怎麼修??

#include<Servo.h>
#include<PID_v1.h>
const int servoPin = 9;     //Servo Pin
float Kp = 1.8;                                                
float Ki = 0.075;                                                      
float Kd = 1.1;
double Setpoint, Input, Output, ServoOutput;                                       
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);                                                                                                                                                                                                                                                                                          
Servo myServo;                                                      

void setup() {

  Serial.begin(9600);                                               
  myServo.attach(servoPin);                                          
  Input = readPosition();                                                                                                                                                                           
  myPID.SetMode(AUTOMATIC);                                       
  myPID.SetOutputLimits(-40,40);                                    
}

void loop()
{

  Setpoint = 15;
  Input = readPosition();                                            

  myPID.Compute();                                                
myServo.write(93+ Output);                                       


}

float readPosition() {
  delay(25);                                                         


const int pingPin = 11;
const int pingPin2 = 12;

long duration, cm;
unsigned long now = millis();
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin2, INPUT);
  duration = pulseIn(pingPin2, HIGH);
cm = duration*(0.01724);



  if(cm > 26)   cm=26;



  Serial.println(cm);

  return cm;                                         

}
回复

使用道具 举报

发表于 2017-7-25 10:02:58 | 显示全部楼层
平衡球的實驗 是什么? 有图么?
回复 支持 反对

使用道具 举报

发表于 2017-7-27 09:15:57 | 显示全部楼层
正常的超声波模块应该使用10~25uS的脉冲触发,所以拉低一会儿之后需要将电平拉高10~20uS。
所以delayMicroseconds(5)改为delayMicroseconds(25)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-27 20:29:01 | 显示全部楼层
本帖最后由 rn9409 于 2017-7-27 20:33 编辑


這是我做的結構

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2017-7-28 01:46:35 | 显示全部楼层
你確定你的超聲波模塊, 最短距離可以達到 1cm 嗎?  一般都是 2cm 以上的.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 05:11 , Processed in 0.052500 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表