Library for easy interface of LidarLite with mbed using I2C
Dependents: LidarLite_mbed Lidar_Magnet
Library for easy interface of LidarLite with mbed using I2C
LidarLite.h@1:543c0d15d3b3, 2016-06-05 (annotated)
- Committer:
- akashvibhute
- Date:
- Sun Jun 05 02:44:57 2016 +0000
- Revision:
- 1:543c0d15d3b3
- Parent:
- 0:8e6304ab38d2
updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akashvibhute | 1:543c0d15d3b3 | 1 | /** LidarLite mBed I2C driver |
akashvibhute | 1:543c0d15d3b3 | 2 | * |
akashvibhute | 1:543c0d15d3b3 | 3 | * @author Akash Vibhute |
akashvibhute | 1:543c0d15d3b3 | 4 | * @author < akash . roboticist [at] gmail . com > |
akashvibhute | 1:543c0d15d3b3 | 5 | * @version 0.1 |
akashvibhute | 1:543c0d15d3b3 | 6 | * @date Feb/17/2015 - v0.1 - First version of library, tested using LPC1768 [powered via mbed 3.3v, no additional pullups on I2C necessary] |
akashvibhute | 1:543c0d15d3b3 | 7 | * @date June/05/2016 - Doc update |
akashvibhute | 1:543c0d15d3b3 | 8 | * |
akashvibhute | 1:543c0d15d3b3 | 9 | * @section LICENSE |
akashvibhute | 1:543c0d15d3b3 | 10 | * |
akashvibhute | 1:543c0d15d3b3 | 11 | * Copyright (c) 2015 Akash Vibhute |
akashvibhute | 1:543c0d15d3b3 | 12 | * |
akashvibhute | 1:543c0d15d3b3 | 13 | * Licensed under the Apache License, Version 2.0 (the "License"); |
akashvibhute | 1:543c0d15d3b3 | 14 | * you may not use this file except in compliance with the License. |
akashvibhute | 1:543c0d15d3b3 | 15 | * You may obtain a copy of the License at |
akashvibhute | 1:543c0d15d3b3 | 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
akashvibhute | 1:543c0d15d3b3 | 17 | * |
akashvibhute | 1:543c0d15d3b3 | 18 | * Unless required by applicable law or agreed to in writing, software |
akashvibhute | 1:543c0d15d3b3 | 19 | * distributed under the License is distributed on an "AS IS" BASIS, |
akashvibhute | 1:543c0d15d3b3 | 20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
akashvibhute | 1:543c0d15d3b3 | 21 | * See the License for the specific language governing permissions and |
akashvibhute | 1:543c0d15d3b3 | 22 | * limitations under the License. |
akashvibhute | 1:543c0d15d3b3 | 23 | * |
akashvibhute | 1:543c0d15d3b3 | 24 | * The above copyright notice and this permission notice shall be included in |
akashvibhute | 1:543c0d15d3b3 | 25 | * all copies or substantial portions of the Software. |
akashvibhute | 1:543c0d15d3b3 | 26 | */ |
akashvibhute | 1:543c0d15d3b3 | 27 | |
akashvibhute | 0:8e6304ab38d2 | 28 | #ifndef LidarLite_H |
akashvibhute | 1:543c0d15d3b3 | 29 | #define LidarLite_H |
akashvibhute | 1:543c0d15d3b3 | 30 | |
akashvibhute | 0:8e6304ab38d2 | 31 | #include <mbed.h> |
akashvibhute | 0:8e6304ab38d2 | 32 | |
akashvibhute | 1:543c0d15d3b3 | 33 | /* Default I2C Address of LIDAR-Lite. */ |
akashvibhute | 1:543c0d15d3b3 | 34 | #define LIDARLite_WriteAdr 0xc4 /// 8-bit slave write address |
akashvibhute | 1:543c0d15d3b3 | 35 | #define LIDARLite_ReadAdr 0xc5 /// 8-bit slave read address |
akashvibhute | 0:8e6304ab38d2 | 36 | |
akashvibhute | 1:543c0d15d3b3 | 37 | /* Commands */ |
akashvibhute | 1:543c0d15d3b3 | 38 | #define SET_CommandReg 0x00 /// Register to write to initiate ranging |
akashvibhute | 1:543c0d15d3b3 | 39 | #define AcqMode 0x04 /// Value to set in control register to initiate ranging |
akashvibhute | 1:543c0d15d3b3 | 40 | |
akashvibhute | 1:543c0d15d3b3 | 41 | /* Read Registers */ |
akashvibhute | 1:543c0d15d3b3 | 42 | #define GET_DistanceHBReg 0x0f /// High byte of distance reading data |
akashvibhute | 1:543c0d15d3b3 | 43 | #define GET_DistanceLBReg 0x10 /// Low byte of distance reading data |
akashvibhute | 1:543c0d15d3b3 | 44 | #define GET_Distance2BReg 0x8f /// Register to get both High and Low bytes of distance reading data in 1 call |
akashvibhute | 1:543c0d15d3b3 | 45 | #define GET_VelocityReg 0x09 /// Velocity measutement data |
akashvibhute | 0:8e6304ab38d2 | 46 | |
akashvibhute | 1:543c0d15d3b3 | 47 | /** LidarLite |
akashvibhute | 1:543c0d15d3b3 | 48 | * |
akashvibhute | 1:543c0d15d3b3 | 49 | * @section DESCRIPTION |
akashvibhute | 1:543c0d15d3b3 | 50 | * This is the LIDAR Lite, a compact high performance optical distance measurement |
akashvibhute | 1:543c0d15d3b3 | 51 | * sensor from PulsedLight. The LIDAR Lite is ideal when used in drone, robot, or |
akashvibhute | 1:543c0d15d3b3 | 52 | * unmanned vehicle situations where you need a reliable and powerful proximity |
akashvibhute | 1:543c0d15d3b3 | 53 | * sensor but don’t possess a lot of space. All you need to communicate with this |
akashvibhute | 1:543c0d15d3b3 | 54 | * sensor is a standard I2C or PWM interface and the LIDAR Lite, with its range of |
akashvibhute | 1:543c0d15d3b3 | 55 | * up to 40 meters, will be yours to command! |
akashvibhute | 1:543c0d15d3b3 | 56 | * |
akashvibhute | 1:543c0d15d3b3 | 57 | * Each LIDAR Lite features an edge emitting, 905nm (75um, 1 watt, 4 mrad, 14mm optic), |
akashvibhute | 1:543c0d15d3b3 | 58 | * single stripe laser transmitter and a surface mount PIN, 3° FOV with 14mm optics receiver. |
akashvibhute | 1:543c0d15d3b3 | 59 | * The LIDAR Lite operates between 4.7 - 5.5VDC with a max of 6V DC and has a current |
akashvibhute | 1:543c0d15d3b3 | 60 | * consumption rate of <100mA at continuous operation. On top of everything else, the |
akashvibhute | 1:543c0d15d3b3 | 61 | * LIDAR Lite has an acquisition time of only 0.02 seconds or less and can be interfaced |
akashvibhute | 1:543c0d15d3b3 | 62 | * via I2C or PWM. |
akashvibhute | 1:543c0d15d3b3 | 63 | * |
akashvibhute | 1:543c0d15d3b3 | 64 | * Note: The LIDAR Lite is designated as Class 1 during all procedures of operation, |
akashvibhute | 1:543c0d15d3b3 | 65 | * however operating the sensor without its optics or housing or making modifications to the |
akashvibhute | 1:543c0d15d3b3 | 66 | * housing can result in direct exposure to laser radiation and the risk of permanent eye damage. |
akashvibhute | 1:543c0d15d3b3 | 67 | * Direct eye contact should be avoided and under no circumstances should you ever stare |
akashvibhute | 1:543c0d15d3b3 | 68 | * straight into the emitter. |
akashvibhute | 1:543c0d15d3b3 | 69 | * |
akashvibhute | 1:543c0d15d3b3 | 70 | * Features: |
akashvibhute | 1:543c0d15d3b3 | 71 | * Range: 0-40m Laser Emitter |
akashvibhute | 1:543c0d15d3b3 | 72 | * Accuracy: +/- 0.025m |
akashvibhute | 1:543c0d15d3b3 | 73 | * Power: 4.7 - 5.5V DC Nominal, Maximum 6V DC |
akashvibhute | 1:543c0d15d3b3 | 74 | * Current Consumption: <100mA continuous operation |
akashvibhute | 1:543c0d15d3b3 | 75 | * Acquisition Time: < 0.02 sec |
akashvibhute | 1:543c0d15d3b3 | 76 | * Rep Rate: 1-100Hz |
akashvibhute | 1:543c0d15d3b3 | 77 | * Interface: I2C or PWM |
akashvibhute | 1:543c0d15d3b3 | 78 | * |
akashvibhute | 1:543c0d15d3b3 | 79 | * Example: |
akashvibhute | 1:543c0d15d3b3 | 80 | * @code |
akashvibhute | 1:543c0d15d3b3 | 81 | * #include "mbed.h" |
akashvibhute | 1:543c0d15d3b3 | 82 | * #include "LidarLite.h" |
akashvibhute | 1:543c0d15d3b3 | 83 | * |
akashvibhute | 1:543c0d15d3b3 | 84 | * #define LIDARLite1_SDA p9 //SDA pin on LPC1768 |
akashvibhute | 1:543c0d15d3b3 | 85 | * #define LIDARLite1_SCL p10 //SCL pin on LPC1768 |
akashvibhute | 1:543c0d15d3b3 | 86 | * |
akashvibhute | 1:543c0d15d3b3 | 87 | * LidarLite sensor1(LIDARLite1_SDA, LIDARLite1_SCL); //Define LIDAR Lite sensor 1 |
akashvibhute | 1:543c0d15d3b3 | 88 | * |
akashvibhute | 1:543c0d15d3b3 | 89 | * Timer dt; |
akashvibhute | 1:543c0d15d3b3 | 90 | * |
akashvibhute | 1:543c0d15d3b3 | 91 | * Serial pc(USBTX,USBRX); |
akashvibhute | 1:543c0d15d3b3 | 92 | * |
akashvibhute | 1:543c0d15d3b3 | 93 | * int main() |
akashvibhute | 1:543c0d15d3b3 | 94 | * { |
akashvibhute | 1:543c0d15d3b3 | 95 | * pc.baud(921600); |
akashvibhute | 1:543c0d15d3b3 | 96 | * dt.start(); |
akashvibhute | 1:543c0d15d3b3 | 97 | * |
akashvibhute | 1:543c0d15d3b3 | 98 | * while(1) |
akashvibhute | 1:543c0d15d3b3 | 99 | * { |
akashvibhute | 1:543c0d15d3b3 | 100 | * //sensor1.refreshRange(); |
akashvibhute | 1:543c0d15d3b3 | 101 | * //sensor1.refreshVelocity(); |
akashvibhute | 1:543c0d15d3b3 | 102 | * sensor1.refreshRangeVelocity(); |
akashvibhute | 1:543c0d15d3b3 | 103 | * |
akashvibhute | 1:543c0d15d3b3 | 104 | * pc.printf("range: %d cm, velocity: %d cm/s, rate: %.2f Hz\n", sensor1.getRange_cm(), sensor1.getVelocity_cms(), 1/dt.read()); |
akashvibhute | 1:543c0d15d3b3 | 105 | * dt.reset(); |
akashvibhute | 1:543c0d15d3b3 | 106 | * } |
akashvibhute | 1:543c0d15d3b3 | 107 | * |
akashvibhute | 1:543c0d15d3b3 | 108 | * } |
akashvibhute | 1:543c0d15d3b3 | 109 | * @endcode |
akashvibhute | 1:543c0d15d3b3 | 110 | */ |
akashvibhute | 0:8e6304ab38d2 | 111 | class LidarLite |
akashvibhute | 0:8e6304ab38d2 | 112 | { |
akashvibhute | 1:543c0d15d3b3 | 113 | public: |
akashvibhute | 1:543c0d15d3b3 | 114 | |
akashvibhute | 1:543c0d15d3b3 | 115 | /** Creates a LidarLite instance connected to specified I2C pins |
akashvibhute | 1:543c0d15d3b3 | 116 | * |
akashvibhute | 1:543c0d15d3b3 | 117 | * @param sda I2C-bus SDA pin |
akashvibhute | 1:543c0d15d3b3 | 118 | * @param scl I2C-bus SCL pin |
akashvibhute | 1:543c0d15d3b3 | 119 | */ |
akashvibhute | 1:543c0d15d3b3 | 120 | LidarLite(PinName sda, PinName scl); |
akashvibhute | 1:543c0d15d3b3 | 121 | |
akashvibhute | 1:543c0d15d3b3 | 122 | /** Queries distance (range) and velocity registers of the sensor |
akashvibhute | 1:543c0d15d3b3 | 123 | * |
akashvibhute | 1:543c0d15d3b3 | 124 | */ |
akashvibhute | 1:543c0d15d3b3 | 125 | void refreshRangeVelocity(); |
akashvibhute | 1:543c0d15d3b3 | 126 | |
akashvibhute | 1:543c0d15d3b3 | 127 | /** Queries velocity register of the sensor |
akashvibhute | 1:543c0d15d3b3 | 128 | * |
akashvibhute | 1:543c0d15d3b3 | 129 | */ |
akashvibhute | 1:543c0d15d3b3 | 130 | void refreshVelocity(); |
akashvibhute | 1:543c0d15d3b3 | 131 | |
akashvibhute | 1:543c0d15d3b3 | 132 | /** Queries distance (range) register of the sensor |
akashvibhute | 1:543c0d15d3b3 | 133 | * |
akashvibhute | 1:543c0d15d3b3 | 134 | */ |
akashvibhute | 1:543c0d15d3b3 | 135 | void refreshRange(); |
akashvibhute | 1:543c0d15d3b3 | 136 | |
akashvibhute | 1:543c0d15d3b3 | 137 | /** Returns distance in cm (range) measured by sensor as read by earlier refresh function |
akashvibhute | 1:543c0d15d3b3 | 138 | * |
akashvibhute | 1:543c0d15d3b3 | 139 | */ |
akashvibhute | 1:543c0d15d3b3 | 140 | int16_t getRange_cm(); |
akashvibhute | 1:543c0d15d3b3 | 141 | |
akashvibhute | 1:543c0d15d3b3 | 142 | /** Returns velocity in cm/s measured by sensor as read by earlier refresh function |
akashvibhute | 1:543c0d15d3b3 | 143 | * |
akashvibhute | 1:543c0d15d3b3 | 144 | */ |
akashvibhute | 1:543c0d15d3b3 | 145 | int16_t getVelocity_cms(); |
akashvibhute | 1:543c0d15d3b3 | 146 | |
akashvibhute | 1:543c0d15d3b3 | 147 | private: |
akashvibhute | 1:543c0d15d3b3 | 148 | I2C* i2c_; |
akashvibhute | 1:543c0d15d3b3 | 149 | int16_t distance_LL; |
akashvibhute | 1:543c0d15d3b3 | 150 | int16_t velocity_LL; |
akashvibhute | 0:8e6304ab38d2 | 151 | }; |
akashvibhute | 0:8e6304ab38d2 | 152 | |
akashvibhute | 0:8e6304ab38d2 | 153 | #endif /* LidarLite_H */ |