LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Timestamp difference

Hi,

So I am working with NI USB 6001 I am trying to acquire data as it is shown in the NI example code it's very simple and self-explanatory. Like this 

import pprint
import datetime

import nidaqmx
from nidaqmx.constants import AcquisitionType
from openpyxl import Workbook

pp = pprint.PrettyPrinter(indent=4)

# Create an Excel workbook and sheet
wb = Workbook()
sheet = wb.active

# Define column headers in Excel sheet
sheet["A1"] = "Timestamp"
sheet["B1"] = "Sample Value"

with nidaqmx.Task() as task:
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")

task.timing.cfg_samp_clk_timing(10000, sample_mode=AcquisitionType.CONTINUOUS)

samples = []

def callback(task_handle, every_n_samples_event_type, number_of_samples, callback_data):
print("Every N Samples callback invoked.")

# Read samples and timestamps
read_data = task.read(number_of_samples_per_channel=10000, timeout=0)

# Get the current timestamp for each sample with milliseconds
timestamps = [datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] for _ in range(len(read_data))]

# Append samples and timestamps
samples.extend(list(zip(timestamps, read_data)))

# Write data to Excel sheet
for i, (timestamp, sample) in enumerate(zip(timestamps, read_data)):
sheet[f"A{i+2+len(samples)-number_of_samples}"] = timestamp
sheet[f"B{i+2+len(samples)-number_of_samples}"] = sample

# Save the workbook
wb.save("data_ai0.xlsx")

return 0

task.register_every_n_samples_acquired_into_buffer_event(10000, callback)

task.start()

input("Running task. Press Enter to stop and see number of accumulated samples.\n")

I am facing a issue that within that particular second the difference in two collected data is 1 millisecond where as the as soon as the second changes the time difference is 922. something. Can someone explain me why is this? what i think is the data is stored in buffer event so after every 1000 samples second is resets the buffer and waits until all the data is collected and then append timestamp to it because of which that delay. I might be wrong any suggestions to improve and get time differnce constant throught the data capturing will be helpful.

import pandas as pd

# Read the Excel file into a DataFrame
df = pd.read_excel('data_ai0.xlsx')

# Convert the 'Timestamp' column to datetime
df['Timestamp'] = pd.to_datetime(df['Timestamp'])

# Get the unique timestamps
unique_timestamps = df['Timestamp'].drop_duplicates()

# Calculate the time difference in milliseconds between unique timestamps
time_diff_ms = unique_timestamps.diff().dt.total_seconds() * 1000

# Create a DataFrame to store the results
result_df = pd.DataFrame({'Timestamp': unique_timestamps, 'TimeDiff_ms': time_diff_ms})

# Save the DataFrame to a CSV file
result_df.to_csv('time_diff_results.csv', index=False)

# Print the unique timestamps and their corresponding time differences
print(result_df)

####result#####

i also tried acquiring single sample and storing the time diff was too much 

              Timestamp  TimeDiff_ms
0   2024-02-19 09:48:47.868          NaN
1   2024-02-19 09:48:47.884         16.0
2   2024-02-19 09:48:47.899         15.0
3   2024-02-19 09:48:47.914         15.0
4   2024-02-19 09:48:47.929         15.0
..                      ...          ...
987 2024-02-19 09:49:03.281         15.0
988 2024-02-19 09:49:03.297         16.0
989 2024-02-19 09:49:03.312         15.0
990 2024-02-19 09:49:03.328         16.0
991 2024-02-19 09:49:03.344         16.0

14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:49.3 1
14:50.3 935
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:50.3 1
14:51.3 932
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
14:51.3 1
0 Kudos
Message 1 of 4
(775 Views)

The problem can either lie in Python code (which I suppose you are using) or in the Excel sheet, but we cannot argue anything if you don't show us useful data: in the first of your image no second change is shown and in the sheet dump we only see the timestamp with tenths of second.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 4
(739 Views)

Hi Thank you for the reply I will attach the code  and the excel file for reference.

import pprint
import datetime

import nidaqmx
from nidaqmx.constants import AcquisitionType
from openpyxl import Workbook

pp = pprint.PrettyPrinter(indent=4)

# Create an Excel workbook and sheet
wb = Workbook()
sheet = wb.active

# Define column headers in Excel sheet
sheet["A1"] = "Timestamp"
sheet["B1"] = "Sample Value"

with nidaqmx.Task() as task:
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")

task.timing.cfg_samp_clk_timing(1000, sample_mode=AcquisitionType.CONTINUOUS)

samples = []

def callback(task_handle, every_n_samples_event_type, number_of_samples, callback_data):
print("Every N Samples callback invoked.")

# Read samples and timestamps
read_data = task.read(number_of_samples_per_channel=1000, timeout=0)

# Get the current timestamp for each sample with milliseconds
timestamps = [datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] for _ in range(len(read_data))]

# Append samples and timestamps
samples.extend(list(zip(timestamps, read_data)))

# Write data to Excel sheet
for i, (timestamp, sample) in enumerate(zip(timestamps, read_data)):
sheet[f"A{i+2+len(samples)-number_of_samples}"] = timestamp
sheet[f"B{i+2+len(samples)-number_of_samples}"] = sample

# Save the workbook
wb.save("data_ai0.xlsx")

return 0

task.register_every_n_samples_acquired_into_buffer_event(1000, callback)

task.start()

input("Running task. Press Enter to stop and see number of accumulated samples.\n")

0 Kudos
Message 3 of 4
(677 Views)

As far as I can see there is an evident problem in how timestamps are calculated and stored in the Excel sheet.

 

This is your sheet modified in order to show timestamps at millisecond level. This reflects in time difference calculation, as you can see in the third column and the graph next to it.

 

Screenshot 2024-02-21 09.27.51.png

 

I am puzzled as to how that datetime.datetime.now() function works, but as it is evident it does not create a linear evaluation of time, which you could calculate by yourself based on time of task start and actual acquisition rate (which you can query from the task and could be different from your settings).

 

By the way, it seems to me that you define a task that acquires 1000 samples and a EveryNSamplesCallback of 1000 samples, which makes the samples to be read when acquisition is completed. Unless it is an artifact just to show us the code, you cold simplify it by just waiting for task completion and post process data after that moment.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 4
(664 Views)