![]() |
Event Detection clashing - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Event Detection clashing (/thread-26761.html) |
Event Detection clashing - EvanS1 - May-12-2020 I have a small problem. I have two bits of code that both work as expected individually but don't work together. Suggestions as to how to fix it please. So some context, this is running on a raspberry Pi and looking at GPIO inputs I have a microswitch that calls some code that records the time does some other things and waits for the switch to be released updates a TKinter GUI and records that time (typically 0.5 seconds or so) I have an optical sensor that calls another bit of code that records the time its triggered (and will eventually also update the GUI when i get that far) (this is typically happening 20 times a second I've not tested at that speed yet though) Both of these work individually but the second doesn't work if its called while the first code is still running. I considered firing them both into threads but you can only start a thread once. A shortened version of the code is below (didnt think the full code was helpful as theres a lot of tkinter bits in there #initiallise GPIO GPIO.setmode(GPIO.BOARD) # set GPIO numbering system to Board GPIO.setup(Leftswitch,GPIO.IN) # set our sensor pin to an input GPIO.setup(Rightswitch,GPIO.IN) # set our sensor pin to an input GPIO.setup(Leftwheel,GPIO.IN) ##Cadance def get_cadance_trig(side, sensor, c): global Stroke_delta if GPIO.input(sensor) == 1: ##checks GPIO value to make sure its not an end of stroke switch bounce return print (side) global Stroke_start, Air_delta, count_TS, Stroke_stop Stroke_start.append(time.time())##sets stroke start time Air_delta = Stroke_start[-1] - Stroke_stop[-1] print("Start stroke, air time = ", Air_delta) count_TS = count_TS + 1 Stroke_count["text"]=count_TS global delta_C delta_C = Stroke_start[-1] - Stroke_start[-sample_C] global SPS, SPM SPS = ((sample_C - 1) / delta_C) #calculate strokes per second (sample - 1 because we are timing gaps not strokes SPM = SPS * 60 # convert strokes per second to per min if Stroke_start[-3] <= (time.time()-cad_clear):# clears stroke count display if more thn 5 seconds old SPM_count["text"]=cad_clear_dis else: SPM_count["text"]=int(round(SPM)) if Target == 0: SPM_count ["fg"] = "white" elif SPM >= Target - Targetwindow and SPM <=Target +Targetwindow: SPM_count ["fg"] = "dark sea green" else: SPM_count["fg"] = "salmon" while GPIO.input(sensor) == 0: pass global Stroke_delta Stroke_stop.append(time.time()) ##sets stooke stop for use elsewhere Stroke_delta = Stroke_stop[-1] - Stroke_start[-1] print("end stroke, water time = ", Stroke_delta) def get_speed_trig(side, sensor, c): global SpeedL, SpeedR print (side, "wheel") if side == "Left": SpeedL.append(time.time()) print(SpeedL) elif side == "Right": SpeedR.append(time.time()) print(SpeedR[-1]) GPIO.add_event_detect(Leftswitch, GPIO.FALLING, callback=partial(get_cadance_trig, "Left", Leftswitch, ) # execute the get_cadance function when a HIGH signal is detected GPIO.add_event_detect(Rightswitch, GPIO.FALLING, callback=partial(get_cadance_trig, "right", Rightswitch, )) # execute the get_cadance function when a HIGH signal is detected GPIO.add_event_detect(Leftwheel, GPIO.FALLING, callback=partial(get_speed_trig, "Left", Leftwheel, )) # execute the get_cadance function when a HIGH signal is detected win.mainloop() |