Beispiel #1
0
 private void  Timer_Tick(object sender, object e)
 {
     /* Display the results (acceleration is measured in m/s^2) */
     accel.getEvent(ref Accelevent);
     Debug.WriteLine("ACCEL  ");
     Debug.WriteLine("X: " + Accelevent.acceleration.x + "  " + "Y: " + Accelevent.acceleration.y + "  " + "Z: " + Accelevent.acceleration.z + "  m/s^2");
     AccelXaxis.Text = Accelevent.acceleration.x.ToString();
     AccelYaxis.Text = Accelevent.acceleration.y.ToString();
     AccelZaxis.Text = Accelevent.acceleration.z.ToString();
     /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
     mag.getEvent(ref Magevent);
     Debug.WriteLine("MAG  ");
     Debug.WriteLine("X: " + Magevent.magnetic.x + "  " + "Y: " + Magevent.magnetic.y + "  " + "Z: " + Magevent.magnetic.z + "  uT");
     MagXaxis.Text = Magevent.magnetic.x.ToString();
     MagYaxis.Text = Magevent.magnetic.y.ToString();
     MagZaxis.Text = Magevent.magnetic.z.ToString();
     /* Display the results (gyrocope values in rad/s) */
     gyro.getEvent(ref Gyroevent);
     Debug.WriteLine("GYRO  ");
     Debug.WriteLine("X: " + Gyroevent.gyro.x + "  " + "Y: " + Gyroevent.gyro.y + "  " + "Z: " + Gyroevent.gyro.z + "  rad/s");
     GyroXaxis.Text = Gyroevent.gyro.x.ToString();
     GyroYaxis.Text = Gyroevent.gyro.y.ToString();
     GyroZaxis.Text = Gyroevent.gyro.z.ToString();
     /* Display the pressure sensor results (barometric pressure is measure in hPa) */
     bmp.getEvent(ref Pressevent);
     if (Pressevent.pressure != 0)
     {
         /* Display atmospheric pressure in hPa */
         Debug.WriteLine("PRESS :" + Pressevent.pressure + " hPa ");
         Pressure.Text = Pressevent.pressure.ToString();
         /* Display ambient temperature in C */
         float temperature = 0;
         bmp.getTemperature(ref temperature);
         Debug.WriteLine("TEMP :" + temperature + " C ");
         Temperature.Text = temperature.ToString();
         /* Then convert the atmospheric pressure, SLP and temp to altitude    */
         /* Update this next line with the current SLP for better results      */
         float seaLevelPressure = Adafruit_BMP085_Unified.SENSORS_PRESSURE_SEALEVELHPA;
         float altitude         = bmp.pressureToAltitude(seaLevelPressure, Pressevent.pressure, temperature);
         Debug.WriteLine("ALTI :" + altitude + " m ");
         Altitude.Text = altitude.ToString();
         DateTime.Text = System.DateTime.Now.ToString();
         SensorData SensorDataObj = new SensorData(Accelevent.acceleration.x, Accelevent.acceleration.y, Accelevent.acceleration.z,
                                                   Magevent.magnetic.x, Magevent.magnetic.y, Magevent.magnetic.z, Gyroevent.gyro.x, Gyroevent.gyro.y, Gyroevent.gyro.z,
                                                   Pressevent.pressure, temperature, altitude, System.DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss tt"));
         String JsonData = Serialize(SensorDataObj);
         EventHubSend(JsonData);
     }
 }
        private async void Start()
        {
            if (!started)
            {
                started = true;
                if (!bmp.begin())
                {
                    /* There was a problem detecting the BMP085 ... check your connections */
                    Debug.WriteLine("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
                    while (true)
                    {
                        ;
                    }
                }
                Pressevent = bmp.getSensorEventObj();

                if (!gyro.begin())
                {
                    /* There was a problem detecting the BMP085 ... check your connections */
                    Debug.WriteLine("Ooops, no L3GD20 detected ... Check your wiring or I2C ADDR!");
                    while (true)
                    {
                        ;
                    }
                }
                Gyroevent = gyro.getSensorEventObj();

                if (!accel.begin())
                {
                    /* There was a problem detecting the BMP085 ... check your connections */
                    Debug.WriteLine("Ooops, no LSM303 detected ... Check your wiring or I2C ADDR!");
                    while (true)
                    {
                        ;
                    }
                }
                Accelevent = accel.getSensorEventObj();

                //if (!mag.begin())
                //{
                //    /* There was a problem detecting the BMP085 ... check your connections */
                //    Debug.WriteLine("Ooops, no LSM303 detected ... Check your wiring or I2C ADDR!");
                //    while (true) ;
                //}
                //Magevent = mag.getSensorEventObj();

                // Start Timer every 1 seconds
                //ReadSensorTimer = new DispatcherTimer();
                //ReadSensorTimer.Interval = TimeSpan.FromMilliseconds(timerMS);
                //ReadSensorTimer.Tick += Timer_Tick;
                //ReadSensorTimer.Start();

                Stopwatch loopDelay = Stopwatch.StartNew();
                Stopwatch sendDelay = Stopwatch.StartNew();
                string    calcXStr, calcZStr, calcYStr;
                try
                {
                    while (true)
                    {
                        accel.getEvent(ref Accelevent);
                        gyro.getEvent(ref Gyroevent);

                        //calcX = XkalmanCalculate((float)(Math.Atan2(Accelevent.acceleration.y, Accelevent.acceleration.z) * Pi180), Gyroevent.gyro.x, elapsed);
                        //calcY = YkalmanCalculate((float)(Math.Atan2(-Accelevent.acceleration.x, Math.Sqrt(Accelevent.acceleration.y * Accelevent.acceleration.y + Accelevent.acceleration.x * Accelevent.acceleration.z)) * Pi180), Gyroevent.gyro.y, elapsed);
                        //calcZ = ZkalmanCalculate((float)(Math.Atan2(Accelevent.acceleration.z, Accelevent.acceleration.y) * Pi180), Gyroevent.gyro.z, elapsed);
                        //COULD USE THE CALIBRATE FOR SQUARE ON THE LOAD THEN USE THE YAW (Z) TO MEASURE HOOK SPIN


                        calcXStr = String.Format("{0:0.0}", XkalmanCalculate((float)(Math.Atan2(Accelevent.acceleration.y, Accelevent.acceleration.z) * Pi180), Gyroevent.gyro.x, (int)loopDelay.ElapsedMilliseconds));
                        calcYStr = String.Format("{0:0.0}", YkalmanCalculate((float)(-Math.Atan2(Accelevent.acceleration.x, Math.Sqrt(Accelevent.acceleration.y * Accelevent.acceleration.y + Accelevent.acceleration.z * Accelevent.acceleration.z)) * Pi180), Gyroevent.gyro.y, (int)loopDelay.ElapsedMilliseconds));
                        //calcZStr = String.Format("{0:0.0}", ZkalmanCalculate((float)(Math.Atan2(Accelevent.acceleration.z, Accelevent.acceleration.y) * (180 / Math.PI)), Gyroevent.gyro.z, (int)loopDelay.ElapsedMilliseconds));
                        loopDelay.Restart();
                        if (sendDelay.ElapsedMilliseconds > 200)
                        {
                            sendDelay.Restart();
                            bmp.getEvent(ref Pressevent);
                            //bmp.getTemperature(ref temperature);
                            //float altitude = bmp.pressureToAltitude(Adafruit_BMP085_Unified.SENSORS_PRESSURE_SEALEVELHPA, Pressevent.pressure);
                            mySocket.Send(calcXStr + ":" + calcYStr + ":" + 0);
                            Status.Text      = loopDelay.ElapsedMilliseconds.ToString();
                            AccelXaxisC.Text = calcXStr;
                            AccelYaxisC.Text = calcYStr;
                        }
                        await Task.Delay(10);
                    }
                }
                catch (Exception ex)
                {
                    var tt = ex;
                    throw;
                }
            }
        }