Skip to content
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

Add hw timed examples for exisiting sw timed examples #561

Closed

Conversation

WayneDroid
Copy link
Collaborator

What does this Pull Request accomplish?

Added HW timed examples for existing SW timed examples.

Why should this Pull Request be merged?

Currently, we only have SW timed examples except ao_voltage_hw_timed.py, this PR is to add HW timed examples for the rest of SW timed examples.

What testing has been done?

poetry run pytest tests/ -q - pass

image

@@ -0,0 +1,43 @@
"""Example of AI voltage sw operation."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Example of AI voltage sw operation."""
"""Example of AI voltage hardware-timed finite acquisition."""

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English has a standard order for using multiple adjectives: https://www.grammarly.com/blog/adjective-order/ . If you don't follow this order, it sounds slightly awkward.

The existing examples don't follow this order and have other style problems like lowercase "sw".

It would be better to reword the text so that it doesn't contain a long list of adjectives. Consider reusing text from the LabVIEW, ANSI C, CVI, or .NET examples. For example:

This example demonstrates how to acquire a finite amount of data
using the DAQ device's internal clock.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description is updated by referring LabVIEW examples.


with nidaqmx.Task() as task:
task.ai_channels.add_ai_voltage_chan("cDAQ1Mod1/ai0")
task.timing.cfg_samp_clk_timing(1000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify these two optional params

Suggested change
task.timing.cfg_samp_clk_timing(1000)
task.timing.cfg_samp_clk_timing(1000, sample_mode=AcquisitionType.FINITE, samps_per_chan=1000)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted, optional params are added for the files which called cfg_samp_clk_timing.

Comment on lines 14 to 43
print("1 Channel 1 Sample Read: ")
data = task.read()
pp.pprint(data)
task.wait_until_done()
task.stop()

data = task.read(number_of_samples_per_channel=1)
pp.pprint(data)
task.wait_until_done()
task.stop()

print("1 Channel N Samples Read: ")
data = task.read(number_of_samples_per_channel=8)
pp.pprint(data)
task.wait_until_done()
task.stop()

task.ai_channels.add_ai_voltage_chan("cDAQ1Mod1/ai1:3")

print("N Channel 1 Sample Read: ")
data = task.read()
pp.pprint(data)
task.wait_until_done()
task.stop()

print("N Channel N Samples Read: ")
data = task.read(number_of_samples_per_channel=2)
print(data)
task.wait_until_done()
task.stop()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify

Suggested change
print("1 Channel 1 Sample Read: ")
data = task.read()
pp.pprint(data)
task.wait_until_done()
task.stop()
data = task.read(number_of_samples_per_channel=1)
pp.pprint(data)
task.wait_until_done()
task.stop()
print("1 Channel N Samples Read: ")
data = task.read(number_of_samples_per_channel=8)
pp.pprint(data)
task.wait_until_done()
task.stop()
task.ai_channels.add_ai_voltage_chan("cDAQ1Mod1/ai1:3")
print("N Channel 1 Sample Read: ")
data = task.read()
pp.pprint(data)
task.wait_until_done()
task.stop()
print("N Channel N Samples Read: ")
data = task.read(number_of_samples_per_channel=2)
print(data)
task.wait_until_done()
task.stop()
print("Reading Data: ")
task.start()
data = task.read()
print(data)
task.stop()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, it's updated.

@@ -0,0 +1,43 @@
"""Example of AI voltage sw operation."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this file ai_voltage_hw_timed_finite.py and let's create one more that is a duplicate, but called ai_voltage_hw_timed_continues.py. The main difference will be:

  • change the params to cfg_samp_clk_timing
  • pass in number_of_samples_per_channel to task.read (1/10th of your sample rate)
  • loop forever reading between start/stop
  • try/catch waiting for a keyboard interrupt to stop (user presses Ctrl+C, ask them to do that in a print)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhindes Have you considered copying the naming of the LabVIEW, ANSI C, CVI, or .NET examples, e.g. ai_acq_int_clk.py, ai_cont_acq_int_clk.py?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or even analog_in/acq_int_clk.py etc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like that!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhindes I'm not sure should we add continuous example as the examples will be run together with the pytest under acceptance folder, I think that will break the pytest.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a fair point. We'll need some way to not run tests that are continuous. We should still have the examples, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, just find that we already have continuous examples taken care, the test will skip if KeyboardInterrupt is found in the example.

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created sub folders for AI, AO, DI, DO, CI and CO examples, including continuous examples.

Comment on lines 11 to 12
task.ci_channels.add_ci_count_edges_chan("Dev1/ctr0")
task.timing.cfg_samp_clk_timing(1000, "/Dev1/PFI6")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample clocks with event counts aren't common. I think this is what you want:

Suggested change
task.ci_channels.add_ci_count_edges_chan("Dev1/ctr0")
task.timing.cfg_samp_clk_timing(1000, "/Dev1/PFI6")
task.ci_channels.add_ci_count_edges_chan("Dev1/ctr0").ci_count_edges_term = "Dev1/PFI6"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably cleaner on two lines. save the channel to a variable then set the ci_count_edges_term

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ci_hw_count_edges.py is renamed to ci_count_edges_ext_clk.py. I still keep the cfg_samp_clk_timing in example to align with LabVIEW example Counter - Count Edges (Finite Clock).vi.

Comment on lines 28 to 42
with nidaqmx.Task() as task:
task.di_channels.add_di_chan("Dev1/port0/line0:3", line_grouping=LineGrouping.CHAN_PER_LINE)
task.timing.cfg_samp_clk_timing(1000)

print("N Channel 1 Sample Read: ")
data = task.read()
pp.pprint(data)
task.wait_until_done()
task.stop()

print("N Channel N Samples Read: ")
data = task.read(number_of_samples_per_channel=3)
print(data)
task.wait_until_done()
task.stop()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow AI review comments

  • two examples - finite, continuous
  • etcetc

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, all examples are updated according to your suggestions.

Comment on lines 12 to 13
print("1 Channel N Lines 1 Sample Unsigned Integer Write: ")
print(task.write(8))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's deleted.

task.do_channels.add_do_chan(
"Dev1/port0/line0:3", line_grouping=LineGrouping.CHAN_FOR_ALL_LINES
)
task.timing.cfg_samp_clk_timing(1000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify optional params

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional params are added.

print(task.write(8))

print("1 Channel N Lines N Samples Unsigned Integer Write: ")
print(task.write([1, 2, 4, 8], auto_start=True))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for finite, wait for done then stop task

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, it's updated according to your suggestion.

@@ -0,0 +1,16 @@
"""Example for writing digital signal."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add continuous, too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuous example is added.

@@ -0,0 +1,43 @@
"""Example of AI voltage sw operation."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English has a standard order for using multiple adjectives: https://www.grammarly.com/blog/adjective-order/ . If you don't follow this order, it sounds slightly awkward.

The existing examples don't follow this order and have other style problems like lowercase "sw".

It would be better to reword the text so that it doesn't contain a long list of adjectives. Consider reusing text from the LabVIEW, ANSI C, CVI, or .NET examples. For example:

This example demonstrates how to acquire a finite amount of data
using the DAQ device's internal clock.

@@ -0,0 +1,43 @@
"""Example of AI voltage sw operation."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhindes Have you considered copying the naming of the LabVIEW, ANSI C, CVI, or .NET examples, e.g. ai_acq_int_clk.py, ai_cont_acq_int_clk.py?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ci_hw_count_edges.py" makes this sound less advanced / corner-case than it is.

The examples for other ADEs tend to use "buffered" and/or "external clock" to describe this use case.

Cnt-Buf-Cont-ExtClk
Counter - Count Edges (Continuous Clock).vi
etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All example files are renamed by referring LabVIEW examples.

@@ -0,0 +1,31 @@
"""Example of analog input voltage acquisition continuously.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The adverb "continuously" normally modifies a verb, but this is a noun phrase that has no verb.

Suggested change
"""Example of analog input voltage acquisition continuously.
"""Example of continuous analog input voltage acquisition.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should rename this to dig_pulse.py and copy the logic used in the ANSI C DigPulse.c.

Removing implicit timing changes this from a pulse train to a single pulse.

If it generates a single pulse, "changing pulse specification" no longer has any effect, unless task.write() auto-starts the task, in which case it generates two pulses, which is weird.

time.sleep(2) is the wrong way to wait for a pulse to complete. We should use task.wait_until_done(). (time.sleep(2) was reasonable when this generated a pulse train and we wanted to change the pulse specification while it was running.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to dig_pulse_train_cont.py as per ANSI C example DigPulseTrain-Cont.c

Comment on lines +20 to +23
sample = CtrTime(high_time=0.001, low_time=0.002)
print("Press Ctrl+C to stop")
print("1 Channel 1 Sample Write: ")
print(task.write(sample, auto_start=True))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing during the pulse train is a more advanced use case and we don't have a basic pulse train example. See DigPulseTrain-Cont.c

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does a finite pulse train.

Do we need an example of this? We don't have one in C.

Comment on lines +17 to +20
sample = CtrTime(high_time=0.001, low_time=0.002)

print("1 Channel 1 Sample Write: ")
print(task.write(sample))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this is a more advanced use case, and we don't have an example of the basic finite pulse train.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI pulse channels are an advanced feature that is only supported on devices introduced in 2009 and later. I don't understand why we have examples of these and not the basic stuff:

  • Measure frequency (with add_ci_freq_chan)
  • Measure period or pulse width (with add_ci_period_chan)
  • Measure position (encoders)
  • Measure 2 edge separation (ok, this is kind of intermediate?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_dig_chan.py?

Copy link
Collaborator

@bkeryan bkeryan Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this rate, this PR review is going to take forever.

Here's a proposal for speeding it up:

  • PR 1: reorganize the existing examples into folders, with no code changes
  • Let's use Teams to discuss which examples should exist, since many of the existing examples demonstrate advanced features and overlook the basics
  • PRs 2-7: for each folder, update/rename the examples or replace them with new examples based on the ANSI C examples, when appropriate

ok?

@WayneDroid @zhindes

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! I added my first pass thoughts on Teams!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I created PR for reorganizing example files, #565.
I will close this PR, new changes will be made in another PRs as per suggestion.

@WayneDroid WayneDroid closed this Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants