【项目回顾】基于Yanshee的AI服务型机器人(二)

1月20日-1月25日
我参加了河海大学和深圳优必选公司联合举办的训练营
作为机器人领域的龙头企业,优必选公司的产品Alpha曾经登陆过春晚进行表演,这次活动中他们的机器人产品Yanshee也非常厉害
在训练营的末尾,我们小组用了一天半的时间做了一个小型的项目,当作我们的结营汇报,以下为项目内容。

功能1-4:【项目回顾】基于Yanshee的AI服务型机器人(一)

功能5-8:

  1. 通过网络爬虫程序获得当前城市当日的天气预报并通过语音播报
  2. 通过红外传感器(Infrared Sensor)进行避障巡逻
  3. 通过红外传感器(Infrared Sensor)进行遇障绕路行走
  4. 通过摄像头的抓拍进行人脸识别、跟踪

这篇文章将回顾项目的第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
ubtSetRobotMotion的介绍
当向前走的次数超过七次的时候向左转,然后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()检测摄像头范围内有没有人脸
ubtVisionDetect的介绍
再选择向前跟随,隔一段时间后停下来继续检测前方还有没有人脸,若没有人脸就会回复:“没有检测到目标”

功能9-12:【项目回顾】基于Yanshee的AI服务型机器人(三)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值