public void ShouldBitShiftAndMaskPushedInputData()
        {
            Converters.Clear();

            var controller = new NIDAQController();
            var s = new NIDigitalDAQInputStream("IN", controller);
            controller.SampleRate = new Measurement(10000, 1, "Hz");

            TimeSpan duration = TimeSpan.FromSeconds(0.5);

            var devices = new List<TestDevice>();

            for (ushort bitPosition = 1; bitPosition < 32; bitPosition += 2)
            {
                TestDevice dev = new TestDevice();
                dev.BindStream(s);
                s.BitPositions[dev] = bitPosition;

                devices.Add(dev);
            }

            var data = new InputData(Enumerable.Range(0, 10000).Select(i => new Measurement((short)(i % 2 * 0xaaaa), Measurement.UNITLESS)).ToList(),
                s.SampleRate, DateTime.Now);

            s.PushInputData(data);
            s.PushInputData(data);

            var expected = Enumerable.Range(0, 10000).Select(i => new Measurement(i % 2, Measurement.UNITLESS)).ToList();
            foreach (var ed in devices)
            {
                Assert.AreEqual(expected, ed.InputData[s].ElementAt(0).Data);
                Assert.AreEqual(expected, ed.InputData[s].ElementAt(1).Data);
            }
        }
예제 #2
0
        private static void OutputStreamFixture(out IList <IOutputData> data, out DAQOutputStream s, int numData)
        {
            data = new List <IOutputData>(3);

            var measurements = new List <IMeasurement>(2)
            {
                new Measurement(1, "V"), new Measurement(2, "V")
            };

            var sampleRate = new Measurement(1000, "Hz");

            for (int i = 0; i < numData; i++)
            {
                bool last = i == numData - 1 ? true : false;

                IOutputData outputData = new OutputData(measurements,
                                                        sampleRate,
                                                        last);
                data.Add(outputData);
            }


            s = new DAQOutputStream("OUT")
            {
                MeasurementConversionTarget = measurements.First().BaseUnit,
                SampleRate = sampleRate
            };

            var outData = new Dictionary <IDAQOutputStream, Queue <IOutputData> >(1);

            outData[s] = new Queue <IOutputData>(data);

            var outDevice = new TestDevice("OUT-DEVICE", outData)
            {
                Controller = new Controller(),
                MeasurementConversionTarget = "V"
            };

            s.Device = outDevice;
        }
예제 #3
0
        public void ThrowsIfDeviceOutputDataSampleRateMismatch()
        {
            IList <IOutputData> data = new List <IOutputData>(3);

            List <IMeasurement> measurements = new List <IMeasurement>(2);

            measurements.Add(new Measurement(1, "V"));
            measurements.Add(new Measurement(2, "V"));

            IMeasurement sampleRate = new Measurement(1000, "Hz");

            data.Add(new OutputData(measurements,
                                    sampleRate, false));
            data.Add(new OutputData(measurements,
                                    sampleRate, false));

            data.Add(new OutputData(measurements,
                                    sampleRate, false));


            DAQOutputStream s = new DAQOutputStream("OUT");

            s.MeasurementConversionTarget = measurements.First().BaseUnit;

            s.SampleRate = new Measurement(sampleRate.QuantityInBaseUnit * 2, "Hz");

            var outData = new Dictionary <IDAQOutputStream, Queue <IOutputData> >(1);

            outData[s] = new Queue <IOutputData>(data);

            TestDevice outDevice = new TestDevice("OUT-DEVICE", outData);

            outDevice.Controller = new Controller();
            s.Device             = outDevice;

            s.PullOutputData(new TimeSpan(100));
        }
        public void FiresStimulusOutputEvents()
        {
            var c = new TestDAQController();
            var srate = new Measurement(10, "Hz");
            var outStream = new DAQOutputStream("out")
                                {
                                    MeasurementConversionTarget = "V",
                                    SampleRate = srate
                                };
            var inStream = new DAQInputStream("in")
                               {
                                   MeasurementConversionTarget = "V",
                                   SampleRate = srate
                               };

            var outputData = new Dictionary<IDAQOutputStream, Queue<IOutputData>>();
            var dataQueue = new Queue<IOutputData>();
            var outputIOData = new OutputData(
                Enumerable.Range(0, 100).Select(i => new Measurement(i, "V")),
                new Measurement(10, "Hz")
                );

            dataQueue.Enqueue(outputIOData);

            outputData[outStream] = dataQueue;

            var dev = new TestDevice("test", outputData) { MeasurementConversionTarget = "V" };

            dev.BindStream(outStream);

            bool fired = false;
            c.StimulusOutput += (daq, args) =>
                                    {
                                        Assert.That(args.Stream == outStream);
                                        Assert.That(args.Data != null);
                                        Assert.That(args.Data.Configuration.Count() > 0);
                                        fired = true;

                                        ((IDAQController) daq).RequestStop();
                                    };

            c.AddStreamMapping(outStream, inStream);

            c.Start(false);

            while (c.IsRunning)
            {
                Thread.Sleep(1);
            }

            Assert.That(fired, Is.True);
        }
        public void ShouldPropagateOutputDataEvents()
        {
            var controllerMock = new Mock<Controller>();

            var device = new TestDevice(controllerMock.Object);

            DateTimeOffset time = DateTime.Now;
            var config = new List<IPipelineNodeConfiguration>();
            controllerMock.Setup(c => c.DidOutputData(device, time, TimeSpan.FromSeconds(0.1), config));

            device.DidOutputData(new DAQOutputStream("test"), time, TimeSpan.FromSeconds(0.1), config);

            controllerMock.VerifyAll();
        }
        public void ShouldBitShiftAndMergeBackground()
        {
            var controller = new NIDAQController();
            var s = new NIDigitalDAQOutputStream("OUT", controller);
            controller.SampleRate = new Measurement(10000, 1, "Hz");

            for (ushort bitPosition = 1; bitPosition < 32; bitPosition += 2)
            {
                TestDevice dev = new TestDevice { Background = new Measurement(1, Measurement.UNITLESS) };
                dev.BindStream(s);
                s.BitPositions[dev] = bitPosition;
            }

            ulong q = 0xaaaaaaaa;
            var expected = new Measurement((long)q, Measurement.UNITLESS);

            Assert.AreEqual(expected, s.Background);
        }
        public void ShouldBitShiftAndMergePulledOutputData()
        {
            var controller = new NIDAQController();
            var s = new NIDigitalDAQOutputStream("OUT", controller);
            controller.SampleRate = new Measurement(10000, 1, "Hz");

            TimeSpan duration = TimeSpan.FromSeconds(0.5);

            for (ushort bitPosition = 1; bitPosition < 32; bitPosition += 2)
            {
                var dataQueue = new Dictionary<IDAQOutputStream, Queue<IOutputData>>();

                dataQueue[s] = new Queue<IOutputData>();
                var data = new OutputData(Enumerable.Range(0, 10000).Select(i => new Measurement(i % 2, Measurement.UNITLESS)).ToList(),
                    s.SampleRate, false);

                dataQueue[s].Enqueue(data.SplitData(duration).Head);
                dataQueue[s].Enqueue(data.SplitData(duration).Head);

                TestDevice dev = new TestDevice("OUT-DEVICE" + bitPosition, dataQueue);
                dev.BindStream(s);
                s.BitPositions[dev] = bitPosition;
            }

            var expected = Enumerable.Range(0, 10000).Select(i => new Measurement((long)(i % 2 * 0xaaaaaaaa), Measurement.UNITLESS)).ToList();

            var pull1 = s.PullOutputData(duration);
            Assert.AreEqual(expected, pull1.Data);

            var pull2 = s.PullOutputData(duration);
            Assert.AreEqual(expected, pull2.Data);
        }
        private void FixtureForController(NIDAQController controller, double durationSeconds = 10)
        {
            controller.SampleRate = new Measurement(10000, "Hz");
            controller.InitHardware();

            OutStream = controller.OutputStreams
                                  .OfType<NIDAQOutputStream>().First(str => str.Name == "ao0");
            InputStream = controller.InputStreams
                                    .OfType<NIDAQInputStream>().First(str => str.Name == "ai0");

            InputStream.Configuration["SampleRate"] = InputStream.SampleRate;

            OutStream.Configuration["SampleRate"] = OutStream.SampleRate;

            IDAQOutputStream s = OutStream;

            var dataQueue = new Dictionary<IDAQOutputStream, Queue<IOutputData>>();

            dataQueue[s] = new Queue<IOutputData>();
            Data = new OutputData(
                Enumerable.Range(0, (int)(TimeSpan.FromSeconds(durationSeconds).Samples(controller.SampleRate)))
                    .Select(i => new Measurement(i % 10, "V")).ToList(),
                s.SampleRate,
                false);

            TimeSpan d = new TimeSpan(controller.ProcessInterval.Ticks / 2);
            var outData = (IOutputData)Data.Clone();
            while (outData.Duration > TimeSpan.Zero)
            {
                var split = outData.SplitData(d);

                dataQueue[s].Enqueue(new OutputData(split.Head, split.Rest.Duration == TimeSpan.Zero));

                outData = split.Rest;
            }

            OutDevice = new TestDevice("Output", dataQueue);
            InputDevice = new TestDevice("Input", null);

            OutDevice.MeasurementConversionTarget = "V";
            InputDevice.MeasurementConversionTarget = "V";

            BindStreams(controller, OutDevice, InputDevice);
        }
        public void PreloadShouldFeedBuffer()
        {
            /*
             * We want to keep HardwareBufferTargetDuration available in the hardware buffer.
             */

            Converters.Clear();
            HekaDAQOutputStream.RegisterConverters();

            var itcMock = new Mock<IHekaDevice>();
            StreamType channelType = StreamType.AO;
            ushort channelNumber = 0;
            var time = DateTimeOffset.Now;

            int availableSamplesStart = 1000000;
            int availableSamples = availableSamplesStart;
            //itcMock.Setup(itc => itc.AvailableSamples(channelType, channelNumber)).Returns(availableSamples);
            itcMock.Setup(itc => itc.PreloadSamples(channelType, channelNumber, It.IsAny<IList<short>>()))
                .Callback<StreamType, ushort, IList<short>>((type, number, sampleList) => availableSamples -= sampleList.Count);

            var controller = new HekaDAQController();
            HekaDAQOutputStream s = new HekaDAQOutputStream("OUT", channelType, channelNumber, controller);
            s.MeasurementConversionTarget = "V";
            controller.SampleRate = new Measurement(10000, 1, "Hz");

            var dataQueue = new Dictionary<IDAQOutputStream, Queue<IOutputData>>();

            dataQueue[s] = new Queue<IOutputData>();
            var bigData = new OutputData(Enumerable.Range(0, 100000).Select(i => new Measurement(i % 5, "V")).ToList(),
                s.SampleRate, false);

            TimeSpan bufferDuration = TimeSpan.FromSeconds(0.45);
            dataQueue[s].Enqueue(bigData.SplitData(bufferDuration).Head);
            dataQueue[s].Enqueue(bigData.SplitData(bufferDuration).Head);

            TestDevice dev = new TestDevice("OUT-DEVICE", dataQueue);
            dev.BindStream(s);
            dev.Controller = new Controller();

            s.Preload(itcMock.Object as IHekaDevice, new OutputData(dataQueue[s].Peek()));

            int expectedSamples = (int)Math.Ceiling(bufferDuration.TotalSeconds * (double)s.SampleRate.QuantityInBaseUnits);
            itcMock.VerifyAll();
            Assert.AreEqual(availableSamplesStart - expectedSamples, availableSamples); //Preload should not affect buffer availablility because PRELOAD_FIFO is used
        }
        public void SetsChannelInfo()
        {
            foreach (HekaDAQController daq in HekaDAQController.AvailableControllers())
            {
                const decimal srate = 10000;

                daq.InitHardware();
                Assert.True(daq.IsHardwareReady);
                Assert.False(daq.HardwareRunning);

                try
                {
                    foreach (IDAQOutputStream s in daq.OutputStreams)
                    {
                        daq.SampleRate = new Measurement(srate, "Hz");
                        TestDevice externalDevice = new TestDevice("OUT-DEVICE", null);

                        s.Devices.Add(externalDevice);
                    }

                    daq.ConfigureChannels();

                    foreach (HekaDAQStream s in daq.OutputStreams.Cast<HekaDAQStream>())
                    {
                        ITCMM.ITCChannelInfo actual = daq.ChannelInfo(s.ChannelType, s.ChannelNumber);

                        ITCMM.ITCChannelInfo expected = s.ChannelInfo;

                        Assert.AreEqual(expected.ChannelNumber, actual.ChannelNumber);
                        Assert.AreEqual(expected.ChannelType, actual.ChannelType);
                        Assert.AreEqual(expected.SamplingIntervalFlag, actual.SamplingIntervalFlag);
                        Assert.AreEqual(expected.SamplingRate, actual.SamplingRate);
                        // Gain set by hardware.
                    }
                }
                finally
                {
                    daq.CloseHardware();
                }
            }
        }
        public void SetsChannels()
        {
            foreach (var daq in NIDAQController.AvailableControllers())
            {
                const decimal srate = 10000;

                daq.InitHardware();
                Assert.True(daq.IsHardwareReady);
                Assert.False(daq.IsRunning);

                try
                {
                    foreach (IDAQOutputStream s in daq.OutputStreams)
                    {
                        if (s is NIDigitalDAQStream && !((NIDigitalDAQStream)s).SupportsContinuousSampling)
                            continue;

                        daq.SampleRate = new Measurement(srate, "Hz");
                        var externalDevice = new TestDevice("OUT-DEVICE", null);

                        s.Devices.Add(externalDevice);
                    }

                    daq.ConfigureChannels();

                    foreach (NIDAQStream s in daq.OutputStreams.Cast<NIDAQStream>())
                    {
                        if (s is NIDigitalDAQStream && !((NIDigitalDAQStream)s).SupportsContinuousSampling)
                            continue;

                        var chan = daq.Channel(s.PhysicalName);

                        Assert.AreEqual(s.PhysicalName, chan.PhysicalName);
                    }
                }
                finally
                {
                    daq.CloseHardware();
                }
            }
        }
예제 #12
0
        private static void OutputStreamFixture(out IList<IOutputData> data, out DAQOutputStream s, int numData)
        {
            data = new List<IOutputData>(3);

            var measurements = new List<IMeasurement>(2) {new Measurement(1, "V"), new Measurement(2, "V")};

            var sampleRate = new Measurement(1000, "Hz");
            for (int i = 0; i < numData; i++)
            {
                bool last = i == numData - 1 ? true : false;

                IOutputData outputData = new OutputData(measurements,
                                                        sampleRate,
                                                        last);
                data.Add(outputData);
            }

            s = new DAQOutputStream("OUT")
                    {
                        MeasurementConversionTarget = measurements.First().BaseUnit,
                        SampleRate = sampleRate
                    };

            var outData = new Dictionary<IDAQOutputStream, Queue<IOutputData>>(1);
            outData[s] = new Queue<IOutputData>(data);

            var outDevice = new TestDevice("OUT-DEVICE", outData)
                                {
                                    Controller = new Controller(),
                                    MeasurementConversionTarget = "V"
                                };

            s.Device = outDevice;
        }
예제 #13
0
        public void ThrowsIfDeviceOutputDataSampleRateMismatch()
        {
            IList<IOutputData> data = new List<IOutputData>(3);

            List<IMeasurement> measurements = new List<IMeasurement>(2);
            measurements.Add(new Measurement(1, "V"));
            measurements.Add(new Measurement(2, "V"));

            IMeasurement sampleRate = new Measurement(1000, "Hz");
            data.Add(new OutputData(measurements,
                sampleRate, false));
            data.Add(new OutputData(measurements,
                sampleRate, false));

            data.Add(new OutputData(measurements,
                sampleRate, false));

            DAQOutputStream s = new DAQOutputStream("OUT");
            s.MeasurementConversionTarget = measurements.First().BaseUnit;

            s.SampleRate = new Measurement(sampleRate.QuantityInBaseUnit * 2, "Hz");

            var outData = new Dictionary<IDAQOutputStream, Queue<IOutputData>>(1);
            outData[s] = new Queue<IOutputData>(data);

            TestDevice outDevice = new TestDevice("OUT-DEVICE", outData);
            outDevice.Controller = new Controller();
            s.Device = outDevice;

            s.PullOutputData(new TimeSpan(100));
        }
예제 #14
0
        public void PushesDataToDevices()
        {
            String units = "V";
            DAQInputStream s = new DAQInputStream("IN");
            s.MeasurementConversionTarget = units;

            TestDevice inDevice = new TestDevice("IN-DEVICE", null);
            inDevice.MeasurementConversionTarget = units;

            inDevice.BindStream(s);

            IList<IMeasurement> data = new List<IMeasurement>(2);
            data.Add(new Measurement(1, units));
            data.Add(new Measurement(2, units));

            IMeasurement sampleRate = new Measurement(1000, "Hz");
            DateTimeOffset time = DateTimeOffset.Now;
            IDictionary<string, object> config = null;

            IInputData inData = new InputData(data, sampleRate, time);
            s.PushInputData(inData);

            IInputData actual = inDevice.InputData[s].First();

            CollectionAssert.AreEqual(inData.Data, actual.Data);
            Assert.AreEqual(inData.InputTime, actual.InputTime);
        }
예제 #15
0
        public void PullsDataFromMultipleExternalDevices()
        {
            const int numDevices = 4;

            IList<IOutputData> data;
            DAQOutputStream s;
            OutputStreamFixture(out data, out s, 3);

            for (int i = 1; i < numDevices; i++)
            {
                var outData = new Dictionary<IDAQOutputStream, Queue<IOutputData>>(1);
                outData[s] = new Queue<IOutputData>(data);

                var outDevice = new TestDevice("OUT-DEVICE" + i, outData)
                {
                    Controller = new Controller(),
                    MeasurementConversionTarget = "V"
                };

                s.Devices.Add(outDevice);
            }

            foreach (IOutputData d in data)
            {
                var measurements = Measurement.FromArray(d.Data.Select(m => m.QuantityInBaseUnits*numDevices).ToArray(), d.Data.BaseUnits());
                var expected = new OutputData(measurements, d.SampleRate, d.IsLast);

                var actual = s.PullOutputData(TimeSpan.FromSeconds(1));
                CollectionAssert.AreEqual(expected.Data, actual.Data);
                Assert.AreEqual(expected.SampleRate, actual.SampleRate);
                if (!actual.IsLast)
                    Assert.True(s.HasMoreData);
            }
        }
        private void SetupOutputPipeline(IDAQController c, out IOutputData expectedOutput, out DAQOutputStream outStream)
        {
            string units = "V";
            var data = Enumerable.Range(0, 1000).Select(i => new Measurement(i, units)).ToList();
            var sampleRate = new Measurement(100, "Hz");
            var config = new Dictionary<string, object>();

            expectedOutput = new OutputData(data,
                sampleRate, true);

            outStream = new DAQOutputStream("OUT");
            outStream.SampleRate = sampleRate;
            outStream.MeasurementConversionTarget = units;
            (c as IMutableDAQController).AddStream(outStream);

            var outQueue = new Dictionary<IDAQOutputStream, Queue<IOutputData>>();
            outQueue[outStream] = new Queue<IOutputData>();
            outQueue[outStream].Enqueue(expectedOutput);

            TestDevice dev = new TestDevice("OUT-DEVICE", outQueue);
            dev.BindStream(outStream);
            dev.MeasurementConversionTarget = units;
        }