Offline Voice Recognition Sensor DRobot

https://dfimg.dfrobot.com/nobody/wiki/4f868c5f5b2b9ae909271b3865939655.png

Obsługa modułu

Uczenie dodatkowej frazy wybudzającej

Aby nauczyć moduł nowej frazy wybudzającej (zawsze aktywne pozostaje “Hello Robot!“) należy po wywołaniu standardowym wygłosić frazę: Learning wake word“. Następnie podać nowe sformułowanie wybudzające. Ja podałem Hello Jane. Trzeba to zrobić trzykrotnie słuchając poleceń.

Biblioteka MicroPython

Biblioteka lekko zmodyfikowana. W oryginalnej był błąd w metodzie get_cmdid(). Poprawiony przez Chat GPT.
Uwaga! Kody nie działają dla ESP8266. Prawdopodobnie problem tkwi w bibliotece I2C. Może trzeba użyć softI2C? Sprawdzę.
Wszystko dobrze działa na ESP32.

from micropython import const
from machine import I2C, Pin
from utime import sleep


DF2301Q_I2C_ADDR = const(0x64)


class DFRobot_DF2301Q_I2C:
    """
    MicroPython class for communication with the DF2301Q from DFRobot via I2C
    """

    DF2301Q_I2C_REG_CMDID = const(0x02)
    DF2301Q_I2C_REG_PLAY_CMDID = const(0x03)
    DF2301Q_I2C_REG_SET_MUTE = const(0x04)
    DF2301Q_I2C_REG_SET_VOLUME = const(0x05)
    DF2301Q_I2C_REG_WAKE_TIME = const(0x06)
    DF2301Q_I2C_8BIT_RANGE = const(0xFF)
    DF2301Q_I2C_PLAY_CMDID_DURATION = const(1)

    def __init__(self, sda, scl, i2c_addr=DF2301Q_I2C_ADDR, i2c_bus=0):
        """
        Initialize the DF2301Q I2C communication
        :param sda: I2C SDA pin
        :param scl: I2C SCL pin
        :param i2c_addr: I2C address
        :param i2c_bus: I2C bus number
        """
        self._addr = i2c_addr

        try:
            self._i2c = I2C(i2c_bus, sda=Pin(sda), scl=Pin(scl))
        except Exception as err:
            print(f'Could not initialize i2c! bus: {i2c_bus}, sda: {sda}, scl: {scl}, error: {err}')

    def _write_reg(self, reg, data) -> None:
        """
        Writes data to the I2C register
        :param reg: register address
        :param data: data to write
        :return: None
        """
        if isinstance(data, int):
            data = [data]

        try:
            self._i2c.writeto_mem(self._addr, reg, bytearray(data))
        except Exception as err:
            print(f'Write issue: {err}')

    def _read_reg(self, reg, length) -> bytes:
        """
        Reads data from the I2C register
        :param reg: register address
        :param length: number of bytes to read
        :return: bytes
        """
        try:
            result = self._i2c.readfrom_mem(self._addr, reg, length)
        except Exception as err:
            print(f'Read issue: {err}')
            result = bytearray(length)

        return result

    def get_cmdid(self) -> int:
        """
        Returns the current command id
        :return: int
        """
        data = self._read_reg(self.DF2301Q_I2C_REG_CMDID, 1)
        return int(data[0])

    def get_wake_time(self) -> int:
        """
        Returns the current wake-up duration
        :return: int
        """
        data = self._read_reg(self.DF2301Q_I2C_REG_WAKE_TIME, 1)
        return int(data[0])

    def play_by_cmdid(self, cmdid: int) -> None:
        """
        Play the current command words by command id
        :param cmdid: command words as integer
        :return: None
        """
        self._write_reg(self.DF2301Q_I2C_REG_PLAY_CMDID, int(cmdid))
        sleep(self.DF2301Q_I2C_PLAY_CMDID_DURATION)

    def set_wake_time(self, wake_time: int) -> None:
        """
        Set the wake-up duration of the device
        :param wake_time: integer between 0 and 255
        :return: None
        """
        wake_up_time = int(wake_time) & self.DF2301Q_I2C_8BIT_RANGE
        self._write_reg(self.DF2301Q_I2C_REG_WAKE_TIME, wake_up_time)

    def set_volume(self, vol: int) -> None:
        """
        Set the volume of the device
        :param vol: integer between 1 and 7
        :return: None
        """
        self._write_reg(self.DF2301Q_I2C_REG_SET_VOLUME, int(vol))

    def set_mute_mode(self, mode) -> None:
        """
        Set the mute mode of the device
        :param mode: integer 0 for off, 1 for on
        :return: None
        """
        self._write_reg(self.DF2301Q_I2C_REG_SET_MUTE, int(bool(mode)))



Kod programu wyświetlającego na konsoli numery komend głosowych

from DFRobot_DF2301Q_I2C import DFRobot_DF2301Q_I2C
from micropython import const
from utime import sleep


SDA_PIN = const(21)
SCL_PIN = const(22)
SLEEP_SECONDS = const(3)


def setup(sensor) -> None:
    """
    Set up the DFRobot DF2301Q sensor
    :param sensor: instance of DFRobot_DF2301Q_I2C
    :return: None
    """
    sensor.set_volume(5)
    sensor.set_mute_mode(0)
    sensor.set_wake_time(20)


def get_cmd_id(sensor) -> int:
    """
    Get the command id from the DF2301Q sensor
    :param sensor: instance of DFRobot_DF2301Q_I2C
    :return: int
    """
    command_id = sensor.get_cmdid()
    return command_id if command_id != 0 else None


if __name__ == "__main__":
    try:
        voice_sensor = DFRobot_DF2301Q_I2C(sda=SDA_PIN, scl=SCL_PIN)
        setup(sensor=voice_sensor)

        print('Speak your commands:')

        while True:
            cmd_id = get_cmd_id(sensor=voice_sensor)

            if cmd_id is not None:
                print(f'COMMAND ID: {cmd_id}')

            sleep(SLEEP_SECONDS)
    except Exception as e:
        print(f'Initialization error: {e}')


LINKI

Źródła:

Konfiguracja i działanie

Trening

Inne

Dodaj komentarz