-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnew_dmd_control.py
279 lines (241 loc) · 11.9 KB
/
new_dmd_control.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
# -*- python=3.8.x
"""
Created on 06/07/2023
New version of DMD control.
@author: Alex Kedziora
"""
from __future__ import annotations ## fixes "images: list[]" issue with list[]
import numpy as np
import ajiledriver as aj
from warnings import warn
import cv2
import example_helper
class DMDdriver:
"""Class defined to wrap Ajile DMD controller."""
# Constants
HEIGHT: int = aj.DMD_IMAGE_HEIGHT_MAX
WIDTH: int = aj.DMD_IMAGE_WIDTH_MAX
# Hardware connection
_system = None
_project = None
_sequence = None
_frames = None
# Configuration
dmd_index: int = None
project_name: str = "DMD_control"
main_sequence_ID: int = 1
total_frames: int = 0
# Variables
frame_time: int = 10
def __init__(self):
"""connects to DMD"""
# Set up interface
comm_interface = aj.USB3_INTERFACE_TYPE
# Set device number
device_number = 0
# Create system
self._system = aj.HostSystem()
## Connection Settings
ipAddress = "192.168.200.1"
netmask = "255.255.255.0"
gateway = "0.0.0.0"
port = 5005
self._system.SetConnectionSettingsStr(ipAddress, netmask, gateway, port)
# Set interface
self._system.SetCommunicationInterface(comm_interface)
# Set USB number
self._system.SetUSB3DeviceNumber(device_number)
# Check
if self._system.StartSystem() != aj.ERROR_NONE:
raise IOError("Error starting AjileSystem. "
"Did you specify the correct interface with the command line arguments, e.g. '--usb3'?")
def create_project(self) -> None:
"""create a project object and set it up"""
if self._project is not None:
warn("Project already exists. Using existing project.", UserWarning)
return
self._project = aj.Project(self.project_name)
# add components to project
self._project.SetComponents(self._system.GetProject().Components())
# Get DMD index
self.dmd_index = self._project.GetComponentIndexWithDeviceType(aj.DMD_4500_DEVICE_TYPE)
def create_main_sequence(self, seqRepCount : int) -> None:
"""
seqRepCount - repetitions of the main sequence
"""
"""Creates a sequence, sequence item and frame object"""
if self._project is None:
raise SystemError("Project must be created before sequence is created")
if self.total_frames == 0:
warn("No frames defined. Sequence should be created after frames are uploaded to the device.", UserWarning)
# Create the sequence
"""
taken from C# driver to understand the meaning of each arg
Sequence(ushort sequenceID, string sequenceName, DeviceType_e hardwareType, SequenceType_e sequenceType, uint sequenceRepeatCount)
SequenceType_e:
SEQ_TYPE_PRELOAD = 0,
SEQ_TYPE_STREAM = 1
"""
seq = aj.Sequence(
self.main_sequence_ID,
self.project_name + str(self.main_sequence_ID),
aj.DMD_4500_DEVICE_TYPE,
aj.SEQ_TYPE_PRELOAD,
seqRepCount
)
# Add the sequence to the project
self._project.AddSequence(seq)
# Check sequence
_, sequence_was_found = self._project.FindSequence(self.main_sequence_ID)
if not sequence_was_found:
raise IOError('Sequence not found on device')
def add_sub_sequence(self, npImage : np.array, seqID : int, frameTime : int = 1000):
"""
npImage - np.array image
seqID - ID of the sequence, starts with 1 and is incremented by 1
frameTime - frame time in MILIseconds
"""
"Add sequence to the main sequence"
# public SequenceItem(ushort sequenceID, uint sequenceItemRepeatCount)
seqItem = aj.SequenceItem(seqID, 1)
self._project.AddSequenceItem(seqItem)
# create two frames and add them to the project
# (added to the last sequence item in the sequence)
""" I believe each Image has to have unique ID
- maybe if we have N images, we can load them and create a pattern from these let,s say (n1,n2,n3,n1,n2,n3,n4,n5...)
without loading n1, n2... multiple times"""
myImage = aj.Image(seqID)
# load the NumPy image into the Image object and convert it to DMD 4500 format
myImage.ReadFromMemory(npImage, 8, aj.ROW_MAJOR_ORDER, aj.DMD_4500_DEVICE_TYPE)
self._project.AddImage(myImage)
# Define frame related to an image
frame = aj.Frame(1)
frame.SetImageID(seqID)
frame.SetFrameTimeMSec(int(frameTime)) # Miliseconds
self._project.AddFrame(frame)
def add_sub_sequence_list(self, npImages : list[np.array], frameTime : int = 1000):
"""
npImage - np.array image
seqID - ID of the sequence, starts with 1 and is incremented by 1
frameTime - frame time in MILIseconds
"""
"Add sequence to the main sequence"
# public SequenceItem(ushort sequenceID, uint sequenceItemRepeatCount)
seqID = 1
for i in range(len(npImages)):
seqID = i+1
seqItem = aj.SequenceItem(seqID, 1)
self._project.AddSequenceItem(seqItem)
# create two frames and add them to the project
# (added to the last sequence item in the sequence)
""" I believe each Image has to have unique ID
- maybe if we have N images, we can load them and create a pattern from these let,s say (n1,n2,n3,n1,n2,n3,n4,n5...)
without loading n1, n2... multiple times"""
myImage = aj.Image(seqID)
# load the NumPy image into the Image object and convert it to DMD 4500 format
myImage.ReadFromMemory(npImages[i], 8, aj.ROW_MAJOR_ORDER, aj.DMD_4500_DEVICE_TYPE)
self._project.AddImage(myImage)
# Define frame related to an image
frame = aj.Frame(1)
frame.SetImageID(seqID)
frame.SetFrameTimeMSec(int(frameTime)) # Miliseconds
self._project.AddFrame(frame)
def create_trigger_rules(self, controller_index: int) -> None:
"""Create a trigger rule to connect the DMD frame started to the external output trigger"""
if self._project is None:
raise IOError('Project must be defined before trigger is created')
rule = aj.TriggerRule()
# Add trigger from device
# TriggerRulePair(byte componentIndex, byte triggerType)
rule.AddTriggerFromDevice(aj.TriggerRulePair(self.dmd_index, aj.FRAME_STARTED))
# Set trigger
rule.SetTriggerToDevice(aj.TriggerRulePair(controller_index, aj.EXT_TRIGGER_OUTPUT_1))
# add the trigger rule to the project
self._project.AddTriggerRule(rule)
def my_trigger(self, controllerIndex: int=0):
dmdIndex = self._project.GetComponentIndexWithDeviceType(aj.DMD_4500_DEVICE_TYPE)
inputTriggerSettings = self._project.Components()[controllerIndex].InputTriggerSettings()
outputTriggerSettings = self._project.Components()[controllerIndex].OutputTriggerSettings()
for index in range(len(outputTriggerSettings)):
outputTriggerSettings[index] = aj.ExternalTriggerSetting(aj.RISING_EDGE, aj.FromMSec(1/32768)) # was 1/16 for Arduino
inputTriggerSettings[index] = aj.ExternalTriggerSetting(aj.RISING_EDGE)
self._project.SetTriggerSettings(controllerIndex, inputTriggerSettings, outputTriggerSettings)
dmdFrameStartedToExtTrigOut = aj.TriggerRule()
dmdFrameStartedToExtTrigOut.AddTriggerFromDevice(aj.TriggerRulePair(dmdIndex, aj.FRAME_STARTED))
dmdFrameStartedToExtTrigOut.SetTriggerToDevice(aj.TriggerRulePair(controllerIndex, aj.EXT_TRIGGER_OUTPUT_1))
# add the trigger rule to the project
self._project.AddTriggerRule(dmdFrameStartedToExtTrigOut)
# This part doesn't work quite right (probably wiring issue)
extTrigInToDMDStartFrame = aj.TriggerRule()
extTrigInToDMDStartFrame.AddTriggerFromDevice(aj.TriggerRulePair(controllerIndex, aj.EXT_TRIGGER_INPUT_1))
extTrigInToDMDStartFrame.SetTriggerToDevice(aj.TriggerRulePair(dmdIndex, aj.START_FRAME))
# add the trigger rule to the project
self._project.AddTriggerRule(extTrigInToDMDStartFrame)
def multiple_patterns_sequence(self, npImages : list[np.array], offImage : np.array, frameTime : int = 1) -> None:
# Image ID 1 - off image
myOffImage = aj.Image(1)
myOffImage.ReadFromMemory(offImage, 8, aj.ROW_MAJOR_ORDER, aj.DMD_4500_DEVICE_TYPE)
self._project.AddImage(myOffImage)
# Create single sequence - seq ID 1
seqItem = aj.SequenceItem(1, 1)
self._project.AddSequenceItem(seqItem)
# ID range : 2 -> len(images) + 1
for i in range(len(npImages)):
# Start with 2
seqID = i+2
# create two frames and add them to the project
# (added to the last sequence item in the sequence)
""" I believe each Image has to have unique ID
- maybe if we have N images, we can load them and create a pattern from these let,s say (n1,n2,n3,n1,n2,n3,n4,n5...)
without loading n1, n2... multiple times"""
myImage = aj.Image(seqID)
# load the NumPy image into the Image object and convert it to DMD 4500 format
myImage.ReadFromMemory(npImages[i], 8, aj.ROW_MAJOR_ORDER, aj.DMD_4500_DEVICE_TYPE)
self._project.AddImage(myImage)
for i in range(len(npImages)):
# Define frame related to an image
frame = aj.Frame()
frame.SetSequenceID(1)
# Off image
frame.SetImageID(1)
frame.SetFrameTimeMSec(frameTime)
self._project.AddFrame(frame)
frame = aj.Frame()
frame.SetSequenceID(1)
frame.SetImageID(i+2)
frame.SetFrameTimeMSec(frameTime)
self._project.AddFrame(frame)
def stop_projecting(self) -> None:
"""Stop projecting"""
self._system.GetDriver().StopSequence(self.dmd_index)
def start_projecting(self, reportingFreq : int = 1) -> None:
"""
Load project, and start sequence
reportingFreq - reporting frequency (must be greater than 0)
"""
self._system.GetDriver().LoadProject(self._project)
self._system.GetDriver().WaitForLoadComplete(-1)
# Start the current sequence
# StartSequence(uint sequenceID, int deviceID, uint reportingFreq=1)
self._system.GetDriver().StartSequence(self.main_sequence_ID, self.dmd_index, reportingFreq)
# Wait to start running
while self._system.GetDeviceState(self.dmd_index).RunState() != aj.RUN_STATE_RUNNING:
pass
def ReturnProject(self, sequenceID=1, sequenceRepeatCount=0, frameTime_ms=-1, components=None):
return self._project
def run_example(self) -> None:
# The project must be already set up
# stop any existing project from running on the device
self._system.GetDriver().StopSequence(self.dmd_index)
# load the project to the device
self._system.GetDriver().LoadProject(self._project)
self._system.GetDriver().WaitForLoadComplete(-1)
for sequenceID, sequence in self._project.Sequences().iteritems():
# if using region-of-interest, switch to 'lite mode' to disable lighting/triggers and allow DMD to run faster
roiWidthColumns = sequence.SequenceItems()[0].Frames()[0].RoiWidthColumns()
if roiWidthColumns > 0 and roiWidthColumns < aj.DMD_IMAGE_WIDTH_MAX:
self._system.GetDriver().SetLiteMode(True, self.dmd_index)
self._system.GetDriver().StartSequence(sequence.ID(), self.dmd_index)
# wait for the sequence to start
while self._system.GetDeviceState(self.dmd_index).RunState() != aj.RUN_STATE_RUNNING: pass