diff --git a/src/logic/unload_to_finda.cpp b/src/logic/unload_to_finda.cpp index 0ea887d..8896a03 100644 --- a/src/logic/unload_to_finda.cpp +++ b/src/logic/unload_to_finda.cpp @@ -24,7 +24,7 @@ void UnloadToFinda::Reset(uint8_t maxTries) { // @@TODO this may end up somewhere else as more code may need to check the distance traveled by the filament int32_t CurrentPositionPulley_mm() { - return mm::stepsToUnit(mm::P_pos_t({ mm::motion.CurPosition(mm::Pulley) })); + return mm::axisUnitToTruncatedUnit(mm::motion.CurPosition()); } bool UnloadToFinda::Step() { @@ -48,7 +48,7 @@ bool UnloadToFinda::Step() { return false; case WaitingForFINDA: { int32_t currentPulley_mm = CurrentPositionPulley_mm(); - if ((abs(unloadStart_mm - currentPulley_mm) > config::fsensorUnloadCheckDistance.v) && mfs::fsensor.Pressed()) { + if ((abs(unloadStart_mm - currentPulley_mm) > mm::truncatedUnit(config::fsensorUnloadCheckDistance)) && mfs::fsensor.Pressed()) { // fsensor didn't trigger within the first fsensorUnloadCheckDistance mm -> stop pulling, something failed, report an error // This scenario should not be tried again - repeating it may cause more damage to filament + potentially more collateral damage state = FailedFSensor; diff --git a/src/modules/axisunit.h b/src/modules/axisunit.h index faa59d7..ba57611 100644 --- a/src/modules/axisunit.h +++ b/src/modules/axisunit.h @@ -112,14 +112,30 @@ static constexpr AU unitToAxisUnit(U v) { return { (typename AU::type_t)(v.v * axisScale[AU::axis].stepsPerUnit) }; } -/// Convert an AxisUnit to unit::Unit. -/// The scaling factor is stored with the pair config::AxisConfig::uSteps and -/// config::AxisConfig::stepsPerUnit (one per-axis). -template -static constexpr typename U::type_t axisUnitToUnit(AU v) { +/// Convert an AxisUnit to a physical unit with a truncated integer type (normally int32_t). +/// Inverse of unitToAxisUnit, with the additional constraint that type casts are +/// performed earlier so that no floating point computation is required at runtime. +/// @param U Base unit type (normally U_mm) +/// @param AU AxisUnit type (implicit) +/// @param T Resulting integer type +/// @param v Value to truncate +/// @param mul Optional pre-multiplier +/// @returns Truncated unit (v * mul) +/// @see unitToAxisUnit +template +static constexpr T axisUnitToTruncatedUnit(AU v, long double mul = 1.) { static_assert(AU::unit == U::unit, "incorrect unit type conversion"); - //static_assert(U::base == axisScale[AU::axis].base, "incorrect unit base conversion"); - return { (typename U::type_t)(v.v / axisScale[AU::axis].stepsPerUnit) }; + static_assert(U::base == axisScale[AU::axis].base, "incorrect unit base conversion"); + return { ((T)v.v / (T)(axisScale[AU::axis].stepsPerUnit / mul)) }; +} + +/// Truncate an Unit type to an integer (normally int32_t) +/// @param v Value to truncate +/// @param mul Optional pre-multiplier +/// @returns Truncated unit (v * mul) +template +static constexpr T truncatedUnit(U v, long double mul = 1.) { + return (T)(v.v * mul); } /// Convert a unit::Unit to a steps type (pos_t or steps_t). @@ -129,13 +145,6 @@ static constexpr typename AU::type_t unitToSteps(U v) { return unitToAxisUnit(v).v; } -/// Convert a steps type (pos_t or steps_t) to a unit::Unit. -/// Extract the raw step count from an AxisUnit with type checking. -template -static constexpr typename U::type_t stepsToUnit(AU pos) { - return axisUnitToUnit(pos); -} - // Pulley typedef AxisUnit P_pos_t; ///< Pulley position type (steps) typedef AxisUnit P_speed_t; ///< Pulley speed type (steps/s)