Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Control loop in C# with a 6024E/USB-6211 board

 Hi

 

I have an application where I want to use some sort of hardware timing to call a function in C# to calculate feedback gains and send a signal back to the DAQ to control an actuator. The board I bought for this application was the USB-6211 but subsequently realised that that I can't use a "SampleComplete" or "SampleClock" event because of it's USB interface. I found a 6024E card that we had and managed to get "SampleComplete" event to work, however I am struggling to read data in the "SampleCompleteEvent" function at any frequency above 1 hz. I suspect that I may be doing something incorrect in the way I read the data. Some of the code (for a 6024E card) I have written follows, as this may explain what I am trying to do a bit better:


            // Setup the channel:
            AI0_Task.AIChannels.

            CreateVoltageChannel(("Dev2/ai0"), null, AITerminalConfiguration.Rse,
            USB_6211.AI0_InputRange[0], USB_6211.AI0_InputRange[1], A0VoltageUnits.Volts);
          
            //  Setup the sample clock:       
            double f_s = 1000;
            AI0_Task.Timing.ConfigureSampleClock(null, f_s, SampleClockActiveEdge.Rising,
            SampleQuantityMode.HardwareTimedSinglePoint, 1);

            // Setup the reader
            AI0_Data = new AnalogSingleChannelReader(AI0_Task.Stream);

            // Setup the control loop function:
            AI0_Task.SampleComplete += new SampleCompleteEventHandler(ControlLoopFunction);
}
   
// Control loop function:    
private void ControlLoopFunction(object sender, SampleCompleteEventArgs e)
{
           AI0_Data = AI0_Data.ReadSingleSample();
           // + More control algorithms
}

Everything works fine, but if I set "f_s" to anything above 1 hz, it doesn't seem to read data using "AI0_Data = AI0_Data.ReadSingleSample();", the application just freezes. I suspect that maybe this is not the way to get data off the card when using a "hardware timed single point" sample quantity mode, but can't find any other way of getting the data off the card in my control loop function. So my question is; "How do you suggest I get a single data point off the card in the control loop function?".

A subsequent question I have is: "Is it possible to use one of the counters on a USB-6211 card to time a control loop?". This would be better as we actually bought the 6211 card for the application in mind. However if this is not possible, we can use the 6024E card. I should also state the the control loop does not have to be very fast. The speed I have in mind is between 100 and 200 hz.

Thank you for your help
0 Kudos
Message 1 of 6
(3,858 Views)
Hello A Sutherland,

What is happening when you perform the Hardware Timed Single Point at a high sample clock is that the application is not keeping up with the hardware itself. If you were looking for the return value and handling the error you would probably get this type of explanation:

"Measurements: DAQmx Wait for Next Sample Clock detected one or more missed sample clocks since the last call to Wait for Next Sample Clock which indicates that your program is not keeping up with the sample clock.

To remove this error, slow down the sample clock, or else change your application so that it can keep up with the sample clock. Alternatively, consider setting the Convert Errors to Warnings property to true and then handling the warning case appropriately."

A rate of 1 kHz is definitely too fast for this type of measurement.

A good example to look at for performing this type of operation would be the General Control Example found by going to Start»National Instruments»NI-DAQ»Text-Based Code».NET 2.0 Examples and then navigating to Control\General\PWMCounterOutput\cs. This is an excellent example of using an analog input and a counter output to perform control applications with a DAQ device.

Regards,
Dan King

0 Kudos
Message 2 of 6
(3,832 Views)

Hi Dan

 

Thank you for your prompt reply. I suspected that, that may have been they case and in the meantime got the program working using another method without using a "SampleComplete" event, at about 500 hz. I was looking to use the hardware timer at around 250 hz, but my computer usually struggled at anything above 1 hz, but sometimes got up to about 25 hz.

 

I don't seem to have any control examples on my DAQmx installation disks (although all the other example files have been loaded). Would it be possible to post the example you mention onto this thread or alternatively email it to me at: ansutherland[at]gmail[dot]com or asutherland[at]csir[dot]co[dot]za?

 

Thank you for your help.

 

Regards

Alan 

0 Kudos
Message 3 of 6
(3,823 Views)
Hello A Sutherland,

I have posted the example code below. This example should have been installed with your DAQmx drivers. What version of DAQmx are you using?

Regards,
Dan King

0 Kudos
Message 4 of 6
(3,807 Views)

Thanks Dan, the example you posted helped a lot. I am using version 8.6 of DAQmx . The examples that were loaded with my installation are:

 

- Analog In

- Analog Out

- Counter

- Digital

- Switches

- Synchronization 

 

Regards

Alan 

0 Kudos
Message 5 of 6
(3,787 Views)
Hello Alan,

The Control example may be from a newer version of NI-DAQmx. In addition to fixing bugs and adding functionality newer versions of NI-DAQmx can also include new examples. When creating a new application it is recommended to use the latest version of the applicable National Instruments drivers. The newest version of the driver can be found here:

NI-DAQmx 8.9 - Windows 2000/Vista x64/Vista x86/XP

Regards,
Dan King

0 Kudos
Message 6 of 6
(3,784 Views)