Escolar Documentos
Profissional Documentos
Cultura Documentos
This sketch demonstrates distance, raw velocity and smoothed velocity readings.
USAGE:
'w' + '0-3' + '2-9' = read average velocity and set scaling register
Examples:
------------------------------------------
http://pulsedlight3d.com/pl3d/wp-content/uploads/2014/10/LIDAR-Lite-Operating-Manual-PRELIM.pdf
It utilizes the 'Arduino I2C Master Library' from DSS Circuits:
http://www.dsscircuits.com/index.php/articles/66-arduino-i2c-master-library
http://arduino.cc/en/Guide/Libraries
*/
#include <I2C.h>
// Global Variables
void setup(){
delay(100); // Waits to make sure everything is powered up before sending or receiving data
void loop(){
smRunStateMachine(); // Run the State Machine that controls actions based on user input
}
/*
=====================================================================================
=====================================================
Basic read and write functions for LIDAR-Lite, waits for success message (0 or ACK) before proceeding
=====================================================================================
========================================================*/
while (nackack != 0){ // While NACK keep going (i.e. continue polling until sucess message (ACK) is
received )
// Read 1-2 bytes from a register and wait until it responds with sucess
while (nackack != 0){ // While NACK keep going (i.e. continue polling until sucess message (ACK) is
received )
}
/*
=====================================================================================
=====================================================
Get 2-byte distance from sensor and combine into single 16-bit int
=====================================================================================
========================================================*/
int llGetDistance(){
int distance = (myArray[0] << 8) + myArray[1]; // Shift high byte [0] 8 to the left and add low byte [1] to
create 16-bit int
return(distance);
/*
=====================================================================================
=====================================================
Get raw velocity readings from sensor and convert to signed int
=====================================================================================
========================================================*/
int llGetVelocity(){
return((int)((char)myArray[0])); // Convert 1 byte to char and then to int to get signed int value for
velocity measurement
}
/*
=====================================================================================
=====================================================
int numberOfReadings - the number of readings you want to average (0-9 are possible, 2-9 are
reccomended)
=====================================================================================
========================================================*/
sum = sum/numberOfReadings; // Divide the total by the number of readings to get the average
return(sum);
sum = sum/numberOfReadings; // Divide the total by the number of readings to get the average
return(sum);
/*
=====================================================================================
=====================================================
SET VELOCITY SCALING VALUES AND RESET REGISTERS BEFORE READING DISTANCE
=====================================================================================
========================================================*/
}else{
/*
=====================================================================================
=====================================================
Serial.println("\n=======================================================");
Serial.println(myString);
Serial.println("=======================================================\n");
String smPrintUserManaual(){
String myString = "Continuous read state machine for LIDAR-Lite User Manual\n\nUSAGE: \n'm' = read
distance\n'n' + '2-9' = read average distance\n'v' + '0-3' = read raw velocity and set scaling register\n'w'
+ '0-3' + '2-9' = read average velocity and set scaling register \nType any other key to
stop\n\nExamples:\n- 'm' will read distance\n- 'n2' will read average distance for two readings\n- 'v0'
will read velocity at 0.1m/s\n- 'w34' will read velocity at 1m/s and average 4 readings";
return myString;
String myString;
if (velocityScaling == 1){
}else{
if(numberOfReadings != 0){
myString += int(numberOfReadings);
smPrintFunctionInit(myString);
int configureFlag = 0;
int i = 0;
char serialArray[3];
char serialRead;
void smRunStateMachine(){
if(i==0){
configureFlag = 0;
serialRead = Serial.read();
if(serialRead == 10 || serialRead == 32 ){
}else{
serialArray[i] = serialRead;
i++;
}else{
i = 0;
switch(serialArray[0]){
if (configureFlag == 0){
llConfigureRegisters(serialArray[0],int(serialArray[1])-48,0);
configureFlag++;
Serial.println(llGetVelocity());
break;
if (configureFlag == 0){
llConfigureRegisters(serialArray[0],int(serialArray[1])-48,int(serialArray[2])-48);
configureFlag++;
Serial.println(llGetVelocityAverage(int(serialArray[2])-48));
break;
if (configureFlag == 0){
llConfigureRegisters(serialArray[0],0,0);
configureFlag++;
Serial.println(llGetDistance());
break;
if (configureFlag == 0){
llConfigureRegisters(serialArray[0],0,int(serialArray[1])-48);
configureFlag++;
Serial.println(llGetDistanceAverage(int(serialArray[1])-48));
default:
if (configureFlag == 0){
smPrintFunctionInit(smPrintUserManaual());
configureFlag++;