Created
June 20, 2024 21:05
-
-
Save Timo614/ff64dcbae994308eb0a2c67bfcb982e4 to your computer and use it in GitHub Desktop.
Adeept Robot Control / DFRobot Gravity Offline Voice Recogition Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
import busio | |
import wifi | |
import socketpool | |
import ipaddress | |
import avoid_obstacles | |
import keep_distance | |
import line_tracking | |
from BPI_PicoW_S3_Car import Motor, Servo, I2CPCF8574Interface, LCD, LCD1602 | |
from DFRobot_DF2301Q import DFRobot_DF2301Q_I2C, DF2301Q_I2C_ADDR | |
from DFRobot_DF2301Q_Commands import CommandWord | |
import neopixel | |
import pwmio | |
# Initialize I2C bus | |
i2c = busio.I2C(board.GP21, board.GP20) | |
DF2301Q = DFRobot_DF2301Q_I2C(i2c, i2c_addr=DF2301Q_I2C_ADDR) | |
# WS2812 LED strip setup | |
NUM_PIXELS = 4 | |
pixels = neopixel.NeoPixel(board.GP11, NUM_PIXELS) | |
# Motor and Servo setup | |
speed_low = 40 | |
servo = Servo() | |
motor = Motor() | |
# Buzzer setup | |
class Buzzer: | |
def __init__(self): | |
self._buzzer = pwmio.PWMOut(board.GP26, duty_cycle=0, frequency=440, variable_frequency=True) | |
def playtone(self, frequency): | |
self._buzzer.duty_cycle = 65535 // 2 # Half-full duty ratio | |
self._buzzer.frequency = frequency | |
def sound(self): | |
self.playtone(1500) | |
def bequiet(self): | |
self._buzzer.duty_cycle = 0 # Empty duty cycle | |
buzzer = Buzzer() | |
# Wi-Fi setup | |
ssid, pw = ('BPI-PicoW-S3', '12345678') | |
wifi.radio.start_ap(ssid=ssid, password=pw) | |
time.sleep(1) | |
print(f'WiFi AP mode Started! SSID is {ssid}, IP={wifi.radio.ipv4_address_ap}') | |
pool = socketpool.SocketPool(wifi.radio) | |
BUFF_SIZE = 1024 | |
# LCD setup | |
lcd = LCD1602(i2c) | |
lcd.print("Initializing...") | |
time.sleep(1) | |
lcd.clear() | |
# Brightness variable | |
brightness = 0.5 | |
# RGB color array | |
rgb_color = [255, 255, 255] # Default to white | |
# Function to set pixel color with brightness adjustment | |
def update_neopixel(): | |
adjusted_color = tuple(int(c * brightness) for c in rgb_color) | |
pixels.fill(adjusted_color) | |
# Function to update RGB color | |
def update_rgb_color(r, g, b): | |
global rgb_color | |
rgb_color = [r, g, b] | |
update_neopixel() | |
# Function to handle voice commands | |
def handle_voice_command(CMDID): | |
global brightness | |
command_name = "" | |
if CMDID == CommandWord.GoForward: | |
motor.move(1, 'forward', speed_low) | |
command_name = "Go Forward" | |
elif CMDID == CommandWord.Retreat: | |
motor.move(1, 'backward', speed_low) | |
command_name = "Retreat" | |
elif CMDID == CommandWord.ParkACar: | |
motor.motor_stop() | |
command_name = "Park a Car" | |
elif CMDID == CommandWord.TurnOnTheLight: | |
update_rgb_color(255, 255, 255) | |
command_name = "Turn On The Light" | |
elif CMDID == CommandWord.TurnOffTheLight: | |
update_rgb_color(0, 0, 0) | |
command_name = "Turn Off The Light" | |
elif CMDID == CommandWord.BrightenTheLight: | |
if brightness < 1.0: | |
brightness += 0.1 | |
update_neopixel() | |
command_name = "Brighten The Light" | |
elif CMDID == CommandWord.DimTheLight: | |
if brightness > 0.0: | |
brightness -= 0.1 | |
update_neopixel() | |
command_name = "Dim The Light" | |
elif CMDID == CommandWord.SetToRed: | |
update_rgb_color(255, 0, 0) | |
command_name = "Set To Red" | |
elif CMDID == CommandWord.SetToOrange: | |
update_rgb_color(255, 165, 0) | |
command_name = "Set To Orange" | |
elif CMDID == CommandWord.SetToYellow: | |
update_rgb_color(255, 255, 0) | |
command_name = "Set To Yellow" | |
elif CMDID == CommandWord.SetToGreen: | |
update_rgb_color(0, 255, 0) | |
command_name = "Set To Green" | |
elif CMDID == CommandWord.SetToCyan: | |
update_rgb_color(0, 255, 255) | |
command_name = "Set To Cyan" | |
elif CMDID == CommandWord.SetToBlue: | |
update_rgb_color(0, 0, 255) | |
command_name = "Set To Blue" | |
elif CMDID == CommandWord.SetToPurple: | |
update_rgb_color(128, 0, 128) | |
command_name = "Set To Purple" | |
elif CMDID == CommandWord.SetToWhite: | |
update_rgb_color(255, 255, 255) | |
command_name = "Set To White" | |
elif CMDID == CommandWord.Reset: | |
servo.set_angle(board.GP7, 0) | |
command_name = "Set Servo To 0 Degrees" | |
elif CMDID == CommandWord.SetServoToTenDegrees: | |
servo.set_angle(board.GP7, 10) | |
command_name = "Set Servo To 10 Degrees" | |
elif CMDID == CommandWord.SetServoToThirtyDegrees: | |
servo.set_angle(board.GP7, 30) | |
command_name = "Set Servo To 30 Degrees" | |
elif CMDID == CommandWord.SetServoToFortyFiveDegrees: | |
servo.set_angle(board.GP7, 45) | |
command_name = "Set Servo To 45 Degrees" | |
elif CMDID == CommandWord.SetServoToSixtyDegrees: | |
servo.set_angle(board.GP7, 60) | |
command_name = "Set Servo To 60 Degrees" | |
elif CMDID == CommandWord.SetServoToNinetyDegrees: | |
servo.set_angle(board.GP7, 90) | |
command_name = "Set Servo To 90 Degrees" | |
elif CMDID == CommandWord.TurnOnTheBuzzer: | |
buzzer.sound() | |
command_name = "Turn On The Buzzer" | |
elif CMDID == CommandWord.TurnOffTheBuzzer: | |
buzzer.bequiet() | |
command_name = "Turn Off The Buzzer" | |
if command_name: | |
lcd.clear() | |
lcd.print(command_name) | |
def setup(): | |
DF2301Q.set_volume(15) | |
DF2301Q.set_mute_mode(0) | |
DF2301Q.set_wake_time(20) | |
print("wake_time = %u\n" % (DF2301Q.get_wake_time())) | |
def loop(): | |
CMDID = DF2301Q.get_CMDID() | |
if CMDID != 0: | |
print("CMDID = %u\n" % CMDID) | |
handle_voice_command(CMDID) | |
time.sleep(1) | |
if __name__ == "__main__": | |
setup() | |
while True: | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment