-
Notifications
You must be signed in to change notification settings - Fork 4
/
driver.h
111 lines (101 loc) · 2.47 KB
/
driver.h
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
#ifndef __DRIVER_H
#define __DRIVER_H
#include <stdint.h>
#include <stddef.h>
/// one ring max point
#define MAX_POINT_SIZE 720
/// max buffer size
#define MAX_BUFFER_SIZE 512
/// 0xAA in packet pos
#define HeaderPos 0
/// Packet Header MSB
#define HEADER_MSB 0xAA
/// Packet Header MSB
#define HEADER_LSB 0x55
/// Packet Header length
#define PackageHeaderSize 10
/// Packet LSN Byte pos
#define LSNPos 3
/// Packet FSA Byte pos
#define FSAPos 4
/// Packet LSA Byte pos
#define LSAPos 6
/// Packet Check code Byte pos
#define CheckCodePos 8
/// Si Byte length
#define SiByteSize 3
/**
*@brief Lidar serial data cache buffer
*/
typedef struct {
uint8_t buf[MAX_BUFFER_SIZE];
uint16_t size;
} lidar_data_t;
/**
* @brief The Laser Point struct
* @note angle unit: rad.\n
* range unit: meter.\n
*/
typedef struct {
/// lidar angle. unit(rad)
float angle;
/// lidar range. unit(m)
float range;
/// lidar intensity
float intensity;
} LaserPoint;
/**
* @brief Laser Points Output
*/
typedef struct {
/// Array of lidar points
LaserPoint points[MAX_POINT_SIZE];
uint16_t npoints;
} LaserFan;
/**
* @c SetScanDataCallback response callback function.
* @param data information of the lidar data, becomes invalid after the function returns.
*/
typedef void (*ScanDataCallback)(const LaserFan *data);
class Lidar {
public:
Lidar();
/**
* Set the callback of listening Lidar scan Data message. When one ring data message is received from ydlidar, cb
* is called.
* @param cb callback for scan data.
*/
void SetScanDataCallback(ScanDataCallback cb);
/**
* @brief lidar_data_parsing
* @param data_in serial data
* @return 1 if parse the complete data, otherwise 0
*/
uint8_t lidar_data_parsing(uint8_t data_in);
protected:
/**
* @brief exclusive OR
* @param data one frame lidar data
* @param size frame size
* @return check code
*/
uint16_t XOR16(uint8_t *data, uint16_t size);
/**
* @brief decode buffer to points
* @param buff lidar buffer
* @param len buffer size
*/
void bufDecode(uint8_t *buff, uint8_t len);
private:
uint8_t last_byte;///< last byte
uint8_t rx_buf[MAX_BUFFER_SIZE]; ///< rx cache buffer
uint16_t rx_ptr;///< rx length
float AngleFSA;///< FSA angle
float AngleLSA;///< LSA angle
uint8_t LSN;///< LSN
LaserFan laserData;///< parsing laser data
LaserFan scanData;///< Parsed
lidar_data_t lidar_data;/// lidar serial buffer
ScanDataCallback m_callback;///< callback
};
#endif