Crate embedded_tfluna

Crate embedded_tfluna 

Source
Expand description

§Embedded TF-Luna

Crates.io Documentation MIT/Apache-2.0 licensed Build Status

Platform agnostic Rust driver for the TF-Luna LiDAR distance sensor, based on the embedded-hal traits.

This library provides a no_std interface for interacting with the TF-Luna LiDAR distance sensor.

The TF-Luna supports both I2C and UART communication protocols. However, this library only supports I2C for now.

§Device

The TF-Luna’s characteristics, taken from the user manual, are summarized in the following table:

DescriptionParameter value
Operating range0.2 ~ 8m1
Accuracy±6cm@ (0.2-3m)2
±2%@ (3m-8m)
Measurement unitcm (Default)
Range resolution1cm
Field of View (FOV)0.2°2
Framerate1~250Hz3 (100Hz default)
Weight≤ 5g4
Power≤ 0.35W4
Dimensions35mm x 21.25mm x 13.5mm4
CommunicationI2C and UART5

§Getting Started

§Usage

See the examples directory for examples of using the TF-Luna with different micro-controllers and boards.

§Example for ESP32 C3
#![no_std]
#![no_main]
#![deny(
    clippy::mem_forget,
    reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
    holding buffers for the duration of a data transfer."
)]

use defmt::info;
use embedded_tfluna::i2c::{Address, TFLuna};
use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay;
use esp_hal::main;
use esp_hal::time::{Duration, Instant};
use esp_hal::{
    i2c::master::{Config as I2cConfig, I2c},
    time::Rate,
};

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();

#[main]
fn main() -> ! {
    rtt_target::rtt_init_defmt!();

    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
    let peripherals = esp_hal::init(config);

    // I2C SDA (Data) Pin
    let sda_pin = peripherals.GPIO8;
    // I2C SCL (Clock) Pin
    let scl_pin = peripherals.GPIO9;
    let i2c_config = I2cConfig::default().with_frequency(Rate::from_khz(100));
    let i2c = I2c::new(peripherals.I2C0, i2c_config)
        .unwrap()
        .with_sda(sda_pin)
        .with_scl(scl_pin);
    let mut tfluna: TFLuna<_, _> = TFLuna::new(i2c, Address::default(), Delay::new()).unwrap();

    // Enable measurements
    tfluna.enable().unwrap();

    loop {
        let measurement = tfluna.get_measurement().unwrap();
        info!("Distance = {:?}", measurement.distance);
        let delay_start = Instant::now();
        while delay_start.elapsed() < Duration::from_millis(100) {}
    }
}

§Feature Flags

  • defmt - Enable logging output using defmt and implement defmt::Format on certain types.
  • async - Enable asynchronous interface.

§License

Licensed under either of

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


  1. Operating range measured indoor based on a standard whiteboard with 90% reflectivity. 

  2. This is the theoretical value, the real value may be different. ↩ 1 2

  3. 100Hz is the default value and only any factor (500/n, n can be any integer in [2, 500] e.g. 250Hz, 125Hz) of 500Hz are available. 

  4. Taken from the TF-Luna’s product page↩ 1 2 3

  5. The choice between the two is made based on how the 5th pin is connected. Ground for I2C and 3.3V for UART. 

Modules§

i2c
Interface for the I2C protocol.

Structs§

FirmwareVersion
Structure containing major, minor, and revision numbers.
SensorReading
Structure containing distance, signal strength, temperature, and timestamp.
SerialNumber
Structure containing the serial number of the device.
Signature
ASCII signature of the device.

Enums§

PowerMode
Enum containing the different power modes of the TF-Luna
RangingMode
Ranging modes of the device.