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
LidarLitev2.cpp@0:7e6c07be1754, 2015-10-22 (annotated)
- Committer:
- sventura3
- Date:
- Thu Oct 22 18:52:54 2015 +0000
- Revision:
- 0:7e6c07be1754
- Child:
- 1:238f6a0108e7
LidarLitev2 Library for distance reading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sventura3 | 0:7e6c07be1754 | 1 | #include "LidarLitev2.h" |
sventura3 | 0:7e6c07be1754 | 2 | #include "math.h" |
sventura3 | 0:7e6c07be1754 | 3 | |
sventura3 | 0:7e6c07be1754 | 4 | int nackack; |
sventura3 | 0:7e6c07be1754 | 5 | //Constructor iniating the I2C connection |
sventura3 | 0:7e6c07be1754 | 6 | LidarLitev2::LidarLitev2(PinName sda, PinName scl, bool fast) : i2c(sda, scl) |
sventura3 | 0:7e6c07be1754 | 7 | { |
sventura3 | 0:7e6c07be1754 | 8 | if (fast) i2c.frequency(400000); //I2C @ 400kHz |
sventura3 | 0:7e6c07be1754 | 9 | else i2c.frequency(100000); //I2C @ 100Khz |
sventura3 | 0:7e6c07be1754 | 10 | wait(0.2); |
sventura3 | 0:7e6c07be1754 | 11 | } |
sventura3 | 0:7e6c07be1754 | 12 | |
sventura3 | 0:7e6c07be1754 | 13 | void LidarLitev2::configure(int config, int LidarLitev2_addr) |
sventura3 | 0:7e6c07be1754 | 14 | { |
sventura3 | 0:7e6c07be1754 | 15 | char reset[2] = {0x00, 0x00}; //Register and value to Reset the Lidar to orgional settings |
sventura3 | 0:7e6c07be1754 | 16 | char aquisition[2] = {0x04, 0x00}; //Register and value to set the acquisition to 1/3 default value |
sventura3 | 0:7e6c07be1754 | 17 | switch (config){ |
sventura3 | 0:7e6c07be1754 | 18 | case 0: //Deafault config |
sventura3 | 0:7e6c07be1754 | 19 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 20 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 21 | { |
sventura3 | 0:7e6c07be1754 | 22 | //wait_ms(1); |
sventura3 | 0:7e6c07be1754 | 23 | nackack = i2c.write(LidarLitev2_addr, reset, 2); // Resets the Lidar settings |
sventura3 | 0:7e6c07be1754 | 24 | } |
sventura3 | 0:7e6c07be1754 | 25 | |
sventura3 | 0:7e6c07be1754 | 26 | break; |
sventura3 | 0:7e6c07be1754 | 27 | case 1: |
sventura3 | 0:7e6c07be1754 | 28 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 29 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 30 | { |
sventura3 | 0:7e6c07be1754 | 31 | //wait_ms(1); |
sventura3 | 0:7e6c07be1754 | 32 | nackack = i2c.write(LidarLitev2_addr, aquisition, 2); // Set the acquisition to 1/3 default value |
sventura3 | 0:7e6c07be1754 | 33 | } |
sventura3 | 0:7e6c07be1754 | 34 | break; |
sventura3 | 0:7e6c07be1754 | 35 | } |
sventura3 | 0:7e6c07be1754 | 36 | } |
sventura3 | 0:7e6c07be1754 | 37 | |
sventura3 | 0:7e6c07be1754 | 38 | void LidarLitev2::beginContinuous(bool modePinLow, char interval, char numberOfReadings,int LidarLitev2_addr) |
sventura3 | 0:7e6c07be1754 | 39 | { |
sventura3 | 0:7e6c07be1754 | 40 | char reg_freq[2] = {0x45, interval}; // Sets the time between measurements through reg 0x45 |
sventura3 | 0:7e6c07be1754 | 41 | |
sventura3 | 0:7e6c07be1754 | 42 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 43 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 44 | { |
sventura3 | 0:7e6c07be1754 | 45 | nackack =i2c.write(LidarLitev2_addr, reg_freq, 2, false); //Write to register 0x45 the interval time |
sventura3 | 0:7e6c07be1754 | 46 | } |
sventura3 | 0:7e6c07be1754 | 47 | // Set register 0x04 to 0x20 to look at "NON-default" value of velocity scale |
sventura3 | 0:7e6c07be1754 | 48 | // If you set bit 0 of 0x04 to "1" then the mode pin will be low when done |
sventura3 | 0:7e6c07be1754 | 49 | char reg_modePin[2] = {0x04, 0x21}; // Default modePin Setting |
sventura3 | 0:7e6c07be1754 | 50 | if (!modePinLow) reg_modePin[1] = 0x20; |
sventura3 | 0:7e6c07be1754 | 51 | |
sventura3 | 0:7e6c07be1754 | 52 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 53 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 54 | { |
sventura3 | 0:7e6c07be1754 | 55 | nackack =i2c.write(LidarLitev2_addr, reg_modePin, 2, false); |
sventura3 | 0:7e6c07be1754 | 56 | } |
sventura3 | 0:7e6c07be1754 | 57 | char reg_numOfReadings[2] = {0x11, numberOfReadings}; // Set the number of readings through reg 0x11 |
sventura3 | 0:7e6c07be1754 | 58 | |
sventura3 | 0:7e6c07be1754 | 59 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 60 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 61 | { |
sventura3 | 0:7e6c07be1754 | 62 | nackack =i2c.write(LidarLitev2_addr, reg_numOfReadings, 2, false); // writes into Reg_numofreadings the number of readings desired |
sventura3 | 0:7e6c07be1754 | 63 | } |
sventura3 | 0:7e6c07be1754 | 64 | char reg_reading_distance[2] = {0x00, 0x04}; // Iniates reg_readDistance |
sventura3 | 0:7e6c07be1754 | 65 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 66 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 67 | { |
sventura3 | 0:7e6c07be1754 | 68 | nackack =i2c.write(LidarLitev2_addr, reg_reading_distance, 2, false); // writes into Reg_readDistance to begin continuos reading |
sventura3 | 0:7e6c07be1754 | 69 | } |
sventura3 | 0:7e6c07be1754 | 70 | } |
sventura3 | 0:7e6c07be1754 | 71 | |
sventura3 | 0:7e6c07be1754 | 72 | int LidarLitev2::distance(bool stablizePreampFlag, bool takeReference, int LidarLitev2_addr){ |
sventura3 | 0:7e6c07be1754 | 73 | // Standard read distance protocol without continuous |
sventura3 | 0:7e6c07be1754 | 74 | |
sventura3 | 0:7e6c07be1754 | 75 | char reg_dc[2] = {0x00, 0x04}; // Iniates reg_readDistance |
sventura3 | 0:7e6c07be1754 | 76 | if(!stablizePreampFlag) reg_dc[1] = 0x03; |
sventura3 | 0:7e6c07be1754 | 77 | |
sventura3 | 0:7e6c07be1754 | 78 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 79 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 80 | { |
sventura3 | 0:7e6c07be1754 | 81 | nackack = i2c.write(LidarLitev2_addr, reg_dc, 2); // writes into Reg_readDistance to begin a reading |
sventura3 | 0:7e6c07be1754 | 82 | } |
sventura3 | 0:7e6c07be1754 | 83 | |
sventura3 | 0:7e6c07be1754 | 84 | char data[2]; |
sventura3 | 0:7e6c07be1754 | 85 | char reg_read[1] = {0x8f}; // Register to read distance value from |
sventura3 | 0:7e6c07be1754 | 86 | |
sventura3 | 0:7e6c07be1754 | 87 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 88 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 89 | { |
sventura3 | 0:7e6c07be1754 | 90 | nackack = i2c.write(LidarLitev2_addr, reg_read, 1); // Ready to read from reg distance |
sventura3 | 0:7e6c07be1754 | 91 | } |
sventura3 | 0:7e6c07be1754 | 92 | |
sventura3 | 0:7e6c07be1754 | 93 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 94 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 95 | { |
sventura3 | 0:7e6c07be1754 | 96 | nackack = i2c.read(LidarLitev2_addr, data, 2); // Read from reg distance 2bytes of information |
sventura3 | 0:7e6c07be1754 | 97 | } |
sventura3 | 0:7e6c07be1754 | 98 | int distance = ((uint8_t)data[0]<<8) + (uint8_t)data[1]; //Combine byes to a int to be returned as the distance measured |
sventura3 | 0:7e6c07be1754 | 99 | return distance; |
sventura3 | 0:7e6c07be1754 | 100 | } |
sventura3 | 0:7e6c07be1754 | 101 | |
sventura3 | 0:7e6c07be1754 | 102 | int LidarLitev2::distanceContinuous(int LidarLitev2_addr) |
sventura3 | 0:7e6c07be1754 | 103 | { |
sventura3 | 0:7e6c07be1754 | 104 | char data[2]; |
sventura3 | 0:7e6c07be1754 | 105 | char reg_read[1] = {0x8f}; // Register to read distance value from |
sventura3 | 0:7e6c07be1754 | 106 | |
sventura3 | 0:7e6c07be1754 | 107 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 108 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 109 | { |
sventura3 | 0:7e6c07be1754 | 110 | nackack =i2c.write(LidarLitev2_addr, reg_read, 1); // Ready to read from reg distance |
sventura3 | 0:7e6c07be1754 | 111 | } |
sventura3 | 0:7e6c07be1754 | 112 | nackack = 1; |
sventura3 | 0:7e6c07be1754 | 113 | while(nackack !=0) |
sventura3 | 0:7e6c07be1754 | 114 | { |
sventura3 | 0:7e6c07be1754 | 115 | nackack =i2c.read(LidarLitev2_addr, data, 2); // Read from reg distance 2bytes of information |
sventura3 | 0:7e6c07be1754 | 116 | } |
sventura3 | 0:7e6c07be1754 | 117 | int distance = ((uint8_t)data[0]<<8) + (uint8_t)data[1]; //Combine byes to a int to be returned as the distance measured |
sventura3 | 0:7e6c07be1754 | 118 | return distance; |
sventura3 | 0:7e6c07be1754 | 119 | } |