0

我正在编写我的第一个 Alexa 技能,目的是通过提供语音输入与机器人进行交互,并允许机器人通过 Alexa 界面发回消息。

我正在使用 Flask-Ask Python 扩展来编写本地后端,使用 ngrok 建立 http 通信和 ROS 与机器人通信。

我创建了启动和停止意图,以及我要求 Alexa 移动机器人的个人意图。

我现在需要做的是创建一个函数,允许我使用 alexa speak 输出来传达消息,而无需用户交互;例如,当if触发自定义条件时,我希望 Alexa 播放示例消息:

if condition == True:
    def advise_function():
        sample_message = 'Messaggio di avviso'
        return statement(sample_message)

这是实际代码(供参考):

#!/usr/bin/env python
import os
import rospy
import threading
import requests
import time

from flask import Flask
from flask_ask import Ask, question, statement, session
from std_msgs.msg import String, Float64


#---------------------------------------------- FLASK & ROS INIZIALIZATION ---------------------------------------------#

app = Flask(__name__)
ask = Ask(app, "/")

# ROS node, publisher, and parameter. The node is started in a separate thread to avoid conflicts with Flask.
# The parameter *disable_signals* must be set if node is not initialized in the main thread.
threading.Thread(target=lambda: rospy.init_node('alexa_ros_node', disable_signals=True)).start()
direction_publisher = rospy.Publisher('alexa/direction', String, queue_size=100)
length_publisher = rospy.Publisher('alexa/lenght', Float64, queue_size=100)
NGROK = rospy.get_param('/ngrok', None)


#------------------------------------------------------- INTENTS -------------------------------------------------------#

# LaunchIntent -> Avvio dell'applicazione: "Apri nodo ros"
@ask.launch
def launch():
    welcome_message = 'Benvenuto nel nodo di controllo ROS. Come posso aiutarti?'
    return question(welcome_message)

# MuoviRobotIntent: "Muovi il robot"
@ask.intent('MuoviRobotIntent', default={'direzione':None, 'lunghezza':None})
def move_robot_function(direzione, lunghezza):
    direction_publisher.publish(direzione)
    length_publisher.publish(float(lunghezza))
    return statement('Ho pubblicato direzione e lunghezza sul topic ROS: {0} di {1} metri.'.format(direzione, lunghezza))


# StopIntent -> Uscita dall'applicazione: "Stop / Esci..."
@ask.session_ended
def session_ended():
    return "{}", 200

# ngrok app running
if __name__ == '__main__':
    if NGROK:
        print 'NGROK mode'
        app.run(host=os.environ['ROS_IP'], port=5000)
    else:
        print 'Manual tunneling mode'
        dirpath = os.path.dirname(__file__)
        cert_file = os.path.join(dirpath, '../config/ssl_keys/certificate.pem')
        pkey_file = os.path.join(dirpath, '../config/ssl_keys/private-key.pem')
        app.run(host=os.environ['ROS_IP'], port=5000,
                ssl_context=(cert_file, pkey_file))
4

1 回答 1

0

查看主动事件 API。我相信它应该涵盖您的用例。 https://developer.amazon.com/en-US/docs/alexa/smapi/proactive-events-api.html

于 2020-06-23T23:11:08.563 回答