Compare commits

..

2 commits

Author SHA1 Message Date
Robert Schauklies
14a61d2f84 magic fafo constant found 2026-03-14 18:23:11 +01:00
Robert Schauklies
0e97a15fab just testing with the magic value 2026-03-14 18:07:23 +01:00
4 changed files with 56 additions and 122 deletions

View file

@ -17,6 +17,7 @@ IFlight XING-E Pro 2207 2750KV
*/ */
const XING_EPRO_22_POLES: u32 = 4; 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(dead_code)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Debug)] #[derive(Debug)]
@ -323,7 +324,8 @@ impl<'a> DShot<'a> {
let erpm = (60 * 1_000_000) / period_ms; let erpm = (60 * 1_000_000) / period_ms;
let rpm = erpm/(XING_EPRO_22_MAGNETS/2); 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 _))
} }
} }
} }

View file

@ -12,18 +12,18 @@ use esp_hal::delay::Delay;
use esp_hal::uart::{Config, Uart}; use esp_hal::uart::{Config, Uart};
use esp_hal::{gpio, main}; use esp_hal::{gpio, main};
use core::fmt::Debug;
use esp_hal::gpio::{Event, OutputConfig}; use esp_hal::gpio::{Event, OutputConfig};
use esp_hal::gpio::{Input, InputConfig}; use esp_hal::gpio::{Input, InputConfig};
use esp_hal::handler; use esp_hal::handler;
use esp_hal::rmt::{Rmt, TxChannelConfig, TxChannelCreator}; 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 //pid-controller
use core::time::Duration; use core::time::Duration;
use discrete_pid::pid::PidContext; use discrete_pid::pid::PidContext;
use discrete_pid::time::InstantLike;
use discrete_pid::time::Millis; use discrete_pid::time::Millis;
use discrete_pid::{pid, time}; use discrete_pid::{pid, time};
use discrete_pid::time::InstantLike;
use core::cell::RefCell; use core::cell::RefCell;
use core::mem::forget; use core::mem::forget;
@ -43,16 +43,12 @@ use crate::dc_driver::dshot;
use crate::peripherals::ErrCommand; use crate::peripherals::ErrCommand;
use esp_hal::rmt::RxChannelConfig; use esp_hal::rmt::RxChannelConfig;
use esp_hal::rmt::RxChannelCreator; use esp_hal::rmt::RxChannelCreator;
use esp_hal::timer::OneShotTimer;
use esp_hal::timer::Timer;
use esp_hal::timer::timg::TimerGroup;
#[panic_handler] #[panic_handler]
fn panic(panic: &core::panic::PanicInfo) -> ! { fn panic(panic: &core::panic::PanicInfo) -> ! {
rprintln!("{}", panic.message()); rprintln!("{}",panic.message());
let trace = Backtrace::capture(); let trace = Backtrace::capture();
loop { loop {
rprintln!("{}", panic.location().unwrap()) // rprintln!("{}",panic.location().unwrap())
} }
} }
use alloc::format; use alloc::format;
@ -62,8 +58,7 @@ extern crate alloc;
//target RPM //target RPM
const DEFAULT_TARGET_RPM: u32 = 4000; const DEFAULT_TARGET_RPM: u32 = 4000;
//in seconds //in seconds
const DEFAULT_SPIN_TIME: u64 = 10; const DEFAULT_SPIN_TIME: u32 = 10;
const DEFAULT_DRY_SPIN_TIME: u64 = 10;
// This creates a default app-descriptor required by the esp-idf bootloader. // This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description> // For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!(); esp_bootloader_esp_idf::esp_app_desc!();
@ -83,6 +78,8 @@ impl InstantLike for Time {
} }
} }
#[main] #[main]
fn main() -> ! { fn main() -> ! {
// generator version: 1.0.1 // generator version: 1.0.1
@ -130,18 +127,43 @@ fn main() -> ! {
let loop_time = Duration::from_micros(10); let loop_time = Duration::from_micros(10);
let cfg = pid::PidConfigBuilder::default() let cfg = pid::PidConfigBuilder::default()
.kp(2.0) .kp(2.0)
.ki(0.4) .ki(0.1).output_limits(100.00, 2040.00)
.output_limits(100.00, 400.00)
.sample_time(loop_time) .sample_time(loop_time)
.filter_tc(0.1) .filter_tc(0.1)
.build() .build()
.expect("Failed to build a PID configuration"); .expect("Failed to build a PID configuration");
let controller = pid::FuncPidController::new(cfg); let controller = pid::FuncPidController::new(cfg);
let mut ctx = PidContext::new(Millis(0), 0.0, 0.0); let mut ctx = PidContext::new(Millis(0), 0.0, 0.0);
let mut control: f32 = 200.0; let set_point = DEFAULT_TARGET_RPM;
let mut timg0 = TimerGroup::new(peripherals.TIMG0); dshot_esc.arm();
let coat_timer = timg0.timer0; let mut rpm = 0;
// 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);; // let mut esc = AfroEsc::new(&mut pwm_pin);;
// esc.set_timestamp(1000); // esc.set_timestamp(1000);
// delay.delay_millis(3000); // delay.delay_millis(3000);
@ -174,12 +196,10 @@ fn main() -> ! {
.with_rx(peripherals.GPIO5) .with_rx(peripherals.GPIO5)
.with_tx(peripherals.GPIO7); .with_tx(peripherals.GPIO7);
let mut display = Nextion::new(&mut uart0); let mut display = Nextion::new(&mut uart0);
//we just set it to page0 to be sure
display.send_command(b"page page0"); display.send_command(b"page page0");
let mut target_rpm = DEFAULT_TARGET_RPM; let mut _rpm = DEFAULT_TARGET_RPM;
let mut timer: u64 = DEFAULT_SPIN_TIME; let mut _timer = DEFAULT_SPIN_TIME;
let mut target_dry_rpm = DEFAULT_DRY_SPIN_TIME;
let mut dry_timer: u64 = DEFAULT_SPIN_TIME;
let mut started = false; let mut started = false;
loop { loop {
if display.read_ready() { if display.read_ready() {
@ -189,7 +209,6 @@ fn main() -> ! {
} }
Ok(Command::Start) => { Ok(Command::Start) => {
rprintln!("START"); rprintln!("START");
display.send_command(b"page page2");
started = true; started = true;
} }
Ok(Command::Stop) => { Ok(Command::Stop) => {
@ -199,19 +218,11 @@ fn main() -> ! {
} }
Ok(Command::SetRpm(x)) => { Ok(Command::SetRpm(x)) => {
rprintln!("SET_RPM with {}", x); rprintln!("SET_RPM with {}", x);
target_rpm = x; _rpm = x;
} }
Ok(Command::SetTimer(x)) => { Ok(Command::SetTimer(x)) => {
rprintln!("SETTING TIMER {}", x); rprintln!("SETTING TIMER {}", x);
timer = x as _; _timer = x;
}
Ok(Command::SetRpm2(x)) => {
rprintln!("SET_RPM with {}", x);
target_dry_rpm = x.try_into().expect("Couldn't cast RPM2 to u64");
}
Ok(Command::SetTimer2(x)) => {
rprintln!("SETTING TIMER {}", x);
dry_timer = x as _;
} }
Ok(Command::SendConfig) => { Ok(Command::SendConfig) => {
rprintln!("SEND CONFIG"); rprintln!("SEND CONFIG");
@ -224,76 +235,12 @@ fn main() -> ! {
Err(ErrCommand::ReadError) => { Err(ErrCommand::ReadError) => {
rprintln!("READ FAILED!"); rprintln!("READ FAILED!");
} }
_ => {
rprintln!("This should never happen!")
}
} }
// display.send_command(b"page page0"); // display.send_command(b"page page0");
} }
if started { if started {
rprintln!("STARTING!"); // spincoater.armed();
let _ = coat_timer rprintln!("STARTING!")
.load_value(esp_hal::time::Duration::from_secs(timer))
.expect("TODO: Could not set timer for coating! ");
dshot_esc.arm();
let mut rpm_fail_ctr = 0;
let set_point = target_rpm;
coat_timer.start();
let mut current_rpm = 0;
//first round!
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 _);
current_rpm = match dshot_esc.get_rpm() {
Some(x) => {
// rprintln!("GOT RPM {}", x);
x
}
None => {
rpm_fail_ctr += 1;
// rprintln!("NO RPM!");
current_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,
current_rpm as _,
set_point as _,
Millis(timestamp),
None,
);
let dumped_context = ctx.last_time().expect("LAST TIME!");
rprintln!("control:{},rpm:{}", control, current_rpm);
//first we send the RPM!
if display.write_ready() {
let running_rpm = format!("rpm1.val={}", current_rpm);
display.send_command(running_rpm.to_string().as_bytes());
let counter = format!("counter.val={}", 1234);
display.send_command(counter.to_string().as_bytes());
}
if display.read_ready(){
match display.read_command() {
Ok(Command::DoDryRun) =>{
break;
},
_ => {//we don't care about the rest}
}
}
}
rprintln!("COATING done!");
rprintln!("RPM_READ_FAILS {}", rpm_fail_ctr);
started = false;
display.send_command(b"page page3");
} }
} }
} }

View file

@ -7,9 +7,6 @@ pub enum Command {
Stop, Stop,
SendConfig, SendConfig,
CommandSuccess, CommandSuccess,
SetRpm2(u32),
SetTimer2(u32),
DoDryRun,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ErrCommand { pub enum ErrCommand {

View file

@ -116,15 +116,6 @@ impl<'a> Nextion<'a> {
Ok(Command::SetTimer(time)) Ok(Command::SetTimer(time))
} }
05 => Ok(Command::SendConfig), 05 => Ok(Command::SendConfig),
06 => {
let rpm = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse rpm!"));
Ok(Command::SetRpm2(rpm))
}
07 => {
let time = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse rpm!"));
Ok(Command::SetTimer2(time))
}
08 => Ok(Command::DoDryRun),
00 => Ok(Command::CommandSuccess), 00 => Ok(Command::CommandSuccess),
_ => Err(ErrCommand::NoValidCmd), _ => Err(ErrCommand::NoValidCmd),
} }
@ -132,9 +123,6 @@ impl<'a> Nextion<'a> {
pub fn read_ready(&mut self) -> bool { pub fn read_ready(&mut self) -> bool {
self.interface.read_ready() self.interface.read_ready()
} }
pub fn write_ready(&mut self) -> bool {
self.interface.write_ready()
}
fn reset(&mut self) { fn reset(&mut self) {
self.state = UartStatemachine::WaitingP; self.state = UartStatemachine::WaitingP;
self.idx = 0; self.idx = 0;