diff --git a/spinnyboy_rust/src/bin/dc_driver/dshot.rs b/spinnyboy_rust/src/bin/dc_driver/dshot.rs index d2b65b7..a5dbe53 100644 --- a/spinnyboy_rust/src/bin/dc_driver/dshot.rs +++ b/spinnyboy_rust/src/bin/dc_driver/dshot.rs @@ -16,7 +16,8 @@ fn calculate_crc(frame: u16) -> u16 { IFlight XING-E Pro 2207 2750KV */ const XING_EPRO_22_POLES: u32 = 4; -const XING_EPRO_22_MAGNETS: u32 = 14; +const XING_EPRO_22_MAGNETS: u32 = 14; + #[allow(dead_code)] #[allow(non_camel_case_types)] #[derive(Debug)] @@ -323,7 +324,8 @@ impl<'a> DShot<'a> { let erpm = (60 * 1_000_000) / period_ms; let rpm = erpm/(XING_EPRO_22_MAGNETS/2); - self.rpm.set(Some(rpm)) + // let rpm = erpm/7; + self.rpm.set(Some((rpm as f32) as _)) } } } diff --git a/spinnyboy_rust/src/bin/main.rs b/spinnyboy_rust/src/bin/main.rs index 5faba26..8905581 100644 --- a/spinnyboy_rust/src/bin/main.rs +++ b/spinnyboy_rust/src/bin/main.rs @@ -12,18 +12,18 @@ use esp_hal::delay::Delay; use esp_hal::uart::{Config, Uart}; use esp_hal::{gpio, main}; -use core::fmt::Debug; use esp_hal::gpio::{Event, OutputConfig}; use esp_hal::gpio::{Input, InputConfig}; use esp_hal::handler; use esp_hal::rmt::{Rmt, TxChannelConfig, TxChannelCreator}; -use esp_hal::time::{Instant, Rate}; +use esp_hal::time::{ Instant, Rate}; +use core::fmt::Debug; //pid-controller use core::time::Duration; use discrete_pid::pid::PidContext; -use discrete_pid::time::InstantLike; use discrete_pid::time::Millis; use discrete_pid::{pid, time}; +use discrete_pid::time::InstantLike; use core::cell::RefCell; use core::mem::forget; @@ -43,16 +43,12 @@ use crate::dc_driver::dshot; use crate::peripherals::ErrCommand; use esp_hal::rmt::RxChannelConfig; use esp_hal::rmt::RxChannelCreator; - -use esp_hal::timer::OneShotTimer; -use esp_hal::timer::Timer; -use esp_hal::timer::timg::TimerGroup; #[panic_handler] fn panic(panic: &core::panic::PanicInfo) -> ! { - rprintln!("{}", panic.message()); + rprintln!("{}",panic.message()); let trace = Backtrace::capture(); loop { - rprintln!("{}", panic.location().unwrap()) + // rprintln!("{}",panic.location().unwrap()) } } use alloc::format; @@ -82,6 +78,8 @@ impl InstantLike for Time { } } + + #[main] fn main() -> ! { // generator version: 1.0.1 @@ -128,21 +126,44 @@ fn main() -> ! { //PID-Controller let loop_time = Duration::from_micros(10); let cfg = pid::PidConfigBuilder::default() - .kp(2.0) - .ki(0.0) - .output_limits(100.00, 400.00) - .sample_time(loop_time) - .filter_tc(0.1) - .build() - .expect("Failed to build a PID configuration"); - let controller = pid::FuncPidController::new(cfg); + .kp(2.0) + .ki(0.1).output_limits(100.00, 2040.00) + .sample_time(loop_time) + .filter_tc(0.1) + .build() + .expect("Failed to build a PID configuration"); + let controller = pid::FuncPidController::new(cfg); let mut ctx = PidContext::new(Millis(0), 0.0, 0.0); let set_point = DEFAULT_TARGET_RPM; + dshot_esc.arm(); let mut rpm = 0; - let mut control: f32 = 200.0; - let mut timg0 = TimerGroup::new(peripherals.TIMG0); - let coat_timer = timg0.timer0; - // shot_esc.set_throttle(0); + let mut control: f32 = 600.0; + // let timestamp = Instant::now(); + loop { + dshot_esc.process(); + //let control_val = dc_driver:: + //this bad boy needs floats, this will be fun :) + dshot_esc.set_throttle(control as _); + rpm = match dshot_esc.get_rpm() { + Some(x) => { + // rprintln!("GOT RPM {}", x); + x + } + None => { + rprintln!("NO RPM!"); + rpm + } + }; + // rprintln!("RPM:{}",rpm); + let last_time = esp_hal::time::Instant::now(); + + let timestamp = esp_hal::time::Instant::now().duration_since_epoch().as_millis(); + (control, ctx) = controller.compute(ctx, rpm as _, set_point as _, Millis(timestamp), None); + let dumped_context = ctx.last_time().expect("LAST TIME!"); + rprintln!("control:{},rpm:{}", control,rpm); + } + + // rprintln!("RMT SENT!"); // let mut esc = AfroEsc::new(&mut pwm_pin);; // esc.set_timestamp(1000); // delay.delay_millis(3000); @@ -175,7 +196,7 @@ fn main() -> ! { .with_rx(peripherals.GPIO5) .with_tx(peripherals.GPIO7); let mut display = Nextion::new(&mut uart0); - //we just set it to page0 to be sure + display.send_command(b"page page0"); let mut _rpm = DEFAULT_TARGET_RPM; let mut _timer = DEFAULT_SPIN_TIME; @@ -188,7 +209,6 @@ fn main() -> ! { } Ok(Command::Start) => { rprintln!("START"); - display.send_command(b"page page2"); started = true; } Ok(Command::Stop) => { @@ -215,63 +235,12 @@ fn main() -> ! { Err(ErrCommand::ReadError) => { rprintln!("READ FAILED!"); } - _ => { - rprintln!("This should never happen!") - } } // display.send_command(b"page page0"); } if started { - rprintln!("STARTING!"); - let _ = coat_timer - .load_value(esp_hal::time::Duration::from_secs(10)) - .expect("TODO: Could not set timer for coating! "); - dshot_esc.arm(); - let mut rpm_fail_ctr = 0; - coat_timer.start(); - while !coat_timer.is_interrupt_set() { - dshot_esc.process(); - //let control_val = dc_driver:: - //this bad boy needs floats, this will be fun :) - control = 200.00; - dshot_esc.set_throttle(control as _); - rpm = match dshot_esc.get_rpm() { - Some(x) => { - // rprintln!("GOT RPM {}", x); - x - } - None => { - rpm_fail_ctr+=1; - // rprintln!("NO RPM!"); - rpm - } - }; - // rprintln!("RPM:{}",rpm); - let last_time = esp_hal::time::Instant::now(); - - let timestamp = esp_hal::time::Instant::now() - .duration_since_epoch() - .as_millis(); - (control, ctx) = - controller.compute(ctx, rpm as _, set_point as _, Millis(timestamp), None); - let dumped_context = ctx.last_time().expect("LAST TIME!"); - rprintln!("control:{},rpm:{}", control,rpm); - //first we send the RPM! - if display.write_ready(){ - let running_rpm = format!("rpm.val={}", rpm); - display.send_command(running_rpm.to_string().as_bytes()); - } - if display.write_ready(){ - let counter = format!("counter.val={}", 1234); - display.send_command(counter.to_string().as_bytes()); - - } - } - - rprintln!("COATING done!"); - rprintln!("RPM_READ_FAILS {}",rpm_fail_ctr); - started = false; - display.send_command(b"page page0"); + // spincoater.armed(); + rprintln!("STARTING!") } } } diff --git a/spinnyboy_rust/src/bin/peripherals/nextion.rs b/spinnyboy_rust/src/bin/peripherals/nextion.rs index ce11981..b25ae65 100644 --- a/spinnyboy_rust/src/bin/peripherals/nextion.rs +++ b/spinnyboy_rust/src/bin/peripherals/nextion.rs @@ -123,9 +123,6 @@ impl<'a> Nextion<'a> { pub fn read_ready(&mut self) -> bool { self.interface.read_ready() } - pub fn write_ready(&mut self) -> bool { - self.interface.write_ready() - } fn reset(&mut self) { self.state = UartStatemachine::WaitingP; self.idx = 0;