Creating a skill

Now that you have completed the preceding steps, let's follow these steps to create an Alexa skill.

Let's create a file called alexa_ros_test.py and copy the code into our repository at https://github.com/PacktPublishing/ROS-Robotics-Projects-SecondEdition/blob/master/chapter_7_ws/alexa_ros_test.py into it. Let's look at the important details of the code here:

...

threading.Thread(target=lambda: rospy.init_node('test_code', disable_signals=True)).start()
pub = rospy.Publisher('test_pub', String, queue_size=1)

app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger("flask_ask").setLevel(logging.DEBUG)

@ask.launch
def launch():
welcome_msg = render_template('welcome')
return question(welcome_msg)

@ask.intent('HelloIntent')
def hello(firstname):
text = render_template('hello', firstname=firstname)
pub.publish(firstname)
return statement(text).simple_card('Hello', text)

...

In the preceding code walkthrough, we made use of render_template from Flask to import custom responses from a stored YAML file. We started the ROS node as a parallel thread and initialized the topics, the app, and the loggers. Then, we created decorators to run a launch (def launch()) and response message (def hello()). After that, we started the app.

Let's create another file named template.yaml and copy the following contents into it:

welcome: Welcome, This is a simple test code to evaluate one of my abilities. How do I call you?
hello: Hello, {{ firstname }}

Now, run roscore in one Terminal and the Python program in another Terminal using the following command:

$ python code.py
While running the Python code in another Terminal, ensure you have initros1 invoked so that you don't see any ROS-related errors.

In a new Terminal, open ngrok to generate a public HTTPS endpoint to the localhost at port 5000:

$ ./ngrok http 5000

You should see the following screen:

ngrok status message

Note down the HTTPS endpoint link at the end (in this case, it is https://7f120e45.ngrok.io). Once done, go to the Alexa developer account page (https://developer.amazon.com/alexa/console/ask?) and execute the following steps:

Please note that every time you close the ngrok Terminal, a new endpoint is created.
  1. In the Alexa developer account page, select Create Skill. Enter the Skill Name as Alexa ros and select a custom model and Provision your own as the method to host the skill's backend resources.
  2. Choose Start from scratchYou should see the following screen (the text and numbers in this image are intentionally illegible):

Alexa developer account page
  1. Select Invocation Name on the right-hand side and set the name as test code.

Once done, on the left-hand side, select Intents to add a new intent. Create a custom intent with the name HelloIntent and add the utterances as shown in the following screenshot. Also, ensure that the slot type is AMAZON.firstname, as shown here:

Utterances reference
  1. You're done. Now, save the model and build it using the Build Model button. Once you're done, you should see the following screen:

Build successful
  1. Once the build is successful, go to Endpoint on the left-hand side and select the HTTPS radio button. Enter the https endpoint link you noted in the ngrok Terminal and paste that into the Default Region textbox.
  2. Select the My development endpoint is a sub-domain of a domain that has a wild card certificate... option. Once done, save the endpoints using the Save Endpoints button.
  3. You're all set now! Assuming you've started roscore, the Python code—alexa_ros_test.py—and ngrok can begin the test on the developer page if we browse to the Test tab.
  4. In the Test tab, select Development from the drop-down list box. Now, you're ready to use the Alexa simulator. You can start by typing the following command:
Alexa, launch test code
You should hear Alexa reply with Welcome, this is a simple test code to evaluate one of my abilities. What do I call you?, since we have provided this as the template in the YAML file.

Type: Alexa, Call me ROS.

You should hear Alexa say Hello ROS.

You should also be able to see the data being published as rostopic, as shown here (the text and numbers in this image are intentionally illegible):

Overall testing

That's it! You have now created an Alexa skill successfully and learned how to publish simple data with ROS. You could make use of the GPIO API usage that we saw for the embedded boards and connect Alexa with them to take control of the devices that are connected to them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.115.120