...
 
Commits (2)
...@@ -55,8 +55,8 @@ if __name__=='__main__': ...@@ -55,8 +55,8 @@ if __name__=='__main__':
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule) myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
input = Input() input = Input()
input.rfidReceived.connect(lambda side: myapp.dispatchMessage({'rfid': rfid, 'source': side})) input.rfidReceived.connect(lambda side, rfid: myapp.dispatchMessage({'rfid': rfid, 'source': side}))
input.goalDetected.connect(lambda side: myapp.dispatchMessage({'goal': True, 'source': side})) input.goalDetected.connect(lambda side : myapp.dispatchMessage({'goal': True, 'source': side}))
input.start() input.start()
threadDownloader = Downloader.instance() threadDownloader = Downloader.instance()
......
...@@ -31,17 +31,25 @@ class Input(QObject): ...@@ -31,17 +31,25 @@ class Input(QObject):
} }
_GoalPins = { _GoalPins = {
'pin_trig': 4, 'pin_trig': 3,
'pin_echo': 16 'pin_echo': 2
} }
_keyButtonBindings = { _keyButtonBindings = {
26: 'up', # 26: 'up',
22: 'left', # 22: 'left',
27: 'right', # 27: 'right',
23: 'down', # 23: 'down',
17: 'return', # 17: 'return',
18: 'escape' # 18: 'escape'
16: 'up',
6: 'left',
12: 'right',
13: 'down',
26: 'return',
20: 'del',
19: 'escape'
} }
rfidReceived = pyqtSignal(Side, str) rfidReceived = pyqtSignal(Side, str)
...@@ -49,6 +57,7 @@ class Input(QObject): ...@@ -49,6 +57,7 @@ class Input(QObject):
def __init__(self): def __init__(self):
QObject.__init__(self) QObject.__init__(self)
self.last_input = time.time()
if ON_RASP: if ON_RASP:
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
...@@ -72,14 +81,18 @@ class Input(QObject): ...@@ -72,14 +81,18 @@ class Input(QObject):
self.goalThread.stop(); self.goalThread.join() self.goalThread.stop(); self.goalThread.join()
def _handleButtonPress(self, button_pin): def _handleButtonPress(self, button_pin):
arrival_time = time.time()
if button_pin not in Input._keyButtonBindings.keys(): if button_pin not in Input._keyButtonBindings.keys():
logging.warn('Unknown button pin: {}'.format(button_pin)) logging.warn('Unknown button pin: {}'.format(button_pin))
else: elif arrival_time-self.last_input>0.5:
self.last_input = arrival_time
key = Input._keyButtonBindings[button_pin] key = Input._keyButtonBindings[button_pin]
logging.debug('Sending {} as {}'.format(button_pin, key)) logging.debug('Sending {} as {}'.format(button_pin, key))
pyautogui.press(key) pyautogui.press(key)
class GPIOThread(Thread): class GPIOThread(Thread):
CLEANED = False
def __init__(self): def __init__(self):
Thread.__init__(self) Thread.__init__(self)
self._running = True self._running = True
...@@ -143,12 +156,12 @@ class GoalThread(GPIOThread): ...@@ -143,12 +156,12 @@ class GoalThread(GPIOThread):
self.parent = parent self.parent = parent
self.pin_trig = pin_trig self.pin_trig = pin_trig
self.pin_echo = pin_echo self.pin_echo = pin_echo
self.last_goal = time.time()
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup (self.pin_echo, GPIO.IN) GPIO.setup (self.pin_echo, GPIO.IN)
GPIO.setup (self.pin_trig, GPIO.OUT) GPIO.setup (self.pin_trig, GPIO.OUT)
GPIO.output(self.pin_trig, GPIO.LOW) GPIO.output(self.pin_trig, GPIO.LOW)
print(self.pin_trig, pin_echo)
def run(self): def run(self):
try: try:
...@@ -156,18 +169,29 @@ class GoalThread(GPIOThread): ...@@ -156,18 +169,29 @@ class GoalThread(GPIOThread):
time.sleep(2) time.sleep(2)
while self.running(): while self.running():
# Trigger a scan # Trigger a scan with a 10us pulse
GPIO.output(self.pin_trig, GPIO.HIGH) GPIO.output(self.pin_trig, GPIO.HIGH)
time.sleep(0.00001) time.sleep(0.00001)
GPIO.output(self.pin_trig, GPIO.LOW) GPIO.output(self.pin_trig, GPIO.LOW)
timeout = False
start_read = time.time()
# Read the echo # Read the echo
while self.running() and GPIO.input(self.pin_echo)==0: while self.running() and GPIO.input(self.pin_echo)==0:
pulse_start_time = time.time() pulse_start_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
while self.running() and GPIO.input(self.pin_echo)==1: while self.running() and GPIO.input(self.pin_echo)==1:
pulse_end_time = time.time() pulse_end_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
if self.running(): if self.running() and not timeout:
pulse_duration = pulse_end_time - pulse_start_time pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2) distance = round(pulse_duration * 17150, 2)
self._handle_dist(distance) self._handle_dist(distance)
...@@ -175,6 +199,11 @@ class GoalThread(GPIOThread): ...@@ -175,6 +199,11 @@ class GoalThread(GPIOThread):
self.clean() self.clean()
def _handle_dist(self, dist): def _handle_dist(self, dist):
logging.debug('Distance: {}cm'.format(dist)) #print('Distance: {}cm'.format(dist))
if dist<3: if dist<10:
self.parent.goalDetected.emit(self.parent.side) if (time.time()-self.last_goal)>1:
print('goal')
self.parent.goalDetected.emit(self.parent.side)
self.last_goal = time.time()
...@@ -64,6 +64,7 @@ class Player(QObject): ...@@ -64,6 +64,7 @@ class Player(QObject):
__query_victories = 'SELECT COUNT(*) AS victories FROM Players INNER JOIN Teams ON (Players.id==Teams.player1 OR Players.id==Teams.player2) INNER JOIN Matchs ON (Teams.id==Matchs.winningTeam) WHERE Players.id==?' __query_victories = 'SELECT COUNT(*) AS victories FROM Players INNER JOIN Teams ON (Players.id==Teams.player1 OR Players.id==Teams.player2) INNER JOIN Matchs ON (Teams.id==Matchs.winningTeam) WHERE Players.id==?'
_placeholder_pic_path = ':ui/img/placeholder_head.jpg' _placeholder_pic_path = ':ui/img/placeholder_head.jpg'
_imgLocalPath = os.path.join(IMG_PATH, '{}.jpg')
_utcPictureURL = 'https://demeter.utc.fr/portal/pls/portal30/portal30.get_photo_utilisateur?username={}' _utcPictureURL = 'https://demeter.utc.fr/portal/pls/portal30/portal30.get_photo_utilisateur?username={}'
def __init__(self, id, rfid, login, fname, lname, stats=None): def __init__(self, id, rfid, login, fname, lname, stats=None):
...@@ -74,10 +75,13 @@ class Player(QObject): ...@@ -74,10 +75,13 @@ class Player(QObject):
self.fname = fname self.fname = fname
self.lname = lname self.lname = lname
self.pic_path = Player._placeholder_pic_path if os.path.isfile(Player._imgLocalPath.format(self.id)):
if self.login: self.pic_path = Player._imgLocalPath.format(self.id)
elif self.login:
self.pic_path = Player._utcPictureURL.format(self.login) self.pic_path = Player._utcPictureURL.format(self.login)
else:
self.pic_path = Player._placeholder_pic_path
if stats==None: if stats==None:
self.stats = { 'time_played': 0, 'goals_scored': 0, 'games_played': 0, 'victories': 0 } self.stats = { 'time_played': 0, 'goals_scored': 0, 'games_played': 0, 'victories': 0 }
else: else:
......
...@@ -83,7 +83,7 @@ class Replay(Thread): ...@@ -83,7 +83,7 @@ class Replay(Thread):
def detectCam(): def detectCam():
if ON_RASP: if ON_RASP:
camdet = subprocess.check_output(["vcgencmd","get_camera"]) camdet = subprocess.check_output(["vcgencmd","get_camera"])
return int(chr(camdet[-2])) return bool(chr(camdet[-2]))
else: else:
return False return False
......
...@@ -133,7 +133,7 @@ class GameModule(Module): ...@@ -133,7 +133,7 @@ class GameModule(Module):
self.players = val self.players = val
elif key=='replayThread': elif key=='replayThread':
self.replayer = val self.camera = val
def resizeEvent(self, event): def resizeEvent(self, event):
# 40% of the window width to have (5% margin)-(40% circle)-(10% middle)-(40% circle)-(5% margin) # 40% of the window width to have (5% margin)-(40% circle)-(10% middle)-(40% circle)-(5% margin)
...@@ -177,7 +177,7 @@ class GameModule(Module): ...@@ -177,7 +177,7 @@ class GameModule(Module):
def goal(self, side): def goal(self, side):
if side not in Side: if side not in Side:
logging.error('Wrong goal side: {}'.format(side)) logging.error('Wrong goal side: {}'.format(side))
else: elif not self.video_player:
self.scores[side] += 1 self.scores[side] += 1
# Show replay # Show replay
......
import logging
import time
import RPi.GPIO as GPIO
keyButtonBindings = {
16: 'up',
6: 'left',
12: 'right',
13: 'down',
26: 'return',
20: 'del',
19: 'escape'
}
def fun(pin):
global last_input
arrival_time = time.time()
if pin not in keyButtonBindings.keys():
print('Unknown button pin: {}'.format(pin))
elif arrival_time-last_input>0.5:
key = keyButtonBindings[pin]
print('Sending {} as {}'.format(pin, key))
last_input = arrival_time
if __name__=='__main__':
GPIO.setmode(GPIO.BCM)
for pin in keyButtonBindings.keys():
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.RISING, callback=fun)
try:
last_input = time.time()
while True:
pass
finally:
GPIO.cleanup()
import time
import RPi.GPIO as GPIO
from threading import Thread
class GPIOThread(Thread):
def __init__(self):
Thread.__init__(self)
self._running = True
def running(self):
return self._running
def start(self):
Thread.start(self)
def stop(self):
self._running = False
def clean(self):
GPIO.cleanup()
class GoalThread(GPIOThread):
def __init__(self, parent, pin_trig, pin_echo):
GPIOThread.__init__(self)
self.parent = parent
self.pin_trig = pin_trig
self.pin_echo = pin_echo
self.last_goal = time.time()
GPIO.setmode(GPIO.BCM)
GPIO.setup (self.pin_echo, GPIO.IN)
GPIO.setup (self.pin_trig, GPIO.OUT)
GPIO.output(self.pin_trig, GPIO.LOW)
def run(self):
try:
# Waiting for sensor to settle
time.sleep(2)
while self.running():
# Trigger a scan with a 10us pulse
GPIO.output(self.pin_trig, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.pin_trig, GPIO.LOW)
timeout = False
start_read = time.time()
# Read the echo
while self.running() and GPIO.input(self.pin_echo)==0:
pulse_start_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
while self.running() and GPIO.input(self.pin_echo)==1:
pulse_end_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
if self.running() and not timeout:
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
self._handle_dist(distance)
finally:
self.clean()
def _handle_dist(self, dist):
print('Distance: {}cm'.format(dist))
if dist<10:
if (time.time()-self.last_goal)>1:
print('goal') #self.parent.goalDetected.emit(self.parent.side)
self.last_goal = time.time()
if __name__=='__main__':
try:
_GoalPins = {
'pin_trig': 3,
'pin_echo': 2
}
goalThread = GoalThread(None, **_GoalPins)
goalThread.start()
while True:
pass
finally:
goalThread.stop()
from pirc522 import RFID
import signal
import time
rf_reader = RFID()
try:
print('Starting...')
while True:
print('Waiting for a tag')
rf_reader.wait_for_tag()
(error, tag_type) = rf_reader.request()
if error:
print('Error: {}'.format(error))
else:
(error, id) = rf_reader.anticoll()
if error:
print('Error: {}'.format(error))
else:
(error, tag_type) = rf_reader.request()
print('RFID: {}'.format([hex(x) for x in id]))
except KeyboardInterrupt:
print('Closing...')
rf_reader.cleanup()
\ No newline at end of file