-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathFocuser.py
176 lines (158 loc) · 5.28 KB
/
Focuser.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
'''
Arducam programable zoom-lens control component.
Copyright (c) 2019-4 Arducam <http://www.arducam.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
'''
import sys
import time
class Focuser:
bus = None
CHIP_I2C_ADDR = 0x0C
BUSY_REG_ADDR = 0x04
# starting_point = [
# 17320, 13870, 10470,
# 7820, 5470, 3720,
# 2320, 1220, 370,
# 0, 0, 0, 0,
# 0, 0, 0, 0,
# 0, 0, 0, 0
# ]
# end_point = [
# 20000, 20000, 17370,
# 14470, 12170, 10270,
# 8770, 7670, 6820,
# 5970, 5520, 5220,
# 4970, 4820, 4770,
# 4820, 4870, 5020,
# 5270, 5520, 5770
# ]
starting_point = [
10000, 10000, 10000,
10720, 8070, 5970,
4320, 2920, 1920,
970, 520, 20, 0,
0, 0, 0, 0, 0, 0,
0, 0
]
end_point = [
20000, 20000, 20000,
20000, 19620, 17020,
14920, 13170, 12020,
10970, 10170, 9770,
9170, 9020, 8820,
8570, 8570, 8570,
8770, 8970, 9170
]
def __init__(self, bus):
try:
import smbus # sudo apt-get install python-smbus
self.bus = smbus.SMBus(bus)
except:
sys.exit(0)
def read(self,chip_addr,reg_addr):
value = self.bus.read_word_data(chip_addr,reg_addr)
value = ((value & 0x00FF)<< 8) | ((value & 0xFF00) >> 8)
return value
def write(self,chip_addr,reg_addr,value):
if value < 0:
value = 0
value = ((value & 0x00FF)<< 8) | ((value & 0xFF00) >> 8)
return self.bus.write_word_data(chip_addr,reg_addr,value)
def isBusy(self):
return self.read(self.CHIP_I2C_ADDR,self.BUSY_REG_ADDR) != 0
def waitingForFree(self):
count = 0
begin = time.time()
while self.isBusy() and count < (5 / 0.01):
count += 1
time.sleep(0.01)
# if count >= (5 / 0.01):
# print "wait timeout."
# elif count != 0:
# print "wait time = %lf"%(time.time() - begin)
OPT_BASE = 0x1000
OPT_FOCUS = OPT_BASE | 0x01
OPT_ZOOM = OPT_BASE | 0x02
OPT_MOTOR_X = OPT_BASE | 0x03
OPT_MOTOR_Y = OPT_BASE | 0x04
OPT_IRCUT = OPT_BASE | 0x05
opts = {
OPT_FOCUS : {
"REG_ADDR" : 0x01,
"MIN_VALUE": 0,
"MAX_VALUE": 20000,
"RESET_ADDR": 0x01 + 0x0A,
},
OPT_ZOOM : {
"REG_ADDR" : 0x00,
"MIN_VALUE": 3000,
"MAX_VALUE": 20000,
"RESET_ADDR": 0x00 + 0x0A,
},
OPT_MOTOR_X : {
"REG_ADDR" : 0x05,
"MIN_VALUE": 0,
"MAX_VALUE": 180,
"RESET_ADDR": None,
},
OPT_MOTOR_Y : {
"REG_ADDR" : 0x06,
"MIN_VALUE": 0,
"MAX_VALUE": 180,
"RESET_ADDR": None,
},
OPT_IRCUT : {
"REG_ADDR" : 0x0C,
"MIN_VALUE": 0x00,
"MAX_VALUE": 0x01, #0x0001 open, 0x0000 close
"RESET_ADDR": None,
}
}
def reset(self,opt,flag = 1):
self.waitingForFree()
info = self.opts[opt]
if info == None or info["RESET_ADDR"] == None:
return
self.write(self.CHIP_I2C_ADDR,info["RESET_ADDR"],0x0000)
self.set(opt,info["MIN_VALUE"])
if flag & 0x01 != 0:
self.waitingForFree()
def get(self,opt,flag = 0):
self.waitingForFree()
info = self.opts[opt]
return self.read(self.CHIP_I2C_ADDR,info["REG_ADDR"])
def set(self,opt,value,flag = 1):
self.waitingForFree()
info = self.opts[opt]
if value > info["MAX_VALUE"]:
value = info["MAX_VALUE"]
elif value < info["MIN_VALUE"]:
value = info["MIN_VALUE"]
self.write(self.CHIP_I2C_ADDR,info["REG_ADDR"],value)
if flag & 0x01 != 0:
self.waitingForFree()
pass
def test():
focuser = Focuser(1)
focuser.reset(Focuser.OPT_FOCUS)
while focuser.get(Focuser.OPT_FOCUS) < 18000:
focuser.set(Focuser.OPT_FOCUS,focuser.get(Focuser.OPT_FOCUS) + 50)
focuser.set(Focuser.OPT_FOCUS,0)
focuser.set(Focuser.OPT_FOCUS,10000)
pass
if __name__ == "__main__":
test()