Bonjour à tous !
Je suis novice en Python et j'essaie avec un raspberry Pi 3 de contrôler des vidéos avec des capteurs de distance.
J'avais besoin de mettre en place une commande clavier pour réinitialiser les GPIO et sortir du programme, alors j'ai importé Tkinter.
Maintenant la touche Echap permet bien de faire tout cela par contre je n'ai plus de contrôle par les capteurs et une petit fenêtre vide s'affiche.
Mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import RPi.GPIO as GPIO
import time, sys
from subprocess import Popen
import subprocess as sp
import tkinter as tk
 
player = False
distance = 400
 
 
# Videos definitions
movie1 = ('/home/pi/Videos/test1.mp4')
movie2 = ('/home/pi/Videos/test2.mp4')
 
# Pin definitions
trigger_pin_left = 8
echo_pin_left = 7
trigger_pin_right = 18
echo_pin_right = 23
 
#Initialise the GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(trigger_pin_left, GPIO.OUT)
GPIO.setup(echo_pin_left, GPIO.IN)
GPIO.setup(trigger_pin_right, GPIO.OUT)
GPIO.setup(echo_pin_right, GPIO.IN)
 
# These three functions encapsulate SR-04 range finder use
# Send a trigger pulse, using the pin supplied as a parameter
# The pulse has a duration of 100 microseconds
def send_trigger_pulse(pin):
    GPIO.output(pin, True)
    time.sleep(0.0001)
    GPIO.output(pin, False)
 
# Wait for the echo pin (supplied as the first parameter) to
# change to high or low as specified in the value parameter
# The timeout parameter prevents the function from hanging, if
# for any reason this doesn't happen
def wait_for_echo(pin, value, timeout):
    count = timeout
    while GPIO.input(pin) != value and count > 0:
        count -= 1
 
 
# Measure the distance in cm, using a rangefinder attached
# to the two pins specified as parameters.
def get_distance(trigger_pin, echo_pin):
    send_trigger_pulse(trigger_pin)
    # Wait for the echo pin to become True indicating that
    # the pulse of ultrasound has finished sending.
    wait_for_echo(echo_pin, True, 10000)
    # make a note of the time
    start = time.time()
    # Wait for the echo to become False indicating that an
    # obstacle has been detected
    wait_for_echo(echo_pin, False, 10000)
    # See how long it took
    finish = time.time()
    pulse_len = finish - start
    # Calculate the dsatnce using the speed of sound
    distance_cm = pulse_len / 0.000058
    return (int(distance_cm))
 
def video_lance():
    # Lancemment des videos en fonction de la distance
        omxc = Popen(['omxplayer', '-b', movie1])
        player = True
 
# Measure the distances from both rangefinders
def mesure_distance():
    left_distance = get_distance(trigger_pin_left, echo_pin_left)
    right_distance = get_distance(trigger_pin_right, echo_pin_right)
    if left_distance < right_distance:
        distance = left_distance
    if right_distance < left_distance:
        distance = right_distance
 
# Fonction pour le contrôle au clavier
def key_in(event):
    ##shows key or tk code for the key
    if event.keysym == 'Escape':
        # set GPIO pins to be inputs
        GPIO.cleanup()
        # Sortir du programme
        sys.exit()
 
root = tk.Tk()
ent=tk.Entry(root)
ent.bind_all('<Key>', key_in)
ent.focus_set()
root.mainloop()
 
while True:
    if player==False:
        mesure_distance()
        if distance < 30:
            video_lance()
            player==True
Merci de votre aide !