1月20日-1月25日
我参加了河海大学和深圳优必选公司联合举办的训练营
作为机器人领域的龙头企业,优必选公司的产品Alpha曾经登陆过春晚进行表演,这次活动中他们的机器人产品Yanshee也非常厉害
在训练营的末尾,我们小组用了一天半的时间做了一个小型的项目,当作我们的结营汇报,以下为项目内容。
功能1-4:【项目回顾】基于Yanshee的AI服务型机器人(一)
功能5-8:
- 通过网络爬虫程序获得当前城市当日的天气预报并通过语音播报
- 通过红外传感器(Infrared Sensor)进行避障巡逻
- 通过红外传感器(Infrared Sensor)进行遇障绕路行走
- 通过摄像头的抓拍进行人脸识别、跟踪
这篇文章将回顾项目的第5-8项功能
5:通过网络爬虫程序获得当前城市当日的天气预报并通过语音播报
#!/usr/bin/python
#_*_ coding:utf-8 _*_
import urllib
import sys
import RobotApi
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
def weather_get():
resp=urllib.urlopen('http://www.weather.com.cn/weather/101270101.shtml')
soup=BeautifulSoup(resp,'html.parser')
tagDate=soup.find('ul', class_="t clearfix")
dates=tagDate.h1.string
tagToday=soup.find('p', class_="tem")
try:
temperatureHigh=tagToday.span.string
except AttributeError as e:
temperatureHigh=tagToday.find_next('p', class_="tem").span.string
temperatureLow=tagToday.i.string
weather=soup.find('p', class_="wea").string
tagWind=soup.find('p',class_="win")
winL=tagWind.i.string
print('今天是:'+dates)
print('风级:'+winL)
print('最低温度:'+temperatureLow)
print('最高温度:'+temperatureHigh)
print('天气:'+weather)
a='今天是:'+str(dates)
b='风级:'+str(winL)
c='最低温度:'+str(temperatureLow)
d='最高温度:'+str(temperatureHigh)
e='天气:'+str(weather)
RobotApi.ubtVoiceTTS(1,a)
RobotApi.ubtVoiceTTS(1,b)
RobotApi.ubtVoiceTTS(1,c)
RobotApi.ubtVoiceTTS(1,d)
RobotApi.ubtVoiceTTS(1,e)
因为天气信息分布在网络上,因此实现这个功能就要使用网络爬虫,又因为Yanshee中内置了Python 2.7的版本,所以这个程序使用了urllib和bs4两个模块,用sys模块进行编码,提取中国天气网上的信息,并通过文字转语音的接口输出出来。
6:通过红外传感器进行避障巡逻
import RobotApi
from time import sleep
def alert1():
i=1
while i<=7:
sensorValue1=RobotApi.UBTEDU_ROBOTINFRARED_SENSOR_T()
ret=-1
while (ret!=0):
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
if (ret!=0):
print("read sensor error!\n")
time.sleep(0.3)
RobotApi.ubtSearchExtendSensor()
if i==7:
ret = RobotApi.ubtSetRobotMotion("turn around","left",5,5)
i=1
if sensorValue1.iValue>300:
ret = RobotApi.ubtSetRobotMotion("walk","front",5,1)
i=i+1
else:
RobotApi.ubtStopRobotAction()
RobotApi.ubtVoiceTTS(1,"碰到障碍物")
break
这个避障巡逻的思路很简单
if sensorValue1.iValue>300:
ret = RobotApi.ubtSetRobotMotion("walk","front",5,1)
i=i+1
当红外检测到300mm内没有障碍物的时候就执行向前走的动作,动作速度为5,重复次数为1
当向前走的次数超过七次的时候向左转,然后i的数值回到1,重复检测障碍物,向前行走
if i==7:
ret = RobotApi.ubtSetRobotMotion("turn around","left",5,5)
i=1
当遇到障碍物的时候,机器人停止动作并且播报:“遇到障碍物”
else:
RobotApi.ubtStopRobotAction()
RobotApi.ubtVoiceTTS(1,"碰到障碍物")
break
7:通过红外传感器进行遇障绕路行走
import RobotApi
from time import sleep
def walk1():
k=1
l=0
i=1
while i<35:
sensorValue1=RobotApi.UBTEDU_ROBOTINFRARED_SENSOR_T()
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
while (ret!=0):
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
if (ret!=0):
print("read sensor error!\n")
time.sleep(0.3)
RobotApi.ubtSearchExtendSensor()
if sensorValue1.iValue<300:
ret = RobotApi.ubtSetRobotMotion("turn around","left",5,2)
k=0
l=l+1
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
while (ret!=0):
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
if (ret!=0):
print("read sensor error!\n")
time.sleep(0.3)
RobotApi.ubtSearchExtendSensor()
else:
ret = RobotApi.ubtSetRobotMotion("walk","front",5,2)
k=1
i=i+1
if l!=0:
if k==1:
ret=RobotApi.ubtSetRobotMotion("turn around","right",5,2)
l=l-1
k=0
绕路行走的时候机器人会向左绕过障碍物,并且不断检测是否已经通过障碍物,通过后即会向右回到原本的路线,继续向前行走
在这段代码中,变量k代表是否向左,若k大于1,机器人就会不断地试图向右回到原来的路径上,但是若是还没有度过障碍物,就会继续向前行走,直到通过障碍物
8:通过摄像头的抓拍进行人脸识别、跟踪
import RobotApi
from time import sleep
def facewalk():
num="0"
ret=RobotApi.ubtVisionDetect("face",num,10)
print(num)
while ret!=0:
RobotApi.ubtVoiceTTS(1,"没有检测到目标")
time.sleep(0.5)
ret=RobotApi.ubtVisionDetect("face",num,10)
print(num)
while True:
sensorValue1=RobotApi.UBTEDU_ROBOTINFRARED_SENSOR_T()
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
while (ret!=0):
ret=RobotApi.ubtReadSensorValue("infrared",sensorValue1,4)
if (ret!=0):
print("read sensor error!\n")
time.sleep(0.3)
RobotApi.ubtSearchExtendSensor()
if sensorValue1.iValue>300:
ret = RobotApi.ubtSetRobotMotion("walk","front",5,3)
else:
RobotApi.ubtStopRobotAction()
RobotApi.ubtVoiceTTS(1,"碰到障碍物")
break
ret=RobotApi.ubtVisionDetect("face",num,10)
print(num)
if ret!=0:
RobotApi.ubtVoiceTTS(1,"没有检测到目标")
RobotApi.ubtStopRobotAction()
break
首先用RobotApi.ubtVisionDetect()检测摄像头范围内有没有人脸
再选择向前跟随,隔一段时间后停下来继续检测前方还有没有人脸,若没有人脸就会回复:“没有检测到目标”
功能9-12:【项目回顾】基于Yanshee的AI服务型机器人(三)