-
Notifications
You must be signed in to change notification settings - Fork 0
/
X4Lidar.java
91 lines (63 loc) · 2.17 KB
/
X4Lidar.java
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
package ca.uoit.crobot.hardware;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.sun.jna.*;
import edu.wlu.cs.levy.breezyslam.components.Laser;
public class X4Lidar implements Lidar {
private static final int MOTOR_CTRL_PIN = 29;
private static final int BAUD_RATE = 230400;
private GpioPinDigitalOutput motorCtrl;
private boolean isInitialized = false;
static {
Native.register("/home/pi/lidar/build/bindings/libydlidar_bindings.so");
}
@Override
public synchronized LidarScan scan() {
final Pointer ptr = scanLidar();
int off = 0;
final int scanSize = ptr.getInt(off);
final boolean success = ptr.getByte(off += 17) != 0;
if (!success)
return null;
float min_angle = ptr.getFloat(off += 4);
float ang_increment = ptr.getFloat(off += 8);
float[] ranges = ptr.getFloatArray(off + 20, scanSize);
final float[] angles = new float[scanSize];
for (int i = 0; i < scanSize; i++) {
angles[i] = min_angle + i * ang_increment;
}
return new LidarScan(angles, ranges);
}
@Override
public void shutdown() {
turnOff();
stopRotation();
isInitialized = false;
}
@Override
public void rotate() {
if (!isInitialized)
throw new IllegalStateException("not initialized");
motorCtrl.setState(PinState.HIGH);
}
@Override
public void stopRotation() {
if (!isInitialized)
throw new IllegalStateException("not initialized");
motorCtrl.setState(PinState.LOW);
}
@Override
public Laser getLaserConfig() {
return new Laser(640 / 4, 7, 360, 10000, 0, 0);
}
@Override
public void init() {
isInitialized = init(BAUD_RATE);
motorCtrl = GpioUtility.getDigitalOutput(MOTOR_CTRL_PIN);
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
}
public static native boolean init(int baud_rate);
public static native void turnOff();
public static native Pointer getSdkVersion();
public static native Pointer scanLidar();
}