Python SDK 入门(2)——让 NAO 行走并说话

 1. 使 NAO 刚化

  • 除非你将 NAO 的 stiffness 的值设为非 0 数,否则它是不会移动的

  • 而要做到这点其实很简单,只要通过调用ALMotionProxy::setStiffnesses()进行设置即可:

    1
    2
    3
    from naoqi import ALProxy
    motion = ALProxy("ALMotion", "nao.local", 9559)
    motion.setStiffnesses("Body", 1.0)

2. 使 NAO 移动

为了让 NAO 移动,我们应该使用 ALMotionProxy::moveInit()函数(以使 NAO 处于合适的姿势),然后再调用ALMotionProxy::moveTo()

1
2
3
4
from naoqi import ALProxy
motion = ALProxy("ALMotion", "nao.local", 9559)
motion.moveInit()
motion.moveTo(0.5, 0, 0)

3. 使 NAO 同时说话并行走

我们创建的每一个代理(proxy)都有一个叫做”post”的属性,且可以通过使用它在后台调用很多方法。

这可以帮助我们使机器人同时做很多事:

1
2
3
4
5
6
from naoqi import ALProxy
motion = ALProxy("ALMotion", "nao.local", 9559)
tts = ALProxy("ALTextToSpeech", "nao.local", 9559)
motion.moveInit()
motion.post.moveTo(0.5, 0, 0)
tts.say("I'm walking")

当然,如果需要等待一个任务结束,我们可以使用 ALProxy 中的等待方法,使用寄出的方法(the post usage)所返回的任务 ID:

1
2
3
4
5
from naoqi import ALProxy
motion = ALProxy("ALMotion", "nao.local", 9559)
motion.moveInit()
id = motion.post.moveTo(0.5, 0, 0)
motion.wait(id, 0)

完整的程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from naoqi import ALProxy
import argparse

motion = ALProxy("ALMotion", "192.168.1.114", 9559) #NAO的动作对象
tts = ALProxy("ALTextToSpeech", "192.168.1.114", 9559) #NAO的语言对象
posture = ALProxy("ALRobotPosture", "192.168.1.114", 9559) #NAO的姿势对象

#首先唤醒NAO
motion.wakeUp()

#让NAO站好
posture.goToPosture("StandInit", 0.5)

#将其刚化
motion.setStiffnesses("Body", 1.0)

#初始化
motion.moveInit()

#让NAO向前走1米,同时返回任务ID给id
id = motion.post.moveTo(1, 0, 0)

tts.say("I'm walking")

#直到id传过来了,再执行wait()函数
motion.wait(id, 0)

tts.say("I will not walk anymore")

#让NAO休眠
motion.rest()