From ac78619b5b89461f56e32d8472d17b0f611a7488 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Tue, 31 Aug 2021 09:19:37 +0200 Subject: [PATCH] Cache TMC error flags inside movable_base class --- src/hal/tmc2130.h | 2 +- src/logic/command_base.cpp | 16 ++++++++-------- src/modules/movable_base.cpp | 3 ++- src/modules/movable_base.h | 6 ++++++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/hal/tmc2130.h b/src/hal/tmc2130.h index 9c8ac5f..6c29536 100644 --- a/src/hal/tmc2130.h +++ b/src/hal/tmc2130.h @@ -39,7 +39,7 @@ struct __attribute__((packed)) ErrorFlags { uint8_t s2g : 1; ///< short to ground uint8_t otpw : 1; ///< over temperature pre-warning uint8_t ot : 1; ///< over temperature hard - inline ErrorFlags() + inline constexpr ErrorFlags() : reset_flag(0) , uv_cp(0) , s2g(0) diff --git a/src/logic/command_base.cpp b/src/logic/command_base.cpp index f0abe5b..3893a9c 100644 --- a/src/logic/command_base.cpp +++ b/src/logic/command_base.cpp @@ -10,22 +10,22 @@ inline ErrorCode &operator|=(ErrorCode &a, ErrorCode b) { return a = (ErrorCode)((uint16_t)a | (uint16_t)b); } -static ErrorCode TMC2130ToErrorCode(const hal::tmc2130::TMC2130 &tmc, uint8_t tmcIndex) { +static ErrorCode TMC2130ToErrorCode(const hal::tmc2130::ErrorFlags &ef, uint8_t tmcIndex) { ErrorCode e = ErrorCode::RUNNING; - if (tmc.GetErrorFlags().reset_flag) { + if (ef.reset_flag) { e |= ErrorCode::TMC_RESET; } - if (tmc.GetErrorFlags().uv_cp) { + if (ef.uv_cp) { e |= ErrorCode::TMC_UNDERVOLTAGE_ON_CHARGE_PUMP; } - if (tmc.GetErrorFlags().s2g) { + if (ef.s2g) { e |= ErrorCode::TMC_SHORT_TO_GROUND; } - if (tmc.GetErrorFlags().otpw) { + if (ef.otpw) { e |= ErrorCode::TMC_OVER_TEMPERATURE_WARN; } - if (tmc.GetErrorFlags().ot) { + if (ef.ot) { e |= ErrorCode::TMC_OVER_TEMPERATURE_ERROR; } @@ -53,11 +53,11 @@ bool CommandBase::Step() { // check the global HW errors - may be we should avoid the modules layer and check for the HAL layer errors directly if (mi::idler.State() == mi::Idler::Failed) { state = ProgressCode::ERRTMCFailed; - tmcErr |= TMC2130ToErrorCode(mm::motion.DriverForAxis(mm::Axis::Idler), mm::Axis::Idler); + tmcErr |= TMC2130ToErrorCode(mi::idler.TMCErrorFlags(), mm::Axis::Idler); } if (ms::selector.State() == ms::Selector::Failed) { state = ProgressCode::ERRTMCFailed; - tmcErr |= TMC2130ToErrorCode(mm::motion.DriverForAxis(mm::Axis::Selector), mm::Axis::Selector); + tmcErr |= TMC2130ToErrorCode(ms::selector.TMCErrorFlags(), mm::Axis::Selector); } // may be we should model the Pulley as well... // if (ms::selector.State() == ms::Selector::Failed) { diff --git a/src/modules/movable_base.cpp b/src/modules/movable_base.cpp index 52d8378..9363993 100644 --- a/src/modules/movable_base.cpp +++ b/src/modules/movable_base.cpp @@ -16,8 +16,9 @@ MovableBase::OperationResult MovableBase::InitMovement(config::Axis axis) { } void MovableBase::PerformMove(config::Axis axis) { - if (!mm::motion.DriverForAxis(axis).GetErrorFlags().Good()) { + if (!mm::motion.DriverForAxis(axis).GetErrorFlags().Good()) { // @@TODO check occasionally, i.e. not every time? // TMC2130 entered some error state, the planned move couldn't have been finished - result of operation is Failed + tmcErrorFlags = mm::motion.DriverForAxis(axis).GetErrorFlags(); // save the failed state state = Failed; } else if (mm::motion.QueueEmpty(axis)) { // move finished diff --git a/src/modules/movable_base.h b/src/modules/movable_base.h index d02d9ca..82ca466 100644 --- a/src/modules/movable_base.h +++ b/src/modules/movable_base.h @@ -1,6 +1,7 @@ #pragma once #include #include "../config/axis.h" +#include "../hal/tmc2130.h" namespace modules { namespace motion { @@ -37,6 +38,8 @@ public: /// @returns internal state of the state machine inline uint8_t State() const { return state; } + inline hal::tmc2130::ErrorFlags TMCErrorFlags() const { return tmcErrorFlags; } + protected: /// internal state of the automaton uint8_t state; @@ -47,6 +50,9 @@ protected: /// current slot uint8_t currentSlot; + /// cached TMC2130 error flags - being read only if the axis is enabled and doing something (moving) + hal::tmc2130::ErrorFlags tmcErrorFlags; + virtual void PrepareMoveToPlannedSlot() = 0; OperationResult InitMovement(config::Axis axis);