This library uses the power enable pin on the lidar lite unit to enable multi-unit sampling. Attempted to add an additional change address function but didn't quite complete it. Thus, functionality of multiple units at the same time is achieved by the power enable pin and not by addressing the units separately.

Dependencies:   mbed

Fork of LidarLite by Akash Vibhute

Committer:
akashvibhute
Date:
Tue Feb 17 12:01:04 2015 +0000
Revision:
0:8e6304ab38d2
Child:
1:a01dc8b52be4
v0.1, 17/Feb/2015 - First version of library, tested using LPC1768 [powered via mbed 3.3v, no additional pullups on I2C necessary]

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akashvibhute 0:8e6304ab38d2 1 /*
akashvibhute 0:8e6304ab38d2 2 * Library for easy interface of LidarLite with mbed using I2C
akashvibhute 0:8e6304ab38d2 3 *
akashvibhute 0:8e6304ab38d2 4 * Akash Vibhute <akash . roboticist [at] gmail . com>
akashvibhute 0:8e6304ab38d2 5 *
akashvibhute 0:8e6304ab38d2 6 * v0.1, 17/Feb/2015 - First version of library, tested using LPC1768 [powered via mbed 3.3v, no additional pullups on I2C necessary]
akashvibhute 0:8e6304ab38d2 7 *
akashvibhute 0:8e6304ab38d2 8 */
akashvibhute 0:8e6304ab38d2 9
akashvibhute 0:8e6304ab38d2 10 #include "LidarLite.h"
akashvibhute 0:8e6304ab38d2 11
akashvibhute 0:8e6304ab38d2 12 LidarLite::LidarLite(PinName sda, PinName scl)
akashvibhute 0:8e6304ab38d2 13 {
akashvibhute 0:8e6304ab38d2 14 i2c_ = new I2C(sda, scl);
akashvibhute 0:8e6304ab38d2 15 i2c_->frequency(100000); //I2C @ 100kHz
akashvibhute 0:8e6304ab38d2 16 wait(0.5);
akashvibhute 0:8e6304ab38d2 17 }
akashvibhute 0:8e6304ab38d2 18
akashvibhute 0:8e6304ab38d2 19 int16_t LidarLite::getRange_cm()
akashvibhute 0:8e6304ab38d2 20 {
akashvibhute 0:8e6304ab38d2 21 return(distance_LL);
akashvibhute 0:8e6304ab38d2 22 }
akashvibhute 0:8e6304ab38d2 23
akashvibhute 0:8e6304ab38d2 24 int16_t LidarLite::getVelocity_cms()
akashvibhute 0:8e6304ab38d2 25 {
akashvibhute 0:8e6304ab38d2 26 if(velocity_LL < 127)
akashvibhute 0:8e6304ab38d2 27 return(velocity_LL*10);
akashvibhute 0:8e6304ab38d2 28 else
akashvibhute 0:8e6304ab38d2 29 return((velocity_LL-256)*10);
akashvibhute 0:8e6304ab38d2 30 }
akashvibhute 0:8e6304ab38d2 31
akashvibhute 0:8e6304ab38d2 32 void LidarLite::refreshRange()
akashvibhute 0:8e6304ab38d2 33 {
akashvibhute 0:8e6304ab38d2 34 uint8_t nackack;
akashvibhute 0:8e6304ab38d2 35
akashvibhute 0:8e6304ab38d2 36 char write[2]={SET_CommandReg, AcqMode};
akashvibhute 0:8e6304ab38d2 37 char read_dist[1]={GET_Distance2BReg};
akashvibhute 0:8e6304ab38d2 38 char read_vel[1]={GET_VelocityReg};
akashvibhute 0:8e6304ab38d2 39
akashvibhute 0:8e6304ab38d2 40 char dist[2];
akashvibhute 0:8e6304ab38d2 41 char vel[1];
akashvibhute 0:8e6304ab38d2 42
akashvibhute 0:8e6304ab38d2 43 nackack=1;
akashvibhute 0:8e6304ab38d2 44 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 45 {
akashvibhute 0:8e6304ab38d2 46 wait_ms(1);
akashvibhute 0:8e6304ab38d2 47 nackack = i2c_->write(LIDARLite_WriteAdr, write, 2);
akashvibhute 0:8e6304ab38d2 48 }
akashvibhute 0:8e6304ab38d2 49
akashvibhute 0:8e6304ab38d2 50 nackack=1;
akashvibhute 0:8e6304ab38d2 51 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 52 {
akashvibhute 0:8e6304ab38d2 53 wait_ms(1);
akashvibhute 0:8e6304ab38d2 54 nackack = i2c_->write(LIDARLite_WriteAdr, read_dist, 1);
akashvibhute 0:8e6304ab38d2 55 }
akashvibhute 0:8e6304ab38d2 56
akashvibhute 0:8e6304ab38d2 57 nackack=1;
akashvibhute 0:8e6304ab38d2 58 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 59 {
akashvibhute 0:8e6304ab38d2 60 wait_ms(1);
akashvibhute 0:8e6304ab38d2 61 nackack = i2c_->read(LIDARLite_ReadAdr, dist, 2);
akashvibhute 0:8e6304ab38d2 62 }
akashvibhute 0:8e6304ab38d2 63 distance_LL = ((uint16_t)dist[0] << 8) + (uint16_t)dist[1];
akashvibhute 0:8e6304ab38d2 64 }
akashvibhute 0:8e6304ab38d2 65
akashvibhute 0:8e6304ab38d2 66 void LidarLite::refreshVelocity()
akashvibhute 0:8e6304ab38d2 67 {
akashvibhute 0:8e6304ab38d2 68 uint8_t nackack;
akashvibhute 0:8e6304ab38d2 69
akashvibhute 0:8e6304ab38d2 70 char write[2]={SET_CommandReg, AcqMode};
akashvibhute 0:8e6304ab38d2 71 char read_dist[1]={GET_Distance2BReg};
akashvibhute 0:8e6304ab38d2 72 char read_vel[1]={GET_VelocityReg};
akashvibhute 0:8e6304ab38d2 73
akashvibhute 0:8e6304ab38d2 74 char dist[2];
akashvibhute 0:8e6304ab38d2 75 char vel[1];
akashvibhute 0:8e6304ab38d2 76
akashvibhute 0:8e6304ab38d2 77 nackack=1;
akashvibhute 0:8e6304ab38d2 78 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 79 {
akashvibhute 0:8e6304ab38d2 80 wait_ms(1);
akashvibhute 0:8e6304ab38d2 81 nackack = i2c_->write(LIDARLite_WriteAdr, write, 2);
akashvibhute 0:8e6304ab38d2 82 }
akashvibhute 0:8e6304ab38d2 83
akashvibhute 0:8e6304ab38d2 84 nackack=1;
akashvibhute 0:8e6304ab38d2 85 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 86 {
akashvibhute 0:8e6304ab38d2 87 wait_ms(1);
akashvibhute 0:8e6304ab38d2 88 nackack = i2c_->write(LIDARLite_WriteAdr, read_vel, 1);
akashvibhute 0:8e6304ab38d2 89 }
akashvibhute 0:8e6304ab38d2 90
akashvibhute 0:8e6304ab38d2 91 nackack=1;
akashvibhute 0:8e6304ab38d2 92 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 93 {
akashvibhute 0:8e6304ab38d2 94 wait_ms(1);
akashvibhute 0:8e6304ab38d2 95 nackack = i2c_->read(LIDARLite_ReadAdr, vel, 1);
akashvibhute 0:8e6304ab38d2 96 }
akashvibhute 0:8e6304ab38d2 97 velocity_LL = (uint16_t)vel[0];
akashvibhute 0:8e6304ab38d2 98 }
akashvibhute 0:8e6304ab38d2 99
akashvibhute 0:8e6304ab38d2 100 void LidarLite::refreshRangeVelocity()
akashvibhute 0:8e6304ab38d2 101 {
akashvibhute 0:8e6304ab38d2 102 uint8_t nackack;
akashvibhute 0:8e6304ab38d2 103
akashvibhute 0:8e6304ab38d2 104 char write[2]={SET_CommandReg, AcqMode};
akashvibhute 0:8e6304ab38d2 105 char read_dist[1]={GET_Distance2BReg};
akashvibhute 0:8e6304ab38d2 106 char read_vel[1]={GET_VelocityReg};
akashvibhute 0:8e6304ab38d2 107
akashvibhute 0:8e6304ab38d2 108 char dist[2];
akashvibhute 0:8e6304ab38d2 109 char vel[1];
akashvibhute 0:8e6304ab38d2 110
akashvibhute 0:8e6304ab38d2 111 nackack=1;
akashvibhute 0:8e6304ab38d2 112 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 113 {
akashvibhute 0:8e6304ab38d2 114 wait_ms(1);
akashvibhute 0:8e6304ab38d2 115 nackack = i2c_->write(LIDARLite_WriteAdr, write, 2);
akashvibhute 0:8e6304ab38d2 116 }
akashvibhute 0:8e6304ab38d2 117
akashvibhute 0:8e6304ab38d2 118 nackack=1;
akashvibhute 0:8e6304ab38d2 119 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 120 {
akashvibhute 0:8e6304ab38d2 121 wait_ms(1);
akashvibhute 0:8e6304ab38d2 122 nackack = i2c_->write(LIDARLite_WriteAdr, read_dist, 1);
akashvibhute 0:8e6304ab38d2 123 }
akashvibhute 0:8e6304ab38d2 124
akashvibhute 0:8e6304ab38d2 125 nackack=1;
akashvibhute 0:8e6304ab38d2 126 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 127 {
akashvibhute 0:8e6304ab38d2 128 wait_ms(1);
akashvibhute 0:8e6304ab38d2 129 nackack = i2c_->read(LIDARLite_ReadAdr, dist, 2);
akashvibhute 0:8e6304ab38d2 130 }
akashvibhute 0:8e6304ab38d2 131 distance_LL = ((uint16_t)dist[0] << 8) + (uint16_t)dist[1];
akashvibhute 0:8e6304ab38d2 132
akashvibhute 0:8e6304ab38d2 133
akashvibhute 0:8e6304ab38d2 134 nackack=1;
akashvibhute 0:8e6304ab38d2 135 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 136 {
akashvibhute 0:8e6304ab38d2 137 wait_ms(1);
akashvibhute 0:8e6304ab38d2 138 nackack = i2c_->write(LIDARLite_WriteAdr, read_vel, 1);
akashvibhute 0:8e6304ab38d2 139 }
akashvibhute 0:8e6304ab38d2 140
akashvibhute 0:8e6304ab38d2 141 nackack=1;
akashvibhute 0:8e6304ab38d2 142 while(nackack !=0)
akashvibhute 0:8e6304ab38d2 143 {
akashvibhute 0:8e6304ab38d2 144 wait_ms(1);
akashvibhute 0:8e6304ab38d2 145 nackack = i2c_->read(LIDARLite_ReadAdr, vel, 1);
akashvibhute 0:8e6304ab38d2 146 }
akashvibhute 0:8e6304ab38d2 147 velocity_LL = (uint16_t)vel[0];
akashvibhute 0:8e6304ab38d2 148 }