此次设计用到的红外测距传感器是夏普(SHARP)的GP2Y0A21YK0F;传感器测试相关文档请阅读:
概述
雷达通常在军事领域运用较多,民用方面也有很多,在此就不一一举例说明了。
本次设计主要是想通过红外测距传感器配合单片机系统实现“雷达”硬件,通过异步串口通信,将数据传输至PC,并通过Peocessing开发上位机“雷达”界面用以显示。
在传感器的测试文档中,小楊通过矩形数量来显示传感器输出值。
今天既然说是“雷达”,那小楊就模拟一个“雷达”界面。
雷达硬件组成:
老套路,发烟测试,通过最简单的方法实现效果。
SHARP的GP2Y0A21YK0F传感器性能稳定,好用不贵,且不需要再设计外围电路。
那剩下的就是具备ADC+串口通信功能的单片机系统咯,出于最简单实现的初衷,小楊就用下面这款了:
开发板兼容ArduinoIDE编程。不在这里过多赘述,相关信息请参阅工作室的其他文档。
整体连接如下:
忘了说,扫描需要云台,就用舵机简单模拟一个,关于舵机控制,请参考:
下面直接展示代码:
// 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
//// 初始化设置
const int VoPin = 14;
long duration;
int distance;
Servo myServo; // Creates a servo object for controlling the servo motor
int Range_SHARP(){
duration = analogRead(VoPin);
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);
}
}
完成了这些准备,下面就该上位机开发了。
雷达上位机
开发用到的软件:Processing,一款非常好用的交互上位机设计软件。
上位机界面如下:
不多说,直接上代码:
import processing.serial