-
Notifications
You must be signed in to change notification settings - Fork 8
/
epc901camera.py
99 lines (84 loc) · 3.34 KB
/
epc901camera.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
# Wrapper library for epc901 breakout board
#
# Copyright 2020 by Adrian Studer
#
# Licensed under the terms of MIT License, all text here must be included in any redistribution.
# See the LICENSE-SW file for details.
import serial
import sys
from textwrap import wrap
from time import sleep
class Camera:
ser = serial.Serial()
def open(self, port, speed=115200):
try:
self.ser = serial.Serial(port, speed, timeout=30)
self.sendCommand("echo off")
while self.ser.in_waiting:
self.ser.readline() # flush any extra lines
self.sendCommand("reset camera")
self.sendCommand("reset sensor")
except Exception as e:
print("Failed to connect to sensor on port ", port)
print("Exception: " + str(e))
sys.exit()
def close(self):
self.sendCommand("echo on")
self.ser.close()
def sendCommand(self, command):
cmd_str = "@{}\n".format(command).encode("utf-8")
self.ser.write(cmd_str)
return self.ser.readline().decode("utf-8")
def setExposure(self, exposure):
exposure_str = "exposure {}".format(exposure)
self.sendCommand(exposure_str)
def setBurst(self, frames, interval):
burst_str = "burst frames {}".format(frames)
self.sendCommand(burst_str)
burst_str = "burst interval {}".format(interval)
self.sendCommand(burst_str)
def setTriggerSource(self, source):
self.sendCommand("trigger on")
trig_str = "trigger source {}".format(source)
self.sendCommand(trig_str)
def setTriggerDelay(self, delay):
trig_str = "trigger delay {}".format(delay)
self.sendCommand(trig_str)
def setTriggerDirection(self, direction):
trig_str = "trigger direction {}".format(direction)
self.sendCommand(trig_str)
def setTriggerLevel(self, level):
trig_str = "trigger level {}".format(level)
self.sendCommand(trig_str)
def setTriggerZone(self, x1, y1, x2, y2):
trig_str = "trigger zone {} {} {} {}".format(x1, y1, x2, y2)
self.sendCommand(trig_str)
def capture(self):
self.sendCommand("burst off")
self.sendCommand("capture")
def captureBurst(self, fast=False):
if fast==True:
self.sendCommand("burst fast")
else:
self.sendCommand("burst on")
self.sendCommand("capture")
def getPixels(self, last=False):
transfer_cmd = "transfer"
if last==True:
# get pixels of last frame, discard the others
transfer_cmd = "transfer last"
frame_meta = self.sendCommand(transfer_cmd) # frame number, timestamp, exposure
while frame_meta.startswith("BUSY"):
sleep(0.1) # wait a bit to avoid hammering the camera
frame_meta = self.sendCommand(transfer_cmd) # retry if camera is busy
if frame_meta is None or frame_meta.startswith("ERROR"):
# no pixels available
return
else:
a, b, c, d = frame_meta.split(",")
self.frame_number = int(a)
self.frame_timestamp = int(b)
self.frame_exposure = int(c)
self.frame_pixels = int(d)
frame_data = [int(x, 16) for x in wrap(self.ser.readline().decode("utf-8"), 3)]
return frame_data