Exemplo n.º 1
0
        /// <summary>
        /// Main entry point
        /// </summary>
        public static void Main(string[] args)
        {
            I2cConnectionSettings mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Ak8963.DefaultI2cAddress);
            var ak8963 = new Ak8963(I2cDevice.Create(mpui2CConnectionSettingmpus));

            Console.WriteLine(
                "Magnetometer calibration is taking couple of seconds, move your sensor in all possible directions! Make sure you don't have a magnet or phone close by.");
            var mag = ak8963.CalibrateMagnetometer();

            Console.WriteLine($"Bias:");
            Console.WriteLine($"Mag X = {mag.X}");
            Console.WriteLine($"Mag Y = {mag.Y}");
            Console.WriteLine($"Mag Z = {mag.Z}");
            Console.WriteLine("Press a key to continue");
            var readKey = Console.ReadKey();

            Console.Clear();
            while (!Console.KeyAvailable)
            {
                var magne = ak8963.ReadMagnetometer(true, TimeSpan.FromMilliseconds(11));
                Console.WriteLine($"Mag X = {magne.X,15}");
                Console.WriteLine($"Mag Y = {magne.Y,15}");
                Console.WriteLine($"Mag Z = {magne.Z,15}");
                Thread.Sleep(200);
            }

            readKey = Console.ReadKey();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize the MPU9250
        /// </summary>
        /// <param name="i2cDevice">The I2C device</param>
        /// <param name="autoDispose">Will automatically dispose the I2C device if true</param>
        public Mpu9250(I2cDevice i2cDevice, bool autoDispose = true)
            : base()
        {
            _i2cDevice = i2cDevice;
            Reset();
            PowerOn();
            if (!CheckVersion())
            {
                throw new IOException($"This device does not contain the correct signature 0x71 for a MPU9250");
            }

            GyroscopeBandwidth     = GyroscopeBandwidth.Bandwidth0250Hz;
            GyroscopeRange         = GyroscopeRange.Range0250Dps;
            AccelerometerBandwidth = AccelerometerBandwidth.Bandwidth1130Hz;
            AccelerometerRange     = AccelerometerRange.Range02G;
            // Setup I2C for the AK8963
            WriteRegister(Register.USER_CTRL, (byte)UserControls.I2C_MST_EN);
            // Speed of 400 kHz
            WriteRegister(Register.I2C_MST_CTRL, (byte)I2cBusFrequency.Frequency400kHz);
            _autoDispose = autoDispose;
            _ak8963      = new Ak8963(i2cDevice, new Ak8963Attached(), false);
            if (!_ak8963.IsVersionCorrect())
            {
                // Try to reset the device first
                _ak8963.Reset();
                // Wait a bit
                if (!_ak8963.IsVersionCorrect())
                {
                    // Try to reset the I2C Bus
                    WriteRegister(Register.USER_CTRL, (byte)UserControls.I2C_MST_RST);
                    Thread.Sleep(100);
                    // Resetup again
                    WriteRegister(Register.USER_CTRL, (byte)UserControls.I2C_MST_EN);
                    WriteRegister(Register.I2C_MST_CTRL, (byte)I2cBusFrequency.Frequency400kHz);
                    // Poorly documented time to wait after the I2C bus reset
                    // Found out that waiting a little bit is needed. Exact time may be lower
                    Thread.Sleep(100);
                    // Try one more time
                    if (!_ak8963.IsVersionCorrect())
                    {
                        throw new IOException($"This device does not contain the correct signature 0x48 for a AK8963 embedded into the MPU9250");
                    }
                }
            }

            _ak8963.MeasurementMode = MeasurementMode.SingleMeasurement;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the MPU9250
        /// </summary>
        /// <param name="i2cDevice">The I2C device</param>
        /// <param name="autoDispose">Will automatically dispose the I2C device if true</param>
        /// <param name="i2CDeviceAk8963">An I2C Device for the AK8963 when exposed and not behind the MPU9250</param>
        public Mpu9250(I2cDevice i2cDevice, bool autoDispose = true, I2cDevice?i2CDeviceAk8963 = null)
            : base(i2cDevice, true)
        {
            Reset();
            PowerOn();
            if (!CheckVersion())
            {
                throw new IOException($"This device does not contain the correct signature 0x71 for a MPU9250");
            }

            GyroscopeBandwidth     = GyroscopeBandwidth.Bandwidth0250Hz;
            GyroscopeRange         = GyroscopeRange.Range0250Dps;
            AccelerometerBandwidth = AccelerometerBandwidth.Bandwidth1130Hz;
            AccelerometerRange     = AccelerometerRange.Range02G;
            // Setup I2C for the AK8963
            WriteRegister(Register.USER_CTRL, (byte)UserControls.I2C_MST_EN);
            // Speed of 400 kHz
            WriteRegister(Register.I2C_MST_CTRL, (byte)I2cBusFrequency.Frequency400kHz);
            _autoDispose = autoDispose;
            // There are 2 options to setup the Ak8963. Either the I2C address is exposed, either not.
            // Trying both and pick one of them
            if (i2CDeviceAk8963 == null)
            {
                try
                {
                    _ak8963 = new Ak8963(i2cDevice, new Ak8963Attached(), false);
                }
                catch (IOException ex)
                {
                    throw new IOException($"Please try to create an I2cDevice for the AK8963, it may be exposed", ex);
                }
            }
            else
            {
                _ak8963 = new Ak8963(i2CDeviceAk8963);
            }

            if (!_ak8963.IsVersionCorrect())
            {
                // Try to reset the device first
                _ak8963.Reset();
                // Wait a bit
                if (!_ak8963.IsVersionCorrect())
                {
                    // Try to reset the I2C Bus
                    WriteRegister(Register.USER_CTRL, (byte)UserControls.I2C_MST_RST);
                    Thread.Sleep(100);
                    // Resetup again
                    WriteRegister(Register.USER_CTRL, (byte)UserControls.I2C_MST_EN);
                    WriteRegister(Register.I2C_MST_CTRL, (byte)I2cBusFrequency.Frequency400kHz);
                    // Poorly documented time to wait after the I2C bus reset
                    // Found out that waiting a little bit is needed. Exact time may be lower
                    Thread.Sleep(100);
                    // Try one more time
                    if (!_ak8963.IsVersionCorrect())
                    {
                        throw new IOException($"This device does not contain the correct signature 0x48 for a AK8963 embedded into the MPU9250");
                    }
                }
            }

            _ak8963.MeasurementMode = MeasurementMode.SingleMeasurement;
        }