guest_worker/sorteerhoed/plotter.py
2019-10-30 12:44:25 +01:00

89 lines
No EOL
3.3 KiB
Python

import queue
import logging
from pyaxidraw import axidraw
from queue import Queue
from threading import Event
from sorteerhoed.Signal import Signal
import time
class Plotter:
def __init__(self, config, eventQ: Queue, runningEvent: Event):
#TODO: scanningEvent -> CentralManagement.isScanning -> prevent plotter move during scan, failsafe
self.config = config
self.eventQ = eventQ
self.q = Queue()
self.isRunning = runningEvent
self.logger = logging.getLogger("sorteerhoed").getChild("plotter")
def park(self):
self.logger.info("Queue to park plotter")
if self.config['dummy_plotter']:
# fastest way home :-)
self.q.put([0,0,0])
else:
# TODO: find a nice way to park the axidraw in the 0 position
self.q.put([0,0,0])
def start(self):
self.axiDrawCueListener()
def axiDrawCueListener(self):
if self.config['dummy_plotter']:
while self.isRunning.is_set():
plotterRan = False
try:
move = self.q.get(True, 1)
plotterRan = True
except queue.Empty as e:
self.logger.log(5, "Empty queue.")
if plotterRan:
plotterRan = False
self.eventQ.put(Signal('plotter.finished'))
else:
time.sleep(.05)
self.logger.debug(f'Dummy plotter move: {move}')
self.logger.info("Stopping dummy plotter")
else:
self.ad = axidraw.AxiDraw()
self.ad.interactive()
connected = self.ad.connect()
if not connected:
raise Exception("Cannot connect to Axidraw")
try:
self.ad.options.units = 1 # set to use centimeters instead of inches
self.ad.options.accel = 100;
self.ad.options.speed_penup = 100
self.ad.options.speed_pendown = 100
self.ad.options.model = 1 # A3, set to 1 for A4
self.ad.moveto(0,0)
plotterWidth = 22
plotterHeight = 18 # 16?
plotterRan = False
while self.isRunning.is_set():
# TODO: set timeout on .get() with catch block, so we can escape if no moves come in
try:
move = self.q.get(True, 1)
plotterRan = True
except queue.Empty as e:
self.logger.log(5, "Empty queue.")
if plotterRan:
plotterRan = False
self.eventQ.put(Signal('plotter.finished'))
else:
self.ad.moveto(move[0]* plotterWidth, move[1]*plotterHeight)
self.logger.debug(f'handler! {move}')
except Exception as e:
self.logger.exception(e)
finally:
self.logger.warning("Close Axidraw connection")
self.ad.moveto(0,0)
self.ad.disconnect()
# send shutdown signal (if not already set)
self.isRunning.clear()