-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFRobot_matrixLidarDistanceSensor.cpp
391 lines (350 loc) · 12.4 KB
/
DFRobot_matrixLidarDistanceSensor.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*!
* @file DFRobot_matrixLidarDistanceSensor.cpp
* @brief This is the implementation file for DFRobot_matrixLidarDistanceSensor
* @copyright Copyright (c) 2024 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [TangJie]([email protected])
* @version V1.0
* @date 2024-04-01
* @url https://github.com/DFRobot/DFRobot_matrixLidarDistanceSensor
*/
#include "DFRobot_matrixLidarDistanceSensor.h"
#define CMD_SETMODE 1
#define CMD_ALLData 2
#define CMD_FIXED_POINT 3
#define CMD_LINE 4
#define CMD_LIST 5
#define CMD_AVOID_OBSTACLE 6
#define CMD_CONFIG_AVOID 7
#define CMD_OBSTACLE_DISTANCE 8
#define CMD_END CMD_OBSTACLE_DISTANCE
#define STATUS_SUCCESS 0x53 ///< Status of successful response
#define STATUS_FAILED 0x63 ///< Status of failed response
#define IIC_MAX_TRANSFER 32 ///< Maximum transferred data via I2C
#define I2C_ACHE_MAX_LEN 32
#define DEBUG_TIMEOUT_MS 8000
#define ERR_CODE_NONE 0x00 ///< Normal communication
#define ERR_CODE_CMD_INVAILED 0x01 ///< Invalid command
#define ERR_CODE_RES_PKT 0x02 ///< Response packet error
#define ERR_CODE_M_NO_SPACE 0x03 ///< Insufficient memory of I2C controller(master)
#define ERR_CODE_RES_TIMEOUT 0x04 ///< Response packet reception timeout
#define ERR_CODE_CMD_PKT 0x05 ///< Invalid command packet or unmatched command
#define ERR_CODE_SLAVE_BREAK 0x06 ///< Peripheral(slave) fault
#define ERR_CODE_ARGS 0x07 ///< Set wrong parameter
#define ERR_CODE_SKU 0x08 ///< The SKU is an invalid SKU, or unsupported by SCI Acquisition Module
#define ERR_CODE_S_NO_SPACE 0x09 ///< Insufficient memory of I2C peripheral(slave)
#define ERR_CODE_I2C_ADRESS 0x0A ///< Invalid I2C address
static uint8_t outDir = 0;
static uint8_t outEmergencyFlag = 0;
static uint16_t outLeft = 0;
static uint16_t outRight = 0;
static uint16_t outMiddle = 0;
typedef struct{
uint8_t head;
uint8_t argsNumH; /**< High byte of parameter number after the command */
uint8_t argsNumL; /**< Low byte of parameter number after the command */
uint8_t cmd; /**< Command */
uint8_t args[0]; /**< The array with 0-data length, its size depends on the value of the previous variables argsNumL and argsNumH */
}__attribute__ ((packed)) sCmdSendPkt_t, *pCmdSendPkt_t;
typedef struct{
uint8_t status; /**< Response packet status, 0x53, response succeeded, 0x63, response failed */
uint8_t cmd; /**< Response packet command */
uint8_t lenL; /**< Low byte of the buf array length excluding packet header */
uint8_t lenH; /**< High byte of the buf array length excluding packet header */
uint8_t buf[0]; /**< The array with 0-data length, its size depends on the value of the previous variables lenL and lenH */
}__attribute__ ((packed)) sCmdRecvPkt_t, *pCmdRecvPkt_t;
DFRobot_matrixLidarDistanceSensor::DFRobot_matrixLidarDistanceSensor(uint8_t addr, TwoWire *pWire):_pWire(pWire),_addr(addr),_timeout(DEBUG_TIMEOUT_MS){
}
uint8_t DFRobot_matrixLidarDistanceSensor::begin(void){
_pWire->begin();
_pWire->beginTransmission(_addr);
if(_pWire->endTransmission()){
return 1;
}
return 0;
}
uint8_t DFRobot_matrixLidarDistanceSensor::getAllDataConfig(eMatrix_t matrix){
uint8_t length = 4;
uint8_t errorCode;
pCmdSendPkt_t sendpkt = NULL;
sendpkt = (pCmdSendPkt_t)malloc(sizeof(sCmdSendPkt_t) + length);
if(sendpkt == NULL) return 1;
sendpkt->head = 0x55;
sendpkt->argsNumH = ((length + 1) >> 8) & 0xFF;
sendpkt->argsNumL = (length + 1) & 0xFF;
sendpkt->cmd = CMD_SETMODE;
sendpkt->args[0] = 0;
sendpkt->args[1] = 0;
sendpkt->args[2] = 0;
sendpkt->args[3] = matrix;
length += sizeof(sCmdSendPkt_t);
DBG(length);
sendPacket(sendpkt, length , true);
free(sendpkt);
pCmdRecvPkt_t rcvpkt = (pCmdRecvPkt_t)recvPacket(CMD_SETMODE, &errorCode);
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_FAILED)) errorCode = rcvpkt->buf[0];
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_SUCCESS)){
length = (rcvpkt->lenH << 8) | rcvpkt->lenL;
if(rcvpkt) free(rcvpkt);
delay(5000);
return 0;
}
return 1;
}
uint8_t DFRobot_matrixLidarDistanceSensor::configAvoidance(uint8_t wall){
uint8_t length = 2;
uint8_t errorCode;
uint16_t _wall = wall * 10;
pCmdSendPkt_t sendpkt = NULL;
sendpkt = (pCmdSendPkt_t)malloc(sizeof(sCmdSendPkt_t) + length);
if(sendpkt == NULL) return 1;
sendpkt->head = 0x55;
sendpkt->argsNumH = ((length + 1) >> 8) & 0xFF;
sendpkt->argsNumL = (length + 1) & 0xFF;
sendpkt->cmd = CMD_CONFIG_AVOID;
sendpkt->args[0] = (_wall >> 8) & 0xff;
sendpkt->args[1] = _wall & 0xff;
length += sizeof(sCmdSendPkt_t);
DBG(length);
sendPacket(sendpkt, length , true);
free(sendpkt);
pCmdRecvPkt_t rcvpkt = (pCmdRecvPkt_t)recvPacket(CMD_CONFIG_AVOID, &errorCode);
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_FAILED)) errorCode = rcvpkt->buf[0];
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_SUCCESS)){
length = (rcvpkt->lenH << 8) | rcvpkt->lenL;
if(rcvpkt) free(rcvpkt);
return 0;
}
return 1;
}
uint8_t DFRobot_matrixLidarDistanceSensor::getAllData(void *buf){
uint8_t length = 0;
uint8_t errorCode;
pCmdSendPkt_t sendpkt = NULL;
sendpkt = (pCmdSendPkt_t)malloc(sizeof(sCmdSendPkt_t) + length);
if(sendpkt == NULL) return 1;
sendpkt->head = 0x55;
sendpkt->argsNumH = ((length + 1) >> 8) & 0xFF;
sendpkt->argsNumL = (length + 1) & 0xFF;
sendpkt->cmd = CMD_ALLData;
length += sizeof(sCmdSendPkt_t);
DBG(length);
sendPacket(sendpkt, length , true);
free(sendpkt);
pCmdRecvPkt_t rcvpkt = (pCmdRecvPkt_t)recvPacket(CMD_ALLData, &errorCode);
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_FAILED)) errorCode = rcvpkt->buf[0];
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_SUCCESS)){
length = (rcvpkt->lenH << 8) | rcvpkt->lenL;
DBG(length);
memcpy(buf,rcvpkt->buf,length);
if(rcvpkt) free(rcvpkt);
return 0;
}
return 1;
}
uint16_t DFRobot_matrixLidarDistanceSensor::getFixedPointData(uint8_t x, uint8_t y){
uint8_t length = 2;
uint16_t ret = 0;
uint8_t errorCode;
pCmdSendPkt_t sendpkt = NULL;
sendpkt = (pCmdSendPkt_t)malloc(sizeof(sCmdSendPkt_t) + length);
if(sendpkt == NULL) return 1;
sendpkt->head = 0x55;
sendpkt->argsNumH = ((length + 1) >> 8) & 0xFF;
sendpkt->argsNumL = (length + 1) & 0xFF;
sendpkt->cmd = CMD_FIXED_POINT;
sendpkt->args[0] = x;
sendpkt->args[1] = y;
length += sizeof(sCmdSendPkt_t);
DBG(length);
sendPacket(sendpkt, length , true);
free(sendpkt);
pCmdRecvPkt_t rcvpkt = (pCmdRecvPkt_t)recvPacket(CMD_FIXED_POINT, &errorCode);
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_FAILED)) errorCode = rcvpkt->buf[0];
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_SUCCESS)){
length = (rcvpkt->lenH << 8) | rcvpkt->lenL;
ret = rcvpkt->buf[1] << 8 | rcvpkt->buf[0];
DBG(length);
if(rcvpkt) free(rcvpkt);
return ret;
}
return 1;
}
uint8_t DFRobot_matrixLidarDistanceSensor::requestObstacleSensorData(void){
uint8_t length = 0;
uint8_t errorCode;
pCmdSendPkt_t sendpkt = NULL;
sendpkt = (pCmdSendPkt_t)malloc(sizeof(sCmdSendPkt_t) + length);
if(sendpkt == NULL) return 1;
sendpkt->head = 0x55;
sendpkt->argsNumH = ((length + 1) >> 8) & 0xFF;
sendpkt->argsNumL = (length + 1) & 0xFF;
sendpkt->cmd = CMD_AVOID_OBSTACLE;
length += sizeof(sCmdSendPkt_t);
DBG(length);
sendPacket(sendpkt, length , true);
free(sendpkt);
pCmdRecvPkt_t rcvpkt = (pCmdRecvPkt_t)recvPacket(CMD_AVOID_OBSTACLE, &errorCode);
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_FAILED)) errorCode = rcvpkt->buf[0];
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_SUCCESS)){
length = (rcvpkt->lenH << 8) | rcvpkt->lenL;
DBG(length);
outDir = rcvpkt->buf[0];
outEmergencyFlag = rcvpkt->buf[1];
//memcpy(buf,rcvpkt->buf,length);
if(rcvpkt) free(rcvpkt);
return 0;
}
return 1;
}
uint8_t DFRobot_matrixLidarDistanceSensor::getDir(void){
return outDir;
}
uint8_t DFRobot_matrixLidarDistanceSensor::getEmergencyFlag(void){
return outEmergencyFlag;
}
uint8_t DFRobot_matrixLidarDistanceSensor::requestObstacleDistance(void){
uint8_t length = 0;
uint8_t errorCode;
pCmdSendPkt_t sendpkt = NULL;
sendpkt = (pCmdSendPkt_t)malloc(sizeof(sCmdSendPkt_t) + length);
if(sendpkt == NULL) return 1;
sendpkt->head = 0x55;
sendpkt->argsNumH = ((length + 1) >> 8) & 0xFF;
sendpkt->argsNumL = (length + 1) & 0xFF;
sendpkt->cmd = CMD_OBSTACLE_DISTANCE;
length += sizeof(sCmdSendPkt_t);
DBG(length);
sendPacket(sendpkt, length , true);
free(sendpkt);
pCmdRecvPkt_t rcvpkt = (pCmdRecvPkt_t)recvPacket(CMD_OBSTACLE_DISTANCE, &errorCode);
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_FAILED)) errorCode = rcvpkt->buf[0];
if((rcvpkt != NULL) && (rcvpkt->status == STATUS_SUCCESS)){
length = (rcvpkt->lenH << 8) | rcvpkt->lenL;
DBG(length);
outLeft = (rcvpkt->buf[0] | rcvpkt->buf[1] << 8) / 10;
outMiddle = (rcvpkt->buf[2] | rcvpkt->buf[3] << 8) / 10;
outRight = (rcvpkt->buf[4] | rcvpkt->buf[5] << 8) / 10;
if(rcvpkt) free(rcvpkt);
return 0;
}
return 1;
}
uint16_t DFRobot_matrixLidarDistanceSensor::getDistance(eDir_t dir){
uint16_t _ret = 0;
switch(dir){
case eLeft:
_ret = outLeft;
break;
case eMiddle:
_ret = outMiddle;
break;
case eRight:
_ret = outRight;
break;
default:
break;
}
return _ret;
}
void DFRobot_matrixLidarDistanceSensor::sendPacket(void *pkt, int length, bool stop){
uint8_t *pBuf = (uint8_t *)pkt;
int remain = length;
if((pkt == NULL) || (length == 0)) return;
_pWire->beginTransmission(_addr);
while(remain){
DBG("a");
length = (remain > IIC_MAX_TRANSFER) ? IIC_MAX_TRANSFER : remain;
_pWire->write(pBuf, length);
remain -= length;
pBuf += length;
#if defined(ESP32)
if(remain) _pWire->endTransmission(true);
#else
if(remain) _pWire->endTransmission(false);
#endif
}
_pWire->endTransmission();
}
void * DFRobot_matrixLidarDistanceSensor::recvPacket(uint8_t cmd, uint8_t *errorCode){
if(cmd > CMD_END){
DBG("cmd is error!");
if(errorCode) *errorCode = ERR_CODE_CMD_INVAILED; //There is no this command
return NULL;
}
sCmdRecvPkt_t recvPkt;
pCmdRecvPkt_t recvPktPtr = NULL;
uint16_t length = 0;
uint32_t t = millis();
while(millis() - t < _timeout/*time_ms*/){
DBG("k");
recvData(&recvPkt.status, 1);
if(recvPkt.status != 0xff){
switch(recvPkt.status){
case STATUS_SUCCESS:
case STATUS_FAILED:{
recvData(&recvPkt.cmd, 1);
if(recvPkt.cmd != cmd){
//recvFlush();
if(errorCode) *errorCode = ERR_CODE_RES_PKT; //Response packet error
DBG("Response pkt is error!");
return NULL;
}
recvData(&recvPkt.lenL, 2);
length = (recvPkt.lenH << 8) | recvPkt.lenL;
if(length<1000){
recvPktPtr = (pCmdRecvPkt_t)malloc(sizeof(sCmdRecvPkt_t) + length);
}else{
return NULL;
}
if(recvPktPtr == NULL){
if(errorCode) *errorCode = ERR_CODE_M_NO_SPACE; //Insufficient memory of I2C controller(master)
DBG("malloc error");
free(recvPktPtr);
return NULL;
}
memcpy(recvPktPtr, &recvPkt, sizeof(sCmdRecvPkt_t));
if(length)recvData(recvPktPtr->buf, length);
if(errorCode) *errorCode = ERR_CODE_NONE;
return recvPktPtr;
}
default:
//restData();
delay(10);
break;
}
}
delay(17);
}
if(errorCode) *errorCode = ERR_CODE_RES_TIMEOUT; //Receive packet timeout
free(recvPktPtr);
DBG("Time out!");
DBG(millis() - t);
return NULL;
}
int DFRobot_matrixLidarDistanceSensor::recvData(void *data, int len){
uint8_t *pBuf = (uint8_t *)data;
int remain = len;
int total = 0;
if(pBuf == NULL){
DBG("pBuf ERROR!! : null pointer");
return 0;
}
while(remain){
len = remain > I2C_ACHE_MAX_LEN ? I2C_ACHE_MAX_LEN : remain;
remain -= len;
#if defined(ESP32)
if(remain) {_pWire->requestFrom(_addr, len, true);}
#else
if(remain){ _pWire->requestFrom(_addr, len, false);}
#endif
else{_pWire->requestFrom(_addr, len, true);}
for(int i = 0; i < len; i++){
pBuf[i] = _pWire->read();
yield();
}
pBuf += len;
total += len;
}
return total;
}