The Garmin LIDAR Lite V3 sensor is an inexpensive (for the accuracy) and simple way of getting distance measurements on the field.
There are two ways to interface this sensor to your robot. I2C and PWM counting in. The PWM counting is the simplest way to get the distance, however, it does take a lot of resources to get the distance. Basically, you need to count the pulses in. On the RIO this needs a Counter. The Rio will then count the pulses, but that distracts from other things the Rio count be doing.
A second way of using the PWM in is the Cross the Road Electronics, CANifier. The CANifier will do the counting and the RIO will ask the CANifier for the outcome. There are a lot of examples for this on the web, so I will not go into them.
On both robots I have an Arduino on the robot that the RIO is already talking I2C to. I decided to go with the I2C route. For one Lite V3 this is easy, the default I2C address is 0x62. To get a reading, read both 0x0F and 0x10 registers, or send a 0x8F, which will read both registers as two bytes.
This will work good for finding what is in front of your robot. However, to follow a wall you need two, one that looks at a 45degree angle to the wall and one to look forward. The one that looks at the wall and adjusts the speed of one side of the robot, if the robot is following the left wall, keep the left drive wheels spinning at a constant speed. If the sensor says we are too close to the wall, slow the right drive down, if the wall is too close then speed up the right wheels. The requested distance away from the wall is read from the sensor and convert the reading into the speed for the right motor. The forward-looking sensor deals with concave angles, to turn before hitting a wall.
I decided to put three one the robot. Two 45 degrees sensors to follow both left and right walls, and then the forward looking one. This is a problem because there is only one I2C address. In order not to have the sensors collide, there is a register on the Lite that changes the I2C address. This register takes in the serial number of the sensor to change the address. I found the three serial numbers of my sensors, and assigned them to addresses 0x20, 0x22, and 0x24. Then read each sensor.
The RIO does not like talking I2C to these sensors very well. The code can setup the three sensors but while reading the I2C port gets stuck causing the watch dog to reset the frame, once this happens the I2C stops talking on the I2C bus. I could not find a way to restart the I2C bus. To get around this I am going to change out the LED ring Arduino for an Adafruit Feather M0. This has two I2C ports, one to talk to the RIO the other to talk to the three Lites.
dist=272 324 287 dist=272 324 287 dist=272 323 287 dist=272 323 287 dist=272 323 287 dist=272 324 287 dist=272 324 287 Watchdog not fed within 0.020000s Warning 1 Loop time of 0.02s overrun edu.wpi.first.wpilibj.IterativeRobotBase. printLoopOverrunMessage(IterativeRobotBase.java:273) Warning at edu.wpi.first.wpilibj.IterativeRobotBase. printLoopOverrunMessage(IterativeRobotBase.java:273): Loop time of 0.02s overrun dist=-28925 324 287 robotPeriodic(): 0.000011s autonomousPeriodic(): 0.092310s dist=-28925 -28925 287 dist=-28925 -28925 -28925 dist=-28925 -28925 -28925 dist=-28925 -28925 -28925 robotPeriodic(): 0.000011s autonomousPeriodic(): 0.266448s Watchdog not fed within 0.020000s Warning 1 Loop time of 0.02s overrun
The source code to the test is on my Github at: TasmanianKoala GarminLidarTest
I have not gotten to adding the Feather to the robot yet.