forked from StudentCV/PiAutofocusCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLenseController.py
159 lines (124 loc) · 4.49 KB
/
LenseController.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
import sys
import time
import RPi.GPIO as GPIO
class LenseController:
"""
Controls the Stepper Motors
"""
GPIO.setmode(GPIO.BCM)
# ID
irisID = 0
focusID = 1
zoomID = 2
# Dir, Step, Enable
irisPins = [26, 19, 13]
focusPins = [21, 20, 16]
zoomPins = [24, 23, 18]
# maximum positions of each stepper motor (ID)
maxPositions = [112, 6000, 4200]
# motor delay
motorDelay = [0.003, 0.001, 0.001]
def __init__(self):
GPIO.setmode(GPIO.BCM)
def open(self):
# open lense controllers
self.iris = DriverController(
self.irisPins,
self.maxPositions[self.irisID],
self.motorDelay[self.irisID])
self.focus = DriverController(
self.focusPins,
self.maxPositions[self.focusID],
self.motorDelay[self.focusID])
self.zoom = DriverController(
self.zoomPins,
self.maxPositions[self.zoomID],
self.motorDelay[self.zoomID])
print('All lenses are now on minimum setting!')
def close(self):
# close lense controller: disable all motors and close serial port
self.iris.disable()
self.focus.disable()
self.zoom.disable()
GPIO.cleanup()
def disable_drivers(self):
# disable all motors
return self.iris.disable(), self.zoom.disable(), self.focus.disable()
def enable_drivers(self):
# enable all motors
return self.iris.enable(), self.zoom.enable(), self.focus.enable()
def get_lense_info(self):
# get information about all three motors
return self.iris.get_min_position(), self.zoom.get_min_position(),\
self.focus.get_min_position(), self.iris.get_position(),\
self.zoom.get_position(), self.focus.get_position(),\
self.iris.get_max_position(), self.zoom.get_max_position(),\
self.focus.get_max_position()
class DriverController:
"""
Controls the Stepper Motors
"""
# setting motor current position
# motors will be set to this position in __init__
def __init__(self, pins, maxPosition, delay):
self.pins = pins
self.maxPosition = maxPosition
self.delay = delay
self.status = 0
# Set all pins as output
for pin in self.pins:
GPIO.setup(pin, GPIO.OUT)
self.enable()
# motor to min position
self.currPosition = 0
self.go_n_steps(-maxPosition)
def init_motor(self):
return self.go_n_steps(-maxPosition)
def go_n_steps(self, stepCount):
if self.isEnabled() is False:
print('Lense is disbaled!')
return False
else:
self.currPosition = self.currPosition + stepCount
if (self.currPosition < 0):
self.currPosition = 0
if (self.currPosition > self.maxPosition):
self.currPosition = self.maxPosition
if (stepCount > 0):
GPIO.output(self.pins[0], True)
elif (stepCount < 0):
GPIO.output(self.pins[0], False),
else:
raise Exception('Direction Error!')
for i in range(0, abs(stepCount)):
GPIO.output(self.pins[1], True)
time.sleep(self.delay)
GPIO.output(self.pins[1], False)
time.sleep(self.delay)
return self.currPosition
def get_position(self):
return self.currPosition
def get_max_position(self):
return self.maxPosition
def get_min_position(self):
return 0
def go_to_position(self, newPosition):
# move motor to position position
# steps=distance between new position and current position
steps = newPosition-self.currPosition
# move lense steps steps
return self.go_n_steps(steps)
def go_to_min(self):
return self.go_to_position(0)
def go_to_max(self):
return self.go_to_position(self.maxPosition)
def enable(self):
GPIO.output(self.pins[2], False)
self.status = True
return True
def disable(self):
GPIO.output(self.pins[2], True)
self.status = False
return True
def isEnabled(self):
return self.status