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:07:23 UTC

[incubator-nuttx-apps] 02/06: examples/foc: separate control thread configuration from general args

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 b1f91528ecb3d02cdbeeaa430570fca8786c31b2
Author: raiden00pl <ra...@railab.me>
AuthorDate: Sat Oct 22 18:06:06 2022 +0200

    examples/foc: separate control thread configuration from general args
---
 examples/foc/foc_cfg.h       | 28 ++++++++++++++++++
 examples/foc/foc_main.c      | 67 +++++++++++++++++++++++---------------------
 examples/foc/foc_parseargs.c | 16 +++++------
 examples/foc/foc_parseargs.h | 28 ++++--------------
 4 files changed, 76 insertions(+), 63 deletions(-)

diff --git a/examples/foc/foc_cfg.h b/examples/foc/foc_cfg.h
index 4874aed58..d80ff25b9 100644
--- a/examples/foc/foc_cfg.h
+++ b/examples/foc/foc_cfg.h
@@ -210,4 +210,32 @@
 #  endif
 #endif
 
+/****************************************************************************
+ * Public Type Definition
+ ****************************************************************************/
+
+struct foc_thr_cfg_s
+{
+  int      fmode;               /* FOC control mode */
+  int      mmode;               /* Motor control mode */
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
+  int      qparam;              /* Open-loop Q setting (x1000) */
+#endif
+
+#ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
+  uint32_t foc_pi_kp;           /* FOC PI Kp (x1000) */
+  uint32_t foc_pi_ki;           /* FOC PI Ki (x1000) */
+#endif
+
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
+  uint32_t torqmax;             /* Torque max (x1000) */
+#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
+  uint32_t velmax;              /* Velocity max (x1000) */
+#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
+  uint32_t posmax;              /* Position max (x1000) */
+#endif
+};
+
 #endif /* __APPS_EXAMPLES_FOC_FOC_CFG_H */
diff --git a/examples/foc/foc_main.c b/examples/foc/foc_main.c
index d9965ec51..7ab3eb3b3 100644
--- a/examples/foc/foc_main.c
+++ b/examples/foc/foc_main.c
@@ -62,40 +62,43 @@
 struct args_s g_args =
 {
   .time  = CONFIG_EXAMPLES_FOC_TIME_DEFAULT,
-  .fmode = CONFIG_EXAMPLES_FOC_FMODE,
-  .mmode = CONFIG_EXAMPLES_FOC_MMODE,
+  .state = CONFIG_EXAMPLES_FOC_STATE_INIT,
+  .en    = INST_EN_DEFAULT,
+  .cfg =
+  {
+    .fmode = CONFIG_EXAMPLES_FOC_FMODE,
+    .mmode = CONFIG_EXAMPLES_FOC_MMODE,
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
-  .qparam = CONFIG_EXAMPLES_FOC_OPENLOOP_Q,
+    .qparam = CONFIG_EXAMPLES_FOC_OPENLOOP_Q,
 #endif
 
 #ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
-  .foc_pi_kp = CONFIG_EXAMPLES_FOC_IDQ_KP,
-  .foc_pi_ki = CONFIG_EXAMPLES_FOC_IDQ_KI,
+    .foc_pi_kp = CONFIG_EXAMPLES_FOC_IDQ_KP,
+    .foc_pi_ki = CONFIG_EXAMPLES_FOC_IDQ_KI,
 #endif
 
-  .state = CONFIG_EXAMPLES_FOC_STATE_INIT,
-  .en    = INST_EN_DEFAULT,
 #ifdef CONFIG_EXAMPLES_FOC_SETPOINT_CONST
 #  ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
-  .torqmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
+    .torqmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
 #  endif
 #  ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
-  .velmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
+    .velmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
 #  endif
 #  ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
-  .posmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
+    .posmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
 #  endif
 #else
 #  ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
-  .torqmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
+    .torqmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
 #  endif
 #  ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
-  .velmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
+    .velmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
 #  endif
 #  ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
-  .posmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
+    .posmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
 #  endif
 #endif
+  }
 };
 
 /****************************************************************************
@@ -113,7 +116,7 @@ static int validate_args(FAR struct args_s *args)
 #ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
   /* Current PI controller */
 
-  if (args->foc_pi_kp == 0 && args->foc_pi_ki == 0)
+  if (args->cfg.foc_pi_kp == 0 && args->cfg.foc_pi_ki == 0)
     {
       PRINTF("ERROR: missing FOC Kp/Ki configuration\n");
       goto errout;
@@ -122,11 +125,11 @@ static int validate_args(FAR struct args_s *args)
 
   /* FOC operation mode */
 
-  if (args->fmode != FOC_FMODE_IDLE &&
-      args->fmode != FOC_FMODE_VOLTAGE &&
-      args->fmode != FOC_FMODE_CURRENT)
+  if (args->cfg.fmode != FOC_FMODE_IDLE &&
+      args->cfg.fmode != FOC_FMODE_VOLTAGE &&
+      args->cfg.fmode != FOC_FMODE_CURRENT)
     {
-      PRINTF("Invalid op mode value %d s\n", args->fmode);
+      PRINTF("Invalid op mode value %d s\n", args->cfg.fmode);
       goto errout;
     }
 
@@ -134,23 +137,23 @@ static int validate_args(FAR struct args_s *args)
 
   if (
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
-    args->mmode != FOC_MMODE_TORQ &&
+    args->cfg.mmode != FOC_MMODE_TORQ &&
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
-    args->mmode != FOC_MMODE_VEL &&
+    args->cfg.mmode != FOC_MMODE_VEL &&
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
-    args->mmode != FOC_MMODE_POS &&
+    args->cfg.mmode != FOC_MMODE_POS &&
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_ALIGN
-    args->mmode != FOC_MMODE_ALIGN_ONLY &&
+    args->cfg.mmode != FOC_MMODE_ALIGN_ONLY &&
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_IDENT
-    args->mmode != FOC_MMODE_IDENT_ONLY &&
+    args->cfg.mmode != FOC_MMODE_IDENT_ONLY &&
 #endif
     1)
     {
-      PRINTF("Invalid ctrl mode value %d s\n", args->mmode);
+      PRINTF("Invalid ctrl mode value %d s\n", args->cfg.mmode);
       goto errout;
     }
 
@@ -341,22 +344,22 @@ int main(int argc, char *argv[])
       /* Get configuration */
 
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
-      foc[i].qparam   = g_args.qparam;
+      foc[i].qparam   = g_args.cfg.qparam;
 #endif
-      foc[i].fmode    = g_args.fmode;
-      foc[i].mmode    = g_args.mmode;
+      foc[i].fmode    = g_args.cfg.fmode;
+      foc[i].mmode    = g_args.cfg.mmode;
 #ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
-      foc[i].foc_pi_kp = g_args.foc_pi_kp;
-      foc[i].foc_pi_ki = g_args.foc_pi_ki;
+      foc[i].foc_pi_kp = g_args.cfg.foc_pi_kp;
+      foc[i].foc_pi_ki = g_args.cfg.foc_pi_ki;
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
-      foc[i].torqmax  = g_args.torqmax;
+      foc[i].torqmax  = g_args.cfg.torqmax;
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
-      foc[i].velmax   = g_args.velmax;
+      foc[i].velmax   = g_args.cfg.velmax;
 #endif
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
-      foc[i].posmax   = g_args.posmax;
+      foc[i].posmax   = g_args.cfg.posmax;
 #endif
 
       if (g_args.en & (1 << i))
diff --git a/examples/foc/foc_parseargs.c b/examples/foc/foc_parseargs.c
index 338c8eb90..9859e49a8 100644
--- a/examples/foc/foc_parseargs.c
+++ b/examples/foc/foc_parseargs.c
@@ -156,13 +156,13 @@ void parse_args(FAR struct args_s *args, int argc, FAR char **argv)
 #ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
           case OPT_FKP:
             {
-              args->foc_pi_kp = atoi(optarg);
+              args->cfg.foc_pi_kp = atoi(optarg);
               break;
             }
 
           case OPT_FKI:
             {
-              args->foc_pi_ki = atoi(optarg);
+              args->cfg.foc_pi_ki = atoi(optarg);
               break;
             }
 #endif
@@ -181,20 +181,20 @@ void parse_args(FAR struct args_s *args, int argc, FAR char **argv)
 
           case 'f':
             {
-              args->fmode = atoi(optarg);
+              args->cfg.fmode = atoi(optarg);
               break;
             }
 
           case 'm':
             {
-              args->mmode = atoi(optarg);
+              args->cfg.mmode = atoi(optarg);
               break;
             }
 
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
           case 'r':
             {
-              args->torqmax = atoi(optarg);
+              args->cfg.torqmax = atoi(optarg);
               break;
             }
 #endif
@@ -202,7 +202,7 @@ void parse_args(FAR struct args_s *args, int argc, FAR char **argv)
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
           case 'v':
             {
-              args->velmax = atoi(optarg);
+              args->cfg.velmax = atoi(optarg);
               break;
             }
 #endif
@@ -210,7 +210,7 @@ void parse_args(FAR struct args_s *args, int argc, FAR char **argv)
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
           case 'x':
             {
-              args->posmax = atoi(optarg);
+              args->cfg.posmax = atoi(optarg);
               break;
             }
 #endif
@@ -230,7 +230,7 @@ void parse_args(FAR struct args_s *args, int argc, FAR char **argv)
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
           case 'o':
             {
-              args->qparam = atoi(optarg);
+              args->cfg.qparam = atoi(optarg);
               break;
             }
 #endif
diff --git a/examples/foc/foc_parseargs.h b/examples/foc/foc_parseargs.h
index ad3759f9c..43b137d4f 100644
--- a/examples/foc/foc_parseargs.h
+++ b/examples/foc/foc_parseargs.h
@@ -28,6 +28,7 @@
 #include <nuttx/config.h>
 
 #include "foc_device.h"
+#include "foc_cfg.h"
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -41,29 +42,10 @@
 
 struct args_s
 {
-  int      time;                /* Run time limit in sec, -1 if forever */
-  int      fmode;               /* FOC control mode */
-  int      mmode;               /* Motor control mode */
-#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
-  int      qparam;              /* Open-loop Q setting (x1000) */
-#endif
-
-#ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
-  uint32_t foc_pi_kp;           /* FOC PI Kp (x1000) */
-  uint32_t foc_pi_ki;           /* FOC PI Ki (x1000) */
-#endif
-
-  int      state;               /* Example state (FREE, CW, CCW, STOP) */
-  int8_t   en;                  /* Enabled instances (bit-encoded) */
-#ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
-  uint32_t torqmax;             /* Torque max (x1000) */
-#endif
-#ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
-  uint32_t velmax;              /* Velocity max (x1000) */
-#endif
-#ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
-  uint32_t posmax;              /* Position max (x1000) */
-#endif
+  int                  time;    /* Run time limit in sec, -1 if forever */
+  int                  state;   /* Example state (FREE, CW, CCW, STOP) */
+  int8_t               en;      /* Enabled instances (bit-encoded) */
+  struct foc_thr_cfg_s cfg;     /* Control thread configuration */
 };
 
 /****************************************************************************