Esempio n. 1
0
        static void MagnetometerCalibrationDeepDive(int calibrationCount)
        {
            var     mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Mpu9250.DefaultI2cAddress);
            Mpu9250 mpu9250 = new Mpu9250(I2cDevice.Create(mpui2CConnectionSettingmpus));

            mpu9250.MagnetometerOutputBitMode   = Iot.Device.Magnetometer.OutputBitMode.Output16bit;
            mpu9250.MagnetometerMeasurementMode = Iot.Device.Magnetometer.MeasurementMode.ContinuousMeasurement100Hz;
            Console.WriteLine("Please move the magnetometer during calibration");
            using (var ioWriter = new StreamWriter("mag.csv"))
            {
                // First we read the data without calibration at all
                Console.WriteLine("Reading magnetometer data without calibration");
                ioWriter.WriteLine($"X;Y;Z");
                for (int i = 0; i < calibrationCount; i++)
                {
                    try
                    {
                        var magne = mpu9250.ReadMagnetometerWithoutCorrection();
                        ioWriter.WriteLine($"{magne.X};{magne.Y};{magne.Z}");
                        // 10 ms = 100Hz, so waiting to make sure we have new data
                        Thread.Sleep(10);
                    }
                    catch (TimeoutException)
                    {
                        Console.WriteLine("Error reading");
                    }
                }
                Console.WriteLine("Performing calibration");
                // then we calibrate
                var magnetoBias = mpu9250.CalibrateMagnetometer(calibrationCount);
                ioWriter.WriteLine();
                ioWriter.WriteLine("Factory calibration data");
                ioWriter.WriteLine($"X;Y;Z");
                ioWriter.WriteLine($"{magnetoBias.X};{magnetoBias.Y};{magnetoBias.Z}");
                ioWriter.WriteLine();
                ioWriter.WriteLine("Magnetometer bias calibration data");
                ioWriter.WriteLine($"X;Y;Z");
                ioWriter.WriteLine($"{mpu9250.MagnometerBias.X};{mpu9250.MagnometerBias.Y};{mpu9250.MagnometerBias.Z}");
                ioWriter.WriteLine();
                // Finally we read the data again
                Console.WriteLine("Reading magnetometer data including calibration");
                ioWriter.WriteLine($"X corr;Y corr;Z corr");
                for (int i = 0; i < calibrationCount; i++)
                {
                    try
                    {
                        var magne = mpu9250.ReadMagnetometer();
                        ioWriter.WriteLine($"{magne.X};{magne.Y};{magne.Z}");
                        // 10 ms = 100Hz, so waiting to make sure we have new data
                        Thread.Sleep(10);
                    }
                    catch (TimeoutException)
                    {
                        Console.WriteLine("Error reading");
                    }
                }
            }
            Console.WriteLine("Calibration deep dive over, file name is mag.csv");
        }
Esempio n. 2
0
        private async void Selector_OnBoardConnected(object sender, Treehopper.Mvvm.BoardConnectedEventArgs e)
        {
            var imu = new Mpu9250(e.Board.I2c);

            imu.EnableMagnetometer = false;
            await imu.Calibrate();

            filter = new ComplementaryFilter(imu, imu, 5, true);
            filter.FilterUpdate += Filter_FilterUpdate;
        }
        private Mpu9250 GetOrCreateMPU9250()
        {
            return(_imu ??= CreateAndCalibrate());

            Mpu9250 CreateAndCalibrate()
            {
                var device = new Mpu9250(GetOrCreateMcu().I2c);

                device.CalibrateMagnetometer();
                device.CalibrateGyroscopeAccelerometer();
                return(device);
            }
        }
Esempio n. 4
0
        static async Task App()
        {
            var board = await ConnectionService.Instance.GetFirstDeviceAsync();

            await board.ConnectAsync();

            Mpu9250 imu;

            while (true)
            {
                var imuList = await Mpu9250.ProbeAsync(board.I2c); // find the first MPU9250 attached to the bus

                if (imuList.Count == 0)
                {
                    Console.WriteLine("No MPU9250 attached (Are you sure you're not using an MPU6050?). Press any key to try again.");
                    Console.ReadKey();
                }
                else
                {
                    imu = imuList[0];
                    break;
                }
            }

            imu.AutoUpdateWhenPropertyRead = false;
            while (!Console.KeyAvailable)
            {
                await imu.UpdateAsync().ConfigureAwait(false);

                Console.WriteLine($"Temperature: {imu.Celsius:0.00} °C ({imu.Fahrenheit:0.00} °F)");
                Console.WriteLine($"IMU: {imu.Accelerometer.X:0.00}, {imu.Accelerometer.Y:0.00}, {imu.Accelerometer.Z:0.00}");
                Console.WriteLine($"Gyro: {imu.Gyroscope.X:0.00}, {imu.Gyroscope.Y:0.00}, {imu.Gyroscope.Z:0.00}");
                Console.WriteLine($"Magnetometer: {imu.Magnetometer.X:0.00}, {imu.Magnetometer.Y:0.00}, {imu.Magnetometer.Z:0.00}");
                await Task.Delay(500);
            }

            board.Disconnect();
        }
Esempio n. 5
0
        public static void MainTest()
        {
            var     mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Mpu9250.DefaultI2cAddress);
            Mpu9250 mpu9250 = new Mpu9250(I2cDevice.Create(mpui2CConnectionSettingmpus));

            Console.WriteLine($"Check version magnetometer: {mpu9250.GetMagnetometerVersion()}");
            Console.WriteLine(
                "Magnetometer calibration is taking couple of seconds, please be patient! Please make sure you are not close to any magnetic field like magnet or phone.");
            Console.WriteLine(
                "Please move your sensor as much as possible in all direction in space to get as many points in space as possible");
            var mag = mpu9250.CalibrateMagnetometer();

            Console.WriteLine($"Hardware bias multiplicative:");
            Console.WriteLine($"Mag X = {mag.X}");
            Console.WriteLine($"Mag Y = {mag.Y}");
            Console.WriteLine($"Mag Z = {mag.Z}");
            Console.WriteLine($"Calculated corrected bias:");
            Console.WriteLine($"Mag X = {mpu9250.MagnometerBias.X}");
            Console.WriteLine($"Mag Y = {mpu9250.MagnometerBias.Y}");
            Console.WriteLine($"Mag Z = {mpu9250.MagnometerBias.Z}");

            var resSelfTest = mpu9250.RunGyroscopeAccelerometerSelfTest();

            Console.WriteLine($"Self test:");
            Console.WriteLine($"Gyro X = {resSelfTest.Item1.X} vs >0.005");
            Console.WriteLine($"Gyro Y = {resSelfTest.Item1.Y} vs >0.005");
            Console.WriteLine($"Gyro Z = {resSelfTest.Item1.Z} vs >0.005");
            Console.WriteLine($"Acc X = {resSelfTest.Item2.X} vs >0.005 & <0.015");
            Console.WriteLine($"Acc Y = {resSelfTest.Item2.Y} vs >0.005 & <0.015");
            Console.WriteLine($"Acc Z = {resSelfTest.Item2.Z} vs >0.005 & <0.015");
            Console.WriteLine("Running Gyroscope and Accelerometer calibration");
            mpu9250.CalibrateGyroscopeAccelerometer();
            Console.WriteLine("Calibration results:");
            Console.WriteLine($"Gyro X bias = {mpu9250.GyroscopeBias.X}");
            Console.WriteLine($"Gyro Y bias = {mpu9250.GyroscopeBias.Y}");
            Console.WriteLine($"Gyro Z bias = {mpu9250.GyroscopeBias.Z}");
            Console.WriteLine($"Acc X bias = {mpu9250.AccelerometerBias.X}");
            Console.WriteLine($"Acc Y bias = {mpu9250.AccelerometerBias.Y}");
            Console.WriteLine($"Acc Z bias = {mpu9250.AccelerometerBias.Z}");
            Console.WriteLine("Press a key to continue");
            var readKey = Console.ReadKey();

            mpu9250.GyroscopeBandwidth     = GyroscopeBandwidth.Bandwidth0250Hz;
            mpu9250.AccelerometerBandwidth = AccelerometerBandwidth.Bandwidth0460Hz;
            Console.Clear();

            while (!Console.KeyAvailable)
            {
                Console.CursorTop = 0;
                var gyro = mpu9250.GetGyroscopeReading();
                Console.WriteLine($"Gyro X = {gyro.X,15}");
                Console.WriteLine($"Gyro Y = {gyro.Y,15}");
                Console.WriteLine($"Gyro Z = {gyro.Z,15}");
                var acc = mpu9250.GetAccelerometer();
                Console.WriteLine($"Acc X = {acc.X,15}");
                Console.WriteLine($"Acc Y = {acc.Y,15}");
                Console.WriteLine($"Acc Z = {acc.Z,15}");
                Console.WriteLine($"Temp = {mpu9250.GetTemperature().Celsius.ToString("0.00")} °C");
                var magne = mpu9250.ReadMagnetometer();
                Console.WriteLine($"Mag X = {magne.X,15}");
                Console.WriteLine($"Mag Y = {magne.Y,15}");
                Console.WriteLine($"Mag Z = {magne.Z,15}");
                Thread.Sleep(100);
            }

            readKey = Console.ReadKey();
            // SetWakeOnMotion
            mpu9250.SetWakeOnMotion(300, AccelerometerLowPowerFrequency.Frequency0Dot24Hz);
            // You'll need to attach the INT pin to a GPIO and read the level. Once going up, you have
            // some data and the sensor is awake
            // In order to simulate this without a GPIO pin, you will see that the refresh rate is very low
            // Setup here at 0.24Hz which means, about every 4 seconds
            Console.Clear();

            while (!Console.KeyAvailable)
            {
                Console.CursorTop = 0;
                var acc = mpu9250.GetAccelerometer();
                Console.WriteLine($"Acc X = {acc.X,15}");
                Console.WriteLine($"Acc Y = {acc.Y,15}");
                Console.WriteLine($"Acc Z = {acc.Z,15}");
                Thread.Sleep(100);
            }
        }
        public Result <Unit> Init()
        {
            _controller = new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver());
            _controller.OpenPin(Pin.HallBottom, PinMode.InputPullUp);
            _controller.OpenPin(Pin.HallTop, PinMode.InputPullUp);
            _controller.OpenPin(Pin.PhotoelectricBarrier, PinMode.InputPullUp);
            _controller.OpenPin(Pin.MotorEnable, PinMode.Output);
            _controller.OpenPin(Pin.MotorLeft, PinMode.Output);
            _controller.OpenPin(Pin.MotorRight, PinMode.Output);
            _controller.OpenPin(Pin.EmergencyTop, PinMode.InputPullUp);
            _controller.OpenPin(Pin.DC12_1, PinMode.Output);
            _controller.OpenPin(Pin.DC12_2, PinMode.Output);

            //_pwmMotor = new SoftwarePwmChannel(Pin.MotorEnable, 200, 0.1);
            //_pwmMotor.Start();

            _controller.Write(Pin.MotorEnable, PinValue.High);

            _controller.Write(Pin.MotorLeft, PinValue.Low);
            _controller.Write(Pin.MotorRight, PinValue.Low);
            _controller.Write(Pin.DC12_1, PinValue.Low);
            _controller.Write(Pin.DC12_2, PinValue.Low);


            Console.WriteLine($"Init sensor");

            _bh1750Fvi = new Bh1750fvi(I2cDevice.Create(new I2cConnectionSettings(1, Bh1750fviExtenstion.DefaultI2cAddress))); // 23

            _vl53L0X = new Vl53L0X(I2cDevice.Create(new I2cConnectionSettings(1, Vl53L0X.DefaultI2cAddress)));                 // 29

            _bme280 = new Bme280(I2cDevice.Create(new I2cConnectionSettings(1, Bmx280Base.SecondaryI2cAddress)));              // 76

            _measurementTime = _bme280.GetMeasurementDuration();
            _bme280.SetPowerMode(Bmx280PowerMode.Normal);
            //Thread.Sleep(_measurementTime);

            //_bme280.TryReadTemperature(out var tempValue);
            //_bme280.TryReadPressure(out var preValue);
            //_bme280.TryReadHumidity(out var humValue);
            //_bme280.TryReadAltitude(out var altValue);

            //Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C");
            //Console.WriteLine($"Pressure: {preValue.Hectopascals:#.##} hPa");
            //Console.WriteLine($"Relative humidity: {humValue.Percent:#.##}%");
            //Console.WriteLine($"Estimated altitude: {altValue.Meters:#} m");

            _amg88xx = new Amg88xx(I2cDevice.Create(new I2cConnectionSettings(1, Amg88xx.AlternativeI2cAddress))); // 69

            try
            {
                _mpu9250 = new Mpu9250(I2cDevice.Create(new I2cConnectionSettings(1, Mpu6500.DefaultI2cAddress))); // 68
            }
            catch (IOException e)
            {
                Console.WriteLine("AK8963 is exposed, try to connect directly.");
                _mpu9250 = new Mpu9250(I2cDevice.Create(new I2cConnectionSettings(1, Mpu6500.DefaultI2cAddress)), i2CDeviceAk8963: I2cDevice.Create(new I2cConnectionSettings(1, Ak8963.DefaultI2cAddress)));
            }

            _mpu9250.MagnetometerMeasurementMode = MeasurementMode.ContinuousMeasurement100Hz;

            Thread.Sleep(100);


            Console.WriteLine($"Finished Init sensor");

            Run();

            Thread.Sleep(100);

            Calibrate();

            return(Unit.Instance);
        }