I am wanting to get the TFMini Distance Shield to work. The good thing is that I am able to get the code for it in python but the problem is that the library doesn’t exist in micropython. On looking up a similar distance shield, I see that the v15311x is available but unfortunately this shield is only able to go up to 4m whereas the TFMini is capable of doing 12m. Is there a way to hack existing libraries for the purpose of getting this device to work? I have sample code for Python which I hope I can modify for use on OpenMV. Code as follows:
For TFmini Plus
from smbus2 import SMBus, i2c_msg
To get the addresses of detected devices type:
sudo i2cdetect -y 1 or 0
I2CBUS = 1
\
LiDAR Address
ADDRESS = 0x10
\
Initialize I2C (SMBus)
bus = SMBus(I2CBUS)
class i2c_device:
def init(self, addr=ADDRESS, port=I2CBUS):
self.addr = addr
self.bus = SMBus(port) # Initialize I2C
Write a single command
def write_cmd(self, cmd):
self.bus.write_byte(self.addr, cmd)
sleep(0.0001)
Write a command and argument
def write_cmd_arg(self, cmd, data):
self.bus.write_byte_data(self.addr, cmd, data)
sleep(0.0001)
Write a block of data
def write_block_data(self, cmd, data):
self.bus.write_i2c_block_data(self.addr, cmd, data)
sleep(0.0001)
Read a single byte
def read(self):
return self.bus.read_byte(self.addr)
Read
def read_data(self, cmd):
return self.bus.read_byte_data(self.addr, cmd)
Read a block of data
def read_block_data(self, cmd):
return self.bus.read_block_data(self.addr, cmd)
return self.bus.read_i2c_block_data(self.addr, cmd, 0)
if name == ‘main’:
lidar = i2c_device()
block = list()
print(“Data reading started…”)
while True:
5A 05 00 01 60
lidar.write_block_data(0x00, [0x5A, 0X05, 0X00, 0X01, 0X60])
sleep(0.01)
we will read nine bytes and then the three parameters
for x in range(0, 9):
byte = lidar.read()
block.insert(x, byte)
Check the headers
if block[0] == 0x59 and block[1] == 0x59:
print(“Printing python3 compatible part”)
distance = block[2] + block[3]*256
strength = block[4] + block[5]*256
temperature = block[6] + block[7]*256
temperature = (temperature/8) - 256
print(“Distance:”+ str(distance))
print(“Strength:” + str(strength))
print(“Temperature:” + str(temperature))
if block[0] == “Y” and block[1] == “Y”:
distL = int(block[2].encode(“hex”), 16)
distH = int(block[3].encode(“hex”), 16)
stL = int(block[4].encode(“hex”), 16)
stH = int(block[5].encode(“hex”), 16)
distance = distL + distH256
strength = stL + stH256
tempL = int(block[6].encode(“hex”), 16)
tempH = int(block[7].encode(“hex”), 16)
temperature = tempL + tempH*256
temperature = (temperature/8) - 256
print(“Printing python2 portion”)
print(“Distance:”+ str(distance) + “\n”)
print(“Strength:” + str(strength) + “\n”)
print(“Temperature:” + str(temperature) + “\n”)
if key == ord(“q”):
break
bus.close()