需要视频的话后台私信
机器人运行要求:
1、机器人在出发线之后等待操作人员发出的出发指令,机器人利用摄像头识别
到相应的指令后出发,进入自动控制流程;
2、在运行过程中,机器人能沿着预设轨道线自动行驶,轨迹线始终在两轮中间,
轮子不得碾压轨迹线,参考姿态见图 1;
3、机器人实时监测轨迹信息,识别到停止线后,待车身全部越过停止线后自动
停止,结束程序。参考姿态见图 2。
4、机器人利用摄像头识别地面轨道和停止线,全程自动运行,禁止任何形式的
人为干预;
提交机器人小车自动循迹行驶完整录像
2、提交机器人小车自动循迹完整源代码
3、提交一份设计分析文档
#部分功能未能实现,但是在此基础上可以进行改善开发
import HiwonderSDK.Board as Board
import time
import threading
import cv2
import numpy as np
print("start")
#小车运行
def Motor_TurnLeft():#左转
Board.setMotor(1,50)
Board.setMotor(2,-80)
def Mortor_TurnRight():#右转
Board.setMotor(1,60)
Board.setMotor(2,-50)
def Motor_Forward():
Board.setMotor(1,75)
Board.setMotor(2,-75)
def Motor_Stop():
Board.setMotor(1, 0)
Board.setMotor(2, 0)
#图像处理
def PathDect(func):
global Path_Dect_px #巡线中心点坐标值
global Path_Dect_fre_count #黑色像素点个数和
while True:
print('中心点坐标值%d'%Path_Dect_px)#打印巡线中心点坐标值
print('num%d'%Path_Dect_fre_count)
if(Path_Dect_fre_count>30):
Motor_Forward()
time.sleep(0.7)
Motor_Stop()
break
if Path_Dect_px<320:#如果巡线中心点偏左,就需要左转来校正
print("turn left")
Motor_TurnLeft()
elif Path_Dect_px>360:#如果巡线中心点偏右,就需要右转来校正
print("turn right")
Mortor_TurnRight()
else :
print("go stright")
Motor_Forward()
time.sleep(0.007)
Motor_Stop()
time.sleep(0.007)
cap=cv2.VideoCapture(0) #摄像头处理
Path_Dect_px_sum=0 #坐标值求和
Path_Dect_px=300
Path_Dect_fre_count =1
threads = []
t1=threading.Thread(target=PathDect,args=(u'监听',))
threads.append(t1)
for t in threads:
t.setDaemon(True)
t.start()
while True:
ret,frame = cap.read() #capture frame_by_frame
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #获取灰度图像
ret,thresh1=cv2.threshold(gray,70,255,cv2.THRESH_BINARY)#对灰度图像进行二值化
Path_Dect_fre_count = 1
Path_Dect_px_sum = 0
for j in range(0,640,5): #采样像素点,5为步进,一共 128 个点
if thresh1[240,j] ==0: #对第240行进行判断
Path_Dect_px_sum =Path_Dect_px_sum+j #黑色像素点坐标值求和
Path_Dect_fre_count= Path_Dect_fre_count+1 #黑色像素点个数求和
Path_Dect_px=(Path_Dect_px_sum)/(Path_Dect_fre_count)#黑色像素中心点为坐标和除以个数
Path_Dect_pxsum =0
cv2.imshow('BINARY',thresh1) #树莓派桌面显示二值化图像,比较占资源默认注释掉调试时可以打开
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()#小车停止
望有用