Skip to content

Commit dbcff4a

Browse files
committed
Fix confusion with accel-to-decel/minimum-cruise-ratio config option
As documented in #61 and #62, there are issues with the config being all wrong when reading this. It appears to be caused by some kind of internal data race in Config. Refactor to have separate fields for the two options and recalculate accel_to_decel as appropriate.
1 parent ecd98f3 commit dbcff4a

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

lib/src/planner.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,10 @@ impl MoveSequence {
734734
pub struct PrinterLimits {
735735
pub max_velocity: f64,
736736
pub max_acceleration: f64,
737-
#[serde(flatten)]
738-
pub max_accel_to_decel: AccelToDecel,
737+
#[serde(default, skip_serializing_if = "Option::is_none")]
738+
pub max_accel_to_decel: Option<f64>,
739+
#[serde(default, skip_serializing_if = "Option::is_none")]
740+
pub minimum_cruise_ratio: Option<f64>,
739741
pub square_corner_velocity: f64,
740742
#[serde(skip)]
741743
pub junction_deviation: f64,
@@ -749,29 +751,13 @@ pub struct PrinterLimits {
749751
pub move_checkers: Vec<MoveChecker>,
750752
}
751753

752-
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
753-
pub enum AccelToDecel {
754-
#[serde(rename = "max_accel_to_decel")]
755-
Static(f64),
756-
#[serde(rename = "minimum_cruise_ratio")]
757-
CruiseRatio(f64),
758-
}
759-
760-
impl AccelToDecel {
761-
pub fn to_absolute(&self, accel: f64) -> f64 {
762-
match self {
763-
Self::Static(v) => v.min(accel),
764-
Self::CruiseRatio(v) => accel * (1.0 - v.clamp(0.0, 1.0)),
765-
}
766-
}
767-
}
768-
769754
impl Default for PrinterLimits {
770755
fn default() -> Self {
771756
PrinterLimits {
772757
max_velocity: 100.0,
773758
max_acceleration: 100.0,
774-
max_accel_to_decel: AccelToDecel::Static(50.0),
759+
max_accel_to_decel: Some(50.0),
760+
minimum_cruise_ratio: None,
775761
square_corner_velocity: 5.0,
776762
junction_deviation: Self::scv_to_jd(5.0, 100000.0),
777763
accel_to_decel: 50.0,
@@ -796,15 +782,18 @@ impl PrinterLimits {
796782
pub fn set_max_acceleration(&mut self, v: f64) {
797783
self.max_acceleration = v;
798784
self.update_junction_deviation();
785+
self.update_accel_to_decel();
799786
}
800787

801788
pub fn set_max_accel_to_decel(&mut self, v: f64) {
802-
self.max_accel_to_decel = AccelToDecel::Static(v);
789+
self.max_accel_to_decel = Some(v);
790+
self.minimum_cruise_ratio = None;
803791
self.update_accel_to_decel();
804792
}
805793

806794
pub fn set_minimum_cruise_ratio(&mut self, v: f64) {
807-
self.max_accel_to_decel = AccelToDecel::CruiseRatio(v.clamp(0.0, 1.0));
795+
self.max_accel_to_decel = None;
796+
self.minimum_cruise_ratio = Some(v.clamp(0.0, 1.0));
808797
self.update_accel_to_decel();
809798
}
810799

@@ -828,7 +817,11 @@ impl PrinterLimits {
828817
}
829818

830819
fn update_accel_to_decel(&mut self) {
831-
self.accel_to_decel = self.max_accel_to_decel.to_absolute(self.max_acceleration);
820+
self.accel_to_decel = match (self.minimum_cruise_ratio, self.max_accel_to_decel) {
821+
(Some(v), _) => self.max_acceleration * (1.0 - v.clamp(0.0, 1.0)),
822+
(_, Some(v)) => v.min(self.max_acceleration),
823+
_ => 50.0f64.min(self.max_acceleration),
824+
}
832825
}
833826
}
834827

tool/src/main.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ impl Opts {
8181
fn load_config(&self) -> anyhow::Result<PrinterLimits> {
8282
use config::Config;
8383

84-
let builder = Config::builder()
85-
.set_default(
86-
"max_accel_to_decel",
87-
Value::new(None, ValueKind::Float(50.0)),
88-
)
89-
.unwrap();
84+
let builder = Config::builder();
9085

9186
let builder = if let Some(url) = &self.config_moonraker {
9287
builder.add_source(MoonrakerSource::new(

0 commit comments

Comments
 (0)