04-04-2023 09:58 AM
Sounds like you are on the right track. I would start with the two Voltage - Continuous examples. The Voltage - Continuous Input.vi for reading your voltage and the Voltage - Continuous Output for generating a square wave. Then you need a way to detect changes in the voltage input from loop iteration to iteration like a shift register comparing values. When you detect a change, you update the analog output (square wave) frequency based on the new voltage reading.
Post the code you're working with so we can take a look...
I'll make a quick example but I only have a USB-6002 DAQ here right now so I can't do anything real fancy.
04-04-2023 10:11 AM
I don't have time to check the analog in and I don't know how you want to convert voltage to frequency but this should give you some idea what I was talking about. I have a control for simulating the changing voltage level. You will need to add your conversion code in the disabled diagram structure and enable it.
You will also need a fancier way to determine when the voltage input changes enough to warrant an update in frequency. Right now it would change every loop due to noise in the Analog input so you'll want some sort of hysteresis programmed in. Even changing the datatype of the frequency variable from a float to an integer would do for a start.
Play around and see what you can come up with. Post your VI (not a picture) if you get stuck on anything.
04-04-2023 10:26 AM
FYI, some good tutorials for getting started with LabVIEW and DAQmx.
Getting Started with NI-DAQmx - NI
04-04-2023 10:46 AM
FWIW, I recall that commonplace MIO boards such as your M-series can *also* allow you to change the AO sample rate on the fly as another method for changing the output waveform's frequency. You would use a DAQmx Timing property node to set SampleClock.Rate. (Mod below added to shipping example and tested on a simulated device).
-Kevin P
04-04-2023 01:36 PM - edited 04-04-2023 01:37 PM
If you are using a counter output, then this is pretty easy. I wrote a frequency sweeping code that uses a counter and posted it here this can change the frequency and duty cycle without having to stop the output. If you are using an analog out, you can get a bit crafty. Years ago I made a continuous AO output with a waveform that just repeats. While the task is running you can replace the buffer with new data. So As long as the number of samples are the same you can change the data without stopping the task. I describe some of the details here. But I don't have the code anymore. At the time I got away with having my frequency step by 100hz increments, which helped in keeping the buffer sizes the same, so some rates would repeat more than others. I've never tried changing the clock rate but that sounds much easier as long as you only have one AO at a time. I think I had N channels N samples at the time.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
04-05-2023 10:37 AM
Thank you this has been very helpful.
I am still a little confused on the disabled diagram structure, and how I should be connecting the varying frequency.
The input voltage will be varying from 1-5V. Is there a way I can have the frequency change double as the voltage increases by one? This would give me a good starting point.
I have attached your code that I have been using/testing and modified slightly. Let me know of any suggestions.
04-06-2023 11:31 AM
Also, is there a way to have the width of the pulse change? Right now it is 1/2 of the period. If possible I would like to have the pulse no more than 5 mV while still being able to adjust the period.
04-07-2023 10:51 AM
I'm using LabVIEW 2019 so I can't open your code. I updated my version to better show how simple math functions (e.g.: Multiply) can scale your voltage input into a frequency. You could use a formula node or any other method as well. The pulse width is controlled by the Duty Cycle input on the square wave generator. It works in terms of a percentage of the wave that is high so 50% is a symmetrical square wave and 10% is more of a pulse wave. It you want a consistent 5mSec pulse you will have to recalculate the percentage to give you that based on your signal's frequency. Then change it on-the-fly just like we are doing with the frequency. I added a control for it so you can see how it affects the signal.
04-07-2023 08:58 PM
I made just a couple mods to the code posted by NIquist, mainly to carry through phase information (which in turn led to writing into the buffer *every* iteration). Then I moved the AI task to be above the AO task to limit wire crossings. And a couple other fairly trivial changes. It isn't a final version, there's still stuff for you to do:
1. the correct math to convert AI voltage to desired square wave frequency
2. calcs to then determine the correct duty cycle when you want a fixed pulse width across all frequencies
3. possible further calcs to modify phase appropriately. (For example, as frequency changes, not changing the phase angle might result in an immediate square wave transition, which you may want to avoid)
4. possibly some work to reduce latency time between changes on the GUI and changes in the real-life signal. Getting down to 1/2 second or so is pretty manageable and robust. Under 50 msec requires more detailed work and increases your risk of buffer underflow errors. I recommend you don't start messing with this unless you *need* to and even then, not until after you take care of the other stuff.
-Kevin P
04-12-2023 09:47 AM
Thank you. I have been working through the program and I think it is almost there. I'm currently getting the error:
Error-200479 occurred at DAQmx Start Task.vi: 7220001
Possible reason(s):
Specified operation cannot be performed while the task is running.
Do you have any insight on what may be causing this error? I have checked all the while loops and they seem to be ok. I have attached my program below.