Tested motor control

This commit is contained in:
Sebastian 2024-10-12 14:33:23 +02:00
parent b4b07b505c
commit 86a33b97a9
3 changed files with 72 additions and 67 deletions

View file

@ -18,4 +18,45 @@ rb = "run --bin"
rrb = "run --release --bin"
[env]
DEFMT_LOG = "debug"
DEFMT_LOG = "debug"
# cargo build/run
[profile.dev]
codegen-units = 1
debug = 2
debug-assertions = true # <-
incremental = false
opt-level = 'z' # <-
overflow-checks = true # <-
# cargo test
[profile.test]
codegen-units = 1
debug = 2
debug-assertions = true # <-
incremental = false
opt-level = 3 # <-
overflow-checks = true # <-
# cargo build/run --release
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = false # <-
incremental = false
lto = 'fat'
opt-level = 3 # <-
overflow-checks = false # <-
# cargo test --release
[profile.bench]
codegen-units = 1
debug = 2
debug-assertions = false # <-
incremental = false
lto = 'fat'
opt-level = 3 # <-
overflow-checks = false # <-

View file

@ -32,50 +32,3 @@ defmt-debug = []
defmt-info = []
defmt-warn = []
defmt-error = []
# cargo build/run
[profile.dev]
codegen-units = 1
debug = 2
debug-assertions = true # <-
incremental = false
opt-level = 'z' # <-
overflow-checks = true # <-
# cargo test
[profile.test]
codegen-units = 1
debug = 2
debug-assertions = true # <-
incremental = false
opt-level = 3 # <-
overflow-checks = true # <-
# cargo build/run --release
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = false # <-
incremental = false
lto = 'fat'
opt-level = 3 # <-
overflow-checks = false # <-
# cargo test --release
[profile.bench]
codegen-units = 1
debug = 2
debug-assertions = false # <-
incremental = false
lto = 'fat'
opt-level = 3 # <-
overflow-checks = false # <-
# uncomment this to switch from the crates.io version of defmt to its git version
# check app-template's README for instructions
# [patch.crates-io]
# defmt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version supported by probe-rs (see changelog)" }
# defmt-rtt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version supported by probe-rs (see changelog)" }
# defmt-test = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version supported by probe-rs (see changelog)" }
# panic-probe = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version supported by probe-rs (see changelog)" }

View file

@ -13,17 +13,14 @@ fn panic() -> ! {
cortex_m::asm::udf()
}
#[rtic::app(
device = stm32f4xx_hal::pac,
dispatchers = [SPI3]
)]
mod app {
use as5048a::AS5048A;
use stm32f4xx_hal::{
gpio::{gpioa, gpiob, gpioc, Output, PushPull},
i2c,
@ -40,12 +37,13 @@ mod app {
use rtic_monotonics::systick::prelude::*;
systick_monotonic!(Mono, 1000);
systick_monotonic!(Mono, 4000);
// Shared resources go here
#[shared]
struct Shared {
az_angle: i32,
az_compass: i32,
}
// Local resources go here
@ -136,7 +134,7 @@ mod app {
(sck, poci, pico),
spi::Mode {
polarity: spi::Polarity::IdleLow,
phase: spi::Phase::CaptureOnFirstTransition,
phase: spi::Phase::CaptureOnSecondTransition,
},
8.MHz(),
&clocks,
@ -144,11 +142,13 @@ mod app {
defmt::info!("SPI Setup done");
let az_enable = gpioa.pa12.into_push_pull_output();
let mut az_enable = gpioa.pa12.into_push_pull_output();
az_enable.set_high();
let az_dir = gpioa.pa15.into_push_pull_output();
let az_step = gpiob.pb3.into_push_pull_output();
let el_enable = gpiob.pb4.into_push_pull_output();
let mut el_enable = gpiob.pb4.into_push_pull_output();
el_enable.set_high();
let el_dir = gpioa.pa8.into_push_pull_output();
let el_step = gpioa.pa9.into_push_pull_output();
@ -156,9 +156,13 @@ mod app {
poll_i2c::spawn().ok();
poll_spi::spawn().ok();
move_az::spawn().ok();
(
Shared { az_angle: 0 },
Shared {
az_angle: 0,
az_compass: 0,
},
Local {
i2cmux,
board_led,
@ -179,8 +183,8 @@ mod app {
)
}
#[task(local = [i2cmux, board_led])]
async fn poll_i2c(cx: poll_i2c::Context) {
#[task(local = [i2cmux, board_led], shared = [az_compass])]
async fn poll_i2c(mut cx: poll_i2c::Context) {
let i2cmux = cx.local.i2cmux;
let board_led = cx.local.board_led;
@ -212,6 +216,11 @@ mod app {
heading -= 2.0 * f32::PI();
}
let heading_degrees = heading * 180.0 / f32::PI();
cx.shared.az_compass.lock(|az_compass| {
*az_compass = heading_degrees as i32 * 10;
});
defmt::info!("Heading1 {}", heading_degrees);
break;
}
@ -276,32 +285,34 @@ mod app {
}
}
#[task(local = [az_enable, az_dir, az_step], shared = [az_angle])]
#[task(local = [az_enable, az_dir, az_step], shared = [az_angle, az_compass])]
async fn move_az(mut cx: move_az::Context) {
let az_enable = cx.local.az_enable;
let az_dir = cx.local.az_dir;
let az_step = cx.local.az_step;
let az_target = 42i32;
loop {
let az_target = cx.shared.az_compass.lock(|az_compass| *az_compass);
let az_angle = cx.shared.az_angle.lock(|az_angle| *az_angle);
let diff = az_angle - az_target;
if diff.abs() > 2 {
az_enable.set_high();
defmt::info!("angle diff: {:?}", diff);
if diff.abs() > 5 {
az_enable.set_low();
if diff > 0 {
az_dir.set_high();
} else {
az_dir.set_low();
}
az_step.set_high();
Mono::delay(250.micros()).await;
az_step.set_low();
Mono::delay(250.micros()).await;
az_step.set_high();
Mono::delay(250.micros()).await;
} else {
az_enable.set_low();
az_enable.set_high();
Mono::delay(500.micros()).await;
}
}