industry/foc/fixed16/foc_ident.c: add b16_t overflow protection

This commit is contained in:
raiden00pl 2022-08-27 17:59:12 +02:00 committed by Xiang Xiao
parent 4beb637925
commit 8609c92081

View File

@ -39,6 +39,7 @@
****************************************************************************/ ****************************************************************************/
#define IDENT_PI_KP (ftob16(0.0f)) #define IDENT_PI_KP (ftob16(0.0f))
#define B16UPPERLIMIT (0x7e000000)
/**************************************************************************** /****************************************************************************
* Private Data Types * Private Data Types
@ -211,6 +212,15 @@ int foc_ident_res_run_b16(FAR struct foc_ident_b16_s *ident,
ident->curr_sum += vector2d_mag_b16(in->foc_state->idq.q, ident->curr_sum += vector2d_mag_b16(in->foc_state->idq.q,
in->foc_state->idq.d); in->foc_state->idq.d);
} }
/* Overflow protection */
if (ident->volt_sum > B16UPPERLIMIT || ident->curr_sum > B16UPPERLIMIT)
{
ret = -EOVERFLOW;
goto errout;
}
if (ident->cntr > ident->cfg.res_steps) if (ident->cntr > ident->cfg.res_steps)
{ {
/* Get resistance */ /* Get resistance */
@ -243,6 +253,7 @@ int foc_ident_res_run_b16(FAR struct foc_ident_b16_s *ident,
ident->volt_sum = 0; ident->volt_sum = 0;
} }
errout:
return ret; return ret;
} }
@ -289,6 +300,14 @@ int foc_ident_ind_run_b16(FAR struct foc_ident_b16_s *ident,
ident->curr2_sum += in->foc_state->idq.d; ident->curr2_sum += in->foc_state->idq.d;
} }
/* Overflow protection */
if (ident->curr1_sum > B16UPPERLIMIT || ident->curr2_sum > B16UPPERLIMIT)
{
ret = -EOVERFLOW;
goto errout;
}
/* Invert voltage to generate square wave D voltage */ /* Invert voltage to generate square wave D voltage */
ident->sign = -ident->sign; ident->sign = -ident->sign;
@ -357,6 +376,7 @@ int foc_ident_ind_run_b16(FAR struct foc_ident_b16_s *ident,
ident->curr2_sum = 0; ident->curr2_sum = 0;
} }
errout:
return ret; return ret;
} }