Exemplo n.º 1
0
        private void ConfigureReadDI(int numberOfMeasurements, double sampleRate, bool triggerSense)
        {
            readDIsTask = new Task("readDIsTask");

            foreach (string inputName in digitalInputs)
            {
                DigitalInputChannel channel = (DigitalInputChannel)Environs.Hardware.DigitalInputChannels[inputName];
                channel.AddToTask(readDIsTask);
            }

            SampleClockActiveEdge       clockEdge   = SampleClockActiveEdge.Rising;
            DigitalEdgeStartTriggerEdge triggerEdge = triggerSense ? DigitalEdgeStartTriggerEdge.Rising : DigitalEdgeStartTriggerEdge.Falling;

            // Get the device that the analog inputs are on so we can use sample clock as well to sync timing
            string device = ((AnalogInputChannel)Environs.Hardware.AnalogInputChannels[analogInputs[0]]).Device;

            readDIsTask.Timing.ConfigureSampleClock(device + "/ai/SampleClock", sampleRate, clockEdge, SampleQuantityMode.FiniteSamples, numberOfMeasurements);
            readDIsTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(device + "/ai/StartTrigger", triggerEdge);

            readDIsTask.Control(TaskAction.Verify);
            digitalReader = new DigitalMultiChannelReader(readDIsTask.Stream);

            // Commiting now apparently saves time when we actually run the task
            readDIsTask.Control(TaskAction.Commit);
        }
Exemplo n.º 2
0
        private void ConfigureReadAI(int numberOfMeasurements, double sampleRate, bool triggerSense, bool autostart) //AND CAVITY VOLTAGE!!!
        {
            readAIsTask = new Task("readAIsTask");

            foreach (string inputName in analogInputs)
            {
                AnalogInputChannel channel = (AnalogInputChannel)Environs.Hardware.AnalogInputChannels[inputName];
                channel.AddToTask(readAIsTask, 0, 10);
            }

            SampleClockActiveEdge       clockEdge   = SampleClockActiveEdge.Rising;
            DigitalEdgeStartTriggerEdge triggerEdge = triggerSense ? DigitalEdgeStartTriggerEdge.Rising : DigitalEdgeStartTriggerEdge.Falling;

            if (!autostart)
            {
                // Use internal clock
                readAIsTask.Timing.ConfigureSampleClock("", sampleRate, clockEdge, SampleQuantityMode.FiniteSamples, numberOfMeasurements);
                readAIsTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(trigger, triggerEdge);
            }

            readAIsTask.Control(TaskAction.Verify);
            analogReader = new AnalogMultiChannelReader(readAIsTask.Stream);


            // Commiting now apparently saves time when we actually run the task
            readAIsTask.Control(TaskAction.Commit);
        }
Exemplo n.º 3
0
        public void RunDAQ()
        {
            DigitalEdgeStartTriggerEdge triggerEdge = DigitalEdgeStartTriggerEdge.Rising;

            try
            {
                string s = string.Empty;
                s          = _name == "/ai1" ? "etalon" : "measured";
                inputTask  = new NationalInstruments.DAQmx.Task(s);
                outputTask = new NationalInstruments.DAQmx.Task(s + "_output");

                inputTask.AIChannels.CreateVoltageChannel(sInputName, "", AITerminalConfiguration.Pseudodifferential, inputDAQMinValue, inputDAQMaxValue, AIVoltageUnits.Volts);
                outputTask.AOChannels.CreateVoltageChannel(sOutputName, "", outputDAQMinValue, outputDAQMaxValue, AOVoltageUnits.Volts);

                inputTask.Timing.ConfigureSampleClock("", dRateIO, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, iInputOutputSamples);
                outputTask.Timing.ConfigureSampleClock("", dRateIO, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, iInputOutputSamples);

                outputTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(terminalNameBase + "ai/StartTrigger", triggerEdge);

                inputTask.Control(TaskAction.Verify);
                outputTask.Control(TaskAction.Verify);

                outputData = fGen.Data;

                writer = new AnalogSingleChannelWriter(outputTask.Stream);
                writer.WriteMultiSample(false, outputData);

                StartTask();
                outputTask.Start();
                inputTask.Start();

                //inputCallback = new AsyncCallback(InputReady);
                reader = new AnalogSingleChannelReader(inputTask.Stream);


                //------------ТЕСТОВЫЙ КУСОК------------ ЧТЕНИЕ В ЭТОМ ЖЕ ПОТОКЕ--
                double[] data = reader.ReadMultiSample(iInputOutputSamples);

                BufReadDAQReceived?.Invoke(data);
                StopTask();
                //----------------------------------------------------------------



                /*// Use SynchronizeCallbacks to specify that the object
                 * // marshals callbacks across threads appropriately.
                 * reader.SynchronizeCallbacks = bSynchronizeCallbacks;
                 *
                 * reader.BeginReadMultiSample(iInputOutputSamples, inputCallback, inputTask);*/
            }
            catch (Exception ex)
            {
                StopTask();
                WarningDAQUpdate?.Invoke(ex.Message);
            }
        }
Exemplo n.º 4
0
        public virtual void WriteScan(double[,] positions, bool async = true,
                                      string triggerSource            = null, DigitalEdgeStartTriggerEdge edge = DigitalEdgeStartTriggerEdge.Rising)
        {
            // Go to initial position. (before the scan starts);
            SetPosition(positions[0, 0], positions[0, 1]);

            // Confugyre the task and force recration of the task.
            ConfigureTask(positions.GetLength(1) * positions.GetLength(0), true);

            // If there is a trigger source then we are currently waiting to writer trigger.
            if (triggerSource != null)
            {
                WriterTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(triggerSource, edge);
                IsWaitingForWriteStartEvent = false;
                IsWriting = true;
            }
            else
            {
                IsWaitingForWriteStartEvent = true;
                IsWriting = false;
            }

            // define the start action.
            TotalScanTicks = positions.GetLength(1);

            MCWriter.WriteMultiSample(false, positions);
            //MCWriter.BeginWriteMultiSample(false, positions, (IAsyncResult rslt) =>
            //{
            //    //CallWriteScanEnd();
            //}, null);

            if (WriterTask.IsDone)
            {
                WriterTask.Start();
            }

            CallStartScanEvent();

            if (triggerSource != null)
            {
                CallStartScanEvent();
            }

            if (!async)
            {
                while (IsWriting || IsWaitingForWriteStartEvent)
                {
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Включение постоянной генерации сигнала. Чтение и контроль ошибок при этом производится в CALLBACK. Для завершения генерации используй метод StopDAQGeneration()
        /// </summary>
        public void RunDAQGeneration()
        {
            DigitalEdgeStartTriggerEdge triggerEdge = DigitalEdgeStartTriggerEdge.Rising;

            try
            {
                string s = string.Empty;
                s          = _name == "/ai1" ? "etalon" : "measured";
                inputTask  = new NationalInstruments.DAQmx.Task(s);
                outputTask = new NationalInstruments.DAQmx.Task(s + "_output");

                inputTask.AIChannels.CreateVoltageChannel(sInputName, "", AITerminalConfiguration.Pseudodifferential, inputDAQMinValue, inputDAQMaxValue, AIVoltageUnits.Volts);
                outputTask.AOChannels.CreateVoltageChannel(sOutputName, "", outputDAQMinValue, outputDAQMaxValue, AOVoltageUnits.Volts);

                inputTask.Timing.ConfigureSampleClock("", dRateIO, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, iInputOutputSamples);
                outputTask.Timing.ConfigureSampleClock("", dRateIO, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, iInputOutputSamples);

                outputTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(terminalNameBase + "ai/StartTrigger", triggerEdge);

                inputTask.Control(TaskAction.Verify);
                outputTask.Control(TaskAction.Verify);

                outputData = fGen.Data;

                writer = new AnalogSingleChannelWriter(outputTask.Stream);
                writer.WriteMultiSample(false, outputData);

                StartTask();
                outputTask.Start();
                //inputTask.Start();

                inputCallback = new AsyncCallback(AnalogInCallback);
                reader        = new AnalogSingleChannelReader(inputTask.Stream);
                reader.SynchronizeCallbacks = true;
                //reader.BeginReadMultiSample(iInputOutputSamples, inputCallback, inputTask);
                reader.BeginReadWaveform(iInputOutputSamples, inputCallback, inputTask);



                //statusDAQtimer.Enabled = true;
            }
            catch (Exception ex)
            {
                StopTask();
                WarningDAQUpdate?.Invoke(ex.Message);
            }
        }
Exemplo n.º 6
0
        public int Configure_Start_Single_Measurement(Parameters Parameters_Instance
                                                      )
        {
            StopTask(); // ensure clear tasks before measurement

            string ParametersFileName = Parameters_Instance.Output_File + ".json";

            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(ParametersFileName))
            {
                file.Write(JsonConvert.SerializeObject(Parameters_Instance, Formatting.Indented));
            }

            _Parameters_Instance = Parameters_Instance;
            if (_Parameters_Instance.NumberOfSamples == "Infinite")
            {
                Number_Of_Samples_Reqired     = -1;
                Is_Number_Of_Samples_Infinite = true;
            }
            else
            {
                Is_Number_Of_Samples_Infinite = false;
                try
                {
                    Number_Of_Samples_Reqired = int.Parse(_Parameters_Instance.NumberOfSamples);
                } catch (Exception)
                {
                    StopTask();
                    MessageBox.Show("Number of samples is not an integer number or 'Infinite'.");
                    return(1);
                }
            }
            Number_Of_Samples_Measured = 0;

            // Find output data format in case of text output
            if (Parameters_Instance.Input_Channel_Value_Save_Format.Substring(0, 4).ToLower() == "text")
            {
                int Index2 = Parameters_Instance.Input_Channel_Value_Save_Format.LastIndexOf(")");
                int Index1 = Parameters_Instance.Input_Channel_Value_Save_Format.IndexOf("(");
                Input_Channel_Value_Save_Format_Syntax = Parameters_Instance.Input_Channel_Value_Save_Format.Substring(Index1 + 1, Index2 - Index1 - 1);
                MainWindow.WindowInstance.Dispatcher.BeginInvoke(new MainWindow.Append_Log_Delegate(MainWindow.WindowInstance.Append_Log),
                                                                 "Output syntax:" + Input_Channel_Value_Save_Format_Syntax + "\n"
                                                                 );
            }

            // Sync pulse
            double Sync_PulseVoltage            = +4.9; // [V]
            double Sync_IdileVoltage            = 0.0;
            int    NumberOfSamples_During_Pulse = 10;
            double WritingRate_Pulse            = NumberOfSamples_During_Pulse / Parameters_Instance.Pulse_Width;


            double[,] Output_Data = new double[2, NumberOfSamples_During_Pulse + 1];
            for (int i = 0; i < NumberOfSamples_During_Pulse; i++)
            {
                Output_Data[0, i] = Sync_PulseVoltage;
                Output_Data[1, i] = Parameters_Instance.Pulse_Voltage;
            }
            Output_Data[0, NumberOfSamples_During_Pulse] = Sync_IdileVoltage;
            Output_Data[1, NumberOfSamples_During_Pulse] = Parameters_Instance.Reverse_Voltage;

            try
            {
                // Create the master and slave tasks
                inputTask  = new Task("InputTask");
                outputTask = new Task("OutputTask");

                // Configure both tasks with the values selected on the UI. #SyncTask
                inputTask.AIChannels.CreateVoltageChannel(Parameters_Instance.Input_Channel,
                                                          "InputChannel",
                                                          AITerminalConfiguration.Differential,
                                                          Parameters_Instance.Input_Channel_MinVoltage,
                                                          Parameters_Instance.Input_Channel_MaxVoltage,
                                                          AIVoltageUnits.Volts);

                outputTask.AOChannels.CreateVoltageChannel(Parameters_Instance.Sync_Channel,
                                                           "",
                                                           Convert.ToDouble(0.0),
                                                           Convert.ToDouble(5.0),
                                                           AOVoltageUnits.Volts);
                outputTask.AOChannels.CreateVoltageChannel(Parameters_Instance.Output_Channel,
                                                           "",
                                                           Parameters_Instance.Output_Channel_MinVoltage,
                                                           Parameters_Instance.Output_Channel_MaxVoltage,
                                                           AOVoltageUnits.Volts);

                // Output pulse
                inputTask.Timing.ConfigureSampleClock("",
                                                      Parameters_Instance.Sampling_Rate,
                                                      SampleClockActiveEdge.Rising,
                                                      SampleQuantityMode.ContinuousSamples,
                                                      Parameters_Instance.BufferSize);
                outputTask.Timing.ConfigureSampleClock("",
                                                       WritingRate_Pulse,
                                                       SampleClockActiveEdge.Rising,
                                                       SampleQuantityMode.FiniteSamples,
                                                       NumberOfSamples_During_Pulse + 1);

                // Set up the start trigger
                DigitalEdgeStartTriggerEdge Input_triggerEdge = DigitalEdgeStartTriggerEdge.Falling;
                inputTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(Parameters_Instance.Trigger_Channel,
                                                                            Input_triggerEdge);

                // Verify the tasks
                inputTask.Control(TaskAction.Verify);
                outputTask.Control(TaskAction.Verify);

                // Write data to each output channel
                Output_Writer = new AnalogMultiChannelWriter(outputTask.Stream);
                Output_Writer.WriteMultiSample(false, Output_Data);

                inputTask.Start();

                inputCallback = new AsyncCallback(InputRead);
                Input_Reader  = new AnalogSingleChannelReader(inputTask.Stream);
                //// Use SynchronizeCallbacks to specify that the object
                //// marshals callbacks across threads appropriately.
                Input_Reader.SynchronizeCallbacks = true;
                Input_Reader.BeginReadMultiSample(Parameters_Instance.BufferSize, inputCallback, inputTask);

                // Generate Sync pulse

                //syncTask.Start();
                outputTask.Start();

                MainWindow.WindowInstance.Dispatcher.BeginInvoke(new MainWindow.Append_Log_Delegate(MainWindow.WindowInstance.Append_Log),
                                                                 "Done: Configure_Start " + Number_Of_measurements_Measured_string + "\n"
                                                                 );
                //inputTask.WaitUntilDone();
            }
            catch (Exception ex)
            {
                StopTask();
                MessageBox.Show(ex.Message);
                return(1);
            }
            return(0);
        }