功能简述
通过运行python文件,可以对NAOv6机器人的动作进行控制,实现基本的前进后退转弯等动作,只需要在终端输入对应命令即可
环境配置及运行说明:
前提:ubuntu18.04 系统
-
安装
Python2.7
,程序仅能在该版本中运行。 -
将
pynaoqi-python2.7-2.8.6.23-linux64
解压放到主目录中。 -
设置环境变量。
在终端输入:
gedit ~/.bashrc
在行尾添加以下语句,具体路径根据本地电脑情况进行更改:
export PYTHONPATH=${PYTHONPATH}:/home/leo/pynaoqi-python2.7-2.8.6.23-linux64/lib/python2.7/site-packages
输入命令使环境变量生效:
source ~/.bashrc
-
用网线连接NAO机器人(确保此时是Naoqi系统,否则需要刷机重置),按压胸口,在浏览器中输入相应的ip地址,此时会要求输入账户和密码,默认都为
nao
,按照指示勾选一些条款后,会进入NAO机器人的WIFI页面,此时,可以手动修改机器人连接的WIFI,确保与电脑在同一局域网下,为了保证通信流畅,最好使用单独的路由器。修改完成后重启机器人。 -
此时,机器人和电脑端就在同一局域网下了,断开网线,按压机器人胸口,获得此时的ip地址,将得到的ip地址填入代码指定位置,具体见注释。
-
在
moveControl.py
文件路径下运行:python2.7 moveControl.py
-
操作命令如下:
W 前进一大步
w 前进
S/s 后退
A/a 左移
D/d 右移
Q/q 左转
E/e 右转
B/b 站起
R/r 坐下
参考代码如下:
#coding=UTF-8
import sys
import time
import almath
import threading
from naoqi import ALProxy
ip="" #机器人ip地址,务必保证此时机器人和电脑端在同一局域网下,按胸口获得地址,如 ip="192.168.1.100"
zero_pi=3.1415 #圆周率π
def lookAround():
toward=1.0
tts=ALProxy("ALMotion",ip,9559)
while True:
tts.setStiffnesses("Head",1.0)
names="HeadYaw"
angles =30.0*almath.TO_RAD*toward
fractionMaxSpeed=0.3
tts.setAngles(names,angles,fractionMaxSpeed)
time.sleep(1)
tts.setStiffnesses("Head",0.0)
toward=-toward
def excute(operate):
tts=ALProxy("ALMotion",ip,9559)
if operate == "r" or operate == "R": #机器人坐下
tts.rest()
if operate == "b" or operate == "B": #机器人站立
tts.wakeUp()
if operate == "w": #前进
tts.moveTo(0.1,0.0,0.0)
tts.waitUntilMoveIsFinished()
if operate == "W": #前进一大步
tts.moveTo(0.5,0.0,0.0,_async=True)
if operate == "s" or operate == "S": #后退
tts.moveTo(-0.1,0.0,0.0)
tts.waitUntilMoveIsFinished()
if operate == "a" or operate == "A": #向左移动
tts.moveTo(0.0,0.05,0.0)
tts.waitUntilMoveIsFinished()
if operate == "d" or operate == "D": #向右移动
tts.moveTo(0.0,-0.05,0.0)
tts.waitUntilMoveIsFinished()
if operate == "q" or operate == "Q": #向左转
tts.moveTo(0.0,0.0,zero_pi/8)
tts.waitUntilMoveIsFinished()
if operate == "e" or operate == "E": #向右转
tts.moveTo(0.0,0.0,-zero_pi/8)
tts.waitUntilMoveIsFinished()
if __name__ == "__main__":
t1=threading.Thread(target=lookAround,name="lookAround") #将转头的操作函数放入进程,持续运行
t1.start()
time.sleep(1)
while True:
operate=raw_input("输入操作:")
excute(operate)
time.sleep(1.0)