-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
335 lines (278 loc) · 11.2 KB
/
Copy pathplotter.py
File metadata and controls
335 lines (278 loc) · 11.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import math
from numpy import arange
from plottercontroller import *
SEGMENT_LENGTH = 50 # shorter segments means straighter lines
# TODO implement a get_xy() method
class Plotter(object):
def __init__(self):
self.x = None
self.y = None
self.s = None
self.t = None
self.w = None
self.plotter_controller = PlotterController()
self.save_file = None
self.restore_stw()
# Wiggle all the actuators, to check let the user know the serial connection is working
self.move_by_st(50, 50)
self.pen_down()
time.sleep(0.5)
self.move_by_st(-50, -50)
self.pen_up()
def restore_stw(self):
"""
Restores the last s, t and w values from self.save_file
"""
with open('save.txt', 'r') as self.save_file:
self.s = int(next(self.save_file))
self.t = int(next(self.save_file))
self.w = int(next(self.save_file))
def save_stw(self):
"""
Saves the current s, t and w values to self.save_file
"""
with open('save.txt', 'w') as self.save_file:
print(self.s, file=self.save_file)
print(self.t, file=self.save_file)
print(self.w, file=self.save_file)
def pen_up(self):
"""
Move the pen up
"""
self.plotter_controller.pen_up()
def pen_down(self):
"""
Move the pen down
"""
self.plotter_controller.pen_down()
def move_to_st(self, s, t, verbose=True):
"""
Move to a location given in s, t coordinates
This just calculates the difference between the given and current s, t coords,
then moves by that difference
"""
self.move_by_st(s - self.s, t - self.t, verbose=verbose)
def move_by_st(self, delta_s, delta_t, verbose=True):
"""
Move by an amount given in s, t values
This also updates and saves the self.s and self.t values
"""
self.plotter_controller.move(delta_s, delta_t)
self.s += delta_s
self.t += delta_t
self.save_stw()
if verbose:
print(f"Moving: delta_s={delta_s}, delta_t={delta_t}, (s, t, w) = {self.get_stw_pos()}")
def move_to_xy(self, x, y, verbose=True):
"""
Move by an amount given in s, t values
This also updates the self.s and self.t values
The self.s and self.t values are saved in the self.move_to_st() method,
which is always called
"""
s = int(math.sqrt(x ** 2 + y ** 2))
t = int(math.sqrt((self.w - x) ** 2 + y ** 2))
if verbose:
print(f"Moving: x={x}, y={y}")
self.move_to_st(s, t, verbose=verbose)
self.x = x
self.y = y
def move_straight_to_xy(self, x, y):
if self.x is None:
self.move_to_xy(x, y)
else:
delta_x = x - self.x
delta_y = y - self.y
segments_remaining = int(math.sqrt(delta_x**2 + delta_y**2) // SEGMENT_LENGTH) # number of segments is line length / SEGMENT_LENGTH
while segments_remaining > 0:
delta_x = x - self.x
delta_y = y - self.y
self.move_to_xy(self.x + delta_x//segments_remaining, self.y + delta_y//segments_remaining)
segments_remaining -= 1
# move remaining distance
self.move_to_xy(x, y)
def calibrate(self, s=None, t=None, w=None, verbose=True):
"""
Depreciated in favour of easy_calibrate
"""
if s and t and w:
self.s = s
self.t = t
self.w = w
else:
print("Starting calibration:")
print("Stepper S going to home.")
# Give the stepper a wiggle so you know which one you're working with
self.plotter_controller.move(75, 0)
self.plotter_controller.move(-75, 0)
while True:
amount = input("Move stepper S upwards by [integer]: ")
if amount == "0" or amount == "":
break
self.plotter_controller.move(int(amount), 0)
# Give the stepper a wiggle so you know which one you're working with
# Give the stepper a wiggle so you know which one you're working with
self.plotter_controller.move(0, 75)
self.plotter_controller.move(0, -75)
print("Stepper T going to home.")
while True:
amount = input("Move stepper T upwards by [integer]: ")
if amount == "0" or amount == "":
break
self.plotter_controller.move(0, int(amount))
# Now both steppers are calibrated to be zero
self.s, self.t = 0, 0
while True:
amount = input("Move stepper S towards T by [integer]: ")
if amount == "0" or amount == "":
break
self.move_by_st(int(amount), 0, verbose=verbose)
self.w = self.s
print(f"Calibration complete, s={self.s}, t={self.t}, w={self.w}")
self.save_stw()
def easy_calibrate(self, verbose=True):
"""
Calibrate the s, t and w values by manually moving the module to the
top left, then top right corners just underneath the stepper motors
"""
print("Welcome to easy calibration!© We appreciate your patronage")
print("Move the module underneath the left stepper")
amount = input("s t:")
while amount != "":
# Assume default of zero if only one argument is given
amount += " 0"
s, t = int(amount.split()[0]), int(amount.split()[1])
self.plotter_controller.move(s, t)
amount = input("s t:")
self.s = 0
print("Now move the module underneath the right stepper")
self.control_using_st(verbose=verbose)
self.w = self.s
self.t = 0
self.save_stw()
print(f"Calibration complete! s={self.s}, t={self.t}, w={self.w}")
def get_stw_pos(self):
"""
:return: the current s, t and w values
"""
return self.s, self.t, self.w
def control_using_st(self, verbose=True):
"""
Using s, t coordinates, control the motors until a newline is entered
Enter a '?' to get a readout of the current stw values
Entering just one number will default the second value to 0
"""
amount = input("s t:")
while amount != "":
if amount == "?":
print(f"(s, t, w) = {self.get_stw_pos()}")
amount = input("x y:")
# Assume default of zero if only one argument is given
amount = amount + " 0"
s, t = int(amount.split()[0]), int(amount.split()[1])
self.move_by_st(s, t, verbose=verbose)
amount = input("s t:")
def control_using_xy(self, verbose=True):
"""
Using x, y coordinates, control the motors until a newline is entered
Enter a '?' to get a readout of the current stw values
Entering just one number will default the second value to 0
"""
amount = input("x y:")
while amount != "":
if amount == "?":
print(f"(s, t, w) = {self.get_stw_pos()}")
amount = input("x y:")
# Assume default of zero for the second argument if only one argument is given
amount = amount + " 0"
x, y = int(amount.split()[0]), int(amount.split()[1])
self.move_to_xy(x, y, verbose=verbose)
amount = input("x y:")
def control_from_cmd_line(self, verbose=True):
"""
Allow control of the module from the command line, with either xy or st coordinate
systems. The user can choose either or switch between them at will
"""
mode = input("Choose control mode: [xy/st/draw/func]: ")
while mode in ["xy", "st", "draw", "func"]:
if mode == "st":
self.control_using_st(verbose=verbose)
if mode == "xy":
self.control_using_xy(verbose=verbose)
if mode == "draw":
self.draw_square(4000)
if mode == "func":
self.draw_func()
mode = input("Control the module using st or xy coordinates? [xy/st/draw]: ")
def draw_square(self, side_length: int):
"""
Draw a square
:param side_length:
:return:
"""
# middle = self.w / 2
middle = 3000 + side_length / 2 # Make (3000,3000) be the top corner
half_side = side_length / 2
self.move_to_xy(middle - half_side, middle - half_side) # Top Left
self.pen_down()
self.move_straight_to_xy(middle + half_side, middle - half_side) # Top Right
self.move_straight_to_xy(middle + half_side, middle + half_side) # Bottom Right
self.move_straight_to_xy(middle - half_side, middle + half_side) # Bottom Left
self.move_straight_to_xy(middle - half_side, middle - half_side) # Top Left
self.pen_up()
def draw_axes(self):
# Draw the horizontal axis
self.move_to_xy(0, self.w / 2)
self.pen_down()
self.move_straight_to_xy(self.w, self.w / 2)
self.pen_up()
# Draw the vertical axis
self.move_to_xy(self.w / 2, 0)
self.pen_down()
self.move_straight_to_xy(self.w / 2, self.w)
self.pen_up()
def draw_func(self, func=None):
func = None
functions = [math.exp, math.sin, math.tan]
if not func:
func = functions[1]
in_min = -2*math.pi
in_max = 2*math.pi
x_vals = [x for x in arange(in_min, in_max, math.pi/12)]
y_vals = []
for x in x_vals:
try:
y_vals.append(func(x))
except ValueError:
y_vals.append(0)
out_min = 500
out_max = self.w - out_min
# out_max = 12500
x_mapped = [((x + abs(min(x_vals))) / (max(x_vals) - min(x_vals))) * (out_max - out_min) + out_min for x in x_vals]
draw_height = 4000
out_min = self.w / 2 - draw_height / 2
out_max = self.w / 2 + draw_height / 2
y_mapped = [((y + abs(min(y_vals))) / (max(y_vals) - min(y_vals))) * (out_max - out_min) + out_min for y in y_vals]
print("\n".join(["\t".join([str(int(x)), str(int(y))]) for x, y in zip(x_mapped, y_mapped)]))
count = 0
for x, y in zip(x_mapped, y_mapped):
if count == 1:
self.pen_down()
time.sleep(2)
self.move_straight_to_xy(x, y)
count += 1
if len(x_mapped) % count == 10:
time.sleep(4)
self.pen_up()
time.sleep(1)
if __name__ == '__main__':
plotter = Plotter()
if "y" == input("s, t, w = " + str(plotter.get_stw_pos()) + ", Do you want to calibrate?[y/n]: "):
plotter.easy_calibrate()
# plotter.draw_func()
plotter.control_from_cmd_line()
# for x in range(100, 7100, 100):
# plotter.move_to_xy(x, 7000)
# time.sleep(0.5)
print("s, t, w = " + str(plotter.get_stw_pos()))
print("Plotter Finished")