极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16230|回复: 6

谁能帮我看下这个两个超声波传感器计算偏转角度的函数哪里错了,应该怎么改?

[复制链接]
发表于 2015-5-9 09:50:21 | 显示全部楼层 |阅读模式
本帖最后由 唐唐llll 于 2015-5-9 19:02 编辑
  1. const int Ltrigpin=5;
  2. const int Lechopin=4;
  3. const int Rtrigpin=3;
  4. const int Rechopin=2;
  5. float Between=4.50;
  6. float Distance,Ldistance,Rdistance,Angle;

  7. void setup()
  8. {
  9.   Serial.begin(9600);
  10.   pinMode(Ltrigpin,OUTPUT);
  11.   pinMode(Rtrigpin,OUTPUT);
  12.   pinMode(Lechopin,INPUT);
  13.   pinMode(Rechopin,INPUT);  //定义端口输入输出

  14. }
  15. float distance(float a,float b,float c)
  16. {
  17.   float d;
  18.   d=sqrt((a*a/2)+(c*c/2)-(b*b/4));
  19.   return d;
  20. }//长度函数
  21. float angle(float b,float c,float d)
  22. {
  23.   float angle;
  24.   angle=90-acos((c*c)+(d*d)-(b*b/4)/b*d);
  25.   return angle;
  26. }//角度函数
  27. void loop()
  28. {
  29.     digitalWrite(Ltrigpin,LOW);
  30.     delayMicroseconds(2);
  31.     digitalWrite(Ltrigpin,HIGH);
  32.     delayMicroseconds(10);
  33.     digitalWrite(Ltrigpin,LOW);
  34.     Ldistance=pulseIn(Lechopin,HIGH)/58.00;//左传感器距离
  35.     delayMicroseconds(1500);
  36.     digitalWrite(Rtrigpin,LOW);
  37.     delayMicroseconds(2);
  38.     digitalWrite(Rtrigpin,HIGH);
  39.     delayMicroseconds(10);
  40.     digitalWrite(Rtrigpin,LOW);
  41.     Rdistance=pulseIn(Rechopin,HIGH)/58.00;//右传感器距离
  42.     Distance=distance(Rdistance,Between,Ldistance);
  43.     Angle=angle(between,Ldistance,Distance);
  44.     Serial.print(Ldistance);
  45.     Serial.println();
  46.     Serial.print(Rdistance);
  47.     Serial.print();
  48.     Serial.print(Distance);
  49.     Serial.print();
  50.     Serial.print(Angle);
  51.     Serial.println();
  52.     Serial.println();
  53.     delay(1500);
  54.    
复制代码
回复

使用道具 举报

 楼主| 发表于 2015-5-9 09:51:12 | 显示全部楼层
本帖最后由 唐唐llll 于 2015-5-9 19:04 编辑

错误已经解决了,但是又有一个问题。
两个传感器之间的延迟只能在1000ms以上是怎么回事?如果调小了就会互相影响。但我的测量距离才不到1m而已。
回复 支持 反对

使用道具 举报

发表于 2015-5-9 21:45:07 | 显示全部楼层
超声波传感器可以测量角度?我只知道测距
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-11 17:06:52 | 显示全部楼层
suoma 发表于 2015-5-9 21:45
超声波传感器可以测量角度?我只知道测距

两个并排的传感器通过距离差可以计算出角度。
回复 支持 反对

使用道具 举报

发表于 2015-5-11 17:24:52 | 显示全部楼层
唐唐llll 发表于 2015-5-11 17:06
两个并排的传感器通过距离差可以计算出角度。

那就是两个可以当一个角度传感器
回复 支持 反对

使用道具 举报

发表于 2015-5-11 17:25:22 | 显示全部楼层
期待你分享原理图
回复 支持 反对

使用道具 举报

发表于 2016-9-6 12:00:42 | 显示全部楼层
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-5-5 01:24 , Processed in 0.045566 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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