Voice Hat Motorsteuerung

Python auf Einplatinencomputer wie Raspberry Pi, Banana Pi / Python für Micro-Controller
janko84
User
Beiträge: 23
Registriert: Sonntag 17. September 2017, 16:52

Geil. Es funktioniert, Danke viel mals.

Jetzt habe ich ein allerletztes problem an dem ich schon seit tagen paralell dran bin und es nicht lösen kann. das letzte problem:

Es funktioniert jetzt nur wenn ich auf den button drücke und dann den befehl sage, jedesmal wenn ich ein befehl sagen will muss ich also den knopf drücken.
Ich möchte das der google assistent immer zuhört und das ich nur das hotword sagen mussund dann den befehl anstatt den button zu drücken.

kann mir da jemand helfen? habe 2 files, eines ist aissistent_library und das andere asisstant_library_local , beide funktionieren wenn ich ok google sage und dan den befehl.

Bei local führt er glaube ich python befehle wie im terminal aus und beim anderen kann man fragen was man will = Google Assistent

mein code:

Code: Alles auswählen

import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
from gpiozero import PWMOutputDevice
from time import sleep
import time


def main():
    recognizer = aiy.cloudspeech.get_recognizer()
    recognizer.expect_phrase('Start')
    recognizer.expect_phrase('Stop')
    recognizer.expect_phrase('stop')
    recognizer.expect_phrase('left')
    recognizer.expect_phrase('Right')
    recognizer.expect_phrase('right')
    recognizer.expect_phrase('Left')

    button = aiy.voicehat.get_button()
    led = aiy.voicehat.get_led()
    aiy.audio.get_recorder().start()

    pwm4 = PWMOutputDevice(4)
    pwm17 = PWMOutputDevice(17)
    
    

    while True:
        print('Drücke den Knopf und Spreche')
        button.wait_for_press()
        print('Höre zu...')
        text = recognizer.recognize()
        if text is None:
          print('Sorry, Ich habe dich nicht gehöhrt')
        else:
            print('You said "', text, '"')
            if 'Start' in text:
                print('Motor fährt los')
                pwm4.on()
                pwm17.on()
                
            elif 'Stop' in text:
                print('Motor haltet an')
                pwm4.off()
                pwm17.off()
            elif 'stop' in text:
                print('Motor haltet an')
                pwm4.off()
                pwm17.off()
            elif 'right' in text:
                print('Motor geht nach rechts')
                pwm4.on()
                pwm17.off()
                sleep(2)
                pwm4.off()
            elif 'left' in text:
                print('Motor geht nach links')
                pwm4.off()
                pwm17.on()
                sleep(5)
                pwm17.off()
            elif 'Right' in text:
                print('Motor geht nach rechts')
                pwm4.on()
                pwm17.off()
                sleep(2)
                pwm4.off()
            elif 'Left' in text:
                print('Motor geht nach links')
                pwm4.off()
                pwm17.on()
                sleep(5)
                pwm17.off()
                
                
         
                
if __name__ == '__main__':
    main()
Aissistant_library Code:

Code: Alles auswählen

import logging
import platform
import sys

import aiy.assistant.auth_helpers
from aiy.assistant.library import Assistant
import aiy.voicehat
from google.assistant.library.event import EventType

logging.basicConfig(
    level=logging.INFO,
    format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
)


def process_event(event):
    status_ui = aiy.voicehat.get_status_ui()
    if event.type == EventType.ON_START_FINISHED:
        status_ui.status('ready')
        if sys.stdout.isatty():
            print('Say "OK, Google" then speak, or press Ctrl+C to quit...')

    elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        status_ui.status('listening')

    elif event.type == EventType.ON_END_OF_UTTERANCE:
        status_ui.status('thinking')

    elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED
          or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT
          or event.type == EventType.ON_NO_RESPONSE):
        status_ui.status('ready')

    elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']:
        sys.exit(1)


def main():
    if platform.machine() == 'armv6l':
        print('Cannot run hotword demo on Pi Zero!')
        exit(-1)

    credentials = aiy.assistant.auth_helpers.get_assistant_credentials()
    with Assistant(credentials) as assistant:
        for event in assistant.start():
            process_event(event)


if __name__ == '__main__':
    main()
Assistant_Library_Local Code:

Code: Alles auswählen

import logging
import platform
import subprocess
import sys


import aiy.assistant.auth_helpers
from aiy.assistant.library import Assistant
import aiy.audio
import aiy.voicehat
from google.assistant.library.event import EventType

logging.basicConfig(
    level=logging.INFO,
    format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
)


def power_off_pi():
    aiy.audio.say('Good bye!')
    subprocess.call('sudo shutdown now', shell=True)
    

def reboot_pi():
    aiy.audio.say('See you in a bit!')
    subprocess.call('sudo reboot', shell=True)


def say_ip():
    ip_address = subprocess.check_output("hostname -I | cut -d' ' -f1", shell=True)
    aiy.audio.say('My IP address is %s' % ip_address.decode('utf-8'))


def process_event(assistant, event):
    status_ui = aiy.voicehat.get_status_ui()
    if event.type == EventType.ON_START_FINISHED:
        status_ui.status('ready')
        if sys.stdout.isatty():
            print('Say "OK, Google" then speak, or press Ctrl+C to quit...')

    elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        status_ui.status('listening')

    elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
        print('You said:', event.args['text'])
        text = event.args['text'].lower()
        if text == 'power off':
            assistant.stop_conversation()
            power_off_pi()
        elif text == 'reboot':
            assistant.stop_conversation()
            reboot_pi()
        elif text == 'ip address':
            assistant.stop_conversation()
            say_ip()

    elif event.type == EventType.ON_END_OF_UTTERANCE:
        status_ui.status('thinking')

    elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED
          or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT
          or event.type == EventType.ON_NO_RESPONSE):
        status_ui.status('ready')

    elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']:
        sys.exit(1)


def main():
    if platform.machine() == 'armv6l':
        print('Cannot run hotword demo on Pi Zero!')
        exit(-1)

    credentials = aiy.assistant.auth_helpers.get_assistant_credentials()
    with Assistant(credentials) as assistant:
        for event in assistant.start():
            process_event(assistant, event)


if __name__ == '__main__':
    main()
Antworten