/
cutter.py
150 lines (114 loc) · 3.43 KB
/
cutter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import sys, platform
import serial
from SVG2PLT import SVG2PLT
from PLT import PLT
from Coord import Coord
import json
class Cutter:
# PLT and SVG
plt = None
svg = None
# CONSTANTS
MAX_X = 10000 # maximum distance on the X axis
MAX_Y = 10000 # maximum distance on the Y axis
BAUDRATE = 57600 # baudrate for the serial port comms
# The current location of the head on the x, y axis
current_x = 0
current_y = 0
step_size = 100 # the number of positions to move with user control
passes = 1 # the number of times to cut the shape
scale = 1.0
# Up speed - speed that the machine moves while up
# us <1-40>
up_speed = 40
# Down speed - speed that the machine moves while down
# ds <1-40>
down_speed = 40
# Plunge speed - speed at which the tool is lowered
# ps <1-40>
plunge_speed = 40
# Lift speed - speed at which the tool is raised
# ls <1-40>
lift_speed = 40
# Depth - default tool depth while down
# dp <1-670>
cutting_depth = 300
# Up lift - Lift tool by an extra amount while up (for special cases)
# ul <0-300>
up_lift = 0
# Up/down state - Value is 1 if up, 0 if down
# returned: up=<0/1>;
up = None
# Tool - Value is 0 for left tool or 1 for right tool
# tool <0/1>
# returned: tool=<0/1>;
current_tool = 0
serial = None
# CONSTRUCTOR
def __init__(self):
if(platform.system() == 'Linux'):
self.serial = serial.Serial ("/dev/ttyAMA0", self.BAUDRATE, timeout=1) # open the serial "/dev/ttyAMA0"
self.svg2plt = SVG2PLT()
self.plt = PLT()
self.home()
def __del__(self):
if(platform.system() == 'Linux'):
self.serial.close()
# home the cutter location
def home(self):
self.current_x = 0
self.current_y = 0
self.move(self.current_x, self.current_y)
# change a setting variable
def change_setting(self, setting, value):
setattr(self, setting, float(value))
print(setting+":"+value)
# load a file
def load_file(self, filename='./static/svg/pattern.svg'):
self.svg2plt.load_file(filename)
self.svg2plt.parse()
self.plt = self.svg2plt.plt # TODO: i'm not completely happy with this idea
self.plt.reset_settings()
output = {"width":self.svg2plt.display_width,"height":self.svg2plt.display_height,"units":self.svg2plt.display_units}
return(json.dumps(output))
# send the PLT to the cutter
def cut(self):
self.plt.scale = self.scale
output = self.plt.build()
print(output)
for line in output:
response = self.send(line)
def move_direction(self, direction):
if(direction=='N'):
self.move((self.step_size*-1), 0)
elif(direction=='S'):
self.move(self.step_size, 0)
elif(direction=='E'):
self.move(0, (self.step_size*-1))
elif(direction=='W'):
self.move(0, self.step_size)
def move(self, x, y):
next_x = self.current_x + x
next_y = self.current_y + y
if(next_x<0):
next_x = 0
elif(next_x>self.MAX_X):
next_x = self.MAX_X
if(next_y<0):
next_y = 0
elif(next_y>self.MAX_Y):
next_y = self.MAX_Y
command = Coord('U', next_x, next_y)
response = self.send(str(command))
if(response):
self.current_x = next_x;
self.current_y = next_y;
self.plt.x_offset = self.current_x # TODO: I'm not sure this is the right place to do this.
self.plt.y_offset = self.current_y
# send a string to the serial port and read the response
def send(self, command):
if(platform.system() == 'Linux'):
response = self.serial.write(command.encode('utf-8'))
else:
response = 1
return(response)