Ejemplo n.º 1
0
        /// <summary>
        /// Resets the device, updates PROM data and clears current measurements
        /// </summary>
        public virtual void Reset()
        {
            // Send reset command
            Hardware.WriteJoinByte((byte)Ms5611Command.Reset, 0);

            // Wait for completion
            Task.Delay(TimeSpanExtensions.FromMicroseconds(ResetTime)).Wait();

            // Update PROM values
            ReadProm();

            // Clear measurement
            Measurement = Ms5611Measurement.Zero;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Resets the device, updates PROM data and clears current measurements.
        /// </summary>
        public void Reset()
        {
            // Send reset command
            I2cExtensions.WriteJoinByte(_hardware, (byte)Ms5611Command.Reset, 0);

            // Wait for completion
            Task.Delay(TimeSpanExtensions.FromMicroseconds(ResetTime)).Wait();

            // Update PROM values
            ReadProm();

            // Clear measurements
            Pressure    = 0;
            Temperature = 0;
        }
Ejemplo n.º 3
0
        public void TestFromMethods()
        {
            if (TimeSpanExtensions.FromNanoseconds(0).Ticks != System.TimeSpan.Zero.Ticks)
            {
                throw new System.Exception("FromNanoseconds");
            }

            if (TimeSpanExtensions.FromNanoseconds(10).Ticks != System.TimeSpan.Zero.Ticks)
            {
                throw new System.Exception("FromNanoseconds");
            }

            if (TimeSpanExtensions.FromNanoseconds(99).Ticks != System.TimeSpan.Zero.Ticks)
            {
                throw new System.Exception("FromNanoseconds");
            }

            if (TimeSpanExtensions.FromNanoseconds(100).Ticks != TimeSpanExtensions.OneTick.Ticks)
            {
                throw new System.Exception("FromNanoseconds");
            }

            if (TimeSpanExtensions.FromNanoseconds(200).Ticks != 2)
            {
                throw new System.Exception("FromNanoseconds");
            }

            if (TimeSpanExtensions.FromMicroseconds(1).Ticks != TimeSpanExtensions.OneMicrosecond.Ticks)
            {
                throw new System.Exception("FromMicroseconds");
            }

            if (TimeSpanExtensions.FromMicroseconds(0.1) != TimeSpanExtensions.OneTick)
            {
                throw new System.Exception("FromMicroseconds");
            }

            if (TimeSpanExtensions.TotalNanoseconds(TimeSpanExtensions.OneMicrosecond) != TimeSpanExtensions.NanosecondsPerMicrosecond)
            {
                throw new System.Exception("TotalNanoseconds");
            }

            if (TimeSpanExtensions.TotalMicroseconds(TimeSpanExtensions.OneMicrosecond) != 1)
            {
                throw new System.Exception("TotalMicroseconds");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Waits for conversion at the specified OSR.
        /// </summary>
        /// <param name="rate">Over-Sampling Rate to wait for.</param>
        protected virtual void WaitForConversion(Ms5611Osr rate)
        {
            var delay = TimeSpanExtensions.FromMicroseconds(GetConvertDelay(rate));

            Task.Delay(delay).Wait();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sends a request packet, checks the returns the response.
        /// </summary>
        /// <param name="request">Request packet with <see cref="Px4ioPacket.Crc"/> calculated.</param>
        /// <returns>Response packet with data read and validated after request.</returns>
        /// <exception cref="FormatException">Thrown when corruption occurred during send or receive.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the request was rejected in error.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the returned register count doesn't match the requested count.</exception>
        public Px4ioPacket Transfer(Px4ioPacket request)
        {
            // Validate
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // Wait a bit
            // TODO: Wait for data ready interrupt from STM32 before reading.
            Task.Delay(TimeSpanExtensions.FromMicroseconds(150)).Wait();

            // Write request to SPI bus
            var writeBuffer = request.ToByteArray();

            _hardware.Write(writeBuffer);

            // Wait a bit
            // TODO: Wait for data ready interrupt from STM32 before reading.
            Task.Delay(TimeSpanExtensions.FromMicroseconds(150)).Wait();

            // Read response from SPI bus
            var readBuffer = new byte[Px4ioPacket.Size];

            _hardware.Read(readBuffer);

            // Check and return response when valid
            var response = new Px4ioPacket(readBuffer);

            switch ((Px4ioResponseCode)response.Code)
            {
            case Px4ioResponseCode.Corrupt:
            {
                // Rejected by RCIO as corrupt (CRC mismatch)
                throw new FormatException(Resources.Strings.Px4ioPacketCorruptTransmit);
            }

            case Px4ioResponseCode.Error:
            {
                // Rejected by RCIO as error (request specific)
                throw new InvalidOperationException(Resources.Strings.Px4ioPacketError);
            }

            case Px4ioResponseCode.Success:
            {
                // Successful response

                // Check CRC
                if (!response.ValidateCrc())
                {
                    throw new FormatException(Resources.Strings.Px4ioPacketCorruptReceive);
                }

                // Return valid data
                return(response);
            }

            default:
            {
                // Corrupt or unsupported response code
                throw new FormatException(Resources.Strings.Px4ioPacketCorruptReceive);
            }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Waits for conversion at the specified OSR.
        /// </summary>
        /// <param name="rate">Over-Sampling Rate to wait for.</param>
        private static void WaitForConversion(Ms5611Osr rate)
        {
            var delay = TimeSpanExtensions.FromMicroseconds(GetConvertDelay(rate));

            Task.Delay(delay).Wait();
        }
        public void TestInterframeGap()
        {
            long speedInBitsPerSecond;

            double gapNanos;

            double gapMicros;

            System.TimeSpan microTime;

            //Todo, method with various types of interfaces
            foreach (var nif in NetworkInterfaceExtensions.GetNetworkInterface(System.Net.NetworkInformation.NetworkInterfaceType.Ethernet, System.Net.NetworkInformation.NetworkInterfaceType.Wireless80211))
            {
                //Speed changes for Wifi between calls but speed always shows 100000 would need interop to ask stack what the current speed or max speed is...
                speedInBitsPerSecond = nif.Speed;

                //Calulcate the gap in Nanoseconds
                gapNanos = NetworkInterfaceExtensions.GetInterframeGapNanoseconds(nif);

                //Caulcate the same gap but in Microseconds
                gapMicros = NetworkInterfaceExtensions.GetInterframeGapMicroseconds(nif);

                //Calulcate a TimeSpan which represents the total Microseconds.
                microTime = TimeSpanExtensions.FromMicroseconds((double)gapMicros);

                //Rounding ...
                //if (TimeSpanExtensions.TotalMicroseconds(microTime) != gapMicros) throw new System.Exception("TotalMicroseconds");

                //Verify that the conversion was correct
                if ((int)TimeSpanExtensions.TotalMicroseconds(microTime) != (int)gapMicros)
                {
                    throw new System.Exception("TotalMicroseconds");
                }

                //Calculate how much difference there is when converting from the microsecond term to the nano second term.
                double diff = gapMicros * TimeSpanExtensions.NanosecondsPerMicrosecond - TimeSpanExtensions.TotalNanoseconds(microTime);

                //If there was any difference
                if (diff > 0)
                {
                    //if that difference is greater than 1 nano second throw an exception
                    if (diff > NetworkInterfaceExtensions.MinimumInterframeGapBits)
                    {
                        throw new System.Exception("TotalNanoseconds");
                    }
                    else
                    {
                        System.Console.WriteLine("μs to ns conversion Different By: " + diff);
                    }
                }

                //Write the information
                System.Console.WriteLine(string.Format("Name: {0}, Type: {1}, \r\nSpeed Bits Per Second: {2}, \r\nGapMicros:{3} GapNanos{4}, MicroTime:{5}", nif.Name, nif.NetworkInterfaceType, speedInBitsPerSecond, gapMicros, gapNanos, microTime));

                System.Console.WriteLine("Speed In MBytes Per Second: " + NetworkInterfaceExtensions.GetSpeedInMBytesPerSecond(nif));

                //Verify results
                switch (speedInBitsPerSecond)
                {
                //1Gpbs
                case 10000000000:
                {
                    if (gapNanos != 9.6)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //1Gpbs
                case 1000000000:
                {
                    if (gapNanos != 96)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //100 Mbps
                case 100000000:
                {
                    if (gapNanos != 960)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //10 Mbps
                case 10000000:
                {
                    if (gapNanos != 9600)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //1 Mbps
                case 1000000:
                {
                    if (gapNanos != 96000)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }
                }
            }

            ////Todo, fix overflow for all speeds.
            //for (int i = 1; i <= 1000000000; ++i)
            //{
            //    speedInBitsPerSecond = i * 100;

            //    gapNanos = NetworkInterfaceExtensions.CaulculateInterframeGapNanoseconds(speedInBitsPerSecond);

            //    gapMicros = (long)(gapNanos * TimeSpanExtensions.NanosecondsPerMicrosecond);

            //    microTime = TimeSpanExtensions.FromMicroseconds((double)gapMicros);

            //    if ((int)TimeSpanExtensions.TotalMicroseconds(microTime) != (int)gapMicros) throw new System.Exception("TotalMicroseconds");

            //    //Calculate how much difference there is when converting from the microsecond term to the nano second term.
            //    double diff = gapMicros * TimeSpanExtensions.NanosecondsPerMicrosecond - TimeSpanExtensions.TotalNanoseconds(microTime);

            //    if (diff > 0)
            //    {
            //        //if that difference is greater than 1 nano second throw an exception
            //        if (diff > TimeSpanExtensions.NanosecondsPerTick) throw new System.Exception("TotalNanoseconds");
            //        else System.Console.WriteLine("μs to ns conversion Different By: " + diff);
            //    }

            //    System.Console.WriteLine(string.Format("Name{0}, Type: {1}, Speed: {2}, GapMicros:{3} GapNanos{4}, MicroTime:{5}", "N/A", "N/A", speedInBitsPerSecond, gapMicros, gapNanos, microTime));

            //    System.Console.WriteLine("Speed In MBytes Per Second: " + speedInBitsPerSecond / TimeSpanExtensions.NanosecondsPerMillisecond);
            //}
        }