[GO] [EV3 MicroPython] Distance measurement using DIST-ToF sensor (& I2C communication)

DIST-ToF

From mindsensors, which manufactures and sells LEGO's third-party products, ToF distance sensor (DIST-ToF / ev3-and-nxt / 210-high-precision-time-of-flight-tof-distance-sensor-for-nxt-or-ev3)) is on sale.

ToF is an abbreviation of "Time of Flight" and is a technology that measures the distance from the time it takes for (infrared) light to bounce off [^ 1]. ToF technology has made it possible to pinpoint and accurately measure the position of an object.

Most of the sensors sold by mindsensors are supported by ev3dev, which is the base of EV3 MicroPython [^ 2]. And the sensor supported by ev3dev can be easily controlled by the ʻEv3devSensor class of the ʻiodevices module of EV3MicoroPythoh (pybricks2.0).

However, this DIST-ToF sensor does not support ev3dev (command is not implemented). However, this sensor can be controlled by I2C communication.

Therefore, this time, I will introduce how to handle I2C communication directly with EV3 MicroPython. Then, we will make it possible to handle the DIST-ToF sensor with Ev3 MicroPython.

I2C communication

I2C is a serial bus (a type of serial communication) developed by Philips (currently NXP), which uses two lines, a serial data line (SDA) and a serial clock line (SCL), to communicate between devices. It is a method to do. It is used in embedded devices and mobile phones because it can communicate with multiple devices with a small number of devices.

The reason why communication between "multiple devices" is possible with two lines is that the address is set for each device [^ 4].

In addition, I2C communication uses the master / slave method [^ 3]. In this case, EV3 is the master and DIST-ToF is the slave.

In EV3MicroPython (Pybricks2.0), the ʻI2CDevice class is provided in the ʻiodevices module as a class that handles I2C communication. ʻFor the specifications of the I2CDevice` class, click here [file: ///Users/asahi/.vscode/extensions/lego-education.ev3-micropython-2.0.0/resources/docs/iodevices.html#i2c-device )Please refer to.

I2C communication with EV3MicoPython

The following source code is an implementation example of a program that acquires the sensor value of DIST-TOF using the ʻI2CDevice` class in EV3MicroPython. The acquired value is displayed on the EV3 LCD screen. The implemented program is based on the code of GitHub published for RobotC.

ms-dist-tof.py


#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.parameters import Port
from pybricks.tools import wait
from pybricks.media.ev3dev import Font
from pybricks.iodevices import I2CDevice

# Write your program here
ev3 = EV3Brick()
ev3.speaker.beep()
font = Font(size=20)

dist = I2CDevice(Port.S4, 0x02 >> 1)

def get_ms_dist(dist):
    data = dist.read(0x42, length=2)
    return (0xFF & data[0]) + ((0xFF & data[1]) << 8)

ev3.screen.set_font(font)
while True:
    ev3.screen.clear()
    ev3.screen.print("DIST :", get_ms_dist(dist))
    wait(100)

First, create a dist instance (class object) from the ʻI2CDevice class (line 14). Enter the port (Port` class) to which the sensor is connected and the sensor address (hexary number) in the arguments of the constructor.

Here, the (default) address of DIST-ToF is 0x02. However, be sure to enter the address shifted to the right by 1 bit [^ 4].

The get_ms_dist function (lines 16-18) performs the following processing.

  1. Pass a class object of the ʻI2CDevice class as an argument (assuming a dist` instance).
  2. Call the read function of the ʻI2CDevice` class to get the sensor value.
  1. Add 2 bytes of data into one data. This is returned as distance data.

It seems that it will be a smarter program to prepare a class for DIST-ToF and implement the get_dist function in the class.

As mentioned above, DIST-ToF uses I2C as the communication method, so by combining it with other sensors with different addresses, it is possible to control multiple sensors with one port of EV3.

[^ 1]: Recently, it has become possible to incorporate a circuit that performs such measurement at the element level of the sensor, and it is often called ToF including this.

[^ 2]: For sensors supported by ev3dev (including those made by third parties), here See /sensors.html).

[^ 3]: Master-slave method: When multiple devices operate in cooperation, a "master" machine that controls and operates multiple devices and a "slave" that operates under the unilateral control of the master machine. This is a method of sharing roles among machines (although this name has recently been a problem ...).

[^ 4]: Think of an "email address". Many devices are connected to the Internet all over the world, but the mail arrives only at the mail address (server) set as the destination.

[^ 5]: In I2C communication, of the 8-bit data, the upper 7 bits are the "slave address" and the lower 1 bit is the "send / receive designated bit". Since the only information required here is the "slave address", enter the value without the lower 1 bit.

Recommended Posts

[EV3 MicroPython] Distance measurement using DIST-ToF sensor (& I2C communication)
SRF02 ultrasonic sensor run on pyton using i2c