[mw_shl_code=python,true]# -*- coding=utf-8 -*-
__author__ = 'sebnil'
#coding utf-8
'''
项目名称:树莓派3B(摄影水滴控制器的第二版(多线程控制四只电磁阀,带相机唤醒)
基本情况:GUI界面,设置五只电磁阀释放水滴以及间隔时间,控制闪光灯(四只)以及相机快门
软件工具:wxFormBuilder/wing 101/wxpython/gpio的python RPI.GPIO官方库0.6.2
使用wx.MilliSleep(),尝试毫秒级延时
threading--多线程控制,从第一版的单线程扩展,每个电磁阀以及闪灯、快门,一共8个线程,并行调度控制,同一个时间轴
硬件:RASPBERRY PI 3B//电磁阀五只,闪光灯四只,ULN2803 TLN-521-4
V0.23: 实现8个线程、同一时间轴控制四只电磁阀以及闪灯、快门,并且唤醒相机,五只电磁阀,实现两喷+三滴
date:2018-10-13
变量:
dcf1 = 18#对应电磁阀1--GPIO18
dcf2 = 23#对应电磁阀2--GPIO23
dcf3 = 24#对应电磁阀3--GPIO24
dcf4 = 25#对应电磁阀4--GPIO25
dcf5 = 12 #对应电磁阀5--GPIO12
flash1= 17 #闪光灯1,2,3,4---GPIO17
door= 5 #快门---GPIO5
call=13 #唤醒---GPIO13
GPIO.setmode(GPIO.BCM)
#设置OUTPUT状态
GPIO.setup(dcf1, GPIO.OUT)
GPIO.setup(dcf2, GPIO.OUT)
GPIO.setup(dcf3, GPIO.OUT)
GPIO.setup(dcf4, GPIO.OUT)
GPIO.setup(dcf5, GPIO.OUT)
GPIO.setup(flash1, GPIO.OUT)
GPIO.setup(door, GPIO.OUT)
GPIO.setup(call, GPIO.OUT)
'''
import wx
import mthreadgui #mthreadgui.py就是使用wxFormBuilder写的界面描述
import time
import RPi.GPIO as GPIO #树莓派GPIO库
import threading #多线程库
from time import ctime
from datetime import datetime
class MyWin(mthreadgui.MyWin):
def __init__(self, parent, title):
mthreadgui.MyWin.__init__(self, parent)
def on_button_click_event( self, event ):#OK按钮
global flash1,door,call
#初始化GPIO口
dcf1 = 18#对应电磁阀1--GPIO18
dcf2 = 23#对应电磁阀2--GPIO23
dcf3 = 24#对应电磁阀3--GPIO24
dcf4 = 25#对应电磁阀4--GPIO25
dcf5 = 12#对应电磁阀5--GPIO12
flash1= 17 #闪光灯1,2,3, 4---GPIO17
door= 5 #快门---GPIO5
call=13 #唤醒--GPIO13
GPIO.setmode(GPIO.BCM)
#设置OUTPUT状态
GPIO.setup(dcf1, GPIO.OUT)
GPIO.setup(dcf2, GPIO.OUT)
GPIO.setup(dcf3, GPIO.OUT)
GPIO.setup(dcf4, GPIO.OUT)
GPIO.setup(dcf5, GPIO.OUT)
GPIO.setup(flash1, GPIO.OUT)
GPIO.setup(door, GPIO.OUT)
GPIO.setup(call, GPIO.OUT)
#设置多线程,电磁阀1~5,闪灯、快门
threads = []
t1 = threading.Thread(target=fun_drip,args=(self.drip11.GetValue(),self.drip12.GetValue(),self.drip13.GetValue(),self.drip14.GetValue(),dcf1)) #电磁阀1线程
threads.append(t1)
t2 = threading.Thread(target=fun_drip,args=(self.drip21.GetValue(),self.drip22.GetValue(),self.drip23.GetValue(),self.drip24.GetValue(),dcf2)) #电磁阀2线程
threads.append(t2)
t3 = threading.Thread(target=fun_drip,args=(self.drip31.GetValue(),self.drip32.GetValue(),self.drip33.GetValue(),self.drip34.GetValue(),dcf3)) #电磁阀3线程
threads.append(t3)
t4 = threading.Thread(target=fun_drip,args=(self.drip41.GetValue(),self.drip42.GetValue(),self.drip43.GetValue(),self.drip44.GetValue(),dcf4)) #电磁阀4线程
threads.append(t4)
t5 = threading.Thread(target=fun_drip,args=(self.drip51.GetValue(),self.drip52.GetValue(),self.drip53.GetValue(),self.drip54.GetValue(),dcf5)) #电磁阀5线程
threads.append(t5)
t6 = threading.Thread(target=fun_flash,args=(self.t_flash.GetValue(),)) #闪灯线程
threads.append(t6)
t7 = threading.Thread(target=fun_shutter,args=(self.t_shutter.GetValue(),)) #快门线程
threads.append(t7)
t8 = threading.Thread(target=fun_call,args=(self.t_call.GetValue(),)) #唤醒线程
threads.append(t8)
#组装并且激活各线程
for t in threads:
t.setDaemon(True)
t.start()
for t in threads:
t.join()
print "all over %s" %ctime()
GPIO.cleanup() #GPIO设置清除释放,这一条很重要
def fun_drip(delta1,delta2,delta3,delta4,dcf):
start = datetime.now()
#延时delta1
wx.MilliSleep(delta1)
#第一滴水,尺寸delta2
GPIO.output(dcf, GPIO.HIGH)
wx.MilliSleep(delta2)
GPIO.output(dcf, GPIO.LOW)
#延时delta3
wx.MilliSleep(delta3)
#第二滴水,尺寸delta4
GPIO.output(dcf, GPIO.HIGH)
wx.MilliSleep(delta4)
GPIO.output(dcf, GPIO.LOW)
end = datetime.now()
delta = end-start
print "set"
print delta1+delta2+delta3+delta4
print "value"
print delta.microseconds/1000
def fun_flash(delta5):#闪灯引闪函数
#延时delta5毫秒
wx.MilliSleep(delta5)
#闪灯打开
GPIO.output(flash1, GPIO.HIGH)
#GPIO.output(flash2, GPIO.HIGH)
#GPIO.output(flash3, GPIO.HIGH)
#GPIO.output(flash4, GPIO.HIGH)
wx.MilliSleep(100) #保持固定脉冲100ms
GPIO.output(flash1, GPIO.LOW)
#GPIO.output(flash2, GPIO.LOW)
#GPIO.output(flash3, GPIO.LOW)
#GPIO.output(flash4, GPIO.LOW)
print "flash %s" %ctime()
def fun_shutter(delta6):#快门函数
#延时delta6毫秒
wx.MilliSleep(delta6)
#打开快门,我们设置快门时间0.4S,足够完成这一动作
GPIO.output(door, GPIO.HIGH)
wx.MilliSleep(100)#固定延时100ms给快门
GPIO.output(door, GPIO.LOW)
print "shutter %s" %ctime()
def fun_call(delta7):#唤醒函数
#延时delta7毫秒
wx.MilliSleep(delta7)
GPIO.output(call, GPIO.HIGH)
wx.MilliSleep(100)#固定延时100ms给唤醒
GPIO.output(call, GPIO.LOW)
print "call %s" %ctime()
# Create wxpython app
class MyApp(wx.App):
def OnInit(self):
self.frame = MyWin(None, " RPI-3B-Drop Control V0.23")
self.SetTopWindow(self.frame)
self.frame.Show(True)
print("wxApp created.")
return True
if __name__ == "__main__":
app = MyApp(redirect=False) # do not redirect stdout to the gui
app.MainLoop() # render gui continuously[/mw_shl_code]