Using a Raspberry Pi and some push buttons I want to control video playback. When someone presses a button the corresponding video plays. The buttons work great. When you press a button the video plays, but when you press a different button or the same button it opens the video without closing the video that was currently playing. I've been searching a while to fix this. I'm pretty new to Python so please keep it as simple as possible. In the code below I'm trying to accomplish it using multithreading. I wasn't able to close the thread though when another thread starts. I am able to close the video after it plays for 10 seconds, but I can't move the quit command anywhere else to close other videos: playSippycup.stdin.write('q')
Here's are the errors I'm currently receiving:
Unhandled exception in thread started by <function shoppingcart at 0xb6c566f0>Playing Sippycup
Unhandled exception in thread started by <function dodgeballs at 0xb6c56670>
Traceback (most recent call last):
File "./labmural2.py", line 53, in dodgeballs
playDodgeballs.stdin.write('q')
NameError: global name 'playDodgeballs' is not defined
Traceback (most recent call last):
File "./labmural2.py", line 71, in shoppingcart
playShoppingcart.stdin.write('q')
NameError: global name 'playShoppingcart' is not defined
Thanks so much for any help you guys can offer!
#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
import time
import thread
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(9, GPIO.IN)
GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
def sippycup( name ):
global playSippycup
while True:
if GPIO.input(11) == True:
print name
time.sleep(1)
playSippycup=subprocess.Popen(['omxplayer','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playSippycup.stdin.write('q')
if GPIO.input(9) == True:
playSippycup.stdin.write('q')
if GPIO.input(10) == True:
playSippycup.stdin.write('q')
def dodgeballs( name ):
global playDodgeballs
while True:
if GPIO.input(9) == True:
print name
time.sleep(1)
playDodgeballs=subprocess.Popen(['omxplayer','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playDodgeballs.stdin.write('q')
if GPIO.input(10) == True:
playDodgeballs.stdin.write('q')
if GPIO.input(11) == True:
playDodgeballs.stdin.write('q')
def dodgeballs( name ):
global playDodgeballs
while True:
if GPIO.input(9) == True:
print name
time.sleep(1)
playDodgeballs=subprocess.Popen(['omxplayer','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playDodgeballs.stdin.write('q')
if GPIO.input(10) == True:
playDodgeballs.stdin.write('q')
if GPIO.input(11) == True:
playDodgeballs.stdin.write('q')
def shoppingcart( name ):
global playShoppingcart
while True:
if GPIO.input(10) == True:
print name
time.sleep(1)
playShoppingcart=subprocess.Popen(['omxplayer','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playShoppingcart.stdin.write('q')
if GPIO.input(9) == True:
playShoppingcart.stdin.write('q')
if GPIO.input(11) == True:
playShoppingcart.stdin.write('q')
thread.start_new_thread( sippycup, ("Playing Sippycup",) )
thread.start_new_thread( dodgeballs, ("Playing Dodgeballs",) )
thread.start_new_thread( shoppingcart, ("Playing Shoppingcart",) )
while True:
pass
GPIO.cleanup()
NEW EDIT:
#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
import subprocess
import time
import thread
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(9, GPIO.IN)
GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
def welcome_loop():
while True:
global playProcess
x = 1
print "LOOPING"
time.sleep(.5)
playProcess=subprocess.Popen(['omxplayer','-b','Desktop/videos/loop/loop.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playProcess.stdin.write('q')
x += 1
def videos():
while True:
if GPIO.input(9):
print "STOP LOOP"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Sippycup"
sippycup_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
sippycup_video.stdin.write('q')
time.sleep(.5)
welcome_loop()
if GPIO.input(10):
print "STOP LOOP"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Dodgeballs"
dodgeballs_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
dodgeballs_video.stdin.write('q')
time.sleep(.5)
welcome_loop()
if GPIO.input(11):
print "STOP LOOP"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Shoppingcart"
shoppingcart_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
shoppingcart_video.stdin.write('q')
time.sleep(.5)
welcome_loop()
thread.start_new_thread( videos, () )
thread.start_new_thread( welcome_loop, () )
while True:
pass
GPIO.cleanup()
ERROR:
Unhandled exception in thread started by
Traceback (most recent call last):
File "./labmural2.py", line 28, in welcome_loop
playProcess.stdin.write('q')
IOError: [Errno 32] Broken pipe
NameError: global name 'playDodgeballs' is not defined means that you are trying to use playDodgeballs before it is defined playDodgeballs = ...
I would simplify your code by removing all globals and threads. subprocess.Popen runs a separate process; it doesn't block your main thread:
names = 'sippycup', 'dodgeballs', 'shoppingcart'
movies = ['Desktop/videos/{name}.mp4'.format(name=name) for name in names]
players = [Player(movie=movie) for movie in movies]
player = players[0]
setup_io() # GPIO setup
while True:
for key in get_key_events(): # get GPIO input
if key == '0':
player = players[0]
elif key == 'space':
player.toggle() # pause/unpause
elif key == 'enter':
player.start()
...
where Player is a simple wrapper around omxplayer subprocess:
import logging
from subprocess import Popen, PIPE, DEVNULL
logger = logging.getLogger(__name__)
class Player:
def __init__(self, movie):
self.movie = movie
self.process = None
def start(self):
self.stop()
self.process = Popen(['omxplayer', self.movie], stdin=PIPE,
stdout=DEVNULL, close_fds=True, bufsize=0)
self.process.stdin.write(start_command) # start playing
def stop(self):
p = self.process
if p is not None:
try:
p.stdin.write(quit_command) # send quit command
p.terminate()
p.wait() # -> move into background thread if necessary
except EnvironmentError as e:
logger.error("can't stop %s: %s", self.movie, e)
else:
self.process = None
def toggle(self):
p = self.process
if p is not None:
try:
p.stdin.write(toggle_command) # pause/unpause
except EnvironmentError as e:
logger.warning("can't toggle %s: %s", self.movie, e)
Specify appropriate start_command, quit_command, toggle_command. You could define different methods depending on what commands omxplayer understands and what commands you need.
Related
I have a program that is supposed to send a few data points over a serial connection to an arduino which will control some motors to move. I can send the control signals individually as well as by txt file which will run repeatedly until the file is complete. While running a txt file, I want to be able to exit the loop like a pause or stop button. I think the best way to do that is via a thread that I can close. I have never done any threading before and my rudimentary attempts have not worked. Here is the function that sends the file data.
def send_file():
# Global vars
global moto1pos
global motor2pos
# Set Ready value
global isready
# Get File location
program_file_name = file_list.get('active')
file_path = "/home/evan/Documents/bar_text_files/"
program_file = Path(file_path + program_file_name)
file = open(program_file)
pos1 = []
pos2 = []
speed1 = []
speed2 = []
accel1 = []
accel2 = []
for each in file:
vals = each.split()
pos1.append(int(vals[0]))
pos2.append(int(vals[1]))
speed1.append(int(vals[2]))
speed2.append(int(vals[3]))
accel1.append(int(vals[4]))
accel2.append(int(vals[5]))
# Send file values
try:
while isready == 1:
for i in range(len(pos1)):
print("Step: " + str(i+1))
data = struct.pack("!llhhhh", pos1[i], pos2[i], speed1[i], speed2[i], accel1[i], accel2[i])
ser.write(data)
try:
pos1time = abs(pos1[i]/speed1[i])
except:
pos1time = 0
try:
pos2time = abs(pos2[i]/speed2[i])
except:
pos2time = 0
time_array = (pos1time, pos2time)
time.sleep(max(time_array))
motor1pos = ser.readline()
motor2pos = ser.readline()
if i < (len(pos1)-1):
isready = ord(ser.read(1))
else:
isready = 0
except:
print("Error: data not sent. Check serial port is open")
Here is the threading command which I want the sendfile command to work from.
def thread():
try:
global isready
isready = 1
t = threading.Thread(name='sending_data', target=command)
t.start()
except:
print("Threading Error: you don't know what you are doing")
And here is the stop function I want the thread to be killed by:
def stop():
try:
global isready
isready = 0
t.kill()
except:
print("Error: thread wasn't killed")
I know you aren't supposed to kill a thread but the data isn't very important. Whats more important is to stop the motors before something breaks.
The button in tkinter is:
run_file_butt = tk.Button(master = file_frame, text = "Run File", command = thread)
When I click the button, the program runs but the stop function does nothing to stop the motion.
Question: run and kill a thread on a button press
There is no such a thing called .kill(....
Start making your def send_file(... a Thread object which is waiting your commands.
Note: As it stands, your inner while isready == 1: will not stop by using m.set_state('stop').
It's mandatory to start the Thread object inside:
if __name__ == '__main__':
m = MotorControl()
import threading, time
class MotorControl(threading.Thread):
def __init__(self):
super().__init__()
self.state = {'is_alive'}
self.start()
def set_state(self, state):
if state == 'stop':
state = 'idle'
self.state.add(state)
def terminate(self):
self.state = {}
# main function in a Thread object
def run(self):
# Here goes your initalisation
# ...
while 'is_alive' in self.state:
if 'start' in self.state:
isready = 1
while isready == 1:
# Here goes your activity
# Simulate activity
print('running')
time.sleep(2)
isready = 0
self.state = self.state - {'start'}
self.state.add('idle')
elif 'idle' in self.state:
print('idle')
time.sleep(1)
if __name__ == '__main__':
m = MotorControl()
time.sleep(2)
m.set_state('start')
time.sleep(3)
m.set_state('stop')
time.sleep(3)
m.set_state('start')
time.sleep(4)
m.terminate()
print('EXIT __main__')
Your tk.Button should look like:
tk.Button(text = "Run File", command = lambda:m.set_state('start'))
tk.Button(text = "Stop File", command = lambda:m.set_state('stop'))
tk.Button(text = "Terminate", command = m.terminate)
The answer I have gone with is simple due to my simple understanding of threading and unique circumstances with which I am using the threading. Instead of terminating the thread in a way I was hoping, I added another conditional statement to the sending line of the send_file function.
while isready == 1:
for i in range(len(pos1)):
if motorstop == False:
print("Step: " + str(i+1))
#data = struct.pack('!llllhhhhhhhh', pos1[i], pos2[i], pos3[i], pos4[i], speed1[i], speed2[i], speed3[i], speed[4], accel1[i], accel2[i], accel3[i], accel4[i])
data = struct.pack("!llhhhh", pos1[i], pos2[i], speed1[i], speed2[i], accel1[i], accel2[i])
ser.write(data)
else:
isready = 0
break
and I have updated my stop() func to the following:
def stop():
try:
global motorstop
global t
motorstop = True
t.join()
except:
print("Error: thread wasn't killed")
I'm not exactly sure how it works but it is much simpler than what was mentioned by #stovefl.
With this code, since the function is mostly just sleeping, it can run but it won't send any new information and then will .join() after the next iteration.
i am building a Phone with my Raspberry Pi and Twinkle.
In my example i am starting Twinkle with a subprocess and checking the output for my code.
Now i need to start a while-loop when someone is calling to check the "end-call button". This works well but i need to end the loop if the far end cancelled the call. How can i break the while loop from another if condition?
Here is my code for example:
#IMPORTS
import sys
import time
import RPi.GPIO as GPIO
from multiprocessing.pool import ThreadPool
import threading
#START TWINKLE
from subprocess import Popen, PIPE
proc = Popen(["twinkle", "-c"], stdin=PIPE, stdout=PIPE, bufsize=1)
for line in iter(proc.stdout.readline, b''):
print line
#BUTTON WATCHER
def button_watch():
input_state = GPIO.input(36)
while (input_state == False):
print "loop"
#END LOOP ON BUTTON PRESS
if (input_state == False):
print('Button Pressed')
input_state = GPIO.input(36)
GPIO.setup(32, GPIO.OUT)
GPIO.output(32, GPIO.LOW)
proc.stdin.write("answer\n")
time.sleep(0.2)
break
#INCOMING CALL
def main():
if (line.find("incoming call") > 0):
print "call is coming"
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.OUT)
GPIO.output(32, GPIO.HIGH)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#START BUTTON WATCHER ON INCOMING CALL
t1 = threading.Thread(target=button_watch)
t1.start()
#CALL CANCELLED
if (line.find("far end cancelled call") > 0):
print "call was cancelled"
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.OUT)
GPIO.output(32, GPIO.LOW)
##############################################
# NEED TO END WHILE LOOP FROM "button_watch" #
##############################################
tmain = threading.Thread(target=main)
tmain.start()
proc.communicate()
I added a comment seeking clarification because I am not sure I understand the intent completely. Now, eventually you will find better and more robust ways to do this but the simplest solution would be to check for a compound condition (assuming you still need input_condition == False).
So you could define a global flag called remote_disconnected, initialize it in a way that your while loop runs in the beginning, and compound it like:
while (input_condition == False and remote_disconnected == False):
make the button_watch depend on a global flag, so you can turn the flag to False
I have some background task that i want to start an be able to safely quit by user input.
To do that i have a thread in which a process pool with a continous task is started. There is a input lock to stop printing and to wait for user input and a event to stop the whole process.
What surprises me is that the processes seem to start and do their work if there is a time.sleep after the start of the thread with processpool (6th line in main).
import multiprocessing as mp
import time
import threading as tr
def init(e, l):
global stop_event
global input_lock
stop_event = e
input_lock = l
def stupid_task(n):
while not stop_event.is_set():
with input_lock:
print(n)
time.sleep(2)
def test_mng(n, event, lock):
with mp.Pool(n, initializer=init, initargs=(event, lock,)) as p:
print("before")
p.map(stupid_task, range(1, n + 1))
print("after")
p.close()
p.join()
def main():
i_lock = mp.Lock()
s_event = mp.Event()
thread = tr.Thread(target=test_mng, args=(3, s_event, i_lock))
init(s_event,i_lock)
thread.start()
time.sleep(1) # if this line is commented out only "before" is printed
while not stop_event.is_set():
input("")
with input_lock:
print("stopped")
eingabe = input("type q to quit")
if eingabe == "q":
stop_event.set()
if __name__ == "__main__":
main()
I ask myself what is stopping the process pool from doing it's work. Do i do something fundamentally wrong? The time.sleep seems a little bit hacky.
I my opinion, you are running your script from an IDE (like PyCharm), but not from the Console. Your IDE is catching the keyboard events.
You can simplify the processing:
The main process can wait for the user input,
The thread can do the "stupid task".
Here is a possible solution:
# coding: utf-8
import multiprocessing as mp
import threading as tr
import time
stop_event = None
def init(event):
global stop_event
stop_event = event
def stupid_task(n):
while not stop_event.is_set():
print(n)
time.sleep(2)
def test_mng(n, event):
with mp.Pool(n, initializer=init, initargs=(event,)) as p:
print("before")
p.map(stupid_task, range(1, n + 1))
print("after")
p.close()
p.join()
def main():
print("type 'q' <ENTER> to quit")
s_event = mp.Event()
init(s_event)
thread = tr.Thread(target=test_mng, args=(3, s_event,))
thread.start()
while not stop_event.is_set():
c = input("")
if c in "qQ":
stop_event.set()
if __name__ == "__main__":
main()
In the if-else statement below, I want the condition GPIO,input(17) has to be different than 0 for at least 5 second until it prints out "COMMUNICATION IS LOST, PLEASE CHECK". Please help me on this issue
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
try:
while True:
if GPIO.input(17):
print "GOOD COMMUNICATION"
else:
print "COMMUNICARION IS LOST, PLEASE CHECK"
sleep (0.1)
finally:
GPIO.cleanup()
try this
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
try:
count = 0
while True:
if GPIO.input(17):
count = 0
print "GOOD COMMUNICATION"
else:
if count == 50:
print "COMMUNICATION IS LOST, PLEASE CHECK"
else:
count += 1
sleep(0.1)
finally:
GPIO.cleanup()
If you mean 17 needs to be 0 for 5 seconds that should work
While overkill for this project a thread can help here quite a bit so it's here for anyone interested.
from threading import Thread, Lock
import time
class ListenThread(Thread):
def __init__(self, lock):
Thread.__init__(self)
self._lock = lock # We lock when we work with our status
self._terminating = False
self._status = "CONNECTION NOT MADE"
def run(self):
self._seconds = 0
while True:
if self._terminating:
break
if self._seconds > 5:
self._lock.acquire()
self._status = "COMMUNICATION IS LOST, PLEASE CHECK"
self._lock.release()
elif GPIO.input(17):
self._seconds = 0
self._lock.acquire()
self._status = "GOOD COMMUNICATION"
self._lock.release()
time.sleep(0.5) #interval
self._seconds += 0.5
def status(self):
return self._status
def end(self):
self._lock.acquire()
self._terminating = True;
self._lock.release()
lock = Lock()
worker = ListenThread(lock)
worker.start()
for i in range(0, 25):
# Do other things! When we want to check on the status
# simply ask. Making sure to lock for safety.
lock.acquire()
print worker.status()
lock.release()
time.sleep(0.3)
worker.end() # Make sure to stop the thread!
This will have the same effect except has the usefulness of a thread so we can keep doing work on our main function. (I've replaced the while loop so that it ends but the same could be done as the OP).
I need a help to stop getCPUtemperature(): function together with robot() function.
def getCPUtemperature():
while True:
res = os.popen('vcgencmd measure_temp').readline()
temp1=int(float(res.replace("temp=","").replace("'C\n","")))
temp2= 9.0/5.0*temp1+32
print temp1,"C", "\n", temp2,"F"
time.sleep(0.5)
if __name__ == '__main__':
try:
#Thread(target = robot).start()
Thread(target = getCPUtemperature).start()
Thread(target = robot).start()
except KeyboardInterrupt:
# CTRL+C exit, turn off the drives and release the GPIO pins
print 'Terminated'
stop_movement()
raw_input("Turn the power off now, press ENTER to continue")
GPIO.cleanup()
quit()strong text
make your child threads daemon and keep the main thread alive to waiting them finish or a ctrl-c pressed.
try the following code:
if __name__ == '__main__':
#Thread(target = robot).start()
t1 = Thread(target = getCPUtemperature)
t1.daemon = True # in daemon mode
t1.start()
t2 = Thread(target = robot)
t2.daemon = True
t2.start()
try:
while threading.active_count() > 1:
time.sleep(1)
except KeyboardInterrupt:
# CTRL+C exit, turn off the drives and release the GPIO pins
print 'Terminated'
stop_movement()
raw_input("Turn the power off now, press ENTER to continue")
GPIO.cleanup()
quit()