Skip to content

Commit

Permalink
prevent divide-by-zero
Browse files Browse the repository at this point in the history
  • Loading branch information
mcells committed Oct 9, 2024
1 parent 8f0d2cb commit 9c9d0cb
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/common/base_classes/FOCMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
}

float resistance = voltage / (correction_factor * (r_currents.d - zerocurrent.d));
if (resistance <= 0.0f)
{
SIMPLEFOC_DEBUG("ERR: MOT: Motor characterisation failed: Calculated resistance <= 0");
return 4;
}

SIMPLEFOC_DEBUG("MOT: Estimated phase to phase resistance: ", 2.0f * resistance);
_delay(100);

Expand Down Expand Up @@ -182,7 +188,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){

// calculate the inductance
float dt = (t1 - t0)/1000000.0f;
if (l_currents.d - zerocurrent.d <= 0)
if (l_currents.d - zerocurrent.d <= 0 || (voltage - resistance * (l_currents.d - zerocurrent.d)) <= 0)
{
continue;
}
Expand Down

0 comments on commit 9c9d0cb

Please sign in to comment.