雷達(dá)通常在軍事領(lǐng)域運(yùn)用較多,民用方面也有很多,在此就不一一舉例說明了。
本次設(shè)計(jì)主要是想通過紅外測距傳感器配合單片機(jī)系統(tǒng)實(shí)現(xiàn)“雷達(dá)”硬件,通過異步串口通信,將數(shù)據(jù)傳輸至PC,并通過Peocessing開發(fā)上位機(jī)“雷達(dá)”界面用以顯示。
在傳感器的測試文檔中,小楊通過矩形數(shù)量來顯示傳感器輸出值。
今天既然說是“雷達(dá)”,那小楊就模擬一個(gè)“雷達(dá)”界面。
雷達(dá)硬件組成:
老套路,發(fā)煙測試,通過最簡單的方法實(shí)現(xiàn)效果。
SHARP的GP2Y0A21YK0F傳感器性能穩(wěn)定,好用不貴,且不需要再設(shè)計(jì)外圍電路。
那剩下的就是具備ADC+串口通信功能的單片機(jī)系統(tǒng)咯,出于最簡單實(shí)現(xiàn)的初衷,小楊就用下面這款了:
開發(fā)板兼容ArduinoIDE編程。不在這里過多贅述,相關(guān)信息請參閱工作室的其他文檔。
整體連接如下:
忘了說,掃描需要云臺(tái),就用舵機(jī)簡單模擬一個(gè),關(guān)于舵機(jī)控制,請參考:
下面直接展示代碼:
// Visual Micro is in vMicro》General》Tutorial Mode
//
/*
Name: LeiDa.ino
Created: 2018/7/22 13:23:31
Author: 禾灮\HeGuang
*/
// Define User Types below here or use a .h file
//
#include 《Servo.h》。
// Define Function Prototypes that use User Types below here or use a .h file
//// 初始化設(shè)置
const int VoPin = 14;
long duration;
int distance;
Servo myServo; // Creates a servo object for controlling the servo motor
int Range_SHARP(){
distance = (1024 - duration)/10 - 50;
return distance;
}
// The setup() function runs once each time the micro-controller starts
void setup(){
pinMode(VoPin, INPUT);
Serial.begin(9600);
myServo.attach(10);
}
// Add the main program code into the continuous loop() function
void loop(){
for(int i=15;i《=165;i++){
myServo.write(i);
distance = Range_SHARP();
Serial.print(i);
Serial.print(“,”);
Serial.print(distance);
Serial.print(“?!保?
delay(50);
}
for(int i=165;i》15;i--){
myServo.write(i);
distance = Range_SHARP();
Serial.print(i);
Serial.print(“,”);
Serial.print(distance);
Serial.print(“?!保?
delay(50);
}
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
完成了這些準(zhǔn)備,下面就該上位機(jī)開發(fā)了。
雷達(dá)上位機(jī)
開發(fā)用到的軟件:Processing,一款非常好用的交互上位機(jī)設(shè)計(jì)軟件。
上位機(jī)界面如下:
不多說,直接上代碼:
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String angle=“”;
String distance=“”;
String data=“”;
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {
size (1200, 700); //這個(gè)分辨率自己根據(jù)你的電腦的配置和顯示屏幕配置進(jìn)行更改。
smooth();
myPort = new Serial(this,“COM7”, 9600); //這個(gè)串口號一定要更改。
myPort.bufferUntil(‘?!?
}
void draw() {
fill(98,245,31);
noStroke();
fill(0,4);
rect(0, 0, width, height-height*0.065);
fill(98,245,31);
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent (Serial myPort) {
data = myPort.readStringUntil(‘?!?
data = data.substring(0,data.length()-1);
index1 = data.indexOf(“,”);
angle= data.substring(0, index1);
distance= data.substring(index1+1, data.length());
iAngle = int(angle);
iDistance = int(distance);
}
void drawRadar() {
pushMatrix();
translate(width/2,height-height*0.074);
noFill();
strokeWeight(2);
stroke(98,245,31);
// draws the arc lines
arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
// draws the angle lines
line(-width/2,0,width/2,0);
line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
line((-width/2)*cos(radians(30)),0,width/2,0);
popMatrix();
}
void drawObject() {
pushMatrix();
translate(width/2,height-height*0.074);
strokeWeight(9);
stroke(255,10,10); // red color
pixsDistance=iDistance*((height-height*0.1666)*0.025);
if(iDistance《40){
line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
}
popMatrix();
}
void drawLine() {
pushMatrix();
strokeWeight(9);
stroke(30,250,60);
translate(width/2,height-height*0.074);
line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle)));
popMatrix();
}
void drawText() {
pushMatrix();
if(iDistance》40) {
noObject = “Out of Range”;
}else {
noObject = “In Range”;
}
fill(0,0,0);
noStroke();
rect(0, height-height*0.0648, width, height);
fill(98,245,31);
textSize(25);
text(“10cm”,width-width*0.3854,height-height*0.0833);
text(“20cm”,width-width*0.281,height-height*0.0833);
text(“30cm”,width-width*0.177,height-height*0.0833);
text(“40cm”,width-width*0.0729,height-height*0.0833);
textSize(28);
text(“Object: ” + noObject, width-width*0.875, height-height*0.0277);
text(“Angle: ” + iAngle +“ °”, width-width*0.48, height-height*0.0277);
text(“Distance: ”, width-width*0.26, height-height*0.0277);
if(iDistance《40) {
text(“ ” + iDistance +“cm”, width-width*0.225, height-height*0.0277);
}
textSize(25);
fill(98,245,60);
translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
rotate(-radians(-60));
text(“30°”,0,0);
resetMatrix();
translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
rotate(-radians(-30));
text(“60°”,0,0);
resetMatrix();
translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
rotate(radians(0));
text(“90°”,0,0);
resetMatrix();
translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
rotate(radians(-30));
text(“120°”,0,0);
resetMatrix();
translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
rotate(radians(-60));
text(“150°”,0,0);
popMatrix();
}