Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source">For example "Dev1/ctr0"</param>
        /// <param name="frequency"></param>
        public NidaqCounterOutput(string source, double frequency)
        {
            var T      = 1 / frequency;
            var t_low  = T / 2;
            var t_high = T / 2;

            _task   = new NationalInstruments.DAQmx.Task();
            _chanCo = _task.COChannels.CreatePulseChannelTime(
                source,
                "",
                NationalInstruments.DAQmx.COPulseTimeUnits.Seconds,
                NationalInstruments.DAQmx.COPulseIdleState.Low,
                0,
                t_low,
                t_high
                );

            _task.Timing.ConfigureImplicit(NationalInstruments.DAQmx.SampleQuantityMode.ContinuousSamples);

            try {
                _task.Control(NationalInstruments.DAQmx.TaskAction.Verify);
            } catch (NationalInstruments.DAQmx.DaqException e) {
                throw new NidaqException(e.Error);
            }
        }
        public void Setup_USB6008()
        {
            //Resets and configures the NI USB6008 Daq boards
            Device dev = DaqSystem.Local.LoadDevice(loc);//added to reset the DAQ boards if they fail to comunicate giving error code 50405

            dev.Reset();
            AIChannel StrainChannel, CurrentChannel;
            AOChannel LateralMotorChannel, TraverseMotorChannel;

            try
            {
                //Setting up NI DAQ for Axial Force Measurment via Strain Circuit and current Measurment of Spindle Motor for torque
                USB6008_AITask = new NationalInstruments.DAQmx.Task();

                StrainChannel = USB6008_AITask.AIChannels.CreateVoltageChannel(
                    loc + "/ai0",                         //Physical name of channel
                    "strainChannel",                      //The name to associate with this channel
                    AITerminalConfiguration.Differential, //Differential Wiring
                    -0.1,                                 //-0.1v minimum
                    NIMaxVolt,                            //1v maximum
                    AIVoltageUnits.Volts                  //Use volts
                    );
                CurrentChannel = USB6008_AITask.AIChannels.CreateVoltageChannel(
                    loc + "/ai1",                         //Physical name of channel
                    "CurrentChannel",                     //The name to associate with this channel
                    AITerminalConfiguration.Differential, //Differential Wiring
                    -0.1,                                 //-0.1v minimum
                    10,                                   //10v maximum
                    AIVoltageUnits.Volts                  //Use volts
                    );
                USB6008_Reader = new AnalogMultiChannelReader(USB6008_1_AITask.Stream);
                ////////////////////////////////////////////////////////////
                USB6008_AOTask       = new NationalInstruments.DAQmx.Task();
                TraverseMotorChannel = USB6008_AOTask.AOChannels.CreateVoltageChannel(
                    loc + "/ao0",            //Physical name of channel)
                    "TravverseMotorChannel", //The name to associate with this channel
                    0,                       //0v minimum
                    5,                       //5v maximum
                    AOVoltageUnits.Volts
                    );
                LateralMotorChannel = USB6008_AOTask.AOChannels.CreateVoltageChannel(
                    loc + "/ao1",          //Physical name of channel)
                    "LateralMotorChannel", //The name to associate with this channel
                    0,                     //0v minimum
                    5,                     //5v maximum
                    AOVoltageUnits.Volts
                    );
                USB6008_Analog_Writter = new AnalogMultiChannelWriter(USB6008_AOTask.Stream);
                ////////////////////////////////////////////////////////////
                USB6008_DOTask = new NationalInstruments.DAQmx.Task();
                USB6008_DOTask.DOChannels.CreateChannel(loc + "/port0", "port0", ChannelLineGrouping.OneChannelForAllLines);
                USB6008_Digital_Writter = new DigitalSingleChannelWriter(USB6008_DOTask.Stream);
            }
            catch (NationalInstruments.DAQmx.DaqException e)
            {
                MessageBox.Show("Error?\n\n" + e.ToString(), "NI USB 6008 1 Error");
            }
        }
        public WatchdogTimerTask(SequenceData sequence, int masterFrequency, NationalInstruments.DAQmx.Task taskToWatch,  double watchDogThresholdTime)
        {
            this.taskToWatch = taskToWatch;
            TimestepTimebaseSegmentCollection segments = sequence.generateVariableTimebaseSegments(SequenceData.VariableTimebaseTypes.AnalogGroupControlledVariableFrequencyClock,
                                         1.0 / ((double)masterFrequency));

            int currentSequenceSampleNumber = 0;
            long currentMasterSampleNumber=0;
            watchdogTimerSegments = new List<WatchdogTimerSegment>();

            int thresholdTime = (int)(masterFrequency*watchDogThresholdTime);

            for (int i = 0; i < sequence.TimeSteps.Count; i++)
            {
                TimeStep step = sequence.TimeSteps[i];
                if (step.StepEnabled)
                {

                    bool stepUsed = false;
                    if (segments.ContainsKey(step))
                    {
                        List<SequenceData.VariableTimebaseSegment> segmentList = segments[step];
                        for (int j = 0; j < segmentList.Count; j++)
                        {
                            SequenceData.VariableTimebaseSegment currentSegment = segmentList[j];

                            if (currentSegment.MasterSamplesPerSegmentSample >= thresholdTime)
                            {
                                if (!stepUsed)
                                {
                                    WatchdogTimerSegment wseg = new WatchdogTimerSegment();
                                    wseg.timeStepName = step.StepName;
                                    wseg.timeStepID = i;
                                    wseg.sequenceSampleNumber = currentSequenceSampleNumber + 1;
                                    wseg.masterSampleNumber = currentMasterSampleNumber + thresholdTime / 2;
                                    stepUsed = true;

                                    watchdogTimerSegments.Add(wseg);
                                }
                            }

                            currentSequenceSampleNumber += currentSegment.NSegmentSamples;
                            currentMasterSampleNumber += currentSegment.NSegmentSamples * currentSegment.MasterSamplesPerSegmentSample;
                        }

                    }
                }

            }
        }
        public void CreateTask(IEnumerable <INidaqMetric> nodes)
        {
            if (!nodes.All(n => n is MetricDigitalOutput))
            {
                throw new InvalidCastException("all passed nodes must be of type MetricDigitalOutput");
            }

            if (!nodes.All(n => n.Channel.Device == Device))
            {
                throw new InvalidOperationException("not all passed nodes are connected to device " + Device.Name);
            }

            ClockRate = ((MetricDigitalOutput)nodes.First()).Samplerate;

            if (!nodes.All(n => ((MetricDigitalOutput)n).Samplerate == ClockRate))
            {
                ClockRate         = 0;
                SamplesPerChannel = 0;
                throw new InvalidOperationException("NIDAQ: not all digital output nodes in graph have the same samplerate");
            }

            foreach (var n in nodes.OfType <MetricDigitalOutput>())
            {
                n.Samplerate = ClockRate;
            }

            var nidaqBufferSizePerChannel = (int)(BufferLengthMs * (long)ClockRate / 1000);
            var localBufferSizePerChannel = (int)(2 * (BufferLengthMs + PrebufferLengthMs) * (long)ClockRate / 1000);

            SamplesPerChannel = nidaqBufferSizePerChannel;

            // ------------------------------
            // Configure DAQMX Task

            _task = new NationalInstruments.DAQmx.Task();
            foreach (var output in nodes.OfType <MetricDigitalOutput>())
            {
                output.ChannelNumber = _nodes.Count;

                _task.DOChannels.CreateChannel(
                    output.Channel.Path,
                    string.Empty,
                    NationalInstruments.DAQmx.ChannelLineGrouping.OneChannelForEachLine
                    );

                _nodes.Add(output);
            }

            _task.Timing.ConfigureSampleClock(
                ClockPath,
                ClockRate,
                NationalInstruments.DAQmx.SampleClockActiveEdge.Rising,
                NationalInstruments.DAQmx.SampleQuantityMode.ContinuousSamples,
                SamplesPerChannel
                );

            try {
                _task.Stream.ConfigureOutputBuffer(SamplesPerChannel);
            } catch (NationalInstruments.DAQmx.DaqException e) {
                throw new NidaqException(e.Error);
            }

            _task.Stream.Timeout = WriteTimeoutMs;
            _task.Stream.WriteRegenerationMode = NationalInstruments.DAQmx.WriteRegenerationMode.DoNotAllowRegeneration;

            try {
                _task.Control(NationalInstruments.DAQmx.TaskAction.Verify);
            } catch (NationalInstruments.DAQmx.DaqException e) {
                throw new NidaqException(e.Error);
            }

            _task.Stream.Buffer.OutputBufferSize = SamplesPerChannel;

            _writer = new NationalInstruments.DAQmx.DigitalMultiChannelWriter(_task.Stream);

            TaskHandle = _task.GetHashCode();

            _usableData   = new int[_nodes.Count, SamplesPerChannel];
            _bufferData   = new int[_nodes.Count, localBufferSizePerChannel];
            _channelCount = new int[_nodes.Count];

            State  = SessionTaskState.Stopped;
            _first = true;
        }