import RPi.GPIO as GPIO
import time
from Tkinter import *
from PIL import Image,ImageTk
import tkFileDialog as filedialog
import cv2
import threading
PWMA=18
IN1=11
IN2=12
PWMB=16
IN3=13
IN4=15
Trig=35
Echo=37
LSenso=29
RSenso=31
L_Senso=40
R_Senso=36
def init():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(IN1,GPIO.OUT)
GPIO.setup(IN2,GPIO.OUT)
GPIO.setup(IN3,GPIO.OUT)
GPIO.setup(IN4,GPIO.OUT)
GPIO.setup(PWMA,GPIO.OUT)
GPIO.setup(PWMB,GPIO.OUT)
GPIO.setup(Trig,GPIO.OUT)
GPIO.setup(Echo,GPIO.IN)
GPIO.setup(LSenso,GPIO.IN)
GPIO.setup(RSenso,GPIO.IN)
GPIO.setup(L_Senso,GPIO.IN)
GPIO.setup(R_Senso,GPIO.IN)
def turn_up(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(IN1,GPIO.HIGH)
GPIO.output(IN2,GPIO.LOW)
R_Motor.ChangeDutyCycle(speed)
GPIO.output(IN3,GPIO.HIGH)
GPIO.output(IN4,GPIO.LOW)
time.sleep(t_time)
def turn_back(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.HIGH)
R_Motor.ChangeDutyCycle(speed)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.HIGH)
time.sleep(t_time)
def turn_left(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(IN1,False)
GPIO.output(IN2,False)
R_Motor.ChangeDutyCycle(speed)
GPIO.output(IN3,GPIO.HIGH)
GPIO.output(IN4,GPIO.LOW)
time.sleep(t_time)
def turn_right(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(IN1,GPIO.HIGH)
GPIO.output(IN2,GPIO.LOW)
R_Motor.ChangeDutyCycle(speed)
GPIO.output(IN3,False)
GPIO.output(IN4,False)
time.sleep(t_time)
def car_stop():
L_Motor.ChangeDutyCycle(0)
GPIO.output(IN1,False)
GPIO.output(IN2,False)
L_Motor.ChangeDutyCycle(0)
GPIO.output(IN3,False)
GPIO.output(IN4,False)
def get_distance():
GPIO.output(Trig,GPIO.HIGH)
time.sleep(0.00015)
GPIO.output(Trig,GPIO.LOW)
while GPIO.input(Echo)!=GPIO.HIGH:
pass
t1=time.time()
while GPIO.input(Echo)==GPIO.HIGH:
pass
t2=time.time()
distance=(t2-t1)*340*100/2
return distance
def bizhang():
safe_dis=40
while True:
barrier_dis=get_distance()
if (barrier_dis < safe_dis) == True:
while (barrier_dis < safe_dis) == True:
L_S=GPIO.input(L_Senso)
R_S=GPIO.input(R_Senso)
if L_S == False and R_S == True:
print "左有障碍物先后退再右转"
turn_back(18,0.5)
turn_right(18,0.2)
if L_S == True and R_S == False:
print "右有障碍物先后退再左转"
turn_back(18,0.5)
turn_left(18,0.2)
if L_S ==False and R_S == False:
print "两边都有障碍物后退"
turn_back(18,0.5)
L_S=GPIO.input(L_Senso)
R_S=GPIO.input(R_Senso)
if L_S == True and R_S == True:
print "右转"
turn_right(18,0.2)
if L_S == False and R_S == True:
print "右转"
turn_right(18,0.2)
if L_S == True and R_S == False:
print "左转"
turn_left(18,0.2)
if L_S == False and R_S == False:
print "后退"
turn_back(18,0.5)
if L_S == True and R_S == True:
print "两边都没有障碍物,后退再右转"
turn_back(18,0.5)
turn_right(18,0.2)
print barrier_dis,'cm'
print ''
barrier_dis=get_distance()
else:
L_S=GPIO.input(L_Senso)
R_S=GPIO.input(R_Senso)
if L_S == True and R_S == True:
print "前方40cm内没有障碍物,且左前和右前方没有障碍物,前进"
turn_up(18,0)
if L_S == False and R_S == True:
print "前方40cm内没有障碍物,左前有障碍物,右前方没有障碍物,后退右转"
turn_back(18,0.5)
turn_right(18,0.2)
if L_S == True and R_S == False:
print "前方40cm内没有障碍物,右前有障碍物,左前方没有障碍物,后退右转"
turn_back(18,0.5)
turn_left(18,0.2)
if L_S == False and R_S == False:
print "前方40cm内没有障碍物,左前,右前方都有障碍物,后退"
turn_back(18,0.5)
L_S=GPIO.input(L_Senso)
R_S=GPIO.input(R_Senso)
if L_S == True and R_S == True:
print "右转"
turn_right(18,0.2)
if L_S == False and R_S == True:
print "右转"
turn_right(18,0.2)
if L_S == True and R_S == False:
print "左转"
turn_left(18,0.2)
if L_S == False and R_S == False:
print "后退"
turn_back(18,0.5)
print barrier_dis,'cm'
print ''
def track():
while True:
LS=GPIO.input(LSenso)
RS=GPIO.input(RSenso)
if LS==True and RS==True:
print "前进"
turn_up(16,0.1)
elif LS==False and RS==True:
print "右转"
turn_right(18,0.1)
elif LS==True and RS==False:
print "左转"
turn_left(18,0.1)
else:
print "停止"
car_stop()
def gpio_clean():
print "清除GPIO占用"
GPIO.cleanup()
def Thread_bizhang():
thread_bizhang=threading.Thread(target=bizhang)
thread_bizhang.start()
def Thread_track():
thread_track=threading.Thread(target=track)
thread_track.start()
def Thread_gpio_clean():
Thread_gpio_clean=threading.Thread(target=gpio_clean)
Thread_gpio_clean.start()
def ConInterface():
def playLocalVideo():
moviePath=filedialog.askopenfilename()
print(moviePath)
playBtn.place_forget()
movie=cv2.VideoCapture(moviePath)
waitTime= 1000/movie.get(5)
print(waitTime)
movieTime = (movie.get(7) / movie.get(5))/60
print(movieTime)
playSc.config(to=movieTime)
while movie.isOpened():
ret,readyFrame=movie.read()
if ret==True:
movieFrame=cv2.cvtColor(readyFrame,cv2.COLOR_BGR2RGBA)
newImage=Image.fromarray(movieFrame).resize((675,360))
newCover=ImageTk.PhotoImage(image=newImage)
video.configure(image=newCover)
video.image=newCover
video.update()
cv2.waitKey(int(waitTime))
else:
break
def playVideo():
playBtn.place_forget()
movie=cv2.VideoCapture(0)
movie_fps=movie.get(5)
print(movie_fps)
waitTime=int(1000/movie_fps)
print(waitTime)
while movie.isOpened():
ret,readyFrame=movie.read()
if ret==True:
movieFrame=cv2.cvtColor(readyFrame,cv2.COLOR_BGR2RGBA)
newImage=Image.fromarray(movieFrame).resize((675,360))
newCover=ImageTk.PhotoImage(image=newImage)
video.configure(image=newCover)
video.image=newCover
video.update()
cv2.waitKey(20)
else:
break
movie.release()
def Thread_playVideo():
thread_playVideo=threading.Thread(target=playVideo)
thread_playVideo.start()
root=Tk()
root.title("控制界面")
root.geometry("1080x720")
root['bg']="#333333"
root.resizable(width=False,height=False)
videof=Frame(root,height=360,width=675,cursor="cross")
videof.place(x=199,y=0)
f1=Frame(root,height=270,width=540,cursor="circle",bg="#333333")
f1.place(x=269,y=359)
f2=Frame(root,height=180,width=540,cursor="plus",bg="#333333")
f2.place(x=269,y=629)
movieImage=Image.open('/home/pi/ZRcode/raspberry.jpg').resize((675,360))
cover=ImageTk.PhotoImage(image=movieImage)
video=Label(videof,width=675,height=360,bd=0,bg="pink",image=cover)
video.place(x=0,y=0)
iconImag=Image.open('/home/pi/ZRcode/play.ico').resize((32,32))
icoBtn=ImageTk.PhotoImage(image=iconImag)
playBtn=Button(videof,image=icoBtn,cursor="hand2",command=Thread_playVideo,relief="groove")
playBtn.place(x=319,y=159)
up=Button(f1,text="前进",command=lambda:turn_up(20,1),activeforeground="green",activebackground="yellow",height=1,width=4)
up.place(x=267,y=39)
left=Button(f1,text="左转",command=lambda:turn_left(20,1),activeforeground="green",activebackground="yellow",height=1,width=4)
left.place(x=132,y=134)
right=Button(f1,text="右转",command=lambda:turn_right(20,1),activeforeground="green",activebackground="yellow",height=1,width=4)
right.place(x=412,y=134)
back=Button(f1,text="后退",command=lambda:turn_back(20,1),activeforeground="green",activebackground="yellow",height=1,width=4)
back.place(x=267,y=230)
stop=Button(f1,text="停止",command=car_stop,activeforeground="green",activebackground="yellow",height=1,width=4)
stop.place(x=267,y=134)
xunji=Button(f2,text="循迹",command=Thread_track,activeforeground="green",activebackground="yellow",height=1,width=4)
xunji.place(x=68,y=44)
bz=Button(f2,text="避障",command=Thread_bizhang,activeforeground="green",activebackground="yellow",height=1,width=4)
bz.place(x=461,y=44)
over=Button(f2,text="OVER",command=Thread_gpio_clean,activeforeground="green",activebackground="yellow",height=1,width=6)
over.place(x=263,y=44)
root.mainloop()
if __name__=="__main__":
init()
L_Motor=GPIO.PWM(PWMA,100)
L_Motor.start(0)
R_Motor=GPIO.PWM(PWMB,100)
R_Motor.start(0)
try:
ConInterface()
except KeyboardInterrupt:
GPIO.cleanup()