You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/11/03 18:08:17 UTC

[incubator-nuttx-apps] 03/03: industry/foc/fixed16/foc_ident.c: add b16_t overflow protection

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 8609c92081e4a3f0fad462000748f17ff1165f01
Author: raiden00pl <ra...@railab.me>
AuthorDate: Sat Aug 27 17:59:12 2022 +0200

    industry/foc/fixed16/foc_ident.c: add b16_t overflow protection
---
 industry/foc/fixed16/foc_ident.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/industry/foc/fixed16/foc_ident.c b/industry/foc/fixed16/foc_ident.c
index fbc8eab78..0ac5bb26b 100644
--- a/industry/foc/fixed16/foc_ident.c
+++ b/industry/foc/fixed16/foc_ident.c
@@ -39,6 +39,7 @@
  ****************************************************************************/
 
 #define IDENT_PI_KP           (ftob16(0.0f))
+#define B16UPPERLIMIT         (0x7e000000)
 
 /****************************************************************************
  * 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,
                                           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)
     {
       /* Get resistance */
@@ -243,6 +253,7 @@ int foc_ident_res_run_b16(FAR struct foc_ident_b16_s *ident,
       ident->volt_sum = 0;
     }
 
+errout:
   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;
     }
 
+  /* Overflow protection */
+
+  if (ident->curr1_sum > B16UPPERLIMIT || ident->curr2_sum > B16UPPERLIMIT)
+    {
+      ret = -EOVERFLOW;
+      goto errout;
+    }
+
   /* Invert voltage to generate square wave D voltage */
 
   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;
     }
 
+errout:
   return ret;
 }