pub struct AdafruitNXP<I2C> {
    pub accel_sensor: Accelerometer,
    pub mag_sensor: Magnetometer,
    pub gyro_sensor: Gyroscope,
    /* private fields */
}
Expand description

Put in the same place all sensors,

ie, the accelerometer, the gyroscope and the magnetometer.

It uses the I2cBus to communicate with the sensor. Within this structure, you have access to the accelerometer, the gyroscope and the magnetometer sensors individually.

Fields

accel_sensor: Accelerometer

Accelerometer sensor.

mag_sensor: Magnetometer

Magnetometer sensor.

gyro_sensor: Gyroscope

Gyroscope sensor.

Implementations

Create a new AdafruitNXP structure.

You must provide an I2C bus to communicate with the sensor. Each sensor needs its own ID. Respectively, 0x8700A, 0x8700B and 0x0021002C (usually used values). The sensor will be configured with the default values. The default values are:

  • Accelerometer: Range 2G
  • Gyroscope: Range 250°/s
  • Magnetometer: Over Sampling Ratio 7
  • Over Sampling rate: 100Hz
  • Sensor mode: Hybrid

Usage:

use adafruit::*;
use rppal::i2c::I2c; // On Raspberry Pi
// Setup the raspberry's I2C interface to create the sensor.
let i2c = I2c::new().unwrap();
// Create an Adafruit object
let mut sensor = AdafruitNXP::new(0x8700A, 0x8700B, 0x0021002C, i2c);
Begin the configuration of the sensor.

The sensor will be configured with the default values by sending I2C messages. The default values are:

  • Accelerometer: Range 2G
  • Gyroscope: Range 250°/s
  • Magnetometer: Over Sampling Ratio 7
  • Over Sampling rate: 100Hz
  • Sensor mode: Hybrid
  • Calibration: Disabled
  • Accelerometer: ODR 100Hz
  • Gyroscope: ODR 100Hz
  • OSR mode: High Resolution
  • Low noise mode: Enabled

Usage:

// Check if the sensor is ready to go
let ready = sensor.begin();
if !ready {
    std::eprintln!("Sensor not detected, check your wiring!");
    std::process::exit(1);
}
Set the accelerometer full scale range.

Defaults to 2g.

Arguments:
  • range - The full scale range to set the accelerometer to

Usage:

use adafruit::*;
sensor.set_accel_range(config::AccelMagRange::Range4g)?;
Set the gyroscope full scale range.

Defaults to 250dps.

Arguments:
  • range - The full scale range to set the gyroscope to

Usage:

use adafruit::*;
sensor.set_gyro_range(config::GyroRange::Range500dps)?;

Get the accelerometer full scale range.

Returns:
  • AccelMagRange - The full scale range of the accelerometer

Set the accelerometer/magnetometer mode. Defaults to Hybrid mode.

Arguments:
  • mode - The mode to set the accelerometer/magnetometer to

Usage:

use adafruit::*;
sensor.set_sensor_mode(config::AccelMagSensorMode::HybridMode)?;

Get the accelerometer/magnetometer mode.

Returns:
  • AccelMagSensorMode - The mode of the accelerometer/magnetometer
Set the Accelerometer/Magnetometer output data rate.

Defaults to 100Hz.

Arguments:
  • rate - The output data rate to set the accelerometer/magnetometer to

Usage:

use adafruit::*;
sensor.set_accelmag_output_data_rate(config::AccelMagODR::ODR200HZ)?;

Set the Gyroscope output data rate. Defaults to 100Hz.

Arguments:
  • rate - The output data rate to set the gyroscope to Usage:
use adafruit::*;
sensor.set_gyro_output_data_rate(config::GyroODR::ODR200HZ)?;

Get the Accelerometer/Magnetometer output data rate.

Returns:
  • AccelMagODR - The output data rate of the accelerometer/magnetometer

Get the Gyroscope output data rate.

Returns:
  • GyroODR - The output data rate of the gyroscope

Set the Magnetometer oversampling ratio. Defaults to OSR7.

Arguments:
  • ratio - The oversampling ratio to set the magnetometer to

Usage:

use adafruit::*;
sensor.set_accelmag_output_data_rate(config::AccelMagODR::ODR200HZ)?;

Get the Magnetometer oversampling ratio.

Returns:
  • AccelMagMagOSR - The oversampling ratio of the magnetometer
Enable low noise mode for the accelerometer and magnetometer.

Note that the FSR setting is restricted to ±2 g or ±4 g mode. This feature cannot be used in ±8 g mode.

Set the oversampling modes for the accelerometer and magnetometer.

This setting, along with the ODR selection determines the wake mode power and noise for acceleration measurements. Defaults to High resolution.

Usage:

use adafruit::*;
sensor.set_accelmag_osr_mode(config::AccelMagOSRMode::HighResolution)?;

Get the accel mag’s accel id.

Get the accel mag’s mag id.

Get the accel mag’s gyro id.

Read data from the sensor.
  • Sets raw data to the accelerometer, magnetometer, and gyroscope.
  • Sets scaled data to the accelerometer, magnetometer, and gyroscope.

Returns true if the data is valid and all the reads were successful.

Usage:

sensor.read_data();
let acc = sensor.accel_sensor.get_scaled_data();
let gyro = sensor.gyro_sensor.get_scaled_data();
let mag = sensor.mag_sensor.get_scaled_data();
Enable the calibration for the accelerometer and gyroscope.

You need to call the calibration function afterwards and only once.

Calibrate the accelerometer and gyroscope.

This function will calibrate the accelerometer and gyroscope. You need to call this function only once before any loop. You must provide num_samples usually 5000 to get accurate results and a Delay interface.

Please note that this function will take some time to execute. During this time, you should NOT touch the sensor. Moreover, the sensor must be on a FLAT surface.

The use of this function is optional. However, in order to get accurate results, you should call it or at least run the calibration code example once. If you use the example code, you will need to hard code the values using the set_offset function linked to the sensors. e.g

sensor.accel_sensor.set_offset(-0.1234, 0.1234, 0.4321);
sensor.gyro_sensor.set_offset(-0.1234, 0.1234, 0.4321);

Usage:

let mut delay = Delay::new();
// After the initialization of the sensor
sensor.enable_calibration(true);
sensor.calibrate(5000, delay);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.