-
Notifications
You must be signed in to change notification settings - Fork 7
/
DFRobot_mmWave_Radar.cpp
246 lines (195 loc) · 5.21 KB
/
DFRobot_mmWave_Radar.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*!
@file DFRobot_mmWave_Radar.cpp
@brief Implement the basic structure of class mmwave rader sensor-human presence detection
@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
@licence The MIT License (MIT)
@author huyujie([email protected])
@version V1.0
@date 2020-3-25
@get from https://www.dfrobot.com
@url https://github.com/DFRobot
*/
#include <DFRobot_mmWave_Radar.h>
DFRobot_mmWave_Radar::DFRobot_mmWave_Radar(Stream *s)
{
_s = s;
}
size_t DFRobot_mmWave_Radar::readN(uint8_t *buf, size_t len)
{
size_t offset = 0, left = len;
int16_t Tineout = 1500;
uint8_t *buffer = buf;
long curr = millis();
while (left) {
if (_s -> available()) {
buffer[offset] = _s->read();
offset++;
left--;
}
if (millis() - curr > Tineout) {
break;
}
}
return offset;
}
bool DFRobot_mmWave_Radar::recdData(uint8_t *buf)
{
int16_t Tineout = 50000;
long curr = millis();
uint8_t ch;
bool ret = false;
while (!ret) {
if (millis() - curr > Tineout) {
break;
}
if (readN(&ch, 1) == 1) {
if (ch == '$') {
buf[0] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'J') {
buf[1] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'Y') {
buf[2] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'B') {
buf[3] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'S') {
buf[4] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'S') {
buf[5] = ch;
if (readN(&buf[6], 9) == 9) {
ret = true;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return ret;
}
bool DFRobot_mmWave_Radar::readPresenceDetection(void)
{
uint8_t dat[15] = {0};
bool ret;
if (recdData(dat)) {
if (dat[7] == '1') {
ret = true;
} else {
if (dat[7] == '0') {
ret = false;
}
}
} else {
while (1);
}
return ret;
}
void DFRobot_mmWave_Radar::DetRangeCfg(float parA_s, float parA_e)
{
char comDetRangeCfg[22] = {0};
int16_t parA_S = parA_s / 0.15;
int16_t parA_E = parA_e / 0.15;
sprintf(comDetRangeCfg, "detRangeCfg -1 %d %d", parA_S, parA_E);
_s->write(comStop);
delay(DELAY);
_s->write(comDetRangeCfg);
delay(DELAY);
_s->write(comSaveCfg);
delay(DELAY);
_s->write(comStart);
delay(DELAY);
}
void DFRobot_mmWave_Radar::DetRangeCfg(float parA_s, float parA_e, float parB_s, float parB_e)
{
char comDetRangeCfg[30] = {0};
int16_t parA_S = parA_s / 0.15;
int16_t parA_E = parA_e / 0.15;
int16_t parB_S = parB_s / 0.15;
int16_t parB_E = parB_e / 0.15;
sprintf(comDetRangeCfg, "detRangeCfg -1 %d %d %d %d", parA_S, parA_E, parB_S, parB_E);
_s->write(comStop);
delay(DELAY);
_s->write(comDetRangeCfg);
delay(DELAY);
_s->write(comSaveCfg);
delay(DELAY);
_s->write(comStart);
delay(DELAY);
}
void DFRobot_mmWave_Radar::DetRangeCfg(float parA_s, float parA_e, float parB_s, float parB_e, float parC_s, float parC_e)
{
char comDetRangeCfg[38] = {0};
int16_t parA_S = parA_s / 0.15;
int16_t parA_E = parA_e / 0.15;
int16_t parB_S = parB_s / 0.15;
int16_t parB_E = parB_e / 0.15;
int16_t parC_S = parC_s / 0.15;
int16_t parC_E = parC_e / 0.15;
sprintf(comDetRangeCfg, "detRangeCfg -1 %d %d %d %d %d %d", parA_S, parA_E, parB_S, parB_E, parC_S, parC_E);
_s->write(comStop);
delay(DELAY);
_s->write(comDetRangeCfg);
delay(DELAY);
_s->write(comSaveCfg);
delay(DELAY);
_s->write(comStart);
delay(DELAY);
}
void DFRobot_mmWave_Radar::DetRangeCfg(float parA_s, float parA_e, float parB_s, float parB_e, float parC_s, float parC_e, float parD_s, float parD_e)
{
char comDetRangeCfg[46] = {0};
int16_t parA_S = parA_s / 0.15;
int16_t parA_E = parA_e / 0.15;
int16_t parB_S = parB_s / 0.15;
int16_t parB_E = parB_e / 0.15;
int16_t parC_S = parC_s / 0.15;
int16_t parC_E = parC_e / 0.15;
int16_t parD_S = parD_s / 0.15;
int16_t parD_E = parD_e / 0.15;
sprintf(comDetRangeCfg, "detRangeCfg -1 %d %d %d %d %d %d %d %d", parA_S, parA_E, parB_S, parB_E, parC_S, parC_E, parD_S, parD_E);
_s->write(comStop);
delay(DELAY);
_s->write(comDetRangeCfg);
delay(DELAY);
_s->write(comSaveCfg);
delay(DELAY);
_s->write(comStart);
delay(DELAY);
}
void DFRobot_mmWave_Radar::OutputLatency(float par1, float par2)
{
char comOutputLatency[28] = {0};
int16_t Par1 = par1 * 1000 / 25;
int16_t Par2 = par2 * 1000 / 25;
sprintf(comOutputLatency, "outputLatency -1 %d %d", Par1 , Par2);
_s->write(comStop);
delay(DELAY);
_s->write(comOutputLatency);
delay(DELAY);
_s->write(comSaveCfg);
delay(DELAY);
_s->write(comStart);
delay(DELAY);
}
void DFRobot_mmWave_Radar::factoryReset(void)
{
_s->write(comStop);
delay(DELAY);
_s->write(comFactoryReset);
delay(DELAY);
_s->write(comSaveCfg);
delay(DELAY);
_s->write(comStart);
delay(DELAY);
}