Skip to content

Commit

Permalink
amcbldc: revised the EXT fAULT management (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoaccame authored Dec 28, 2021
1 parent 394a293 commit 6bfcbff
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

//#define TEST_DURATION_FOC

#define DISABLE_EXTFAULT
//#define EXTFAULT_enabled
#define EXTFAULT_handler_will_disable_motor

// --------------------------------------------------------------------------------------------------------------------
// - pimpl: private implementation (see scott meyers: item 22 of effective modern c++, item 31 of effective c++
Expand Down Expand Up @@ -223,9 +224,11 @@ struct embot::app::application::theMBDagent::Impl
bool applychanges {false};

static constexpr embot::hw::BTN buttonEXTfault {embot::hw::BTN::one};
static void onEXTFAULTpressed(void *owner);
volatile bool EXTFAULTisPRESSED {false};
static void onEXTFAULTpressedreleased(void *owner);
volatile bool EXTFAULTisPRESSED {false};
volatile bool prevEXTFAULTisPRESSED {false};
volatile embot::core::Time EXTFAULTpressedtime {0};
volatile embot::core::Time EXTFAULTreleasedtime {0};
};


Expand Down Expand Up @@ -257,16 +260,17 @@ bool embot::app::application::theMBDagent::Impl::initialise()
}

// init the external fault.
// we use a hw::button because we dont have a hw::switch
// if the HW is well filtered and the push is clean, then we can just
// call cbkOnEXTfault_pressed.execute() if the button is pressed.
// in the callback we set the FAULT on and in the ::tick() we must somehow set it off w/ polling
//

#if defined(DISABLE_EXTFAULT)
#else
embot::core::Callback cbkOnEXTFAULT_pressed {onEXTFAULTpressed, this};
embot::hw::button::init(buttonEXTfault, {embot::hw::button::Mode::TriggeredOnPress, cbkOnEXTFAULT_pressed, 0});
// we call cbkOnEXTfault.execute() when the button is pressed or released.
// in the callback we set EXTFAULTisPRESSED true or false depending on value of
// embot::hw::button::pressed(buttonEXTfault)
// we also disable the motors if true.

#if defined(EXTFAULT_enabled)
embot::core::Callback cbkOnEXTFAULT {onEXTFAULTpressedreleased, this};
embot::hw::button::init(buttonEXTfault, {embot::hw::button::Mode::TriggeredOnPressAndRelease, cbkOnEXTFAULT, 0});
prevEXTFAULTisPRESSED = EXTFAULTisPRESSED = embot::hw::button::pressed(buttonEXTfault);
#else
prevEXTFAULTisPRESSED = EXTFAULTisPRESSED = false;
#endif

// init MBD
Expand Down Expand Up @@ -313,37 +317,41 @@ bool embot::app::application::theMBDagent::Impl::initialise()
}


void embot::app::application::theMBDagent::Impl::onEXTFAULTpressed(void *owner)
void embot::app::application::theMBDagent::Impl::onEXTFAULTpressedreleased(void *owner)
{
Impl * impl = reinterpret_cast<Impl*>(owner);
if(nullptr == impl)
{
return;
}

impl->EXTFAULTpressedtime = embot::core::now();

// ok ... in case the debouncing is not required because the HW filters the spikes aways...
// we just set the bool variable EXTFAULTisPRESSED and disable the motor
// then it will be the ::tick() and the :;onCurrents_FOC_innerloop() that will manage EXTFAULTisPRESSED
impl->EXTFAULTisPRESSED = true;

#if defined(DISABLE_EXTFAULT)
#else
embot::hw::motor::setpwmUVW(embot::hw::MOTOR::one, 0, 0, 0);
embot::hw::motor::enable(embot::hw::MOTOR::one, false);
#endif
impl->EXTFAULTisPRESSED = embot::hw::button::pressed(buttonEXTfault);

// if the transition high->low and viceversa is noisy, then we could set EXTFAULTisPRESSED to true
// inside ::tick() a few ms after EXTFAULTpressedtime
if(true == impl->EXTFAULTisPRESSED)
{
impl->EXTFAULTpressedtime = embot::core::now();
#if defined(EXTFAULT_handler_will_disable_motor)
embot::hw::motor::setpwmUVW(embot::hw::MOTOR::one, 0, 0, 0);
embot::hw::motor::enable(embot::hw::MOTOR::one, false);
#endif
}
else
{
impl->EXTFAULTreleasedtime = embot::core::now();
}
}

// Called every 1 ms
bool embot::app::application::theMBDagent::Impl::tick(const std::vector<embot::prot::can::Frame> &inpframes, std::vector<embot::prot::can::Frame> &outframes)
{
measureTick->start();

// remember to manage EXTFAULTisPRESSED ............
if(prevEXTFAULTisPRESSED != EXTFAULTisPRESSED)
{
prevEXTFAULTisPRESSED = EXTFAULTisPRESSED;
// and manage the transitions [pressed -> unpressed] or vice-versa and use also
// EXTFAULTpressedtime and / or EXTFAULTreleasedtime and
}

uint8_t rx_data[8] {0};
uint8_t rx_size {0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

//#define TEST_DURATION_FOC

#define DISABLE_EXTFAULT
//#define EXTFAULT_enabled
#define EXTFAULT_handler_will_disable_motor

// --------------------------------------------------------------------------------------------------------------------
// - pimpl: private implementation (see scott meyers: item 22 of effective modern c++, item 31 of effective c++
Expand Down Expand Up @@ -176,9 +177,11 @@ struct embot::app::application::theMBDagent::Impl
bool applychanges {false};

static constexpr embot::hw::BTN buttonEXTfault {embot::hw::BTN::one};
static void onEXTFAULTpressed(void *owner);
volatile bool EXTFAULTisPRESSED {false};
static void onEXTFAULTpressedreleased(void *owner);
volatile bool EXTFAULTisPRESSED {false};
volatile bool prevEXTFAULTisPRESSED {false};
volatile embot::core::Time EXTFAULTpressedtime {0};
volatile embot::core::Time EXTFAULTreleasedtime {0};
};


Expand Down Expand Up @@ -210,16 +213,17 @@ bool embot::app::application::theMBDagent::Impl::initialise()
}

// init the external fault.
// we use a hw::button because we dont have a hw::switch
// if the HW is well filtered and the push is clean, then we can just
// call cbkOnEXTfault_pressed.execute() if the button is pressed.
// in the callback we set the FAULT on and in the ::tick() we must somehow set it off w/ polling
//

#if defined(DISABLE_EXTFAULT)
#else
embot::core::Callback cbkOnEXTFAULT_pressed {onEXTFAULTpressed, this};
embot::hw::button::init(buttonEXTfault, {embot::hw::button::Mode::TriggeredOnPress, cbkOnEXTFAULT_pressed, 0});
// we call cbkOnEXTfault.execute() when the button is pressed or released.
// in the callback we set EXTFAULTisPRESSED true or false depending on value of
// embot::hw::button::pressed(buttonEXTfault)
// we also disable the motors if true.

#if defined(EXTFAULT_enabled)
embot::core::Callback cbkOnEXTFAULT {onEXTFAULTpressedreleased, this};
embot::hw::button::init(buttonEXTfault, {embot::hw::button::Mode::TriggeredOnPressAndRelease, cbkOnEXTFAULT, 0});
prevEXTFAULTisPRESSED = EXTFAULTisPRESSED = embot::hw::button::pressed(buttonEXTfault);
#else
prevEXTFAULTisPRESSED = EXTFAULTisPRESSED = false;
#endif

// init MBD
Expand All @@ -238,37 +242,41 @@ bool embot::app::application::theMBDagent::Impl::initialise()
}


void embot::app::application::theMBDagent::Impl::onEXTFAULTpressed(void *owner)
void embot::app::application::theMBDagent::Impl::onEXTFAULTpressedreleased(void *owner)
{
Impl * impl = reinterpret_cast<Impl*>(owner);
if(nullptr == impl)
{
return;
}

impl->EXTFAULTpressedtime = embot::core::now();

// ok ... in case the debouncing is not required because the HW filters the spikes aways...
// we just set the bool variable EXTFAULTisPRESSED and disable the motor
// then it will be the ::tick() and the :;onCurrents_FOC_innerloop() that will manage EXTFAULTisPRESSED
impl->EXTFAULTisPRESSED = true;

#if defined(DISABLE_EXTFAULT)
#else
embot::hw::motor::setpwmUVW(embot::hw::MOTOR::one, 0, 0, 0);
embot::hw::motor::enable(embot::hw::MOTOR::one, false);
#endif
impl->EXTFAULTisPRESSED = embot::hw::button::pressed(buttonEXTfault);

// if the transition high->low and viceversa is noisy, then we could set EXTFAULTisPRESSED to true
// inside ::tick() a few ms after EXTFAULTpressedtime
if(true == impl->EXTFAULTisPRESSED)
{
impl->EXTFAULTpressedtime = embot::core::now();
#if defined(EXTFAULT_handler_will_disable_motor)
embot::hw::motor::setpwmUVW(embot::hw::MOTOR::one, 0, 0, 0);
embot::hw::motor::enable(embot::hw::MOTOR::one, false);
#endif
}
else
{
impl->EXTFAULTreleasedtime = embot::core::now();
}
}

// Called every 1 ms
bool embot::app::application::theMBDagent::Impl::tick(const std::vector<embot::prot::can::Frame> &inpframes, std::vector<embot::prot::can::Frame> &outframes)
{
measureTick->start();

// remember to manage EXTFAULTisPRESSED ............
if(prevEXTFAULTisPRESSED != EXTFAULTisPRESSED)
{
prevEXTFAULTisPRESSED = EXTFAULTisPRESSED;
// and manage the transitions [pressed -> unpressed] or vice-versa and use also
// EXTFAULTpressedtime and / or EXTFAULTreleasedtime and
}

uint8_t rx_data[8] {0};
uint8_t rx_size {0};
Expand Down

0 comments on commit 6bfcbff

Please sign in to comment.