刚做好的超声雷达与大家分享一下!
刚做好的超声雷达与大家分享一下!http://www.robotway.com/bbs/forum.php?mod=viewthread&tid=2696
(出处: 探索者——探索与机器人有关的一切)
今天用超声波做了一个雷达探测实验,同学们毕业设计有没有想做倒车雷达的,可以参考一下,废话不多说,直接上代码
[*]#include <Servo.h>
[*]
[*]#define ECHOPIN A0
[*]#define TRIGPIN A1
[*]
[*]Servo myServo;
[*]int servo_port={4,9,10,3,6,5};
[*]
[*]void setup()
[*]{
[*]Serial.begin(9600);
[*]pinMode(ECHOPIN, INPUT);
[*]pinMode(TRIGPIN, OUTPUT);
[*]
[*]ServoGo(0,0);
[*]delay(100);
[*]}
[*]
[*]void loop()
[*]{
[*]int distance;
[*]
[*]for(int i=0; i<180; i++)
[*]{
[*] ServoGo(0, i);
[*]
[*] digitalWrite(TRIGPIN, LOW);
[*] delayMicroseconds(2);
[*] digitalWrite(TRIGPIN, HIGH);
[*] delayMicroseconds(10);
[*] digitalWrite(TRIGPIN, LOW);
[*] distance = pulseIn(ECHOPIN, HIGH, 14705);
[*] distance = (int)(distance/58);
[*]
[*] Serial.print(i);
[*] Serial.print(',');
[*] Serial.println(distance);
[*]
[*] delay(100);
[*]}
[*]for(int i=180; i>0; i--)
[*]{
[*] ServoGo(0, i);
[*]
[*] digitalWrite(TRIGPIN, LOW);
[*] delayMicroseconds(2);
[*] digitalWrite(TRIGPIN, HIGH);
[*] delayMicroseconds(10);
[*] digitalWrite(TRIGPIN, LOW);
[*] distance = pulseIn(ECHOPIN, HIGH, 14705);
[*] distance = (int)(distance/58);
[*]
[*] Serial.print(i);
[*] Serial.print(',');
[*] Serial.println(distance);
[*] delay(100);
[*]}
[*]
[*]while(1){
[*]}
[*]
[*]}
[*]
[*]void ServoStart(int which)
[*]{
[*]if(!myServo.attached())myServo.attach(servo_port);
[*]pinMode(servo_port, OUTPUT);
[*]}
[*]
[*]
[*]void ServoStop(int which)
[*]{
[*]myServo.detach();
[*]digitalWrite(servo_port,LOW);
[*]}
[*]
[*]void ServoGo(int which , int where)
[*]{
[*]//if(where<20)where=20;
[*]//if(where>160)where=160;
[*]if(where!=200)
[*]{
[*] if(where==201) ServoStop(which);
[*] else
[*] {
[*] ServoStart(which);
[*] myServo.write(where);
[*] }
[*]}
[*]}
[*]
[*]void ServoMove(int which, int start, int finish, int t)
[*]{
[*]int a;
[*]if((start-finish)>0) a=-1;
[*]else a=1;
[*]for(int i=0;i<abs(start-finish);i++) {ServoGo(which,start+i*a);delay(t/(abs(start-finish)));}
[*]}
[*]
复制代码
超声波的工作原理大家应该都懂得吧,具体的工作模块用的谁家的就不做广告了,不懂的可以跟帖交流!再附上实验模型一张
在processing上做了个简陋的雷达界面,显示雷达数据
processing的代码在这里
[*]import processing.serial.*;
[*]
[*]Serial myPort; // Create object from Serial class
[*]String inputString="";
[*]boolean stringComplete = false;
[*]int degree;
[*]int distance;
[*]int[] data = new int;
[*]
[*]void setup()
[*]{
[*]fill(0);
[*]size(500,500);
[*]background(255);
[*]myPort = new Serial(this, "COM52", 9600); //change the COM number to meet your computer
[*]noFill();
[*]//arc(250, 250, 500, 500, 0, PI);
[*]}
[*]
[*]void draw()
[*]{
[*]if ( myPort.available() > 0 )
[*]{
[*] char inChar = (char)myPort.read(); //get serial data
[*] inputString += inChar;
[*] if(inChar=='\n') stringComplete=true;
[*]}
[*]if(stringComplete)
[*]{
[*] background(255);
[*] stringComplete=false;
[*] text(inputString,10,10);
[*] Decoding();
[*] text(degree,10,20);
[*] text(distance,10,30);
[*] Radar();
[*] inputString="";
[*]}
[*]}
[*]
[*]void Decoding()
[*]{
[*]degree=int(inputString.substring(0,inputString.indexOf(',')));
[*]distance=int(inputString.substring(inputString.indexOf(',')+1,inputString.indexOf('\n')-1));
[*]if(distance>50)distance=50;
[*]if(distance<1)distance=50;
[*]if(degree>179)degree=179;
[*]data=distance;
[*]}
[*]
[*]void Radar()
[*]{
[*]for(int i=0;i<180;i++)
[*]{
[*] noFill();
[*] arc(250, 250, data*8, data*8, radians(i+180), radians(i+1+180));
[*]}
[*]}
复制代码
http://player.youku.com/player.php/sid/XNzM1NTgxMjQ0/v.swf
赞! 我前年做过一个,界面感觉比你这个好看些……那个看起来真的像是军用雷达少苗的图……而且还用LCD装载小车上显示了…… hp198969 发表于 2014-7-5 10:22
我前年做过一个,界面感觉比你这个好看些……那个看起来真的像是军用雷达少苗的图……而且还用LCD装载小车 ...
是吗?如果能分享一下就太好了。 walker无动力 发表于 2014-7-14 20:51
是吗?如果能分享一下就太好了。
Processing互动艺术这本书里面有一个雷达的例子,我当初就是按照这个来做的。两年前的事了……我写的那些代码都不知道丢哪儿了额…… 啊, 我这个界面也是参考这本书做的,不过为了赶时间,没有做好看,呵呵
页:
[1]