|
| 1 | +//! # IMU Sensor Example |
| 2 | +//! |
| 3 | +//! This example demonstrates how to read the accelerometer and gyroscope data from the onboard ICM42670 sensor. |
| 4 | +//! The ICM42670 sensor is a low-power sensor that can be used to measure acceleration and angular velocity. |
| 5 | +//! This example demonstrates how to use an actor to read the sensor every 20 seconds. |
| 6 | +//! The actor will read the sensor for 1 minute before stopping. |
| 7 | +
|
| 8 | +#![no_std] |
| 9 | +#![no_main] |
| 10 | + |
| 11 | +use core::future::pending; |
| 12 | + |
| 13 | +use esp_backtrace as _; |
| 14 | + |
| 15 | +use embassy_executor::Spawner; |
| 16 | +use embassy_time::{Duration, Timer}; |
| 17 | + |
| 18 | +use esp32c3_devkit_demo::{ |
| 19 | + bsp::Board, |
| 20 | + imu::{self, Message}, |
| 21 | +}; |
| 22 | + |
| 23 | +#[esp_hal_embassy::main] |
| 24 | +async fn main(spawner: Spawner) -> ! { |
| 25 | + esp_println::logger::init_logger_from_env(); |
| 26 | + |
| 27 | + let board = Board::init(); |
| 28 | + |
| 29 | + // For now we only have one element in the configuration. |
| 30 | + // But we could add more elements to the configuration to pass to the actor. |
| 31 | + let config = imu::Config { |
| 32 | + i2c_bus: board.i2c_bus, |
| 33 | + }; |
| 34 | + let actor = imu::spawn_actor(spawner, config).expect("failed to spawn"); |
| 35 | + |
| 36 | + // Set the power mode to normal mode. |
| 37 | + actor |
| 38 | + .send(Message::SetPowerMode(icm42670::PowerMode::SixAxisLowNoise)) |
| 39 | + .await; |
| 40 | + |
| 41 | + Timer::after_secs(1).await; |
| 42 | + |
| 43 | + // Start the actor to read the sensor every 20 milliseconds. |
| 44 | + actor.send(Message::Start(Duration::from_millis(20))).await; |
| 45 | + |
| 46 | + // Stop the actor after 60 seconds. |
| 47 | + Timer::after_secs(60).await; |
| 48 | + actor.send(Message::Stop).await; |
| 49 | + |
| 50 | + pending().await |
| 51 | +} |
0 commit comments