Skip to main content

Corrected FRAME_5 constant from 0x0003 to 0x0005

Project description

tfmpi2c

A Python module for the Benewake TFMini-Plus Lidar sensor in I2C mode

The TFMini-Plus is largely compatible with the TFMini-S and therefore that device is able to use this module. One difference is that upon command to change communication mode, such as SET_I2C_MODE or SET_SERIAL_MODE, the TFMini-Plus switches mode immediately, whereas the TFMini-S requires a subsequent SAVE_SETTING command before the change takes place. This module is not compatible with the TF-Luna in I2C mode. This module is also not compatible with the TFMini, which is an entirely different product with its own command set and data structure.

From hardware v1.3.5 and firmware v1.9.0 and above, the TFMini-Plus interface can be configured in either UART (Serial) or I2C (two-wire) communication mode. This module is only for the I2C mode.

Internally, the device samples at about 4KHz. It presents frames of data to the interface at a programmable rate from 0 up to about 10KHz. The default data-frame rate is 100Hz. "Standard" data-frame rates are: 0, 1, 2, 5, 10, 20, 25, 50, 100, 125, 200, 250, 500, and 1000Hz. Single data-frames can be obtained by setting the frame rate to 0 and sending a TRIGGER_DETECTION command.

After a getData() command, three module variables are updated:
  ●  dist Distance to target in centimeters. Range: 0 - 1200
  ●  flux Strength or quality of return signal or error. Range: -1, 0 - 32767
  ●  temp Temperature of device chip in code. Range: -25°C to 125°C

Installed with the module is a tests folder that contains an example python script, tfmpi2c_test.py. All of the code for this module and the test script is richly commented to assist with understanding and in problem solving.


I2C mode operation

This module supports only the I2C communication interface. It is up to the user to ensure that the device is set to that mode.

To configure the device for I2C communication, a command must be sent using the UART interface. This reconfiguration should be made prior to the device's service installation, either by the serial GUI test application and command code supplied by the manufacturer, or by using the example script included with the tfmplus python module to send a SET_I2C_MODE command.

The device will remain in I2C mode after power has been removed and restored. The only way to return to serial mode is with the SET_SERIAL_MODE command. Even a RESTORE_FACTORY_SETTINGS command will NOT restore the device to its default, UART communication interface mode.

The TFMini-Plus functions like an I2C slave device. The default address is 0x10 (16 Decimal) but programmable with the SET_I2C_ADDRESS command and a parameter in the range of 1 to 127. The new setting will take effect immediately and permanently without a SAVE_SETTINGS command. A RESTORE_FACTORY_SETTINGS command will restore the default address. The I2C address can be set while still in serial communication mode or, if in I2C mode, the test script tfmpi2c_test.py included with the module can be used to test and change the address.


Three tfmpi2c module functions

Three main functions are defined in the module. Status code, command and parameter variables are also declared.

begin( portNumber, devAddress) passes the I2C port number and I2C bus device address to the module, tries to open the port, performs a quick write operation and returns a boolean value indicating successful completion.

getData() commands the device to transmit one data-frame, reads the data-frame and extracts the three measurement data values. It sets the error status byte code and returns a boolean value indicating function 'pass/fail'.

sendCommand( cmnd, param) sends a coded command and a parameter to the device. It sets the status error code byte and returns a boolean 'pass/fail' value. A proper command (cmnd) must be selected from the module's list of twenty defined commands. A parameter (param) may be chosen from the module's list of defined parameters (such as data-frame rate) or entered directly (such as device I2C address). An erroneous parameter can block communication and there is no external means of resetting the device to factory defaults.

Any change of device settings must be followed by a SAVE_SETTINGS command or else the modified values may be lost when power is removed. SYSTEM_RESET and RESTORE_FACTORY_SETTINGS do not require a SAVE_SETTINGS command.


Using the I/O modes of the device

The so-called I/O modes are not supported in this module. Please do not attempt to use any I/O commands that you may find to be defined in this module.

The I/O output mode is enabled and disabled by this 9 byte command:
5A 09 3B MODE DL DH ZL ZH SU

Command byte number:
0    0x5A: Header byte, starts every command frame
1    0x09: Command length, number of bytes in command frame
2    0x3B: Command number

3    MODE:
     0x00: I/O Mode OFF, standard data output mode
     0x01: I/O Mode ON, output: near = high and far = low
     0x02: I/O Mode ON, output: near = low and far = high

4    DL: Near distance lo order byte of 16 bit integer
5    DH: Near distance hi order byte

6    ZL: Zone width lo byte
7    ZL: Zone width hi byte

8   SU: Checkbyte (the lo order byte of the sum of all the other bytes in the frame)

If an object's distance is greater than the Near distance (D) plus the Zone width (Z) then the object is "far."
If the distance is less than the Near distance (D) then the object is "near".
The Zone is a neutral area. Any object distances measured in this range do not change the output.
The output can be set to be either high when the object is near and low when it's far (Mode 1); or low when it's near and high when it's far (Mode 2).
The high level is 3.3V, the low level is 0V.


Finally

Benewake is not very forthcoming about the internals, but they did share this:

Some commands that modify internal parameters are processed within 1ms. Some commands require the MCU to communicate with other chips may take several ms. And some commands, such as saving configuration and restoring the factory settings need to erase the FLASH of the MCU, which may take several hundred ms.

And also:

1- the measuring frequency of the module should be 2.5 times larger than the IIC reading frequency.
2- the iic reading frequency should not exceed 100hz

Since the Data-Frame Rate is limited to 1000Hz, this condition implies a 400Hz data sampling limit in I2C mode. Benewake says sampling should not exceed 100Hz. They don't say why; but you might keep that limitation in mind when you consider using the I2C interface.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tfmpi2c-0.0.7.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tfmpi2c-0.0.7-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file tfmpi2c-0.0.7.tar.gz.

File metadata

  • Download URL: tfmpi2c-0.0.7.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for tfmpi2c-0.0.7.tar.gz
Algorithm Hash digest
SHA256 29bb5506db03ce0337e6f06cfb11e24939febeef0b305d1d4b2ea8b4bbe82543
MD5 36dad3f01018c97c06596a8ddaee8f13
BLAKE2b-256 cce0b7f30ce5857286e0592d7fccd9a3cb1373f0bc383058a8ce0fa35b440b2a

See more details on using hashes here.

File details

Details for the file tfmpi2c-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: tfmpi2c-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for tfmpi2c-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 cd26686cd264b1f2d5ac66df2ffb70db1ef6a66f596ee7d7dd994f9c368ca749
MD5 a48dc97bec55d58a6f77c1c676bddbc0
BLAKE2b-256 9d50a4519cd2bd00755027f8544535b9baf61abc0950c8b8a09b508218aa986c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page