Esempio n. 1
0
  /** This function registers sensor listeners for the accelerometer, magnetometer and gyroscope. */
  private void initListeners() {
    sensorMgr = (SensorManager) mContext.getSystemService(Activity.SENSOR_SERVICE);
    fuseTimer = new Timer();

    sensors = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
    if (sensors.size() > 0) {
      sensorGravRunning = false;
      sensorGrav = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      sensorMgr.registerListener(this, sensorGrav, sensorRate);
    }
    sensors = sensorMgr.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
    if (sensors.size() > 0) {
      sensorMagRunning = false;
      sensorMag = sensorMgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
      sensorMgr.registerListener(this, sensorMag, sensorRate);
    }
    sensors = sensorMgr.getSensorList(Sensor.TYPE_GYROSCOPE);
    if (sensors.size() > 0) {
      sensorGyroRunning = false;
      sensorGyro = sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
      sensorMgr.registerListener(this, sensorGyro, sensorRate);
      fuseTimer.scheduleAtFixedRate(
          new calculateFusedOrientationTask(), 1000, TIME_CONSTANT); // use the gyro
      NLog.d(TAG, "initListeners() - " + "Gyro found");
    } else {
      fuseTimer.scheduleAtFixedRate(
          new calculateUnfusedOrientationTask(), 1000, TIME_CONSTANT); // don't use the gyro
      NLog.d(TAG, "initListeners() - " + "No gyro found");
    }

    locationMgr = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);

    try {
      Location gps = locationMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      Location network = locationMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      if (gps != null) onLocationChanged(gps);
      else if (network != null) onLocationChanged(network);
      else onLocationChanged(NCompassARData.hardFix);
    } catch (Exception ex2) {
      onLocationChanged(NCompassARData.hardFix);
    }

    float angleY = -90 * toRadians;

    gmf =
        new GeomagneticField(
            (float) NCompassARData.getCurrentLocation().getLatitude(),
            (float) NCompassARData.getCurrentLocation().getLongitude(),
            (float) NCompassARData.getCurrentLocation().getAltitude(),
            System.currentTimeMillis());
    angleY = -gmf.getDeclination() * toRadians;

    synchronized (mageticNorthCompensation) {
      mageticNorthCompensation.toIdentity();

      // Counter-clockwise rotation at negative declination around the y-axis
      // note2: declination is the difference between true north and magnetic north
      mageticNorthCompensation.set(
          FloatMath.cos(angleY),
          0f,
          FloatMath.sin(angleY),
          0f,
          1f,
          0f,
          -FloatMath.sin(angleY),
          0f,
          FloatMath.cos(angleY));

      // Rotate the matrix to match the orientation
      mageticNorthCompensation.prod(xAxisRotation);
    }
  }