2019-10-23 10:56:28 +02:00
|
|
|
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):
|
2019-10-30 12:44:25 +01:00
|
|
|
#TODO: scanningEvent -> CentralManagement.isScanning -> prevent plotter move during scan, failsafe
|
2019-10-23 10:56:28 +02:00
|
|
|
self.config = config
|
|
|
|
self.eventQ = eventQ
|
|
|
|
self.q = Queue()
|
|
|
|
self.isRunning = runningEvent
|
|
|
|
self.logger = logging.getLogger("sorteerhoed").getChild("plotter")
|
2019-10-31 13:55:22 +01:00
|
|
|
self.pen_down = False
|
|
|
|
|
|
|
|
self.plotWidth = self.config['scanner']['width'] / 10 / 2.54
|
|
|
|
self.plotHeight = self.config['scanner']['height'] / 10 / 2.54
|
|
|
|
self.xPadding = self.config['scanner']['left_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")
|
2019-10-23 10:56:28 +02:00
|
|
|
|
2019-10-23 22:33:37 +02:00
|
|
|
def park(self):
|
|
|
|
self.logger.info("Queue to park plotter")
|
|
|
|
|
|
|
|
if self.config['dummy_plotter']:
|
|
|
|
# TODO: find a nice way to park the axidraw in the 0 position
|
2019-10-31 16:47:11 +01:00
|
|
|
self.q.put([(-1/2.54)/self.plotWidth,0,0])
|
2019-10-23 22:33:37 +02:00
|
|
|
|
2019-10-23 10:56:28 +02:00
|
|
|
def start(self):
|
2019-10-31 13:55:22 +01:00
|
|
|
try:
|
|
|
|
if not self.config['dummy_plotter']:
|
|
|
|
self.ad = axidraw.AxiDraw()
|
|
|
|
|
|
|
|
self.ad.interactive()
|
|
|
|
# self.ad.plot_path()
|
|
|
|
|
|
|
|
connected = self.ad.connect()
|
|
|
|
if not connected:
|
|
|
|
raise Exception("Cannot connect to Axidraw")
|
|
|
|
|
2019-10-23 22:33:37 +02:00
|
|
|
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
|
2019-10-31 13:55:22 +01:00
|
|
|
self.ad.options.model = 1 # 2 for A3, 1 for A4
|
2019-10-23 10:56:28 +02:00
|
|
|
|
2019-10-23 22:33:37 +02:00
|
|
|
self.ad.moveto(0,0)
|
2019-10-23 10:56:28 +02:00
|
|
|
|
2019-10-31 13:55:22 +01:00
|
|
|
# plotterWidth = 25
|
|
|
|
# plotterHeight = 21
|
2019-10-23 10:56:28 +02:00
|
|
|
|
2019-10-31 13:55:22 +01:00
|
|
|
else:
|
|
|
|
self.ad = None
|
|
|
|
self.axiDrawCueListener()
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.exception(e)
|
|
|
|
finally:
|
|
|
|
self.logger.warning("Close Axidraw connection")
|
|
|
|
if self.ad:
|
2019-10-23 22:33:37 +02:00
|
|
|
self.ad.moveto(0,0)
|
|
|
|
self.ad.disconnect()
|
2019-10-31 13:55:22 +01:00
|
|
|
|
|
|
|
# send shutdown signal (if not already set)
|
|
|
|
self.isRunning.clear()
|
|
|
|
|
|
|
|
def draw_segments(self, segments = []):
|
|
|
|
coordinates = []
|
|
|
|
for segment in segments:
|
|
|
|
coordinate = [
|
|
|
|
# mm to cm to inches
|
|
|
|
(segment[0]) * self.plotWidth,
|
|
|
|
(1-segment[1]) * self.plotHeight
|
|
|
|
]
|
|
|
|
#prevent drawing when not in drwaing area
|
|
|
|
# this is a failsafe for a malicious working or glitching script, as this should also be done in the javascript
|
|
|
|
if self.pen_down:
|
|
|
|
if coordinate[0] < self.xPadding or coordinate[0] > self.xPadding+self.plotWidth or \
|
|
|
|
coordinate[1] < self.yPadding or coordinate[1] > self.yPadding + self.plotHeight:
|
|
|
|
self.logger.warn(f"Skip drawing for: {coordinates} out of bounds")
|
|
|
|
continue
|
|
|
|
|
|
|
|
coordinates.append(coordinate)
|
2019-10-31 15:49:12 +01:00
|
|
|
self.logger.debug(f"Plot: {coordinates}")
|
2019-10-31 13:55:22 +01:00
|
|
|
if self.ad:
|
|
|
|
self.ad.plan_trajectory(coordinates)
|
|
|
|
# self.ad.moveto(move[0]* plotterWidth, move[1]*plotterHeight)
|
|
|
|
# self.logger.debug(f'handler! {move}')
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setPenDown(self, pen_state):
|
|
|
|
"""
|
|
|
|
False: pen raised, True: pen_lower
|
|
|
|
"""
|
|
|
|
if pen_state != self.pen_down:
|
|
|
|
self.pen_down = pen_state
|
|
|
|
self.logger.info("Changed pen: {}".format('down' if pen_state else 'up'))
|
|
|
|
if self.ad:
|
|
|
|
if pen_state:
|
|
|
|
self.ad.pen_lower()
|
|
|
|
else:
|
|
|
|
self.ad.pen_raise()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def axiDrawCueListener(self):
|
|
|
|
plotterRan = False
|
|
|
|
segments = []
|
|
|
|
while self.isRunning.is_set():
|
|
|
|
# TODO: queue that collects a part of the path data
|
|
|
|
# on a specific limit or on a specific time interval, do the plot
|
|
|
|
# also, changing ad.pen_raise() or ad.pen_lower() trigger a new segment
|
|
|
|
# Plot with ad.plan_trajectory() ??
|
|
|
|
try:
|
|
|
|
# if no info comes in for .5sec, plot that segment
|
|
|
|
segment = self.q.get(True, .5)
|
2019-10-23 10:56:28 +02:00
|
|
|
|
2019-10-31 13:55:22 +01:00
|
|
|
# change of pen state? draw previous segments!
|
|
|
|
if (segment[2] == 1 and not self.pen_down) or (segment[2] == 0 and self.pen_down) or len(segments) > 150:
|
|
|
|
if len(segments) > 1:
|
|
|
|
self.draw_segments(segments)
|
|
|
|
plotterRan = True
|
|
|
|
segments = [] #reset
|
|
|
|
|
|
|
|
# and change pen positions
|
|
|
|
self.setPenDown(segment[2] == 1)
|
|
|
|
|
|
|
|
|
|
|
|
segments.append(segment)
|
|
|
|
|
|
|
|
except queue.Empty as e:
|
|
|
|
self.logger.debug("Timeout queue.")
|
|
|
|
if len(segments):
|
|
|
|
# segments to plot!
|
|
|
|
self.draw_segments(segments)
|
|
|
|
plotterRan = True
|
|
|
|
segments = []
|
|
|
|
elif plotterRan:
|
|
|
|
plotterRan = False
|
|
|
|
self.eventQ.put(Signal('plotter.finished'))
|
|
|
|
# else:
|
|
|
|
# time.sleep(.05)
|
|
|
|
# self.logger.debug(f'Plotter move: {move}')
|
|
|
|
self.logger.info("Stopping plotter")
|
|
|
|
|