-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
172 lines (147 loc) · 6.08 KB
/
camera.py
File metadata and controls
172 lines (147 loc) · 6.08 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
import datetime
import time
import numpy as np
import pylibapogee.pylibapogee as apg
import pylibapogee.pylibapogee_setup as SetupDevice
class apogee_U2000():
def __init__(self, camera_idx):
self.cam = self._connect(camera_idx=camera_idx)
def capture(self, exposureTime, reshape=True, override=False):
if self.cam == None:
raise RuntimeError("No camera initialised")
if (not override and (exposureTime > self.cam.GetMaxExposureTime() or
exposureTime < self.cam.GetMinExposureTime())):
raise RuntimeError("Exposure time selected violates camera's min/max " +
"recommended exposure times.")
expDatetime = datetime.datetime.now()
self.cam.StartExposure(exposureTime, True)
status = None
while status != apg.Status_ImageReady:
status = self.cam.GetImagingStatus()
if(status == apg.Status_ConnectionError or
status == apg.Status_DataError or
status == apg.Status_PatternError):
msg = "Capture failed. Error in camera status = %d" % (status)
raise RuntimeError(msg)
start = time.time()
data = self.cam.GetImage()
pixel_readout_rate = 1/((time.time() - start)/len(data))
if reshape:
data = np.reshape(data, (self.cam.GetMaxImgCols(), self.cam.GetMaxImgRows()))
attr = {}
attr['CCDTEMP0'] = (float(self.cam.GetTempCcd()), "CCD temperature (deg)")
attr['CCDTEMP1'] = (float(self.cam.GetTempHeatsink()), "heatsink temperature (deg)")
if self.cam.GetCcdAdcResolution() == apg.Resolution_TwelveBit:
attr['ADCBITS'] = (12, "resolution of ADC (bits)")
elif self.cam.GetCcdAdcResolution() == apg.Resolution_SixteenBit:
attr['ADCBITS'] = (16, "resolution of ADC (bits)")
attr['SETPOINT'] = (float(self.cam.GetCoolerSetPoint()), "cooler setpoint (deg)")
attr['COOLPOW'] = (float(self.cam.GetCoolerDrive()), "cooler power (%)")
if self.cam.GetCoolerStatus() == apg.CoolerStatus_Off:
attr['COOLSTAT'] = ("OFF", "cooler status flag")
elif self.cam.GetCoolerStatus() == apg.CoolerStatus_RampingToSetPoint:
attr['COOLSTAT'] = ("RAMPING", "cooler status flag")
elif self.cam.GetCoolerStatus() == apg.CoolerStatus_AtSetPoint:
attr['COOLSTAT'] = ("AT_SETPOINT", "cooler status flag")
elif self.cam.GetCoolerStatus() == apg.CoolerStatus_Revision:
attr['COOLSTAT'] = ("WAITING", "cooler status flag")
if self.cam.GetFanMode() == apg.FanMode_Off:
attr['FANMODE'] = ("OFF", "fan status flag")
elif self.cam.GetFanMode() == apg.FanMode_Low:
attr['FANMODE'] = ("LOW", "fan status flag")
elif self.cam.GetFanMode() == apg.FanMode_Medium:
attr['FANMODE'] = ("MEDIUM", "fan status flag")
elif self.cam.GetFanMode() == apg.FanMode_High:
attr['FANMODE'] = ("HIGH", "fan status flag")
attr['ADGAIN'] = (float(self.cam.GetAdcGain(self.cam.GetCcdAdcSpeed(), 0)),
"ADC gain")
attr['ADOFFSET'] = (float(self.cam.GetAdcOffset(self.cam.GetCcdAdcSpeed(), 0)),
"ADC offset")
attr['DATETIME'] = (str(expDatetime), "frame datetime")
attr['READOUTS'] = (float(int(round(pixel_readout_rate))), "pixel readout rate (Hz)")
attr['EXPTIME'] = (float(exposureTime), "exposure time (s)")
return data, attr
def _connect(self, camera_idx=0):
'''
Connect to a USB camera.
'''
devices = SetupDevice.GetUsbDevices()
if(len(devices) == 0):
raise RuntimeError("No USB devices found")
cam = SetupDevice.CreateAndConnectCam(devices[camera_idx])
return cam
def disconnect(self):
'''
Disconnect the camera from this instance.
'''
if self.cam == None:
raise RuntimeError("No camera initialised")
self.cam.CloseConnection()
def getCoolerStatus(self):
'''
Get the current cooler status.
'''
if self.cam.GetCoolerStatus() == apg.CoolerStatus_Off:
status = "OFF"
elif self.cam.GetCoolerStatus() == apg.CoolerStatus_RampingToSetPoint:
status = "RAMPING"
elif self.cam.GetCoolerStatus() == apg.CoolerStatus_AtSetPoint:
status = "AT_SETPOINT"
elif self.cam.GetCoolerStatus() == apg.CoolerStatus_Revision:
status = "WAITING"
print "CCD temp:\t" + str(self.cam.GetTempCcd())
print "setpoint:\t" + str(self.cam.GetCoolerSetPoint())
print "status:\t\t" + status
def init(self, ROSpeed='normal', ADCGain=0, ADCOffset=0):
'''
Initialise common settings for a camera.
'''
self.setROSpeed(ROSpeed)
self.setADCGain(ADCGain)
self.setADCOffset(ADCOffset)
def setADCGain(self, gain=0):
'''
Set the gain for current ADC. Should be between 0 and 1023
'''
if self.cam == None:
raise RuntimeError("No camera initialised")
if gain < 0 or gain > 1023:
raise RuntimeError("Invalid gain setting")
self.cam.SetAdcGain(gain, self.cam.GetCcdAdcSpeed(), 0)
def setADCOffset(self, offset=0):
'''
Set the offset for current ADC. Should be between 0 and 255
'''
if self.cam == None:
raise RuntimeError("No camera initialised")
if offset < 0 or offset > 255:
raise RuntimeError("Invalid offset setting")
self.cam.SetAdcOffset(offset, self.cam.GetCcdAdcSpeed(), 0)
def setCooler(self, mode):
'''
Set the cooler on or off.
'''
if self.cam == None:
raise RuntimeError("No camera initialised")
self.cam.SetCooler(int(mode))
def setCoolerSetPoint(self, sp):
'''
Set the cooler setpoint.
'''
if self.cam == None:
raise RuntimeError("No camera initialised")
self.cam.SetCoolerSetPoint(float(sp))
def setROSpeed(self, speed='normal'):
'''
Set the readout speed. Can be 'normal' (16bit) or 'fast' (12bit)
'''
if self.cam == None:
raise RuntimeError("No camera initialised")
if speed == 'normal':
self.cam.SetCcdAdcSpeed(apg.AdcSpeed_Normal)
print "Set readout speed to normal"
elif speed == 'fast':
self.cam.SetCcdAdcSpeed(apg.AdcSpeed_Fast)
print "Set readout speed to fast"
else:
raise RuntimeError("Readout speed value not recognised")