简体   繁体   English

arduino uno和超声波传感器的结果不正确

[英]Inaccurate results for arduino uno and ultrasonic sensor

Good day, I have the following code for an ultrasonic sensor that I'm using, but the result is not being accurate and I'd like to display the distance from the target in centimetres. 美好的一天,我有一个下面的代码用于我正在使用的超声波传感器,但结果并不准确,我想以厘米为单位显示距目标的距离。

    #include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;    //Convert miles to inches by multipling by 63360 (inches per mile)

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" inches");  //Print your units.
  delay(250); //pause to let things settle


  }  

I will appreciate any help. 我将不胜感激。

You are performing a lot of floating point calculations on every iteration of your loop . 您对您的每次迭代进行大量的浮点运算的loop Rather than performing these calculations you can pre-compute a single multiplier that will convert the pingTime to a distance in centimetres. 除了执行这些计算,您还可以预先计算一个乘数,该乘数会将pingTime转换为以厘米为单位的距离。

First we need to calculate the metric speed of sound from your 776.5 mph. 首先,我们需要从您的776.5英里/小时计算声速。

776.5 miles/hour * 1609.34 (meters in a mile) = 1249652.51 metres/hour 776.5英里/小时* 1609.34(英里)= 1249652.51米/小时

1249652.51 metres/hour / 3600 = 347.126 metres/second 1249652.51米/小时/ 3600 = 347.126米/秒

So we can precompute a multiplier as follows: 因此我们可以按如下方式预先计算一个乘数:

float speedOfSound = 347.126; // metres per second
float speedOfSound_cm_p_us = speedOfSound * 100 / 1000000; // speedOfSound in centimetres per microsecond

Obviously, this could be reduced to simply: 显然,这可以简化为:

float speedOfSound_cm_p_us = 0.0347126; // cm per microsecond

Next we can precompute the multiplier by dividing speedOfSound_cm_p_us by 2 as the sound has to travel to the reflective surface and back again. 接下来,我们可以通过将speedOfSound_cm_p_us除以2来预先计算乘数,因为声音必须传播到反射表面并再次传播。

float pingTime_multiplier = speedOfSound_cm_p_us / 2; // The sound travels there and back so divide by 2.

Again, we could simply replace this step with: 同样,我们可以简单地用以下步骤替换此步骤:

float pingTime_multiplier = 0.0173563;

However, this magic number should be well documented in your code. 但是,此数应该在您的代码中有据可查。

Once we have pingTime_multiplier calculated the loop function becomes much simpler. 一旦计算出pingTime_multiplierloop函数就会变得更加简单。 Simply multiply the pingTime by the pingTime_multiplier to get the distance in centimetres. 只需将pingTime乘以pingTime_multiplier即可得出以厘米为单位的距离。

pingTime = pulseIn(echoPin, HIGH);  // pingTime is presented in microseconds
targetDistance = pingTime * pingTime_multiplier;

This significantly reduces the amount of work that the Arduino has to do each time through the loop. 这显着减少了Arduino在整个循环中每次要做的工作量。

I'd like to display the distance from the target in centimetres. 我想以厘米为单位显示距目标的距离。

Google says an inch is 2.54 cm, so just multipy your result by this. Google表示,一英寸为2.54厘米,因此只需乘以您的结果即可。

the result is not being accurate 结果不准确

For more accurate results, take an average of a number of samples (3 or more), and you could implement some heuristics which discard grossly over/under range results. 为了获得更准确的结果,请平均抽取多个样本(3个或更多),然后可以实施一些启发式方法,以完全丢弃超出范围的结果。

Say if three readings are around 0-5cm apart, then a fourth reading 40cm away is most likely an error. 假设三个读数相距0-5厘米左右,那么距离40厘米的第四个读数很可能是一个错误。 So just do the average of the three similar results. 因此,只需对三个相似结果进行平均即可。

Convert inches to centimeters by multiplying by 2.54 乘以2.54将英寸转换为厘米

float targetDistanceCentimeters = targetDistance*2.54;

Your code looks correct, so my guess is that this is a hardware problem. 您的代码看起来正确,所以我的猜测是这是硬件问题。 In my experience ultra sound sensors are typically quite bad at measuring distance to any object that is not a wall or other large object with relatively flat surface. 以我的经验,超声波传感器通常很难测量到与不是墙壁的其他物体或具有相对平坦表面的其他物体的距离。 So if you want to test the accuracy of your system, test it against such object ie wall or book. 因此,如果要测试系统的准确性,请针对诸如墙或书本之类的物体进行测试。 If your sensor is moving while making the measurements make sure that it is not moving too fast. 如果您的传感器在测量时正在移动,请确保它的移动速度不太快。 If you are measuring distance to small or thin object you will need to filter and take the average of the measurements. 如果要测量到小物体或薄物体的距离,则需要过滤并取平均值。 It's all up to you how accurate you want to be, I'd say that average of twenty or so of filtered measurements will give you appropriate result. 这完全取决于您要达到的精度,我想说,平均大约二十次过滤后的测量将为您提供适当的结果。

Another things to consider is that you have the correct supply voltage and that the sensor is not directed against the floor at some angle, as this might give unwanted reflections. 要考虑的另一件事是,您具有正确的电源电压,并且传感器没有以一定角度对准地板,因为这可能会产生不必要的反射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM