/
skratch.py
70 lines (54 loc) · 2.07 KB
/
skratch.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
class Servo:
def __init__(self):
self.y_min = 0
self.y_max = 100
self.steps_per_rev = 400
self.revs_per_coordinate = 2
self.current_pos = 30
self.next_pos = 0
self.planned_steps = None
self.direction = False
def calc_planned_coordinates(self):
return self.next_pos - self.current_pos
def calc_planned_revs(self):
return self.calc_planned_coordinates() * self.revs_per_coordinate
def update_planned_steps(self):
self.planned_steps = self.calc_planned_revs() * self.steps_per_rev
def print_info(self):
print('-------------------INFO----------------------')
print('current position: {}'.format(self.current_pos))
print('moving to: {}'.format(self.next_pos))
print('y_coordinates to go: {}'.format(str(self.calc_planned_coordinates())))
print('revs to go: {}'.format(str(self.calc_planned_revs())))
print('steps to go: {}'.format(str(self.planned_steps)))
dir = None
if self.direction == True:
dir = 'down'
else:
dir = 'up'
print('direction: {}'.format(dir))
print('---------------------------------------------')
def run(self):
self.update_planned_steps()
if self.planned_steps == 0:
print("I'm where I'm supposed to be! :--)")
return
if self.planned_steps < 0:
self.direction = False
self.planned_steps = self.planned_steps - self.steps_per_rev * self.revs_per_coordinate
self.current_pos -= 1
else:
self.direction = True
self.planned_steps = self.planned_steps + self.steps_per_rev * self.revs_per_coordinate
self.current_pos += 1
self.print_info()
def set_pos(self, pos):
print('position set to: {}'.format(pos))
self.next_pos = pos
if __name__ == '__main__':
s = Servo()
while True:
inp = input("next position?")
if inp < 101:
s.set_pos(inp)
s.run()