LidarLitev2 Library for distance reading. Capable of both single distance reads and continuous distance read setup.
Dependents: Lidar_Distance Lidar_DistanceContinuous Lidar_2D_Mapping Motions_Secure_Server_IUPUI ... more
Revision 1:238f6a0108e7, committed 2015-10-22
- Comitter:
- sventura3
- Date:
- Thu Oct 22 23:49:53 2015 +0000
- Parent:
- 0:7e6c07be1754
- Child:
- 2:b0deebd36eb3
- Commit message:
- Commenting
Changed in this revision
LidarLitev2.cpp | Show annotated file Show diff for this revision Revisions of this file |
LidarLitev2.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/LidarLitev2.cpp Thu Oct 22 18:52:54 2015 +0000 +++ b/LidarLitev2.cpp Thu Oct 22 23:49:53 2015 +0000 @@ -14,85 +14,77 @@ { char reset[2] = {0x00, 0x00}; //Register and value to Reset the Lidar to orgional settings char aquisition[2] = {0x04, 0x00}; //Register and value to set the acquisition to 1/3 default value - switch (config){ + switch (config) { case 0: //Deafault config nackack = 1; - while(nackack !=0) - { + while(nackack !=0) { //wait_ms(1); nackack = i2c.write(LidarLitev2_addr, reset, 2); // Resets the Lidar settings } - + break; case 1: nackack = 1; - while(nackack !=0) - { + while(nackack !=0) { //wait_ms(1); nackack = i2c.write(LidarLitev2_addr, aquisition, 2); // Set the acquisition to 1/3 default value } break; - } + } } void LidarLitev2::beginContinuous(bool modePinLow, char interval, char numberOfReadings,int LidarLitev2_addr) { char reg_freq[2] = {0x45, interval}; // Sets the time between measurements through reg 0x45 - + nackack = 1; - while(nackack !=0) - { - nackack =i2c.write(LidarLitev2_addr, reg_freq, 2, false); //Write to register 0x45 the interval time - } - // Set register 0x04 to 0x20 to look at "NON-default" value of velocity scale - // If you set bit 0 of 0x04 to "1" then the mode pin will be low when done + while(nackack !=0) { + nackack =i2c.write(LidarLitev2_addr, reg_freq, 2, false); //Write to register 0x45 the interval time + } + // Set register 0x04 to 0x20 to look at "NON-default" value of velocity scale + // If you set bit 0 of 0x04 to "1" then the mode pin will be low when done char reg_modePin[2] = {0x04, 0x21}; // Default modePin Setting if (!modePinLow) reg_modePin[1] = 0x20; - + nackack = 1; - while(nackack !=0) - { - nackack =i2c.write(LidarLitev2_addr, reg_modePin, 2, false); - } + while(nackack !=0) { + nackack =i2c.write(LidarLitev2_addr, reg_modePin, 2, false); + } char reg_numOfReadings[2] = {0x11, numberOfReadings}; // Set the number of readings through reg 0x11 - + nackack = 1; - while(nackack !=0) - { - nackack =i2c.write(LidarLitev2_addr, reg_numOfReadings, 2, false); // writes into Reg_numofreadings the number of readings desired - } + while(nackack !=0) { + nackack =i2c.write(LidarLitev2_addr, reg_numOfReadings, 2, false); // writes into Reg_numofreadings the number of readings desired + } char reg_reading_distance[2] = {0x00, 0x04}; // Iniates reg_readDistance nackack = 1; - while(nackack !=0) - { - nackack =i2c.write(LidarLitev2_addr, reg_reading_distance, 2, false); // writes into Reg_readDistance to begin continuos reading - } + while(nackack !=0) { + nackack =i2c.write(LidarLitev2_addr, reg_reading_distance, 2, false); // writes into Reg_readDistance to begin continuos reading + } } -int LidarLitev2::distance(bool stablizePreampFlag, bool takeReference, int LidarLitev2_addr){ +int LidarLitev2::distance(bool stablizePreampFlag, bool takeReference, int LidarLitev2_addr) +{ // Standard read distance protocol without continuous - + char reg_dc[2] = {0x00, 0x04}; // Iniates reg_readDistance if(!stablizePreampFlag) reg_dc[1] = 0x03; - + nackack = 1; - while(nackack !=0) - { + while(nackack !=0) { nackack = i2c.write(LidarLitev2_addr, reg_dc, 2); // writes into Reg_readDistance to begin a reading } - + char data[2]; char reg_read[1] = {0x8f}; // Register to read distance value from - + nackack = 1; - while(nackack !=0) - { + while(nackack !=0) { nackack = i2c.write(LidarLitev2_addr, reg_read, 1); // Ready to read from reg distance } - + nackack = 1; - while(nackack !=0) - { + while(nackack !=0) { nackack = i2c.read(LidarLitev2_addr, data, 2); // Read from reg distance 2bytes of information } int distance = ((uint8_t)data[0]<<8) + (uint8_t)data[1]; //Combine byes to a int to be returned as the distance measured @@ -103,17 +95,15 @@ { char data[2]; char reg_read[1] = {0x8f}; // Register to read distance value from - + nackack = 1; - while(nackack !=0) - { - nackack =i2c.write(LidarLitev2_addr, reg_read, 1); // Ready to read from reg distance - } + while(nackack !=0) { + nackack =i2c.write(LidarLitev2_addr, reg_read, 1); // Ready to read from reg distance + } nackack = 1; - while(nackack !=0) - { - nackack =i2c.read(LidarLitev2_addr, data, 2); // Read from reg distance 2bytes of information - } + while(nackack !=0) { + nackack =i2c.read(LidarLitev2_addr, data, 2); // Read from reg distance 2bytes of information + } int distance = ((uint8_t)data[0]<<8) + (uint8_t)data[1]; //Combine byes to a int to be returned as the distance measured return distance; }
--- a/LidarLitev2.h Thu Oct 22 18:52:54 2015 +0000 +++ b/LidarLitev2.h Thu Oct 22 23:49:53 2015 +0000 @@ -1,25 +1,59 @@ #ifndef LidarLitev2_H -#define LidarLitev2_H +#define LidarLitev2_H #include "mbed.h" +/** My LidarLite class +* Used for controlling and interacting with the LidarLitev2 +Example: +* @code +* //Measures distance from the lidarlite and prints it through serial +* #include "LidarLitev2.h" +* LidarLitev2 Lidar(p28, p27); +* Serial pc(USBTX,USBRX); +* +* +* Timer dt; +* int main() +* { +* +* pc.baud(115200); +* Lidar.configure(); +* dt.start(); +* while(1){ +* pc.printf("distance = %d cm frequency = %.2f Hz\n", Lidar.distance(), 1/dt.read()); +* dt.reset(); +* } +* } +* @endcode +* +* +*/ class LidarLitev2 { public: LidarLitev2(PinName sda, PinName scl, bool = true); // Constructor iniates I2C setup - void configure(int = 0, int = 0xc4); // Configure the mode and slave address + + /** Configure the different modes of the Lidar */ + void configure(int = 0, int = 0xc4); // Configure the mode and slave address + + /** Sets the Lidar to read continuously, indicating its ready to be read from by the modepin pulling down, a cerntain amount of times */ void beginContinuous(bool = true, char = 0x04, char = 0xff, int = 0xc4); //Enable if using continous setup with mode from Lidar and pulldown + + int distance(bool = true, bool = true, int = 0xc4); //Calclulates distance through I2C protocol of activating sensor with a write, then a write for the register, and a read. // Returns distance as a integer in cm + - int distanceContinuous(int = 0xc4); // 1.) Set Lidar circuit for continuous mode - // 2.) utilize the beginContinous function and configure as desired - // This function returns distance without any need to activate the lidar senore through a write command, - // instead the mode pin pulls down when the lidar is ready for a read + int distanceContinuous(int = 0xc4); + // 1.) Set Lidar circuit for continuous mode + // 2.) utilize the beginContinous function and configure as desired + // This function returns distance without any need to activate the lidar senore through a write command, + // instead the mode pin pulls down when the lidar is ready for a read // Returns distance as a integer in cm - + private: // I2C Functions // ///////////////////