Skip to content
Snippets Groups Projects
Commit 96fc0a64 authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Separate relative tick count from encoder

parent 3e35e85d
No related branches found
No related tags found
1 merge request!4Separate relative tick count from encoder
Pipeline #97897 passed
......@@ -7,8 +7,6 @@ use embassy_stm32::{
pub struct Encoders<'a> {
left: Qei<'a, TIM3>,
right: Qei<'a, TIM2>,
count_left: u16,
count_right: u16,
}
pub struct EncoderValue {
......@@ -16,6 +14,11 @@ pub struct EncoderValue {
direction: Direction,
}
pub struct Relative {
count_left: u16,
count_right: u16,
}
impl core::fmt::Debug for EncoderValue {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
......@@ -36,12 +39,7 @@ impl Encoders<'_> {
let left = Qei::new(tim3, QeiPin::new_ch1(pa6), QeiPin::new_ch2(pa7));
let right = Qei::new(tim2, QeiPin::new_ch1(pa0), QeiPin::new_ch2(pa1));
pac::TIM2.ccer().modify(|w| w.set_ccp(1, true));
Self {
left,
right,
count_left: 0,
count_right: 0,
}
Self { left, right }
}
pub fn read(&self) -> (EncoderValue, EncoderValue) {
......@@ -56,9 +54,19 @@ impl Encoders<'_> {
},
)
}
}
impl Relative {
pub fn new(encoders: &Encoders<'_>) -> Self {
let (count_left, count_right) = (encoders.left.count(), encoders.right.count());
Self {
count_left,
count_right,
}
}
pub fn ticks(&mut self) -> (i16, i16) {
let (left, right) = (self.left.count(), self.right.count());
pub fn ticks(&mut self, encoders: &Encoders<'_>) -> (i16, i16) {
let (left, right) = (encoders.left.count(), encoders.right.count());
(
ticks_since(&mut self.count_left, left),
ticks_since(&mut self.count_right, right),
......
use crate::{encoders::Encoders, tb6612fng::Tb6612fng};
use crate::{
encoders::{Encoders, Relative},
tb6612fng::Tb6612fng,
};
use embassy_executor::Spawner;
use embassy_stm32::{peripherals::WWDG, time::hz};
use futures::FutureExt;
......@@ -14,13 +17,16 @@ pub const I2C_ADDRESS: u8 = 0x57;
struct State {
_i2c: I2C2,
encoders: Encoders<'static>,
tick_count: Relative,
}
impl State {
fn new(i2c2: I2C2, encoders: Encoders<'static>) -> Self {
let tick_count = Relative::new(&encoders);
Self {
_i2c: i2c2,
encoders,
tick_count,
}
}
}
......@@ -144,7 +150,7 @@ fn process_command(
response.push(watchdog::get_shutdown_timeout()).unwrap();
}
[CMD_ENCODER_TICKS] => {
let (left, right) = state.encoders.ticks();
let (left, right) = state.tick_count.ticks(&state.encoders);
let (left, right) = (left.to_le_bytes(), right.to_le_bytes());
response.push(left[0]).unwrap();
response.push(left[1]).unwrap();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment