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,6 +280,7 @@ class CentralManagement():
self.logger.debug(notification_info) self.logger.debug(notification_info)
def cleanDrawing(self): def cleanDrawing(self):
with self.scanLock:
self.eventQueue.put(Signal('scan.start')) self.eventQueue.put(Signal('scan.start'))
# Scan to reset # Scan to reset
cmd = [ cmd = [
@ -286,7 +288,7 @@ class CentralManagement():
] ]
proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
# opens connection to scanner, but only starts scanning when output becomes ready: # opens connection to scanner, but only starts scanning when output becomes ready:
o, e = proc.communicate(80) _, 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,9 +304,7 @@ 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')) self.eventQueue.put(Signal('scan.start'))
cmd = [ cmd = [
'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg', 'sudo', 'scanimage', '-d', 'epkowa', '--format', 'jpeg',

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,12 +21,14 @@ 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']:
# TODO: find a nice way to park the axidraw in the 0 position
self.q.put([(-1/2.54)/self.plotWidth,0,0]) self.q.put([(-1/2.54)/self.plotWidth,0,0])
def start(self): def start(self):
@ -60,6 +62,7 @@ class Plotter:
finally: finally:
self.logger.warning("Close Axidraw connection") self.logger.warning("Close Axidraw connection")
if self.ad: if self.ad:
with self.scannerLock:
self.ad.moveto(0,0) self.ad.moveto(0,0)
self.ad.disconnect() self.ad.disconnect()
@ -67,6 +70,11 @@ class Plotter:
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}')