Beispiel #1
0
        /// <summary>
        /// 配置Ao输出任务
        /// </summary>
        /// <returns></returns>
        private API_RETURN_CODE ConfigAoTask()
        {
            API_RETURN_CODE code = API_RETURN_CODE.API_SUCCESS;

            try
            {
                m_aoTask = new Task();

                m_aoTask.AOChannels.CreateVoltageChannel(GetAoPhysicalChannelName(), "", -10.0, 10.0, AOVoltageUnits.Volts);
                m_aoTask.Control(TaskAction.Verify);

                m_aoTask.Timing.SampleClockRate = m_params.AoSampleRate;
                m_aoTask.Timing.ConfigureSampleClock("",
                                                     m_aoTask.Timing.SampleClockRate,
                                                     SampleClockActiveEdge.Rising,
                                                     SampleQuantityMode.ContinuousSamples,
                                                     m_params.AoSampleCountPerFrame);

                // 路由Ao Sample Clcok到PFI0
                //if (m_config.Debugging)
                //{
                //    Logger.Info(string.Format("route ao sample clock to PFI0."));
                //    m_aoTask.ExportSignals.SampleClockOutputTerminal = string.Concat("/", NI_CARD_NAME_DEFAULT, "/", "PFI0");
                //}

                //string source = m_sysConfig.GetStartSyncSignal();
                //m_aoTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(source, DigitalEdgeStartTriggerEdge.Rising);

                // 写入波形
                AnalogMultiChannelWriter  writer = new AnalogMultiChannelWriter(m_aoTask.Stream);
                AnalogWaveform <double>[] waves;
                if (m_config.GetScanMirrorNum() == SCAN_MIRROR_NUM.THREEE)
                {
                    waves    = new AnalogWaveform <double> [3];
                    waves[2] = AnalogWaveform <double> .FromArray1D(m_waver.Y2Wave);
                }
                else
                {
                    waves = new AnalogWaveform <double> [2];
                }
                waves[0] = AnalogWaveform <double> .FromArray1D(m_waver.XWave);

                waves[1] = AnalogWaveform <double> .FromArray1D(m_waver.Y1Wave);

                writer.WriteWaveform(false, waves);
            }
            catch (Exception e)
            {
                Logger.Error(string.Format("config ao task exception: [{0}].", e));
                code = API_RETURN_CODE.API_FAILED_NI_CONFIG_AO_TASK_EXCEPTION;
            }
            return(code);
        }
Beispiel #2
0
        private void Start()
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                // 创建模拟输出任务
                galvTask = new Task();
                // 创建模拟电压输出通道
                galvTask.AOChannels.CreateVoltageChannel("Dev1/ao0:2", "", -10.0, 10.0, AOVoltageUnits.Volts);
                // 验证任务
                galvTask.Control(TaskAction.Verify);

                double        desiredFrequency = aoSampleRate / sampleCountPerAO;
                double        samplesPerBuffer = sampleCountPerAO;
                double        cyclesPerBuffer  = 1.0;
                WaveGenerator wave             = new WaveGenerator(
                    galvTask.Timing, desiredFrequency, samplesPerBuffer, cyclesPerBuffer, WaveformType.GalvWave, 2.0);

                // configure the sample clock with the calculated rate
                galvTask.Timing.ConfigureSampleClock("",
                                                     wave.ResultingSampleClockRate,
                                                     SampleClockActiveEdge.Rising,
                                                     SampleQuantityMode.ContinuousSamples, sampleCountPerAO);

                AnalogMultiChannelWriter  writer = new AnalogMultiChannelWriter(galvTask.Stream);
                AnalogWaveform <double>[] waves  = new AnalogWaveform <double> [3];
                waves[0] = AnalogWaveform <double> .FromArray1D(xWaves);

                waves[1] = AnalogWaveform <double> .FromArray1D(y1Waves);

                waves[2] = AnalogWaveform <double> .FromArray1D(y2Waves);

                //write data to buffer
                writer.WriteWaveform(false, waves);

                //start writing out data
                galvTask.Start();
            }
            catch (DaqException e)
            {
                Logger.Error(string.Format("daq exception [{0}].", e));
                MessageBox.Show(e.Message);
                galvTask.Dispose();
            }
            Cursor.Current = Cursors.Default;
        }
Beispiel #3
0
        private void DaqInit()
        {
            try
            {
                daqTask  = new NationalInstruments.DAQmx.Task();
                daqTask2 = new NationalInstruments.DAQmx.Task();
                daqTask3 = new NationalInstruments.DAQmx.Task();
                daqTask.AIChannels.CreateVoltageChannel("Dev1/ai0", "", (AITerminalConfiguration)(-1), -10, 10, AIVoltageUnits.Volts);
                daqTask.AIChannels.CreateVoltageChannel("Dev1/ai1", "", (AITerminalConfiguration)(-1), -10, 10, AIVoltageUnits.Volts);
                daqTask2.AOChannels.CreateVoltageChannel("Dev1/ao0", "", -10, 10, AOVoltageUnits.Volts);
                daqTask2.AOChannels.CreateVoltageChannel("Dev1/ao1", "", -10, 10, AOVoltageUnits.Volts);
                daqTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, sampleRate);
                daqTask.AIChannels.All.DataTransferMechanism = AIDataTransferMechanism.Dma;

                daqTask2.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, sampleRate);
                daqTask2.AOChannels.All.DataTransferMechanism = AODataTransferMechanism.Dma;
                daqTask2.Stream.WriteRegenerationMode         = WriteRegenerationMode.DoNotAllowRegeneration;
                reader = new AnalogMultiChannelReader(daqTask.Stream);
                int inBufSize1 = sampleRate, inBufSize2 = 2 * sampleRate;

                inDriveSignal = reader.ReadWaveform(inBufSize1);

                /*fftw_plan mplan;
                 * double[] din = new double[inBufSize1];
                 * double[] freq = new double[inBufSize1*2];
                 * GCHandle hdin = GCHandle.Alloc(din, GCHandleType.Pinned);
                 * GCHandle hdout = GCHandle.Alloc(freq, GCHandleType.Pinned);
                 * fftw_complexarray min = new fftw_complexarray(din);
                 * fftw_complexarray mout = new fftw_complexarray(freq);
                 * IntPtr fplan5 = fftw.dft_r2c_1d(sampleRate, hdin.AddrOfPinnedObject(), hdout.AddrOfPinnedObject(), fftw_flags.Estimate);
                 * Array.Copy(inDriveSignal[0].GetRawData(), din, inBufSize1);
                 * fftw.execute(fplan5);
                 * measuredFreq1 = (double)Util.AbsMaxIndex(freq) / 2;*/

                /*int rem = (int)((double)(measuredFreq1 - (int)measuredFreq1) * 1000);
                 * int mul;
                 * if (rem > 0)
                 * {
                 *  mul = 1000 / (int)Integers.GCD(rem, 1000);
                 *  measuredFreq1 *= mul;
                 *
                 * }*/

                //Array.Copy(inDriveSignal[1].GetRawData(), din, inBufSize1);
                //fftw.execute(fplan5);
                //measuredFreq2 = (double)Util.AbsMaxIndex(freq) / 2;
                ZX1 = Util.GetZeroCrossings(inDriveSignal[0].GetRawData());
                ZX2 = Util.GetZeroCrossings(inDriveSignal[1].GetRawData());

                /*rem = (int)((double)(measuredFreq2 - (int)measuredFreq2) * 1000);
                 * if (rem > 0)
                 * {
                 *  mul = 1000 / (int)Integers.GCD(rem, 1000);
                 *  measuredFreq2 *= mul;
                 * }*/
                //samplePerChannel = (int)Math.Max(ch1, ch2);
                //sampleRate = 500*(int)Integers.LCM((long)measuredFreq1, (long)measuredFreq2);
                samplePerChannel = sampleRate / 25;
                daqTask2.EveryNSamplesWrittenEventInterval = samplePerChannel;
                daqTask2.EveryNSamplesWritten += daqTask2_EveryNSamplesWritten;
                writer  = new AnalogMultiChannelWriter(daqTask2.Stream);
                bufsize = sampleRate;

                waveX   = new double[180][];
                waveY   = new double[180][];
                outWave = new double[2, samplePerChannel];
                for (int i = 0; i < 180; i++)
                {
                    waveX[i] = new double[bufsize];
                    waveY[i] = new double[bufsize];
                }
                waveX[0] = inDriveSignal[0].GetRawData(0, bufsize);
                waveY[0] = inDriveSignal[1].GetRawData(0, bufsize);

                //Array.Copy(waveX[0], waveX[0].Length / 2, waveY[0], 0, waveY[0].Length / 2);
                //Array.Copy(waveX[0], 0, waveY[0], waveY[0].Length / 2, waveY[0].Length / 2);

                // initialize rotations
                for (int i = 1; i < 180; i++)
                {
                    for (int j = 0; j < bufsize; j++)
                    {
                        double theta = ((i) / 180.0) * Math.PI;
                        double x = waveX[0][j]; double y = waveY[0][j];
                        waveX[i][j] = x * Math.Cos(theta) - y * Math.Sin(theta);
                        waveY[i][j] = x * Math.Sin(theta) + y * Math.Cos(theta);
                    }
                }

                double[] X = new double[samplePerChannel];
                double[] Y = new double[samplePerChannel];

                Array.Copy(waveX[0], ZX1[1], X, 0, samplePerChannel);
                Array.Copy(waveY[0], ZX2[1], Y, 0, samplePerChannel);
                sampleIndex  = samplePerChannel;
                sampleIndex2 = samplePerChannel;
                AnalogWaveform <double>[] waves = { AnalogWaveform <double> .FromArray1D(X), AnalogWaveform <double> .FromArray1D(Y) };
                writer.WriteWaveform <double>(false, waves);
                daqTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.HardwareTimedSinglePoint, 0);
                double[] sample;
                bool     negative = false;

                daqTask2.Start();
                //daqTask2.AOChannels.All.DataTransferMechanism = AODataTransferMechanism.Dma;
                dcOffsetX = dcOffsetY = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }