Skip to content

Commit 6c7646a

Browse files
add imu example (#4)
1 parent c251062 commit 6c7646a

File tree

5 files changed

+383
-3
lines changed

5 files changed

+383
-3
lines changed

Cargo.lock

Lines changed: 83 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ trouble-host = { git = "https://github.com/embassy-rs/trouble.git", features = [
5252
"log",
5353
] }
5454
bt-hci = { version = "0.2.0", features = ["log"] }
55+
imu-fusion = "0.2.5"
56+
micromath = "2.1.0"
5557

5658

5759
[profile.dev]

examples/imu.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)