WIP locking & parking of plotter/scanner

This commit is contained in:
Ruben van de Ven 2019-11-01 17:18:28 +01:00
parent 9aee12decd
commit fca62dbc83
2 changed files with 47 additions and 33 deletions

View File

@ -36,6 +36,7 @@ class CentralManagement():
self.eventQueue = Queue() self.eventQueue = Queue()
self.isRunning = threading.Event() self.isRunning = threading.Event()
self.isScanning = threading.Event() self.isScanning = threading.Event()
self.scanLock = threading.Lock()
def loadConfig(self, filename): def loadConfig(self, filename):
@ -77,7 +78,7 @@ class CentralManagement():
sqsThread.start() sqsThread.start()
# the plotter itself # the plotter itself
self.plotter = Plotter(self.config, self.eventQueue, self.isRunning) self.plotter = Plotter(self.config, self.eventQueue, self.isRunning, self.scanLock)
plotterThread = threading.Thread(target=self.plotter.start, name='plotter') plotterThread = threading.Thread(target=self.plotter.start, name='plotter')
plotterThread.start() plotterThread.start()
@ -130,9 +131,9 @@ class CentralManagement():
self.server.statusPage.set('state', self.currentHit.getStatus()) self.server.statusPage.set('state', self.currentHit.getStatus())
self.makeHit() self.makeHit()
elif signal.name == 'scan.start': elif signal.name == 'scan.start':
self.isScanning.set() pass
elif signal.name == 'scan.finished': elif signal.name == 'scan.finished':
self.isScanning.clear() pass
elif signal.name == 'hit.info': elif signal.name == 'hit.info':
if signal.params['hit_id'] != self.currentHit.id: if signal.params['hit_id'] != self.currentHit.id:
self.logger.warning(f"hit.info hit_id != currenthit.id: {signal}") self.logger.warning(f"hit.info hit_id != currenthit.id: {signal}")
@ -279,14 +280,15 @@ class CentralManagement():
self.logger.debug(notification_info) self.logger.debug(notification_info)
def cleanDrawing(self): def cleanDrawing(self):
self.eventQueue.put(Signal('scan.start')) with self.scanLock:
# Scan to reset self.eventQueue.put(Signal('scan.start'))
cmd = [ # Scan to reset
'sudo', 'scanimage', '-d', 'epkowa' cmd = [
] 'sudo', 'scanimage', '-d', 'epkowa'
proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) ]
# opens connection to scanner, but only starts scanning when output becomes ready: proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
o, e = proc.communicate(80) # opens connection to scanner, but only starts scanning when output becomes ready:
_, e = proc.communicate(80)
if e: if e:
self.logger.critical(f"Scanner caused: {e.decode()}") self.logger.critical(f"Scanner caused: {e.decode()}")
@ -302,20 +304,18 @@ class CentralManagement():
""" """
Run scanimage on scaner and returns a string with the filename Run scanimage on scaner and returns a string with the filename
""" """
if self.isScanning.is_set(): with self.scanLock:
raise Exception("Already scanning!") self.eventQueue.put(Signal('scan.start'))
cmd = [
self.eventQueue.put(Signal('scan.start')) 'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg',
cmd = [ '--resolution=100', '-l','20','-t','30','-x',str(self.config['scanner']['height']),
'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg', '-y',str(self.config['scanner']['width'])
'--resolution=100', '-l','20','-t','30','-x',str(self.config['scanner']['height']), ]
'-y',str(self.config['scanner']['width']) self.logger.info(f"{cmd}")
] filename = self.currentHit.getImagePath()
self.logger.info(f"{cmd}") proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
filename = self.currentHit.getImagePath() # opens connection to scanner, but only starts scanning when output becomes ready:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) o, e = proc.communicate(80)
# opens connection to scanner, but only starts scanning when output becomes ready:
o, e = proc.communicate(80)
if e: if e:
self.logger.critical(f"Scanner caused: {e.decode()}") self.logger.critical(f"Scanner caused: {e.decode()}")
#TODO: should clear self.isRunning.clear() ? #TODO: should clear self.isRunning.clear() ?

View File

@ -2,12 +2,12 @@ import queue
import logging import logging
from pyaxidraw import axidraw from pyaxidraw import axidraw
from queue import Queue from queue import Queue
from threading import Event from threading import Event, Lock
from sorteerhoed.Signal import Signal from sorteerhoed.Signal import Signal
import time import time
class Plotter: class Plotter:
def __init__(self, config, eventQ: Queue, runningEvent: Event): def __init__(self, config, eventQ: Queue, runningEvent: Event, scannerLock: Lock):
#TODO: scanningEvent -> CentralManagement.isScanning -> prevent plotter move during scan, failsafe #TODO: scanningEvent -> CentralManagement.isScanning -> prevent plotter move during scan, failsafe
self.config = config self.config = config
self.eventQ = eventQ self.eventQ = eventQ
@ -21,13 +21,15 @@ class Plotter:
self.xPadding = self.config['scanner']['left_padding'] / 10 / 2.54; self.xPadding = self.config['scanner']['left_padding'] / 10 / 2.54;
self.yPadding = self.config['scanner']['top_padding'] / 10 / 2.54; self.yPadding = self.config['scanner']['top_padding'] / 10 / 2.54;
self.logger.info(f"Paddings x: {self.xPadding} inch y: {self.yPadding} inch") self.logger.info(f"Paddings x: {self.xPadding} inch y: {self.yPadding} inch")
self.scannerLock = scannerLock
self.goPark = False
self.locked = False
self.ad = None
def park(self): def park(self):
self.logger.info("Queue to park plotter") self.logger.info("Queue to park plotter")
self.goPark = True
if self.config['dummy_plotter']: self.q.put([(-1/2.54)/self.plotWidth,0,0])
# TODO: find a nice way to park the axidraw in the 0 position
self.q.put([(-1/2.54)/self.plotWidth,0,0])
def start(self): def start(self):
try: try:
@ -60,13 +62,19 @@ class Plotter:
finally: finally:
self.logger.warning("Close Axidraw connection") self.logger.warning("Close Axidraw connection")
if self.ad: if self.ad:
self.ad.moveto(0,0) with self.scannerLock:
self.ad.disconnect() self.ad.moveto(0,0)
self.ad.disconnect()
# send shutdown signal (if not already set) # send shutdown signal (if not already set)
self.isRunning.clear() self.isRunning.clear()
def draw_segments(self, segments = []): def draw_segments(self, segments = []):
if not self.locked:
# acquire lock if not already done so
self.scannerLock.acquire()
self.locked = True
coordinates = [] coordinates = []
for segment in segments: for segment in segments:
coordinate = [ coordinate = [
@ -140,6 +148,12 @@ class Plotter:
elif plotterRan: elif plotterRan:
plotterRan = False plotterRan = False
self.eventQ.put(Signal('plotter.finished')) self.eventQ.put(Signal('plotter.finished'))
if self.goPark:
self.eventQ.put(Signal('plotter.parked'))
if self.locked:
self.scannerLock.release()
self.locked = False
self.goPark = False
# else: # else:
# time.sleep(.05) # time.sleep(.05)
# self.logger.debug(f'Plotter move: {move}') # self.logger.debug(f'Plotter move: {move}')