Compare commits

...

3 Commits

Author SHA1 Message Date
~erin e057fd368e
Channel works as an async queue im dumb 2023-07-21 13:06:00 -04:00
~erin d527c14f04
Release profile optimization 2023-07-20 12:09:49 -04:00
~erin e061866cf4
Basic lerp function 2023-07-20 12:05:19 -04:00
2 changed files with 24 additions and 34 deletions

View File

@ -19,3 +19,7 @@ embassy-sync = { version = "0.2.0", git = "https://github.com/embassy-rs/embassy
static_cell = { version = "1.1", features = ["nightly"]} static_cell = { version = "1.1", features = ["nightly"]}
nanorand = { version = "0.7.0", default-features = false, features = ["wyrand"] } nanorand = { version = "0.7.0", default-features = false, features = ["wyrand"] }
tinyvec = "1.6.0" tinyvec = "1.6.0"
[profile.release]
lto = "fat"
codegen-units = 1

View File

@ -18,12 +18,12 @@ use {defmt_rtt as _, panic_probe as _};
static mut CORE1_STACK: Stack<4096> = Stack::new(); static mut CORE1_STACK: Stack<4096> = Stack::new();
static EXECUTOR0: StaticCell<Executor> = StaticCell::new(); static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
static EXECUTOR1: StaticCell<Executor> = StaticCell::new(); static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
static CHANNEL: Channel<CriticalSectionRawMutex, Buffer, 1> = Channel::new(); static BUF_CHANNEL: Channel<CriticalSectionRawMutex, Buffer, 256> = Channel::new();
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
ADC_IRQ_FIFO => InterruptHandler; ADC_IRQ_FIFO => InterruptHandler;
}); });
const BUF_SIZE: usize = 64; const BUF_SIZE: usize = 256;
#[derive(Format)] #[derive(Format)]
enum State { enum State {
@ -61,15 +61,15 @@ fn main() -> ! {
spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
let executor1 = EXECUTOR1.init(Executor::new()); let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| unwrap!(spawner.spawn(core1_task()))); executor1.run(|spawner| unwrap!(spawner.spawn(process_data())));
}); });
let executor0 = EXECUTOR0.init(Executor::new()); let executor0 = EXECUTOR0.init(Executor::new());
executor0.run(|spawner| unwrap!(spawner.spawn(core0_task()))); executor0.run(|spawner| unwrap!(spawner.spawn(read_eeg())));
} }
#[embassy_executor::task] #[embassy_executor::task]
async fn core0_task() { async fn read_eeg() {
let p = embassy_rp::init(Default::default()); let p = embassy_rp::init(Default::default());
let mut adc = Adc::new(p.ADC, Irqs, Config::default()); let mut adc = Adc::new(p.ADC, Irqs, Config::default());
let mut eeg = Pin::new(p.PIN_26, Pull::None); let mut eeg = Pin::new(p.PIN_26, Pull::None);
@ -85,7 +85,7 @@ async fn core0_task() {
// When full, send buffer to be added to the queue // When full, send buffer to be added to the queue
loop { loop {
if count >= BUF_SIZE { if count >= BUF_SIZE {
CHANNEL.send(buf.clone()).await; BUF_CHANNEL.send(buf.clone()).await;
count = 0; count = 0;
} else { } else {
buf.0[count] = adc.read(&mut eeg).await.unwrap(); buf.0[count] = adc.read(&mut eeg).await.unwrap();
@ -95,37 +95,23 @@ async fn core0_task() {
} }
#[embassy_executor::task] #[embassy_executor::task]
async fn core1_task() { async fn process_data() {
use tinyvec::ArrayVec;
let mut queue = ArrayVec::<[Buffer; 64]>::new();
info!("Hello from core 1");
loop { loop {
// Shouldn't block on waiting for message? let mut buffer = BUF_CHANNEL.recv().await;
// Need to test // Low-pass filter
queue.push(CHANNEL.recv().await); // FFT (w/ hanning window)
info!("Running FFT...");
match process_data(&queue[0]).await {
Some(s) => {
info!("Parsed '{}' state from EEG data", &s);
set_servo_position(s).await;
}
None => warn!("Unable to match EEG data to state"),
}
queue.remove(0);
} }
} }
async fn process_data(buf: &Buffer) -> Option<State> { // async fn set_servo_position(state: State) {
// Low-pass filter // // Use a map of positions for each state, and move the servos towards it
// FFT (w/ hanning window) // // Use lerp and randomness
info!("Running FFT..."); // // I2C control board manages servos
return None; // info!("Setting position to {}", state.servo_positions());
} // }
async fn set_servo_position(state: State) { #[inline]
// Use a map of positions for each state, and move the servos towards it fn lerp(v0: f32, v1: f32, t: f32) -> f32 {
// Use lerp and randomness return (1. - t) * v0 + t * v1;
// I2C control board manages servos
info!("Setting position to {}", state.servo_positions());
} }