使用Python对Nao机器人进行基础的编程

pynaoqi库的安装

下载文件
下载完成后直接打开文件,运行安装即可

在Choregraphe上进行python编程

在主程序区新建python盒子在这里插入图片描述
在这里插入图片描述

通过python让Nao说话

在Choregraphe中写入这样一段代码

 def onInput_onStart(self):		
        #self.onStopped() #activate the output of the box
        tts = ALProxy("ALTextToSpeech")  # 创建ALProxy中“ALTextToSpeech”的代理				
        tts.say("Hello")				# 通过调用其中的.say()方法使机器人说话	
        self.onStopped()					

通过python让Nao行走

    def onInput_onStart(self):
        #self.onStopped() #activate the output of the box
        motion = ALProxy("ALMotion")		# 创建"ALMotion"的代理
        motion.moveTo(0.1, 0, 0)			# 调用moveTo()方法,传递三个参数,分别为前进的距离,向左的距离,身体转的角度
        self.onStopped()

在Pycharm中对nao进行控制

在成功安装naoqi库以后,我们就可以在Pycharm中控制nao机器人

通过代理建立连接

# 通过调用naoqi中的ALProxy类 建立代理连接
from naoqi import ALProxy
ALExpression = ALProxy("模块名称", robot_IP, port)

模块名称通过查阅API文档获得: naoAPI

通过调用模块中的方法来实现对机器人的控制

# 实现Nao机器人说话
robot_IP = "192.168.31.206"			# 输入Nao的IP地址
port = 9559				# 端口号默认为9559
tts = ALProxy("ALTextToSpeech", robot_IP, port)
tts.say("Hello World")

通过pyCharm调用Nao机器人的摄像头进行图像识别

需要类库:opencv-python、numpy、pynaoqi
下面是调用小nao识别红球的代码

# -*- coding: UTF-8 -*-
import cv2
import numpy as np
from naoqi import ALProxy
import vision_definitions
import math

port = 9559  # crf 机器人端口
robot_ip = "192.168.31.206"  # crf 机器人IP
cameraProxy = ALProxy("ALVideoDevice", robot_ip, port)

# 基本参数
resolution = vision_definitions.kVGA
colorSpace = vision_definitions.kBGRColorSpace
fps =20
frameHeight = 0
frameWidth = 0
frameChannels = 0
frameArray = None
cameraPitchRange = 47.64/180*math.pi
cameraYawRange = 60.97/180*math.pi

# 获取图片
cameraProxy.setActiveCamera(1)
videoClient = cameraProxy.subscribe("python_GVM",resolution, colorSpace,fps)
frame = cameraProxy.getImageRemote(videoClient)
cameraProxy.unsubscribe(videoClient)

#读取图片
frameWidth = frame[0]
frameHeight = frame[1]
frameChannels = frame[2]
frameArray = np.frombuffer(frame[6], dtype=np.uint8).reshape([frameHeight, frameWidth, frameChannels])
# 转换为HSV
hue_image = cv2.cvtColor(frameArray, cv2.COLOR_BGR2HSV)

# 用颜色分割图像
low_range = np.array([160, 83, 100])
high_range = np.array([180, 255, 255])
th = cv2.inRange(hue_image, low_range, high_range)
cv2.imshow('result', th)
cv2.waitKey(0)

# 平滑处理
gaus=cv2.GaussianBlur(th,(7,7),1.5)
cv2.imshow('result', gaus)
cv2.waitKey(0)

# 腐蚀
eroded = cv2.erode(gaus, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4)), iterations=2)
cv2.imshow('result', eroded)
cv2.waitKey(0)

# 膨胀
dilated = cv2.dilate(eroded, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
cv2.imshow('result', dilated)
cv2.waitKey(0)

# Hough Circle
circles = cv2.HoughCircles(dilated, cv.CV_HOUGH_GRADIENT, 1, 100, param1=15, param2=7, minRadius=15, maxRadius=100)

# 绘制
if circles is not None:
    x, y, radius = circles[0][0]
    center = (x, y)
    cv2.circle(frameArray, center, radius, (0, 255, 0), 2)
cv2.imshow('result', frameArray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Nao的语音识别

科大讯飞网站:
https://www.xfyun.cn/?ch=bdtg&b_scene_zt=1&renqun_youhua=646957

  • 可通过下载科大讯飞语音识别代码 自行学习语音识别 通过阅读API文档 自行将程序与机器人相结合
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多敲代码灬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值