Fix exposure time adjusting when setting the same value and add test
Closes #433
This commit is contained in:
parent
9966cb0fbb
commit
4e78f3d104
2 changed files with 47 additions and 9 deletions
|
|
@ -1,3 +1,10 @@
|
||||||
|
"""
|
||||||
|
Check exposure times do not drift.
|
||||||
|
|
||||||
|
This can get very tedious. Recommende running pytest with -s option
|
||||||
|
to monitor progress.
|
||||||
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
@ -11,7 +18,7 @@ from openflexure_microscope_server.things.camera.picamera import StreamingPiCame
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
def test_exposure_time_drift():
|
def _test_exposure_time_drift(desired_time):
|
||||||
"""
|
"""
|
||||||
Capture 10 full resolution images and check that the exposure time remains constant
|
Capture 10 full resolution images and check that the exposure time remains constant
|
||||||
|
|
||||||
|
|
@ -23,16 +30,40 @@ def test_exposure_time_drift():
|
||||||
|
|
||||||
with TestClient(server.app) as test_client:
|
with TestClient(server.app) as test_client:
|
||||||
client = ThingClient.from_url("/camera/", client=test_client)
|
client = ThingClient.from_url("/camera/", client=test_client)
|
||||||
# 5444 is an exposure time supported by PiCamera2
|
exposure_tol = cam.persistent_control_tolerances["ExposureTime"]
|
||||||
client.exposure_time = 5444
|
client.exposure_time = desired_time
|
||||||
|
print(f"Setting desired time of {desired_time}")
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
initial_et = client.exposure_time
|
pre_capture_et = client.exposure_time
|
||||||
print(f"Before capture, et is {client.exposure_time}")
|
print(f"Pre-capture the time is set to {pre_capture_et}")
|
||||||
for i in range(5):
|
# Check exp is set correctly within known tolerance
|
||||||
|
assert abs(pre_capture_et - desired_time) < exposure_tol
|
||||||
|
for i in range(10):
|
||||||
client.capture_jpeg(resolution="full")
|
client.capture_jpeg(resolution="full")
|
||||||
print(f"After capture, et is {client.exposure_time}")
|
if i == 0:
|
||||||
final_et = client.exposure_time
|
# Exposure can update on first capture, due to frame rate restrictions
|
||||||
assert initial_et == final_et
|
first_et = client.exposure_time
|
||||||
|
assert abs(first_et - pre_capture_et) < exposure_tol
|
||||||
|
frame_et = client.exposure_time
|
||||||
|
print(f"Frame {i} captured with exposure time {frame_et}")
|
||||||
|
# Check no further drift in value
|
||||||
|
assert first_et == frame_et
|
||||||
|
|
||||||
|
# Set the exposure time to the value it already is. To check it doesn't shift
|
||||||
|
print(f"Setting exposure time to {frame_et} to check it doesn't change")
|
||||||
|
client.exposure_time = frame_et
|
||||||
|
time.sleep(0.5)
|
||||||
|
# Check before and after capture
|
||||||
|
assert client.exposure_time == frame_et
|
||||||
|
client.capture_jpeg(resolution="full")
|
||||||
|
assert client.exposure_time == frame_et
|
||||||
|
print("Exposure time didn't change!!")
|
||||||
|
print(f"End of test for exposure target {desired_time}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_exposure_time_drift():
|
||||||
|
for desired_time in [100, 1000, 10000]:
|
||||||
|
_test_exposure_time_drift(desired_time)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -201,6 +201,13 @@ class StreamingPiCamera2(BaseCamera):
|
||||||
"ExposureTime", int, description="The exposure time in microseconds"
|
"ExposureTime", int, description="The exposure time in microseconds"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@exposure_time.setter
|
||||||
|
def exposure_time(self, value: int):
|
||||||
|
with self.picamera() as cam:
|
||||||
|
# Note: This set a value 1 higher than requested as picamera2 always sets
|
||||||
|
# a lower value than requested, even if the requested is allowed
|
||||||
|
cam.set_controls({"ExposureTime": value + 1})
|
||||||
|
|
||||||
_sensor_modes = None
|
_sensor_modes = None
|
||||||
|
|
||||||
@thing_property
|
@thing_property
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue