Add key listener to runFlyWheel
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / utils / Lidar.java
CommitLineData
e7bf265c
RR
1package org.usfirst.frc.team3501.robot.utils;
2
3import java.util.TimerTask;
4import edu.wpi.first.wpilibj.I2C;
5import edu.wpi.first.wpilibj.Timer;
6import edu.wpi.first.wpilibj.I2C.Port;
7import edu.wpi.first.wpilibj.PIDSource;
8import edu.wpi.first.wpilibj.PIDSourceType;
9
10public class Lidar implements PIDSource {
11 private I2C i2c;
12 private byte[] distance;
13 private java.util.Timer updater;
14
15 private final int LIDAR_ADDR = 0x62;
16 private final int LIDAR_CONFIG_REGISTER = 0x00;
17 private final int LIDAR_DISTANCE_REGISTER = 0x8f;
18
19 public Lidar(Port port) {
20 i2c = new I2C(port, LIDAR_ADDR);
21
22 distance = new byte[2];
23
24 updater = new java.util.Timer();
25 }
26
27 // Distance in cm
28 public int getDistance() {
29 return (int) Integer.toUnsignedLong(distance[0] << 8)
30 + Byte.toUnsignedInt(distance[1]);
31 }
32
33 @Override
34 public double pidGet() {
35 return getDistance();
36 }
37
38 // Start 10Hz polling
39 public void start() {
40 updater.scheduleAtFixedRate(new LIDARUpdater(), 0, 100);
41 }
42
43 // Start polling for period in milliseconds
44 public void start(int period) {
45 updater.scheduleAtFixedRate(new LIDARUpdater(), 0, period);
46 }
47
48 public void stop() {
49 updater.cancel();
50 updater = new java.util.Timer();
51 }
52
53 // Update distance variable
54 public void update() {
55 i2c.write(LIDAR_CONFIG_REGISTER, 0x04); // Initiate measurement
56 Timer.delay(0.04); // Delay for measurement to be taken
57 i2c.read(LIDAR_DISTANCE_REGISTER, 2, distance); // Read in measurement
58 Timer.delay(0.005); // Delay to prevent over polling
59 }
60
61 // Timer task to keep distance updated
62 private class LIDARUpdater extends TimerTask {
63 @Override
64 public void run() {
65 while (true) {
66 update();
67 try {
68 Thread.sleep(10);
69 } catch (InterruptedException e) {
70 e.printStackTrace();
71 }
72 }
73 }
74 }
75
76 @Override
77 public void setPIDSourceType(PIDSourceType pidSource) {
78 // TODO Auto-generated method stub
79
80 }
81
82 @Override
83 public PIDSourceType getPIDSourceType() {
84 // TODO Auto-generated method stub
85 return null;
86 }
87}