-
Notifications
You must be signed in to change notification settings - Fork 188
New Example for Time Based Synchronization #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||||||||||||||||||||||||||||||
| import nidaqmx | ||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||
| from nidaqmx.constants import AcquisitionType | ||||||||||||||||||||||||||||||||||
| from datetime import timedelta, datetime, timezone | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Function to perform the measurement | ||||||||||||||||||||||||||||||||||
| def perform_measurement(): | ||||||||||||||||||||||||||||||||||
| with nidaqmx.Task() as task: | ||||||||||||||||||||||||||||||||||
| task.ai_channels.add_ai_voltage_chan("Dev1/ai0") | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Configure finite sampling | ||||||||||||||||||||||||||||||||||
| num_samples = 1000 | ||||||||||||||||||||||||||||||||||
| task.timing.cfg_samp_clk_timing(1000, sample_mode=AcquisitionType.FINITE, samps_per_chan=num_samples) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Calculate the start time (1 minute from now) | ||||||||||||||||||||||||||||||||||
| start_time = datetime.now(timezone.utc) + timedelta(minutes=1) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+16
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 minute is a lot...
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: I was going to ask whether it's really necessary to specify UTC here, but then I looked up whether https://discuss.python.org/t/datetime-api-no-atomic-way-to-get-aware-current-local-datetime-when-moving-between-time-zones/55041/8 |
||||||||||||||||||||||||||||||||||
| print(f"Scheduled start time: {start_time}") | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Configure the time start trigger | ||||||||||||||||||||||||||||||||||
| task.triggers.start_trigger.cfg_time_start_trig(start_time) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| print("Starting the task...") | ||||||||||||||||||||||||||||||||||
| task.start() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| print("Waiting for the scheduled start time...") | ||||||||||||||||||||||||||||||||||
| # Wait until the scheduled start time | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+26
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
Suggested change
|
||||||||||||||||||||||||||||||||||
| while datetime.now(timezone.utc) < start_time: | ||||||||||||||||||||||||||||||||||
| time.sleep(1) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+28
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a DAQmx API for this, I think you can wait for either I assume you are doing this to avoid getting a read timeout. FYI, another solution is to increase the read timeout to account for the scheduled start time, but that's probably less clear. |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Read data after the task starts | ||||||||||||||||||||||||||||||||||
| data = task.read(number_of_samples_per_channel=num_samples) | ||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a more standard read from a finite task:
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note you'll need to import READ_ALL_AVAILABLE or fully specify it |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Perform the measurement | ||||||||||||||||||||||||||||||||||
| perform_measurement() | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our examples usually prefer simplicity. I would remove this function and simply put your |
||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With my suggested change to the read, this can be simplified