Struct adafruit_nxp::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
sourceimpl<I2C, E> AdafruitNXP<I2C> where
I2C: Write<Error = E> + WriteRead<Error = E>,
impl<I2C, E> AdafruitNXP<I2C> where
I2C: Write<Error = E> + WriteRead<Error = E>,
sourcepub fn new(accel_id: i32, mag_id: i32, gyro_id: i32, i2c: I2C) -> Self
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);
sourcepub fn begin(&mut self) -> Result<bool, SensorError<E>>
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);
}
sourcepub fn set_accel_range(
&mut self,
range: AccelMagRange
) -> Result<(), SensorError<E>>
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)?;
sourcepub fn set_gyro_range(&mut self, range: GyroRange) -> Result<(), SensorError<E>>
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)?;
sourcepub fn get_accel_range(&self) -> AccelMagRange
pub fn get_accel_range(&self) -> AccelMagRange
Get the accelerometer full scale range.
Returns:
AccelMagRange
- The full scale range of the accelerometer
sourcepub fn set_sensor_mode(
&mut self,
mode: AccelMagSensorMode
) -> Result<(), SensorError<E>>
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)?;
sourcepub fn get_sensor_mode(&self) -> AccelMagSensorMode
pub fn get_sensor_mode(&self) -> AccelMagSensorMode
Get the accelerometer/magnetometer mode.
Returns:
AccelMagSensorMode
- The mode of the accelerometer/magnetometer
sourcepub fn set_accelmag_output_data_rate(
&mut self,
rate: AccelMagODR
) -> Result<(), SensorError<E>>
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)?;
sourcepub fn set_gyro_output_data_rate(
&mut self,
rate: GyroODR
) -> Result<(), SensorError<E>>
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)?;
sourcepub fn get_output_data_rate(&self) -> AccelMagODR
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
sourcepub fn get_gyro_output_data_rate(&self) -> GyroODR
pub fn get_gyro_output_data_rate(&self) -> GyroODR
sourcepub fn set_mag_oversampling_ratio(
&mut self,
ratio: AccelMagMagOSR
) -> Result<(), SensorError<E>>
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)?;
sourcepub fn get_mag_oversampling_ratio(&self) -> AccelMagMagOSR
pub fn get_mag_oversampling_ratio(&self) -> AccelMagMagOSR
Get the Magnetometer oversampling ratio.
Returns:
AccelMagMagOSR
- The oversampling ratio of the magnetometer
sourcepub fn set_accelmag_low_noise_mode(
&mut self,
enable: bool
) -> Result<(), SensorError<E>>
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.
sourcepub fn set_accelmag_osr_mode(
&mut self,
mode: AccelMagOSRMode
) -> Result<(), SensorError<E>>
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)?;
sourcepub fn get_accel_id(&self) -> i32
pub fn get_accel_id(&self) -> i32
Get the accel mag’s accel id.
sourcepub fn get_mag_id(&self) -> i32
pub fn get_mag_id(&self) -> i32
Get the accel mag’s mag id.
sourcepub fn get_gyro_id(&self) -> i32
pub fn get_gyro_id(&self) -> i32
Get the accel mag’s gyro id.
sourcepub fn read_data(&mut self) -> Result<(), SensorError<E>>
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();
sourcepub fn enable_calibration(&mut self, enable: bool)
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.
sourcepub fn calibration<D: DelayMs<u8>>(
&mut self,
num_samples: u16,
delay: &mut D
) -> Result<(), SensorError<E>>
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> 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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.