|
刚做好的超声雷达与大家分享一下!
http://www.robotway.com/bbs/forum.php?mod=viewthread&tid=2696
(出处: 探索者——探索与机器人有关的一切)
今天用超声波做了一个雷达探测实验,同学们毕业设计有没有想做倒车雷达的,可以参考一下,废话不多说,直接上代码
- #include <Servo.h>
- #define ECHOPIN A0
- #define TRIGPIN A1
- Servo myServo[6];
- int servo_port[6]={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[which].attached())myServo[which].attach(servo_port[which]);
- pinMode(servo_port[which], OUTPUT);
- }
- void ServoStop(int which)
- {
- myServo[which].detach();
- digitalWrite(servo_port[which],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[which].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)));}
- }
[color=rgb(51, 102, 153) !important]复制代码
超声波的工作原理大家应该都懂得吧,具体的工作模块用的谁家的就不做广告了,不懂的可以跟帖交流!再附上实验模型一张
在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[180];
- 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[degree]=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));
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
[color=rgb(51, 102, 153) !important]
|
|