Arduino与Proteus仿真实例-雷达扫描仿真

雷达扫描仿真

本文将通过超声波传感器和舵机模拟雷达扫描仿真。舵机机带动超声波传感器进行周期性扫描,然后将扫描结果实时绘制在LCD中。

在前面的文章中,对舵机的仿真及驱动作了详细的介绍,请参考:

在前面的文章中,对超声波传感的仿真驱动和应用作了详细的介绍,请参考:

在前面的文章中,对LCD的仿真驱动作为详细的介绍,请参考:

1、仿真电路原理图

在这里插入图片描述

2、仿真代码实现

本次实例使用到如下开源库:

第一步,导入依赖头文件及设备创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <Servo.h>. 

#include <SPI.h>
#include "Ucglib.h"
const int trigPin = 3;
const int echoPin = 4;
int Ymax = 128;
int Xmax = 160;
int base = 8;
int pos = base+6;
int deg=0;
int x;
int val =200;
int j = 2;
Servo myServo;

long duration;
int distance;
int k;

Ucglib_ST7735_18x128x160_HWSPI ucg(/\*cd=\*/ 9, /\*cs=\*/ 10, /\*reset=\*/ 8);

第二步,初始化设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void setup(void)
{
// 初始化舵机
myServo.write(80);
myServo.attach(2);

// 初始化LCD
ucg.begin(UCG_FONT_MODE_SOLID);
ucg.setFont(ucg_font_6x10_tr);
ucg.clearScreen();
ucg.setRotate270();
// 初始化超声波传感器
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

// 初始化串口
Serial.begin(9600);
}

第三步,超声波测距

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int calculateDistance(){ 

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// 将trig引脚拉高10毫秒再拉低
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// 读取echo引脚返回信号
duration = pulseIn(echoPin, HIGH);

// 计算距离
distance= duration\*0.034/2;
return distance;
}

第四步,绘制雷达

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void drawRadar(){
ucg.setColor(255, 0, 0);
ucg.drawDisc(Xmax/2, base, 5, UCG_DRAW_LOWER_RIGHT);
ucg.drawDisc(Xmax/2, base, 5, UCG_DRAW_LOWER_LEFT);
ucg.setColor(225, 255, 50);
ucg.drawCircle(80, base, 115, UCG_DRAW_LOWER_RIGHT);
ucg.drawCircle(80, base, 115, UCG_DRAW_LOWER_LEFT);

ucg.drawCircle(80, base, 78, UCG_DRAW_LOWER_RIGHT);
ucg.drawCircle(80, base, 78, UCG_DRAW_LOWER_LEFT);

ucg.drawCircle(80, base, 40, UCG_DRAW_LOWER_RIGHT);
ucg.drawCircle(80, base, 40, UCG_DRAW_LOWER_LEFT);

ucg.drawLine(0, base, 160,base);

ucg.setColor(0, 0, 0);
ucg.drawBox(100, 0, 30, 8);
}

最后,周期性扫描及数据绘制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
void loop(void)
{
drawRadar();
// 顺时针扫描
for ( x=80; x >= 10; x--){
// 测距
distance = calculateDistance();

// 舵机旋转
k = map(x, 80, 10, 15,165);
myServo.write(k);

// 距离小于30CM绘制红色警报
if (distance < 30){
int f = x+6;
ucg.setColor(255, 0, 0);
ucg.drawLine(Xmax/2, pos, -val\*cos(radians(f\*2)),val\*sin(radians(f\*2)));
}
ucg.setColor(0, 207, 0);
ucg.drawLine(Xmax/2, pos, -200\*cos(radians(x\*2)),200\*sin(radians(x\*2)));

int d = x+1;
ucg.setColor(0, 207, 0);
int c = x+2;
ucg.setColor(0, 207, 0);
int b = x+3;
ucg.setColor(0, 102, 0);
int a = x+4;
ucg.setColor(0, 102, 0);
int e = x+5;
ucg.setColor(0, 0, 0);
ucg.drawLine(Xmax/2, pos, -200\*cos(radians(e\*2)),200\*sin(radians(e\*2)));
ucg.setColor(255, 0, 0);
ucg.setPrintPos(160,0);
ucg.setPrintDir(2);
ucg.print("Deg :");
deg = map (x, 80, 10 , 0, 180);
ucg.setPrintPos(120,0);
ucg.setPrintDir(2);
ucg.print(deg);
ucg.setPrintPos(10,0);
ucg.print(distance);
ucg.setColor(0, 0, 255);
ucg.setPrintPos(90,38);
ucg.setPrintDir(2);
ucg.print("0.25");
ucg.setPrintPos(90,70);
ucg.print("0.50");
ucg.setPrintPos(90,110);
ucg.print("1.00");
}
// ucg.clearScreen();

drawRadar();

// 逆时针扫描
for ( x=10; x <= 80; x++){
distance = calculateDistance();
Serial.println(distance);
k = map(x, 10, 80, 165,15);
myServo.write(k);
if (distance < 10){
int e = x-5;
ucg.setColor(255, 0, 0);
ucg.drawLine(Xmax/2, pos, -val\*cos(radians(e\*2)),val\*sin(radians(e\*2)));
}


ucg.setColor(0, 207, 0);
ucg.drawLine(Xmax/2, pos, -200\*cos(radians(x\*2)),200\*sin(radians(x\*2)));

int a = x-1;


int b = x-2;
ucg.setColor(0, 102, 0);

int c = x-3;
ucg.setColor(0, 102, 0);

int d = x-4;
ucg.setColor(0, 0, 0);
ucg.drawLine(Xmax/2, pos, -200\*cos(radians(d\*2)),200\*sin(radians(d\*2)));
ucg.setColor(255, 0, 0);
ucg.setPrintPos(160,0);
ucg.setPrintDir(2);
ucg.print("Deg :");
deg = map (x, 10, 80 , 0, 180);
ucg.setPrintPos(120,0);
ucg.setPrintDir(2);
ucg.print(deg);
ucg.setPrintPos(10,0);
ucg.print(distance);

ucg.setColor(0, 0, 255);
ucg.setPrintPos(90,38);
ucg.setPrintDir(2);
ucg.print("0.25");
ucg.setPrintPos(90,70);
ucg.print("0.50");
ucg.setPrintPos(90,110);
ucg.print("1.00");


}
//ucg.clearScreen();
}

3、仿真结果

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/128460069