-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLidarObject.h
191 lines (161 loc) · 6.79 KB
/
LidarObject.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
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
#include <Arduino.h>
#include <Wire.h>
#include "I2CFunctions.h"
// We got a Lidar object per laser.
#ifndef LIDAR_OBJECT_H
#define LIDAR_OBJECT_H
#define LIDAR_RESET_US 20000
#define LIDAR_TIMEOUT_US 200000
enum LIDAR_STATE {
SHUTING_DOWN = 240, // Shutdown the laser to reset it
NEED_RESET = 48, // Too much outliers, need to reset
RESET_PENDING = 80, // Wait 15ms after you reset the Lidar, we are waiting in this state
ACQUISITION_IN_PROGRESS = 64, // The acquisition in on progress
};
enum LIDAR_MODE {
NONE = 0,
DISTANCE = 1,
VELOCITY = 2,
DISTANCE_AND_VELOCITY = 3
};
enum LIDAR_TYPE {
I2C_TYPE = 0,
CONTINUOUS_I2C_TYPE = 1,
PWM_TYPE = 2
};
class LidarObject {
public:
/*******************************************************************************
Constructor
*******************************************************************************/
LidarObject() {};
/*******************************************************************************
begin : Begin the I2C master device
If fasti2c is true, use 400kHz I2C
*******************************************************************************/
void begin(uint8_t _EnablePin = 12, uint8_t _ModePin = 13, uint8_t _TrigPin = 11, uint8_t _Lidar = 0x62, uint8_t _configuration = 2, LIDAR_MODE _mode = DISTANCE, char _name = 'A'){
pinMode(_EnablePin, OUTPUT);
off();
last_distance = 0;
distance = 0;
velocity = 0;
strength = 0;
mode = _mode;
configuration = _configuration;
address = _Lidar;
EnablePin = _EnablePin;
ModePin = _ModePin;
TrigPin = _TrigPin;
name = _name;
};
/*******************************************************************************
on : Power On the device
*******************************************************************************/
void on(){
digitalWrite(EnablePin, HIGH);
};
/*******************************************************************************
off : Power Off the device
*******************************************************************************/
void off(){
digitalWrite(EnablePin, LOW);
};
/*******************************************************************************
beginPWM : Sets the pinMode of the trigger and the mode pin. It allows PWM
readings but ... it makes I2C reading slower on v2. (not tested on v3).
*******************************************************************************/
void beginPWM(){
pinMode(ModePin, INPUT);
pinMode(TrigPin, OUTPUT);
};
/*******************************************************************************
enable : ask for PWM reading and allow continuous readings
*******************************************************************************/
void enable(){
digitalWrite(TrigPin, LOW);
};
/*******************************************************************************
disable : stops PWM reading and allow continuous readings
*******************************************************************************/
void disable(){
digitalWrite(TrigPin, HIGH);
};
/*******************************************************************************
timerUpdate : Update the timer to the current time to start the timer.
*******************************************************************************/
void timerUpdate(){
timeReset = micros();
};
/*******************************************************************************
checkTimer : Check the reset timer to see if the laser is correctly reset
The laser takes 20ms to reset
*******************************************************************************/
bool checkTimer(){
if(lidar_state != RESET_PENDING)
return true;
return (micros() - timeReset > LIDAR_RESET_US);
};
/*******************************************************************************
checkLastMeasure : If the laser do not give new data, simply reset it
*******************************************************************************/
bool checkLastMeasure(){
return (micros() - lastMeasureTime > LIDAR_TIMEOUT_US);
};
/*******************************************************************************
resetNacksCount : The nack counter makes the Arduino able to know if a laser
needs to be reset
*******************************************************************************/
bool resetNacksCount(){
nacksCount = 0;
};
/*******************************************************************************
setCallbackDistance : sets the distance callback
*******************************************************************************/
void setCallbackDistance(void (*_callback)(LidarObject * self)){
notify_distance_cb = _callback;
};
/*******************************************************************************
setCallbackVelocity : sets the velocity callback
*******************************************************************************/
void setCallbackVelocity(void (*_callback)(LidarObject * self, unsigned long dt)){
notify_velocity_cb = _callback;
};
/*******************************************************************************
notify_distance : wraps the callback & callback existence verification
*******************************************************************************/
inline void notify_distance(){
if(notify_distance_cb) notify_distance_cb(this);
};
/*******************************************************************************
notify_velocity : wraps the callback & callback existence verification
*******************************************************************************/
inline void notify_velocity(unsigned long dt){
if(notify_velocity_cb) notify_velocity_cb(this, dt);
};
/*******************************************************************************
change_type : change the acqusition type, has to be changed before being added
to the controller. Otherwise, reset it.
*******************************************************************************/
void change_type(LIDAR_TYPE _type = I2C_TYPE){
type = _type;
};
int16_t last_distance = -1; // Last distance measured
int16_t distance = -1; // Newest distance
float velocity = 0; // Newest velocity
uint8_t strength = 0; // Newest signal strength
uint8_t nacksCount = 0;
unsigned long timeReset = 0;
uint8_t configuration;
uint8_t address;
uint8_t EnablePin;
uint8_t ModePin;
uint8_t TrigPin;
long lastMeasureTime = 0;
char name;
LIDAR_STATE lidar_state = NEED_RESET;
LIDAR_MODE mode = DISTANCE;
LIDAR_TYPE type = I2C_TYPE;
void (*notify_distance_cb)(LidarObject * self);
void (*notify_velocity_cb)(LidarObject * self, unsigned long dt);
};
#endif