├── README.md
├── lib
├── __init__.py
├── constants.py
└── lidar.py
└── main.py
/README.md:
--------------------------------------------------------------------------------
1 | # tf-luna
2 | A simple micropython i2c library for TF-Luna LiDAR Module.
3 |
4 | **Repo-status**: See [Implemented functions](#implemented-functions)
5 |
6 | ## Getting started
7 |
8 | To enable i2c, connect pin 5 to GND, see [Datasheet](https://www.robotshop.com/media/files/content/b/ben/pdf/tf-luna-8m-lidar-distance-sensor-instructions-manual.pdf)
9 |
10 | To run main.py, connect RXD/SDA to P7 and TXD/SCL to P8
11 |
12 |
13 | | Pin & Function | Description |
14 | | --------------------- | ------------------ |
15 | | 1 VCC | Vin (5V) |
16 | | 2 RXD/SDA | Receiving/Data |
17 | | 3 TXD/SCL | Transmitting/Clock |
18 | | 4 GND | GND |
19 | | 5 Configuration Input | Ground: I2C mode
/3.3V: Serial port
Communications mode |
20 | | 6 Multiplexing output | Default: on/off mode output
I2C mode: Data availability
signal on but not switching value mode |
21 |
22 |
23 | ## Implemented functions
24 | * Distance
25 | * Chip temperature
26 | * Signal Amplitude
27 | * Set Min/Max range
28 | * Reboot
29 | * Reset to factory defaults
30 | * Change sampling rate / freq
31 |
32 | ## To-Do
33 | * Trigger mode
34 | * Change slave address
35 | * Tick
36 | * Error
37 | * UART
38 |
--------------------------------------------------------------------------------
/lib/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davmoz/tf-luna-micropython/e8a6024a73e3bd9b84951596112b4c448c0a9fe9/lib/__init__.py
--------------------------------------------------------------------------------
/lib/constants.py:
--------------------------------------------------------------------------------
1 | DIST_LOW = 0x00 # cm
2 | DIST_HIGH = 0x01
3 | AMP_LOW = 0x02
4 | AMP_HIGH = 0x03
5 | TEMP_LOW = 0x04 # Unit: 0.01 Celsius
6 | TEMP_HIGH = 0x05
7 | TICK_LOW = 0x06 # Timestamp
8 | TICK_HIGH = 0x07
9 | ERROR_LOW = 0x08
10 | ERROR_HIGH = 0x09
11 | VERSION_REVISION = 0x0A
12 | VERSION_MINOR = 0x0B
13 | VERSION_MAJOR = 0x0C
14 | SN = 0x10 # Production code in 14 bytes ASCI code (0x10 is the first byte)
15 | SAVE = 0x20 # Write 0x01 to save current setting
16 | SHUTDOWN_REBOOT = 0x21 # Write 0x02 to reboot
17 | SLAVE_ADDR = 0x22 # Default: 0x10, Range: [0x08, 0x77]
18 | MODE = 0x23 # Default: 0x00 | Continuous ranging mode: 0x00 Trigger mode: 0x01
19 | TRIG_ONE_SHOT = 0x24 # 0x01: Trigger once (only on trigger mode)
20 | ENABLE = 0x25 # Turn on LiDAR: 0x00, Turn off LiDAR: 0x01
21 | FPS_LOW = 0x26 # Default: 0x64 100Hz, 0xFA 250Hz
22 | FPS_HIGH = 0x27
23 | LOW_POWER = 0x28 # Default: 0x00, Normal: 0x00, Power saving mode: 0x01
24 | RESTORE_FACTORY_DEFAULTS = 0x29 # Write 0x01 to restore factory default settings
25 | AMP_THR_LOW = 0x2A # Default: 0x64, Amp threshold value
26 | MIN_DIST_LOW = 0x2E # Default: 0x00, Minimum dist in cm, but not working on DUMMY_DIST
27 | MIN_DIST_HIGH = 0x2F # Default: 0x00
28 | MAX_DIST_LOW = 0x30 # Default: 0x20, Maximum dist in cm, but not working on DUMMY_DIST
29 | MAX_DIST_HIGH = 0x31 # Default: 0x03
30 |
--------------------------------------------------------------------------------
/lib/lidar.py:
--------------------------------------------------------------------------------
1 | import struct, utime
2 | import constants as const
3 |
4 | class LIDAR:
5 | '''docstring for LIDAR.'''
6 |
7 | def __init__(self, i2c, addr):
8 | self.i2c = i2c
9 | self.addr = addr
10 |
11 | def addr(self):
12 | return self.addr
13 |
14 | def _read(self, addr, bytes):
15 | return self.i2c.readfrom_mem(self.addr, addr, bytes)
16 |
17 | def _write(self, addr, value):
18 | self.i2c.writeto_mem(self.addr, addr, value)
19 |
20 | def save(self):
21 | self._write(const.SAVE, 0x01)
22 | utime.sleep_ms(100)
23 |
24 | def reboot(self):
25 | utime.sleep_ms(50)
26 | self.save()
27 | self._write(const.SHUTDOWN_REBOOT, 0x02)
28 |
29 | def _save_reboot(self):
30 | self.reboot()
31 | utime.sleep_ms(500)
32 |
33 | def distance(self):
34 | dist = self._read(const.DIST_LOW, 2)
35 | return struct.unpack('> 8, min & 0XFF
70 | self._write(const.MIN_DIST_HIGH, high)
71 | self._write(const.MIN_DIST_LOW, low)
72 |
73 | high, low = max >> 8, max & 0XFF
74 | self._write(const.MAX_DIST_HIGH, high)
75 | self._write(const.MAX_DIST_LOW, low)
76 | self._save_reboot()
77 |
78 | def read_all(self):
79 | return 'Distance {}, ChipTemp {}, SignalAmp {}'.format(
80 | self.distance(),
81 | self.temp(),
82 | self.signal_amp())
83 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from machine import I2C
2 | import utime
3 | import sys
4 | from lib.lidar import LIDAR
5 |
6 | # TF-Luna has the default slave_address 0x10
7 | LIDAR_ADDRESS = 0x10
8 |
9 | i2c_0 = I2C(0, mode=I2C.MASTER, baudrate=400000, pins=('P7', 'P8'))
10 | utime.sleep_ms(50)
11 |
12 | slaves = i2c_0.scan()
13 | if LIDAR_ADDRESS not in slaves:
14 | print('Bus error: Please check LIDAR wiring')
15 | sys.exit()
16 |
17 |
18 | lidar = LIDAR(i2c_0, LIDAR_ADDRESS)
19 | print(lidar.version())
20 |
21 | # Output limit when out of range
22 | # Output only when between 20cm and 150cm (Up to 800cm)
23 | lidar.set_min_max(20, 150)
24 |
25 | lidar.set_frequency(250)
26 |
27 | while True:
28 | print(lidar.distance())
29 | utime.sleep_ms(10)
30 |
--------------------------------------------------------------------------------