Struct AdafruitNXP

Source
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§

Source§

impl<I2C, E> AdafruitNXP<I2C>
where I2C: Write<Error = E> + WriteRead<Error = E>,

Source

pub fn new(accel_id: i32, mag_id: i32, gyro_id: i32, i2c: I2C) -> Self

§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);
Source

pub fn begin(&mut self) -> Result<bool, SensorError<E>>

§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);
}
Source

pub fn set_accel_range( &mut self, range: AccelMagRange, ) -> Result<(), SensorError<E>>

§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)?;
Source

pub fn set_gyro_range(&mut self, range: GyroRange) -> Result<(), SensorError<E>>

§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)?;
Source

pub fn get_accel_range(&self) -> AccelMagRange

Get the accelerometer full scale range.

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

pub fn set_sensor_mode( &mut self, mode: AccelMagSensorMode, ) -> Result<(), SensorError<E>>

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)?;
Source

pub fn get_sensor_mode(&self) -> AccelMagSensorMode

Get the accelerometer/magnetometer mode.

§Returns:
  • AccelMagSensorMode - The mode of the accelerometer/magnetometer
Source

pub fn set_accelmag_output_data_rate( &mut self, rate: AccelMagODR, ) -> Result<(), SensorError<E>>

§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)?;
Source

pub fn set_gyro_output_data_rate( &mut self, rate: GyroODR, ) -> Result<(), SensorError<E>>

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)?;
Source

pub fn get_output_data_rate(&self) -> AccelMagODR

Get the Accelerometer/Magnetometer output data rate.

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

pub fn get_gyro_output_data_rate(&self) -> GyroODR

Get the Gyroscope output data rate.

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

pub fn set_mag_oversampling_ratio( &mut self, ratio: AccelMagMagOSR, ) -> Result<(), SensorError<E>>

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)?;
Source

pub fn get_mag_oversampling_ratio(&self) -> AccelMagMagOSR

Get the Magnetometer oversampling ratio.

§Returns:
  • AccelMagMagOSR - The oversampling ratio of the magnetometer
Source

pub fn set_accelmag_low_noise_mode( &mut self, enable: bool, ) -> Result<(), SensorError<E>>

§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.

Source

pub fn set_accelmag_osr_mode( &mut self, mode: AccelMagOSRMode, ) -> Result<(), SensorError<E>>

§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)?;
Source

pub fn get_accel_id(&self) -> i32

Get the accel mag’s accel id.

Source

pub fn get_mag_id(&self) -> i32

Get the accel mag’s mag id.

Source

pub fn get_gyro_id(&self) -> i32

Get the accel mag’s gyro id.

Source

pub fn read_data(&mut self) -> Result<(), SensorError<E>>

§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();
Source

pub fn enable_calibration(&mut self, enable: bool)

§Enable the calibration for the accelerometer and gyroscope.

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

Source

pub fn calibration<D: DelayMs<u8>>( &mut self, num_samples: u16, delay: &mut D, ) -> Result<(), SensorError<E>>

§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§

§

impl<I2C> Freeze for AdafruitNXP<I2C>
where I2C: Freeze,

§

impl<I2C> RefUnwindSafe for AdafruitNXP<I2C>
where I2C: RefUnwindSafe,

§

impl<I2C> Send for AdafruitNXP<I2C>
where I2C: Send,

§

impl<I2C> Sync for AdafruitNXP<I2C>
where I2C: Sync,

§

impl<I2C> Unpin for AdafruitNXP<I2C>
where I2C: Unpin,

§

impl<I2C> UnwindSafe for AdafruitNXP<I2C>
where I2C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

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

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

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

fn from_subset(element: &SS) -> SP

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.