-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotbot.py
executable file
·265 lines (221 loc) · 9.25 KB
/
plotbot.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
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
#!/usr/bin/env python3
import argparse
import copy
import traceback
from pb.homing_motor import HomingMotor
import pb.plotbot as PB
current_profile = 'default'
steps_per_mm = {'x': 1 / 0.2, 'y': 1 / 0.2, 'z': 1 / 0.0064}
def parse_args():
"""Defines CLI arguments, parses args, and returns parsed results"""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title='Command', dest="command", required=True)
parser_reset = subparsers.add_parser('reset', help='Move print head to home position')
parser_status = subparsers.add_parser('status', help='Display current status')
# create parser for the goto command
parser_move = subparsers.add_parser('goto', help='Move the print head to a named position')
parser_move.add_argument('axis', choices='xyz', help='Select which axis to move')
parser_move.add_argument('name', help='Name of the saved position')
# create parser for the move command
parser_move = subparsers.add_parser('move', help='Move the print head')
parser_move.add_argument('axis', choices='xyz', help='Select which axis to move')
parser_move.add_argument('count', help='Distance to move the print head')
parser_move.add_argument('units', choices=['steps', 'mm'], help='Units to use for distance')
# create parser for the set command
parser_set = subparsers.add_parser('set', help='Save current position to profile')
parser_set.add_argument('axis', choices='xyz', help='Select which axis position to save')
parser_set.add_argument('name', type=str, help='Name for the saved position')
# create parser for the profile command
parser_profile = subparsers.add_parser('profile', help='Profile actions')
group = parser_profile.add_mutually_exclusive_group()
group.required = True
group.add_argument('-l', '--list', help='List available profiles', action="store_true")
group.add_argument('-s', '--save', help='Save current profile as name', action='store', metavar='name')
group.add_argument('--load', help='Load named profile', metavar='name')
group.add_argument('--delete', help='Delete named profile', metavar='name')
args = parser.parse_args()
return args
def get_profile(config, name: str):
"""returns the a profile named in the config"""
return [p for p in config['profiles'] if p['id'] == name][0]
def handle_move(args):
"""Move the print head a specific distance along a given axis"""
(config, motors) = PB.init()
motor = motors[args.axis]
count = args.count
units = args.units
if count.startswith('+') or count.startswith('-'):
move_relative(motor, count, units)
else:
move_absolute(motor, count, units)
PB.save(config, motors)
def move_relative(motor: HomingMotor, dist_arg: str, unit_arg: str):
if dist_arg[0] == '+':
forward = True
else:
forward = False
try:
num = float(str(dist_arg)[1:])
except ValueError:
raise RuntimeError('Distance must be an numeric value')
if unit_arg == "steps":
steps = int(num)
elif unit_arg == "mm":
steps = num * motor.get_step_size() * steps_per_mm[motor.get_name()]
else:
raise RuntimeError("Units must be 'steps' or 'mm'")
count = 0
for _ in range(int(steps)):
if forward:
count += motor.step_forward()
else:
count += motor.step_backward()
print('{} moved {} pulses to get to current position: {}/{}'.format(motor.get_name(), count, motor.get_pos(),
motor.get_max_steps()))
def move_absolute(motor: HomingMotor, dist_arg: int, unit_arg: str):
"""Move to an absolute position for the given axis"""
if unit_arg == "steps":
try:
pos = float(dist_arg)
except ValueError:
raise RuntimeError('Distance in steps must be an numeric value')
elif unit_arg == "mm":
try:
pos = float(dist_arg) * steps_per_mm[motor.get_name()]
except ValueError:
raise RuntimeError('Distance in mm must be an numeric value')
else:
raise RuntimeError("Units must be 'steps' or 'mm'")
count = motor.goto_pos(pos)
print('{} moved {} pulses to get to current position: {}/{}'
.format(motor.get_name(), count, motor.get_pos(), motor.get_max_steps()))
def handle_reset():
"""Move the print head to home position"""
PB.reset()
def handle_goto(args):
"""Move the print head to a position specifically named in the current profile"""
(config, motors) = PB.init()
axis = args.axis
motor = motors[axis]
name = args.name
if name == 'min':
count = motor.go_home()
print('{} moved {} pulses to get to MIN position: {}/{}'
.format(motor.get_name(), count, motor.get_pos(), motor.get_max_steps()))
elif name == 'mid':
mid = (motor.get_max_steps() / 2)
count = motor.goto_pos(mid)
print('{} moved {} pulses to get to MID position: {}/{}'
.format(motor.get_name(), count, motor.get_pos(), motor.get_max_steps()))
elif name == 'max':
count = motor.goto_pos(motor.get_max_steps())
print('{} moved {} pulses to get to MAX position: {}/{}'
.format(motor.get_name(), count, motor.get_pos(), motor.get_max_steps()))
else:
global current_profile
profiles = config['profiles']
profile = [p for p in profiles if p['id'] == current_profile][0]
points = profile['named-points'][axis]
if name in points.keys():
pos = points[name]
move_absolute(motor, pos, 'steps')
else:
print("Error: named-point '{}' does not exist for {} axis in the active profile"
.format(name, axis))
PB.save(config, motors)
def get_profile(name):
(config, motors) = PB.init()
profiles = config['profiles']
result = [p for p in profiles if p['id'] == name][0]
return result;
def handle_set(args):
"""Add the current position as a named position to the current profile"""
(config, motors) = PB.init()
axis = args.axis
motor = motors[axis]
name = args.name
print(args)
profiles = config['profiles']
profile = [p for p in profiles if p['id'] == current_profile][0]
if 'named-points' not in profile:
profile['named-points'] = {}
named_points = profile['named-points']
if motor.get_name() not in named_points:
named_points[motor.get_name()] = {}
points_for_motor = named_points[motor.get_name()]
points_for_motor[name] = motor.get_pos()
PB.save(config, motors)
print("Set named-point '{}' for {} axis at position {}".format(name, motor.get_name(), motor.get_pos()))
def delete_profile(config, name):
"""Remove the named profile from the given config object"""
if name == 'default':
print('Error: default profile cannot be deleted')
return
profiles = config['profiles']
if name not in (p['id'] for p in profiles):
print('Error: profile "{}" does not exist'.format(name))
else:
config['profiles'] = [p for p in profiles if p['id'] != name]
print('Deleted profile "{}"'.format(name))
def save_profile(config, name):
"""Makes a copy of current_profile with name and adds to the the config"""
global current_profile
profiles = config['profiles']
original = [p for p in profiles if p['id'] == current_profile][0]
exists = [p for p in profiles if p['id'] == name]
if exists:
print("Error: profile '{}' already exists".format(name))
return
clone = copy.deepcopy(original)
clone['id'] = name
profiles.append(clone)
print("Profile '{}' saved as '{}'".format(current_profile, name))
def handle_profile(args):
"""Handle profile CRUD operations"""
(config, motors) = PB.init()
profiles = config['profiles']
if args.list:
for p in profiles:
print(p['id'])
elif args.save:
save_profile(config, args.save)
elif args.load:
if args.load not in (p['id'] for p in profiles):
print('Error: profile "{}" does not exist'.format(args.load))
else:
config['current-profile'] = args.load
print("Active profile set to '{}'".format(args.load))
elif args.delete:
delete_profile(config, args.delete)
PB.save(config, motors)
def handle_status():
"""Display status information"""
config = PB.read_default_config()
print('current-profile: {}'.format(current_profile))
print('position: {}'.format(config['position']))
def main():
try:
args = parse_args()
config = PB.read_default_config()
global current_profile
current_profile = config['current-profile']
if args.command == 'move':
handle_move(args)
elif args.command == 'set':
handle_set(args)
elif args.command == 'profile':
handle_profile(args)
elif args.command == 'reset':
handle_reset()
elif args.command == 'goto':
handle_goto(args)
elif args.command == 'status':
handle_status()
except Exception as ex:
print('Error: {}'.format(ex))
print(traceback.format_exc())
finally:
# cleanup
PB.cleanup()
if __name__ == '__main__':
main()