Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 53 additions & 48 deletions gpt_examples/gpt_car.py → gpt_examples/gpt_car2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import os
import sys
import json # Import the json module
import paho.mqtt.client as mqtt # Import the paho-mqtt library

os.popen("pinctrl set 20 op dh") # enable robot_hat speake switch
current_path = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -38,7 +40,7 @@
# =================================================================
openai_helper = OpenAiHelper(OPENAI_API_KEY, OPENAI_ASSISTANT_ID, 'picarx')

LANGUAGE = []
LANGUAGE = ['no']
# LANGUAGE = ['zh', 'en'] # config stt language code, https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes

# VOLUME_DB = 5
Expand Down Expand Up @@ -82,24 +84,6 @@
time.sleep(.5)
print('\n')

# speech_recognition init
# =================================================================
'''
self.energy_threshold = 300 # minimum audio energy to consider for recording
self.dynamic_energy_threshold = True
self.dynamic_energy_adjustment_damping = 0.15
self.dynamic_energy_ratio = 1.5
self.pause_threshold = 0.8 # seconds of non-speaking audio before a phrase is considered complete
self.operation_timeout = None # seconds after an internal operation (e.g., an API request) starts before it times out, or ``None`` for no timeout

self.phrase_threshold = 0.3 # minimum seconds of speaking audio before we consider the speaking audio a phrase - values below this are ignored (for filtering out clicks and pops)
self.non_speaking_duration = 0.5 # seconds of non-speaking audio to keep on both sides of the recording

'''
recognizer = sr.Recognizer()
recognizer.dynamic_energy_adjustment_damping = 0.16
recognizer.dynamic_energy_ratio = 1.6

# speak_hanlder
# =================================================================
speech_loaded = False
Expand Down Expand Up @@ -212,7 +196,21 @@ def action_handler():
action_thread = threading.Thread(target=action_handler)
action_thread.daemon = True


# MQTT Functions
# =================================================================
def on_connect(client, userdata, flags, rc):
"""Callback function for when the MQTT client connects."""
if rc == 0:
print("Connected to MQTT broker")
client.subscribe("picarx/stt") # Subscribe to the topic
else:
print(f"Failed to connect to MQTT broker, rc={rc}")

def on_message(client, userdata, msg):
"""Callback function for when a message is received on the subscribed topic."""
stt_text = msg.payload.decode('utf-8')
print(f"Received STT text from MQTT: {stt_text}")

# main
# =================================================================
def main():
Expand All @@ -226,49 +224,56 @@ def main():

speak_thread.start()
action_thread.start()

# Setup MQTT Client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

# MQTT Broker Address
mqtt_broker_address = "IP_ADRESS" # Replace with your broker's address
mqtt_username = "user"
mqtt_password = "pass"
try:
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_broker_address, 1883, 60) # Connect to the broker
print("Connecting to MQTT broker...") # Add log
except Exception as e:
print(f"\033[31mERROR: Could not connect to MQTT broker: {e}\033[m")
print("Please check your MQTT broker address and ensure it is running.")
sys.exit(1)

while True:
if input_mode == 'voice':
my_car.set_cam_tilt_angle(DEFAULT_HEAD_TILT)

# listen
# ----------------------------------------------------------------
gray_print("listening ...")

with action_lock:
action_status = 'standby'

_stderr_back = redirect_error_2_null() # ignore error print to ignore ALSA errors
# If the chunk_size is set too small (default_size=1024), it may cause the program to freeze
with sr.Microphone(chunk_size=8192) as source:
cancel_redirect_error(_stderr_back) # restore error print
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
# client.connect("localhost", 1883, 60)

# stt
# ----------------------------------------------------------------
st = time.time()
_result = openai_helper.stt(audio, language=LANGUAGE)
gray_print(f"stt takes: {time.time() - st:.3f} s")
client.loop_start() # Start the MQTT loop in a non-blocking thread

if _result == False or _result == "":
print() # new line
continue
print("Waiting for STT data from Home Assistant via MQTT topic 'picarx/stt'")
try:
while True:
time.sleep(0.1) # Keep the main thread alive
except KeyboardInterrupt:
print("Stopping...")
my_car.reset()
client.loop_stop() # Stop the MQTT loop
client.disconnect() # Disconnect from the broker

elif input_mode == 'keyboard':
while True:
if input_mode == 'voice':
my_car.set_cam_tilt_angle(DEFAULT_HEAD_TILT)
print("listening ...")

with action_lock:
action_status = 'standby'

_result = input(f'\033[1;30m{"intput: "}\033[0m').encode(sys.stdin.encoding).decode('utf-8')
st = time.time()
_result = stt_text(f'\033[1;30m{"intput: "}\033[0m').encode(sys.stdin.encoding).decode('utf-8')

if _result == False or _result == "":
print() # new line
continue

else:
raise ValueError("Invalid input mode")
print("failing ...")

# chat-gpt
# ----------------------------------------------------------------
Expand Down