/**************************************************************************/ /*! * @brief Reads the sensor and returns the data as a sensors_event_t */ /**************************************************************************/ public void getEvent(ref sensors_event_t Event) { float pressure_kPa = 0; Event.type = 6; //SENSOR_TYPE_PRESSURE; Event.timestamp = 0; Event.sensor_id = _sensorID; getPressure(ref pressure_kPa); Event.pressure = pressure_kPa / 100.0F; }
/**************************************************************************/ /*! * @brief Gets the most recent sensor event */ /**************************************************************************/ public void getEvent(ref sensors_event_t Event) { /* Read new data */ read(); Event.version = 1; Event.sensor_id = _sensorID; Event.type = 1; //SENSOR_TYPE_ACCELEROMETER; Event.timestamp = 0; Event.acceleration.x = _accelData.x * _lsm303Accel_MG_LSB * SENSORS_GRAVITY_STANDARD; Event.acceleration.y = _accelData.y * _lsm303Accel_MG_LSB * SENSORS_GRAVITY_STANDARD; Event.acceleration.z = _accelData.z * _lsm303Accel_MG_LSB * SENSORS_GRAVITY_STANDARD; }
/**************************************************************************/ /*! * @brief Gets the most recent sensor event */ /**************************************************************************/ public void getEvent(ref sensors_event_t Event) { bool readingValid = false; Event.sensor_id = _sensorID; Event.type = 4; // SENSOR_TYPE_GYROSCOPE; while (!readingValid) { Event.timestamp = 0; byte[] addr = new byte[] { (byte)((byte)gyroRegisters_t.GYRO_REGISTER_OUT_X_L | 0x80) }; byte[] data = new byte[6]; I2CDev.WriteRead(addr, data); byte xlo = data[0]; byte xhi = data[1]; byte ylo = data[2]; byte yhi = data[3]; byte zlo = data[4]; byte zhi = data[5]; /* Shift values to create properly formed integer (low byte first) */ Event.gyro.x = (short)(xlo | (xhi << 8)); Event.gyro.y = (short)(ylo | (yhi << 8)); Event.gyro.z = (short)(zlo | (zhi << 8)); /* Make sure the sensor isn't saturating if auto-ranging is enabled */ if (!_autoRangeEnabled) { readingValid = true; } else { /* Check if the sensor is saturating or not */ if ((Event.gyro.x >= 32760) | (Event.gyro.x <= -32760) | (Event.gyro.y >= 32760) | (Event.gyro.y <= -32760) | (Event.gyro.z >= 32760) | (Event.gyro.z <= -32760)) { /* Saturating .... increase the range if we can */ switch (_range) { case gyroRange_t.GYRO_RANGE_500DPS: /* Push the range up to 2000dps */ _range = gyroRange_t.GYRO_RANGE_2000DPS; write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG1, 0x00); write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG1, 0x0F); write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG4, 0x20); write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG5, 0x80); readingValid = false; // Serial.println("Changing range to 2000DPS"); break; case gyroRange_t.GYRO_RANGE_250DPS: /* Push the range up to 500dps */ _range = gyroRange_t.GYRO_RANGE_500DPS; write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG1, 0x00); write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG1, 0x0F); write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG4, 0x10); write8((byte)gyroRegisters_t.GYRO_REGISTER_CTRL_REG5, 0x80); readingValid = false; // Serial.println("Changing range to 500DPS"); break; default: readingValid = true; break; } } else { /* All values are withing range */ readingValid = true; } } } /* Compensate values depending on the resolution */ switch (_range) { case gyroRange_t.GYRO_RANGE_250DPS: Event.gyro.x *= GYRO_SENSITIVITY_250DPS; Event.gyro.y *= GYRO_SENSITIVITY_250DPS; Event.gyro.z *= GYRO_SENSITIVITY_250DPS; break; case gyroRange_t.GYRO_RANGE_500DPS: Event.gyro.x *= GYRO_SENSITIVITY_500DPS; Event.gyro.y *= GYRO_SENSITIVITY_500DPS; Event.gyro.z *= GYRO_SENSITIVITY_500DPS; break; case gyroRange_t.GYRO_RANGE_2000DPS: Event.gyro.x *= GYRO_SENSITIVITY_2000DPS; Event.gyro.y *= GYRO_SENSITIVITY_2000DPS; Event.gyro.z *= GYRO_SENSITIVITY_2000DPS; break; } /* Convert values to rad/s */ //Event.gyro.x *= SENSORS_DPS_TO_RADS; //Event.gyro.y *= SENSORS_DPS_TO_RADS; //Event.gyro.z *= SENSORS_DPS_TO_RADS; }
/**************************************************************************/ /*! * @brief Gets the most recent sensor event */ /**************************************************************************/ public void getEvent(ref sensors_event_t Event) { bool readingValid = false; while (!readingValid) { /* Read new data */ read(); /* Make sure the sensor isn't saturating if auto-ranging is enabled */ if (!_autoRangeEnabled) { readingValid = true; } else { Debug.WriteLine(_magData.x + " " + _magData.y + " " + _magData.z + " "); /* Check if the sensor is saturating or not */ if ((_magData.x >= 4090) | (_magData.x <= -4090) | (_magData.y >= 4090) | (_magData.y <= -4090) | (_magData.z >= 4090) | (_magData.z <= -4090)) { /* Saturating .... increase the range if we can */ switch (_magGain) { case lsm303MagGain.LSM303_MAGGAIN_5_6: setMagGain(lsm303MagGain.LSM303_MAGGAIN_8_1); readingValid = false; Debug.WriteLine("Changing range to +/- 8.1"); break; case lsm303MagGain.LSM303_MAGGAIN_4_7: setMagGain(lsm303MagGain.LSM303_MAGGAIN_5_6); readingValid = false; Debug.WriteLine("Changing range to +/- 5.6"); break; case lsm303MagGain.LSM303_MAGGAIN_4_0: setMagGain(lsm303MagGain.LSM303_MAGGAIN_4_7); readingValid = false; Debug.WriteLine("Changing range to +/- 4.7"); break; case lsm303MagGain.LSM303_MAGGAIN_2_5: setMagGain(lsm303MagGain.LSM303_MAGGAIN_4_0); readingValid = false; Debug.WriteLine("Changing range to +/- 4.0"); break; case lsm303MagGain.LSM303_MAGGAIN_1_9: setMagGain(lsm303MagGain.LSM303_MAGGAIN_2_5); readingValid = false; Debug.WriteLine("Changing range to +/- 2.5"); break; case lsm303MagGain.LSM303_MAGGAIN_1_3: setMagGain(lsm303MagGain.LSM303_MAGGAIN_1_9); readingValid = false; Debug.WriteLine("Changing range to +/- 1.9"); break; default: readingValid = true; break; } } else { /* All values are withing range */ readingValid = true; } } } Event.version = 1; Event.sensor_id = _sensorID; Event.type = 2; // SENSOR_TYPE_MAGNETIC_FIELD; Event.timestamp = 0; Event.magnetic.x = _magData.x / _lsm303Mag_Gauss_LSB_XY * SENSORS_GAUSS_TO_MICROTESLA; Event.magnetic.y = _magData.y / _lsm303Mag_Gauss_LSB_XY * SENSORS_GAUSS_TO_MICROTESLA; Event.magnetic.z = _magData.z / _lsm303Mag_Gauss_LSB_Z * SENSORS_GAUSS_TO_MICROTESLA; }