BenV
Posts: 2
Joined: Fri Mar 31, 2017 5:27 am

Lidar Lite V3 - Raspberry Pi - Python

Fri Mar 31, 2017 5:32 am

Hi guys,

Pretty new to this! Is there any library I can use to get distance and velocity from the Lidar Lite v3? Im on a Pi 3 and using Python.

Thanks,
B

wh7qq
Posts: 610
Joined: Thu Oct 09, 2014 2:50 am

Re: Lidar Lite V3 - Raspberry Pi - Python

Sat Apr 01, 2017 12:26 am

If you google "lidar lite raspberry pi" you will find a lot of references to doing just that.
Sent by WH7QQ from an RPi3/Raspbian/pixel desktop.

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Sat Apr 01, 2017 7:30 am

Here's mine:

Code: Select all

####################################################################################################
#
#  Garmin LIDAR-Lite V3 range finder
#
####################################################################################################
class GLL:
    i2c = None

    __GLL_ACQ_COMMAND       = 0x00
    __GLL_STATUS            = 0x01
    __GLL_SIG_COUNT_VAL     = 0x02
    __GLL_ACQ_CONFIG_REG    = 0x04
    __GLL_VELOCITY          = 0x09
    __GLL_PEAK_CORR         = 0x0C
    __GLL_NOISE_PEAK        = 0x0D
    __GLL_SIGNAL_STRENGTH   = 0x0E
    __GLL_FULL_DELAY_HIGH   = 0x0F
    __GLL_FULL_DELAY_LOW    = 0x10
    __GLL_OUTER_LOOP_COUNT  = 0x11
    __GLL_REF_COUNT_VAL     = 0x12
    __GLL_LAST_DELAY_HIGH   = 0x14
    __GLL_LAST_DELAY_LOW    = 0x15
    __GLL_UNIT_ID_HIGH      = 0x16
    __GLL_UNIT_ID_LOW       = 0x17
    __GLL_I2C_ID_HIGHT      = 0x18
    __GLL_I2C_ID_LOW        = 0x19
    __GLL_I2C_SEC_ADDR      = 0x1A
    __GLL_THRESHOLD_BYPASS  = 0x1C
    __GLL_I2C_CONFIG        = 0x1E
    __GLL_COMMAND           = 0x40
    __GLL_MEASURE_DELAY     = 0x45
    __GLL_PEAK_BCK          = 0x4C
    __GLL_CORR_DATA         = 0x52
    __GLL_CORR_DATA_SIGN    = 0x53
    __GLL_ACQ_SETTINGS      = 0x5D
    __GLL_POWER_CONTROL     = 0x65

    def __init__(self, address=0x62, rate=10):
        self.i2c = I2C(address)
        self.rate = rate

        #-------------------------------------------------------------------------------------------
        # Set to continuous sampling after initial read.
        #-------------------------------------------------------------------------------------------
        self.i2c.write8(self.__GLL_OUTER_LOOP_COUNT, 0xFF)

        #-------------------------------------------------------------------------------------------
        # Set the sampling frequency as 2000 / Hz:
        # 10Hz = 0xc8
        # 20Hz = 0x64
        # 100Hz = 0x14
        #-------------------------------------------------------------------------------------------
        self.i2c.write8(self.__GLL_MEASURE_DELAY, int(2000 / rate))

        #-------------------------------------------------------------------------------------------
        # Include receiver bias correction 0x04
        #-------------------------------------------------------------------------------------------
        self.i2c.write8(self.__GLL_ACQ_COMMAND, 0x04)

        #-------------------------------------------------------------------------------------------
        # Acquisition config register:
        # 0x01 Data ready interrupt
        # 0x20 Take sampling rate from MEASURE_DELAY
        #-------------------------------------------------------------------------------------------
        self.i2c.write8(self.__GLL_ACQ_CONFIG_REG, 0x21)


    def read(self):
        #-------------------------------------------------------------------------------------------
        # Distance is in cm
        # Velocity is in cm between consecutive reads; sampling rate converts these to a velocity
        # Reading the list from 0x8F seems to get the previous reading, probably cached for the sake
        # of calculating the velocity next time round.
        #-------------------------------------------------------------------------------------------
        '''
        gll_bytes = self.i2c.readList(0x80 | self.__GLL_LAST_DELAY_HIGH, 2)
        dist1 = gll_bytes[0]
        dist2 = gll_bytes[1]
        distance = ((dist1 << 8) + dist2) / 100
        '''

        dist1 = self.i2c.readU8(self.__GLL_FULL_DELAY_HIGH)
        dist2 = self.i2c.readU8(self.__GLL_FULL_DELAY_LOW)
        distance = ((dist1 << 8) + dist2) / 100

        velocity = -self.i2c.readS8(self.__GLL_VELOCITY) * self.rate / 100
        return distance, velocity
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

BenV
Posts: 2
Joined: Fri Mar 31, 2017 5:27 am

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun Apr 02, 2017 6:28 am

Thanks for sharing! Will definitely try that and get back to you.

jasonlayton2121
Posts: 1
Joined: Wed Apr 19, 2017 4:06 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Wed Apr 19, 2017 4:07 pm

Why didn't you use the smbus on your python code for the lidar v3 range finder?

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Thu Apr 20, 2017 5:18 am

I do use the smbus in my code, but because I have 3 sensors using it, it's broken out in a different class shared by all three. Have a look on GitHub at Quadcopter.py https://github.com/PiStuffing/Quadcopte ... dcopter.py. The code up to line 145 is the smbus class which the LiDAR code calls.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
KrookdFinger
Posts: 6
Joined: Sat May 13, 2017 11:41 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Sat May 13, 2017 11:53 pm

Hey Guys,

I had the Lidar Lite V3 working just fine... just upgraded firmware and now I am not getting any distance information back.
i2cdetect shows connected at 0x62. No errors in my software... distances just not coming in now.
Firmware version 4.9.27-v7+

Any ideas what to do?

Thanks.

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 14, 2017 5:05 am

KrookdFinger wrote:Hey Guys,

I had the Lidar Lite V3 working just fine... just upgraded firmware and now I am not getting any distance information back.
i2cdetect shows connected at 0x62. No errors in my software... distances just not coming in now.
Firmware version 4.9.27-v7+

Any ideas what to do?

Thanks.
I've never upgraded my firmware, didn't know you could, so sorry, I don't know how to help here.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
KrookdFinger
Posts: 6
Joined: Sat May 13, 2017 11:41 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Mon May 15, 2017 2:39 pm

Sorry. Just read my post. I never said what I upgraded. I upgraded my raspberry pi to 4.9.27-v27. Some library must be different. I went to the first build of jessy with pixel and all works fine. Any idea were I should look to check i2c settings between versions?

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Mon May 15, 2017 3:28 pm

Any official version of Jessie should work fine - just get the latest version: https://www.raspberrypi.org/downloads/raspbian/

I use the lite version - I don't want PIXEL and all the other GUI stuff taking up CPU power, but that will have no effect on the I2C.

The only changes I make to I2C are enable I2C in raspi-config and then add this line to /boot/config.txt

Code: Select all

dtparam=i2c_arm_baudrate=400000
The last bit is only necessary as I need this speed for the IMU also running over I2C.

Where did you get your version of Raspian PIXEL from?
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
KrookdFinger
Posts: 6
Joined: Sat May 13, 2017 11:41 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Mon May 15, 2017 8:07 pm

I did a rpi-update and i2c for lidar-lite v3 stopped working.
i2cDetect shows lidar as connected... but it will no longer return distances... they come back as zero.

I reverted back to raspbian-2017-01-10 from http://downloads.raspberrypi.org/raspbian/images/
and everything works again.

I'm just wondering what library may be causing the break.

Thank you,

Robert

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Tue May 16, 2017 6:16 am

Hi Robert,

Sorry, can't help; I think mine are running on the March Jessie lite, but I know they've run on the January & February versions too.I'd be very surprised that I2C got broken in the April version. Do you have any other I2C devices that are still working fine?

I have found the LiDAR Lite is very sensitive to cable length. It never works with the full length cable they supply. A couple of centimetres shorter and all's fine. The symptoms I saw was it was messing up the I2C bus - I kept getting I2C errors trying to read the IMU; unplug the LiDAR and IMU worked. Shorten the LiDAR cable and IMU worked.

Andy
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

zurmikopa
Posts: 4
Joined: Fri May 19, 2017 8:19 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Fri May 19, 2017 8:31 pm

I have the same issue as KrookdFinger. It just returns 0s when using the pi.

I set up the pi recently, so I haven't tried reverting to a previous version to see if that helps.

I have, however, tried the lidar lite on an arduino with i2c, which works fine, so I don't think it's a cable length issue.

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Sat May 20, 2017 5:28 am

Do you have anything else on the I2C bus other than Garmin? My Garmin works perfectly, but iff its plugged in, my MPU-9250 struggles.

Have you set the I2C baudrate to 400kbps? Have you tried the Python code I posted earlier - this works for me.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

zurmikopa
Posts: 4
Joined: Fri May 19, 2017 8:19 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 28, 2017 4:58 am

Stupid question, in your (Hove's) code, I see

self.i2c = I2C(address)

But I don't see I2C defined anywhere.
What would I need to import to get that?

Thanks

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 28, 2017 6:06 am

zurmikopa wrote:Stupid question, in your (Hove's) code, I see

self.i2c = I2C(address)

But I don't see I2C defined anywhere.
What would I need to import to get that?

Thanks
I2C is just a thin wrapper around smbus which is imported at the top. Take a look at github.com/PiStuffing/Quadcopter/Quadcopter.py, and look for "class I2C:" and "class GLL:"
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

zurmikopa
Posts: 4
Joined: Fri May 19, 2017 8:19 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 28, 2017 6:57 am

Ah, thanks.

Tried your code.
Unfortunately it is still always returning (0,0) when I call read.

I am also getting zeros with this library someone made for lidar lite:
https://github.com/Sanderi44/Lidar-Lite

I added dtparam=i2c_arm_baudrate=400000 to my /boot/config.txt
i2cdetect seems to indicate it's there.

Nothing else is on the I2C bus.

My version of pixel was downloaded through noobs, followed by an update/upgrade.

Despite my lack of success, I really appreciate the responses, thanks.

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 28, 2017 7:14 am

How is your LiDAR attached to the Raspberry Pi? I haven't seen problems matching exactly what you see, but I have seen getting rubbish values from it. The main problem I've had is the wires they give are too long - if they are 1/2 or less in length, it works a lot better. Also, the LiDAR spec says to use a 680uF capacitor across 5v and ground - if I use that, I only get rubbish out.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

zurmikopa
Posts: 4
Joined: Fri May 19, 2017 8:19 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 28, 2017 7:31 am

I do have the 680uF capacitor across 5v and ground, what might you suggest in place of it, just nothing?

The rest of the wiring is blue -> pin 3 , green -> pin 5, yellow & orange unconnected.

I use the same wiring setup with an Arduino and it works. (Except the last bit is male instead of female on the Arduino, but same wire length.)

I'll see if I can get the wires shorter just to check though.

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Sun May 28, 2017 7:38 am

I just removed the capacitor completely and shorten the wires. To me it seems about 10cm cables is about the point things start failing even with the capacitor removed.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
KrookdFinger
Posts: 6
Joined: Sat May 13, 2017 11:41 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Tue May 30, 2017 5:20 pm

Hi Hove,

So are you saying that you shortened the wires with the latest version of Jessie and it worked for you?

Thanks,

Robert

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Tue May 30, 2017 6:15 pm

Haven't tried the latest jessie, but for all between January and March, using the full length cables (with or without the electrolytic) always cause corruption / noise on I2C. Shortening them as short as you can always fixes the problem.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
KrookdFinger
Posts: 6
Joined: Sat May 13, 2017 11:41 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Thu Jun 01, 2017 7:08 pm

Hi Hove,

Just an FYI... This is causing the issue for me at least.
May be helpful if anyone upgrades...

http://www.robotshop.com/forum/post-p130228#p130228

User avatar
KrookdFinger
Posts: 6
Joined: Sat May 13, 2017 11:41 pm

Re: Lidar Lite V3 - Raspberry Pi - Python

Thu Jun 01, 2017 7:15 pm

Latest Kernel Patch for i2c discussed here

https://patchwork.kernel.org/patch/9351755/

User avatar
Hove
Posts: 1173
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Lidar Lite V3 - Raspberry Pi - Python

Thu Jun 01, 2017 8:27 pm

KrookdFinger wrote:Latest Kernel Patch for i2c discussed here

https://patchwork.kernel.org/patch/9351755/
I'll stick with my March RPi kernel then until I've got time to read the details of what's posted - thanks for sharing.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 36 guests