Exemplo n.º 1
0
        /// <summary>
        ///     Constructs a DAQ object.
        /// </summary>
        /// <param name="normalChannelId">The channel of the normal force probe</param>
        /// <param name="shearChannelId">The channel of the shear force probe</param>
        public DAQ(string normalChannelId, string shearChannelId)
        {
            Console.WriteLine("RUN INIT");
            //Initialize lists
            dataPoints = new List <dataPoint>();
            start      = DateTime.Now;
            try
            {
                //Define channels
                normalForceChannel = analogInTask.AIChannels.CreateVoltageChannel(normalChannelId,
                                                                                  Constants.NORMALCHANNELNAME, AITerminalConfiguration.Differential, 0, 5, AIVoltageUnits.Volts);
                shearForceChannel = analogInTask.AIChannels.CreateVoltageChannel(shearChannelId,
                                                                                 Constants.SHEARCHANNELNAME, AITerminalConfiguration.Differential, 0, 5, AIVoltageUnits.Volts);

                //Define reader
                reader = new AnalogMultiChannelReader(analogInTask.Stream);

                var dataCollection = new Thread(beginTrial);
                dataCollection.IsBackground = true;
                dataCollection.Start();

                beginTrial();
            }
            catch (DaqException e)
            {
                Console.Write("DAQEXCEPTION: " + e.Message);
                MessageBox.Show(
                    "Failed to connect to force probes. Please check that they are connected properly and that all required drivers are installed.");
                var data = new dataPoint();
                data.time        = -1;
                data.normalForce = 0;
                data.shearForce  = 0;
                dataPoints.Add(data);
            }
        }
        public DeviceAccession()
        {
            //Az attributumok elérésénél a lockhoz a szinkronizációs objektum
            lockAttributes = new object();

            DAQ = DaqSystem.Local.LoadDevice("Dev1");
            DAQ.SelfCalibrate();

            ditask = new Task();
            dotask = new Task();
            aitask = new Task();

            chLeftEnd = ditask.DIChannels.CreateChannel("Dev1/port0/line0", "Left End", ChannelLineGrouping.OneChannelForEachLine);
            chRightEnd = ditask.DIChannels.CreateChannel("Dev1/port0/line1", "Right End", ChannelLineGrouping.OneChannelForEachLine);

            chMoveToLeft = dotask.DOChannels.CreateChannel("Dev1/port1/line0", "Move to the Left", ChannelLineGrouping.OneChannelForEachLine);
            chMoveToRight = dotask.DOChannels.CreateChannel("Dev1/port1/line1", "Move to the Right", ChannelLineGrouping.OneChannelForEachLine);

            chAngle = aitask.AIChannels.CreateVoltageChannel("Dev1/ai0", "Angle", AITerminalConfiguration.Rse, -10, 10, AIVoltageUnits.Volts);
            chPosition = aitask.AIChannels.CreateVoltageChannel("Dev1/ai1", "Position", AITerminalConfiguration.Rse, -10, 10, AIVoltageUnits.Volts);

            ditask.Start();
            dotask.Start();
            aitask.Start();

            digreader = new DigitalMultiChannelReader(ditask.Stream);
            digwriter = new DigitalMultiChannelWriter(dotask.Stream);
            anreader = new AnalogMultiChannelReader(aitask.Stream);
        }
Exemplo n.º 3
0
        public int ReadAnalogChannel(string lines, string name, AITerminalConfiguration config, ref double[] channelData, double min, double max)
        {
            //Create a task such that it will be disposed after
            //we are done using it.
            Task      analogReadTask = new Task();
            AIChannel ch             = null;

            //channelData = new double[analogReadTask.AIChannels.Count];
            //double[] channelData2 = new double[analogReadTask.AIChannels.Count];
            double[] channelData2 = null;

            if (max == 0)
            {
                max = 10;
            }

            try
            {
                //If min > ai.Minimum And max < ai.Maximum Then
                //Create a virtual channel
                ch = analogReadTask.AIChannels.CreateVoltageChannel(lines, "", config, min, max, AIVoltageUnits.Volts);

                //Verify the Task
                analogReadTask.Control(TaskAction.Verify);
                //InitializeDataTable(myTask.AIChannels, DataTable)

                channelData  = new double[analogReadTask.AIChannels.Count];
                channelData2 = new double[analogReadTask.AIChannels.Count];

                AnalogMultiChannelReader reader = new AnalogMultiChannelReader(analogReadTask.Stream);

                analogReadTask.Start();
                channelData2 = reader.ReadSingleSample();
                analogReadTask.Stop();

                for (int i = 0; i < analogReadTask.AIChannels.Count; i++)
                {
                    Debug.Print(channelData2[0].ToString("#0.00"));
                }
                //return 0;
                //Else

                //End If

                //Update the Acquired Sample Table
                //dataToDataTable(data, DataTable)
                //acquisitionDataGrid.DataSource = DataTable
            }
            catch (DaqException ex) {
                DaqError(ex.Message);
            }
            finally {
                //analogReadTask.Dispose();
                Array.Copy(channelData2, channelData, analogReadTask.AIChannels.Count);
                channelData = channelData2;
            }
            return(0);
        }
Exemplo n.º 4
0
        public int ReadWaveformAnalogChannel(string lines, string name, AITerminalConfiguration config, ref AnalogWaveform <double>[] channelData, double min, double max, int nsamples)
        {
            //Create a task such that it will be disposed after
            //we are done using it.
            Task      analogReadTask = new Task();
            AIChannel ch             = null;

            AnalogWaveform <double>[] channelData2 = new AnalogWaveform <double> [analogReadTask.AIChannels.Count];

            if (max == 0)
            {
                max = 10;
            }
            if (nsamples == 0)
            {
                nsamples = -1;
            }

            try
            {
                //If min > ai.Minimum And max < ai.Maximum Then
                //Create a virtual channel
                ch = analogReadTask.AIChannels.CreateVoltageChannel(lines, "", config, min, max, AIVoltageUnits.Volts);

                //Verify the Task
                analogReadTask.Control(TaskAction.Verify);
                //InitializeDataTable(myTask.AIChannels, DataTable)

                AnalogMultiChannelReader reader = new AnalogMultiChannelReader(analogReadTask.Stream);

                analogReadTask.Start();
                channelData2 = reader.ReadWaveform(nsamples);
                analogReadTask.Stop();

                Debug.Print(String.Format("0x{0:X}", channelData2));
                return(0);
                //Else

                //End If

                //Update the Acquired Sample Table
                //dataToDataTable(data, DataTable)
                //acquisitionDataGrid.DataSource = DataTable
            }
            catch (DaqException ex) {
                DaqError(ex.Message);
            }
            finally {
                analogReadTask.Dispose();
                Array.Copy(channelData2, channelData, analogReadTask.AIChannels.Count);
                channelData = channelData2;
            }
            return(0);
        }
Exemplo n.º 5
0
 public DaqAi(string channel)
 {
     _myAiChannel = _analogInTask.AIChannels.CreateVoltageChannel(
         "dev3/" + channel,
         "myAIChannel",
         AITerminalConfiguration.Differential,
         0,
         5,
         AIVoltageUnits.Volts
         );
     _reader = new AnalogSingleChannelReader(_analogInTask.Stream);
 }
Exemplo n.º 6
0
    public double[,] getChannelsValuesContinuous(int lineStart, int lineStop, int numOfSamples)
    {
        Task      task        = new Task();
        string    devFullname = string.Format("{0}/ai{1}:{2}", name, lineStart, lineStop);
        AIChannel channel     = task.AIChannels.CreateVoltageChannel(devFullname, "", AITerminalConfiguration.Rse, 0.0, 10.0, AIVoltageUnits.Volts);

        task.Start();
        AnalogMultiChannelReader reader = new AnalogMultiChannelReader(task.Stream);
        IAsyncResult             result = reader.BeginReadMultiSample(numOfSamples, null, null); // 2d array 2xN when lineStart=0, lineStop=1

        double[,] values = reader.EndReadMultiSample(result);                                    // 2d array 3xN when lineStart=0, lineStop=2
        task.Stop();
        return(values);
    }
Exemplo n.º 7
0
    public double[,] getChannelValues(int line)
    {
        Task      task        = new Task();
        string    devFullname = string.Format("{0}/ai{1}", name, line);
        AIChannel channel     = task.AIChannels.CreateVoltageChannel(devFullname, "", AITerminalConfiguration.Rse, 0.0, 10.0, AIVoltageUnits.Volts);

        task.Start();
        AnalogMultiChannelReader reader = new AnalogMultiChannelReader(task.Stream);
        IAsyncResult             result = reader.BeginReadMultiSample(1000, null, null);

        double[,] values = reader.EndReadMultiSample(result);
        task.Stop();
        return(values);
    }
Exemplo n.º 8
0
 public override void ConfigChannel(int chID, double range)
 {
     //if channel exists, only reconfig the channel
     if (aiTask.Channels.Exists(x => x.ChannelID == chID))
     {
         AIChannel channel = aiTask.Channels.Find(x => x.ChannelID == chID);
         channel.RangeLow  = range * (-1.0);
         channel.RangeHigh = range;
     }
     else
     {
         aiTask.AddChannel(chID, range * (-1.0), range);
     }
 }
Exemplo n.º 9
0
        public double ReadTemperature()
        {
            var       niTask      = new NationalInstruments.DAQmx.Task();
            AIChannel analogInput = niTask.AIChannels.CreateVoltageChannel(
                "Dev8/ai0",
                "Temperature",
                AITerminalConfiguration.Rse,
                1,
                5,
                AIVoltageUnits.Volts
                );
            var reader = new AnalogSingleChannelReader(niTask.Stream);

            return(PlantCalculations.CalcTemperature(reader.ReadSingleSample()));
        }
Exemplo n.º 10
0
        public double GetCurrentGain()
        {
            var       niTask      = new NationalInstruments.DAQmx.Task();
            AIChannel analogInput = niTask.AIChannels.CreateVoltageChannel(
                "Dev9/ai0",
                "Gain",
                AITerminalConfiguration.Differential,
                0,
                5,
                AIVoltageUnits.Volts
                );
            var reader = new AnalogSingleChannelReader(niTask.Stream);

            return(reader.ReadSingleSample());
        }
Exemplo n.º 11
0
        public static void InitializeInstrument()
        {
            //Initializing Task
            motorLoadTask      = new Task();
            motorControlTask   = new Task();
            motorRotationsTask = new Task();

            //Initializing channels
            //Voltage analog in
            motorLoadChannel = motorLoadTask.AIChannels.CreateVoltageChannel("dev1/ai0", "Motor Load Channel", AITerminalConfiguration.Rse, 0, 10, AIVoltageUnits.Volts);

            //Voltage analog out
            motorControlChannel = motorControlTask.AOChannels.CreateVoltageChannel("dev1/ao0", "Motor Control Channel", 0, 5, AOVoltageUnits.Volts);

            //Voltage digital in
            motorRotationsChannel = motorRotationsTask.DIChannels.CreateChannel("dev1/port0", "Motor Rotation Channel", ChannelLineGrouping.OneChannelForAllLines);
        }
Exemplo n.º 12
0
 public double Read(string input)
 {
     try
     {
         Task      analogInTask = new Task();
         AIChannel myAIChannel  = analogInTask.AIChannels.CreateVoltageChannel(
             Device + "/" + input,
             "myAIChannel",
             AITerminalConfiguration.Rse,
             Settings.Vmin,
             Settings.Vmax,
             AIVoltageUnits.Volts);
         AnalogSingleChannelReader reader = new AnalogSingleChannelReader(analogInTask.Stream);
         double analogDataIn = reader.ReadSingleSample();
         return(analogDataIn);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         return(0);
     }
 }
Exemplo n.º 13
0
        public DAQHandler(int s, int secLength)
        {
            sampleRate = s;

            // Setting up for accelerometers
            try
            {
                // Create a new task for DAQ
                dataTable = new DataTable {
                    TableName = "DAQTest"
                };;
                myTask = new NationalInstruments.DAQmx.Task("sensors");

                AIChannel[] aiChannel = new AIChannel[3];

                aiChannel[0] = myTask.AIChannels.CreateAccelerometerChannel("cDAQ1Mod1/ai0", "",
                                                                            terminalConfiguration, rangeMinimum, rangeMaximum,
                                                                            sensitvity, sensitivityUnits, excitationSource,
                                                                            excitation, AIAccelerationUnits.G);
                aiChannel[1] = myTask.AIChannels.CreateAccelerometerChannel("cDAQ1Mod1/ai1", "",
                                                                            terminalConfiguration, rangeMinimum, rangeMaximum,
                                                                            sensitvity, sensitivityUnits, excitationSource,
                                                                            excitation, AIAccelerationUnits.G);
                aiChannel[2] = myTask.AIChannels.CreateAccelerometerChannel("cDAQ1Mod1/ai2", "",
                                                                            terminalConfiguration, rangeMinimum, rangeMaximum,
                                                                            sensitvity, sensitivityUnits, excitationSource,
                                                                            excitation, AIAccelerationUnits.G);

                // Setup the input coupling
                for (int i = 0; i < 3; i++)
                {
                    aiChannel[i].Coupling = AICoupling.AC;
                }


                // Configure the timing parameters
                //myTask.Timing.ConfigureSampleClock("", sampleRate,
                //SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, samplesPerChannel);

                myTask.Timing.ConfigureSampleClock("", sampleRate,
                                                   SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 1000);

                //// Configure the Analog Trigger
                //myTask.Triggers.StartTrigger.ConfigureAnalogEdgeTrigger(triggerSource, triggerSlope, triggerLevel);
                //myTask.Triggers.StartTrigger.AnalogEdge.Hysteresis = triggerHysteresis;

                // Verify the Task
                myTask.Control(TaskAction.Verify);

                //Prepare the table for Data
                InitializeDataTable(myTask.AIChannels, ref dataTable, secLength);

                signalLength = secLength * Convert.ToInt32(sampleRate);

                runningTask    = myTask;
                reader         = new AnalogMultiChannelReader(myTask.Stream);
                analogCallback = new AsyncCallback(AnalogInCallback);

                reader.SynchronizeCallbacks = true;
                reader.BeginReadWaveform(samplesPerChannel, analogCallback, myTask);
            }

            catch (DaqException exception)
            {
                // Display Errors
                Console.WriteLine(exception.Message);
                runningTask = null;
                myTask.Dispose();
            }
        }
Exemplo n.º 14
0
    public void run()
    {
        // out
        int FIL_PREHEAT_CH = 0;
        int FIL_LIMIT_CH   = 0;
        int KV_SET_CH      = 2;
        int MA_SET_CH      = 3;

        // ramp up

        // disable DXM interlock
        ro.setPort(0x00);
        Thread.Sleep(1000);

        // set fil.preheat
        // ao.setChannel(0, 10);
        // Thread.Sleep(1000);

        // set fil.limit
        // ao.setChannel(1, 10);
        // Thread.Sleep(1000);

        // set fil.preheat and fil.limit
        //
        ao.setChannel(FIL_PREHEAT_CH, 10);
        Thread.Sleep(1000);

        // set kv
        ao.setChannel(KV_SET_CH, 10);
        Thread.Sleep(1000);

        // set ma
        ao.setChannel(MA_SET_CH, 0);
        Thread.Sleep(1000);

        // enable DXM interlock
        ro.setPort(0x01);
        Thread.Sleep(1000);

        // sample and show the voltages
        Task   task        = new Task();
        string devFullname = string.Format("{0}Mod2/ai0:2", serialNumber);

        Console.WriteLine(devFullname);
        AIChannel channel = task.AIChannels.CreateVoltageChannel(devFullname, "", AITerminalConfiguration.Rse, 0.0, 10.0, AIVoltageUnits.Volts);
        AnalogMultiChannelReader reader = new AnalogMultiChannelReader(task.Stream);

        task.Timing.ConfigureSampleClock("", rate: 100, activeEdge: SampleClockActiveEdge.Rising, sampleMode: SampleQuantityMode.ContinuousSamples, samplesPerChannel: 1000);
        task.Control(TaskAction.Verify);
        //var sw = new Stopwatch(); // debug
        for (uint i = 0; i < 1000; i++)
        {
            //sw.Reset();
            //sw.Start();
            double[] vals = reader.ReadSingleSample();
            //Console.WriteLine("data length {0}, stopwatch elapsed {1}", vals.GetLength(0), sw.Elapsed);
            //sw.Stop();
            string   kVmon1        = (vals[0] / 10.0 * 50).ToString("0.000", CultureInfo.InvariantCulture).PadLeft(8);
            string   kVmon2        = (vals[1] / 10.0 * 50).ToString("0.000", CultureInfo.InvariantCulture).PadLeft(8);
            string   mAmon         = (vals[2] / 10.0 * 17).ToString("0.000", CultureInfo.InvariantCulture).PadLeft(8);
            DateTime timestamp     = DateTime.UtcNow;
            int      unixTimestamp = (int)(timestamp.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            string   ms            = timestamp.Millisecond.ToString("000");
            string   line          = string.Format("{0}_{1}: {2}, {3}, {4}", unixTimestamp, ms, kVmon1, kVmon2, mAmon);
            if (silent == false)
            {
                Console.WriteLine(line);
            }
            if (outputFile != string.Empty)
            {
                System.IO.File.AppendAllText(outputFile, line + Environment.NewLine);
            }
        }

        // ramp down
        //
        // set ma
        ao.setChannel(MA_SET_CH, 0);
        Thread.Sleep(1000);
        //
        // set kv
        ao.setChannel(KV_SET_CH, 0);
        Thread.Sleep(1000);

        for (uint i = 0; i < 500; i++)
        {
            //sw.Reset();
            //sw.Start();
            double[] vals = reader.ReadSingleSample();
            //Console.WriteLine("data length {0}, stopwatch elapsed {1}", vals.GetLength(0), sw.Elapsed);
            //sw.Stop();
            string   kVmon1        = (vals[0] / 10.0 * 50).ToString("0.000", CultureInfo.InvariantCulture).PadLeft(8);
            string   kVmon2        = (vals[1] / 10.0 * 50).ToString("0.000", CultureInfo.InvariantCulture).PadLeft(8);
            string   mAmon         = (vals[2] / 10.0 * 17).ToString("0.000", CultureInfo.InvariantCulture).PadLeft(8);
            DateTime timestamp     = DateTime.UtcNow;
            int      unixTimestamp = (int)(timestamp.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            string   ms            = timestamp.Millisecond.ToString("000");
            string   line          = string.Format("{0}_{1}: {2}, {3}, {4}", unixTimestamp, ms, kVmon1, kVmon2, mAmon);
            if (silent == false)
            {
                Console.WriteLine(line);
            }
            if (outputFile != string.Empty)
            {
                System.IO.File.AppendAllText(outputFile, line + Environment.NewLine);
            }
        }

        //
        // disable DXM interlock
        ro.setPort(0x00);
        Thread.Sleep(1000);
    }