Hi everyone,
Objective of the code: After confirming that the data is ready, the code reads the data from the specified channels. The goal is to identify changes in the signal, particularly detecting when there is a transition or edge change in the input.
Setup: The code configures a system to read digital inputs from two channels, DIO5 and DIO6.
Trigger Setup: It sets up a trigger mechanism, likely to start the data acquisition process when a specific condition (trigger) is met on the digital input.
Data Ready Check: It checks if the data is ready to be read. This is crucial to ensure that the system is prepared to provide meaningful information.
def counter(device_data, channel1,channel2, timout,counts,rpm):
"""
Count the edge transitions on 2 channels
parameters: - device data
- selected DIO channel number
- selected DIO channel number
- duration of counter
- return value of counts
- calculated rpm value
"""
sts = ctypes.c_byte()
hzDI = ctypes.c_double()
cSamples = 4096
rgwSamples = (ctypes.c_uint16*cSamples)()
current_time = datetime.now()
# Print the current time
print("Current System Time:", current_time)
print("Waiting for acquisition...")
while True:
dwf.FDwfDigitalInStatus(device_data.handle, ctypes.c_int(1), ctypes.byref(sts))
print(sts.value)
if sts.value != None :
break
time.sleep(1)
print(" done")
# Get the current system time
current_time = datetime.now()
# Print the current time
print("Current System Time:", current_time)
# get samples, byte size
dwf.FDwfDigitalInStatusData(device_data.handle, rgwSamples, 2*cSamples)
dwf.FDwfDeviceCloseAll()
print(rgwSamples)
# Extract dio1 (f1) and dio2 (f2) values
dioa = [(sample >> channel1-1) for sample in rgwSamples]
diob = [(sample >> channel2-1) for sample in rgwSamples]
count = 0
edge_indices_dio1, edge_samples_dio1 = find_edges_with_indices(dioa)
edge_indices_dio2, edge_samples_dio2 = find_edges_with_indices(diob)
for idx, sample in zip(edge_indices_dio1, edge_samples_dio1):
print(f"DIO1 Edge at Sample {idx}, Value: {sample}")
count = count +1
for idx, sample in zip(edge_indices_dio2, edge_samples_dio2):
print(f"DIO2 Edge at Sample {idx}, Value: {sample}")
speeds_dio1 = calculate_speed(edge_indices_dio1, edge_samples_dio1, hzDI.value/100e6)
speeds_dio2 = calculate_speed(edge_indices_dio2, edge_samples_dio2, hzDI.value/100e6)
for i, speed in enumerate(speeds_dio1, start=1):
print(f"DIO1 Speed between Edges {i} and {i+1}: {speed:.2f} units per second")
for i, speed in enumerate(speeds_dio2, start=1):
print(f"DIO2 Speed between Edges {i} and {i+1}: {speed:.2f} units per second")
# time.sleep(100)
def find_edges_with_indices(dio):
edge_indices = [] # List to store the indices of detected edges
edge_samples = [] # List to store the sample values at edge transitions
prev_sample = dio[0] # Initialize with the first sample
for i, sample in enumerate(dio[1:], start=1):
if sample != prev_sample:
edge_indices.append(i)
edge_samples.append(sample)
prev_sample = sample
return edge_indices, edge_samples
def input_trigger_set(device_data,channel1,channel2):
print("input_trigger set")
set_mode(device_data=device_data,channel=channel1,output=True)
# pull_direction = pull()
# set_pull(device_data=device_data,channel=channel1, direction=pull_direction.idle)
set_mode(device_data=device_data,channel=channel2,output=True)
# set_pull(device_data=device_data,channel=channel2, direction=pull_direction.idle)
sts = ctypes.c_byte()
hzDI = ctypes.c_double()
dwf.FDwfDigitalInInternalClockInfo(device_data.handle, ctypes.byref(hzDI))
dwf.FDwfDigitalInDividerSet(device_data.handle, ctypes.c_int(int(hzDI.value/100e6))) # 100MHz
# sample rate = system frequency / divider
hzDI = ctypes.c_double()
dwf.FDwfDigitalInInternalClockInfo(device_data.handle, ctypes.byref(hzDI))
dwf.FDwfDigitalInDividerSet(device_data.handle, ctypes.c_int(int(hzDI.value/100e6))) # 100MHz
# 16bit per sample format
dwf.FDwfDigitalInSampleFormatSet(device_data.handle, ctypes.c_int(16))
# set number of sample to acquire
cSamples = 4096
rgwSamples = (ctypes.c_uint16*cSamples)()
dwf.FDwfDigitalInBufferSizeSet(device_data.handle, ctypes.c_int(cSamples))
# Set trigger
dwf.FDwfDigitalInTriggerSourceSet(device_data.handle,ctypes.c_int(3))
dwf.FDwfDigitalInTriggerPositionSet(device_data.handle, ctypes.c_int(int(cSamples/10-1)))
dwf.FDwfDigitalInTriggerSet(device_data.handle, ctypes.c_int(0), ctypes.c_int(0), ctypes.c_int(1<<channel1), ctypes.c_int(1<<channel1))
# begin acquisition
dwf.FDwfDigitalInConfigure(device_data.handle, ctypes.c_int(1), ctypes.c_int(1))
The data i get out
dioa: [21, 29, 2926, 29, 21, 29, 2926, 29, 21, 29, 2926, 29, 21, 29, ...]
diob [10, 12, 1463, 12, 10, 12, 1463, 12, 10, 12, 1463, 12, 10, 12, ...]
what could be the problem?