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 2021/10/30 06:18:17 UTC

[incubator-nuttx-apps] 01/03: industry/foc: add general FOC routine handler

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 084cfa4e5e3dac43809436e78e3630260f3f5c89
Author: raiden00pl <ra...@railab.me>
AuthorDate: Fri Oct 29 13:10:39 2021 +0200

    industry/foc: add general FOC routine handler
---
 include/industry/foc/fixed16/foc_routine.h | 140 ++++++++++++++++++++++
 include/industry/foc/float/foc_routine.h   | 140 ++++++++++++++++++++++
 industry/foc/Makefile                      |   2 +
 industry/foc/fixed16/foc_routine.c         | 179 +++++++++++++++++++++++++++++
 industry/foc/float/foc_routine.c           | 179 +++++++++++++++++++++++++++++
 5 files changed, 640 insertions(+)

diff --git a/include/industry/foc/fixed16/foc_routine.h b/include/industry/foc/fixed16/foc_routine.h
new file mode 100644
index 0000000..409b334
--- /dev/null
+++ b/include/industry/foc/fixed16/foc_routine.h
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * apps/include/industry/foc/fixed16/foc_routine.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INDUSTRY_FOC_FIXED16_FOC_ROUTINE_H
+#define __INDUSTRY_FOC_FIXED16_FOC_ROUTINE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include "industry/foc/fixed16/foc_handler.h"
+
+/****************************************************************************
+ * Public Type Definition
+ ****************************************************************************/
+
+/* Routine run return */
+
+enum foc_routine_run_e
+{
+  FOC_ROUTINE_RUN_NOTDONE = 0,
+  FOC_ROUTINE_RUN_DONE    = 1
+};
+
+/* FOC routine input */
+
+struct foc_routine_in_b16_s
+{
+  FAR struct foc_state_b16_s *foc_state; /* FOC controller state */
+  b16_t                       angle;     /* Angle now */
+  b16_t                       vel;       /* Velocity now */
+  b16_t                       vbus;      /* VBUS now */
+};
+
+/* FOC routine output */
+
+struct foc_routine_out_b16_s
+{
+  dq_frame_b16_t dq_ref;        /* Output DQ reference */
+  dq_frame_b16_t vdq_comp;      /* Output DQ voltage compensation */
+  b16_t          angle;         /* Output phase angle */
+  int            foc_mode;      /* Output FOC mode */
+};
+
+/* Forward declaration */
+
+typedef struct foc_routine_b16_s foc_routine_b16_t;
+
+/* FOC routine operations */
+
+struct foc_routine_ops_b16_s
+{
+  /* Initialize */
+
+  CODE int (*init)(FAR foc_routine_b16_t *h);
+
+  /* Deinitialize */
+
+  CODE void (*deinit)(FAR foc_routine_b16_t *h);
+
+  /* Configure */
+
+  CODE int (*cfg)(FAR foc_routine_b16_t *h, FAR void *cfg);
+
+  /* Run routine */
+
+  CODE int (*run)(FAR foc_routine_b16_t *h,
+                  FAR struct foc_routine_in_b16_s *in,
+                  FAR struct foc_routine_out_b16_s *out);
+
+  /* Run routine */
+
+  CODE int (*final)(FAR foc_routine_b16_t *h, FAR void *data);
+};
+
+/* FOC routine data */
+
+struct foc_routine_b16_s
+{
+  FAR struct foc_routine_ops_b16_s *ops;
+  FAR void                         *data;
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: foc_routine_init_b16
+ ****************************************************************************/
+
+int foc_routine_init_b16(FAR foc_routine_b16_t *r,
+                         FAR struct foc_routine_ops_b16_s *ops);
+
+/****************************************************************************
+ * Name: foc_routine_deinit_b16
+ ****************************************************************************/
+
+int foc_routine_deinit_b16(FAR foc_routine_b16_t *r);
+
+/****************************************************************************
+ * Name: foc_routine_cfg_b16
+ ****************************************************************************/
+
+int foc_routine_cfg_b16(FAR foc_routine_b16_t *r, FAR void *cfg);
+
+/****************************************************************************
+ * Name: foc_routine_run_b16
+ ****************************************************************************/
+
+int foc_routine_run_b16(FAR foc_routine_b16_t *r,
+                        FAR struct foc_routine_in_b16_s *in,
+                        FAR struct foc_routine_out_b16_s *out);
+
+/****************************************************************************
+ * Name: foc_routine_final_b16
+ ****************************************************************************/
+
+int foc_routine_final_b16(FAR foc_routine_b16_t *r, FAR void *data);
+
+#endif /* __INDUSTRY_FOC_FIXED16_FOC_ROUTINE_H */
diff --git a/include/industry/foc/float/foc_routine.h b/include/industry/foc/float/foc_routine.h
new file mode 100644
index 0000000..892e5e0
--- /dev/null
+++ b/include/industry/foc/float/foc_routine.h
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * apps/include/industry/foc/float/foc_routine.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INDUSTRY_FOC_FLOAT_FOC_ROUTINE_H
+#define __INDUSTRY_FOC_FLOAT_FOC_ROUTINE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include "industry/foc/float/foc_handler.h"
+
+/****************************************************************************
+ * Public Type Definition
+ ****************************************************************************/
+
+/* Routine run return */
+
+enum foc_routine_run_e
+{
+  FOC_ROUTINE_RUN_NOTDONE = 0,
+  FOC_ROUTINE_RUN_DONE    = 1
+};
+
+/* FOC routine input */
+
+struct foc_routine_in_f32_s
+{
+  FAR struct foc_state_f32_s *foc_state; /* FOC controller state */
+  float                       angle;     /* Angle now */
+  float                       vel;       /* Velocity now */
+  float                       vbus;      /* VBUS now */
+};
+
+/* FOC routine output */
+
+struct foc_routine_out_f32_s
+{
+  dq_frame_f32_t dq_ref;        /* Output DQ reference */
+  dq_frame_f32_t vdq_comp;      /* Output DQ voltage compensation */
+  float          angle;         /* Output phase angle */
+  int            foc_mode;      /* Output FOC mode */
+};
+
+/* Forward declaration */
+
+typedef struct foc_routine_f32_s foc_routine_f32_t;
+
+/* FOC routine operations */
+
+struct foc_routine_ops_f32_s
+{
+  /* Initialize */
+
+  CODE int (*init)(FAR foc_routine_f32_t *h);
+
+  /* Deinitialize */
+
+  CODE void (*deinit)(FAR foc_routine_f32_t *h);
+
+  /* Configure */
+
+  CODE int (*cfg)(FAR foc_routine_f32_t *h, FAR void *cfg);
+
+  /* Run routine */
+
+  CODE int (*run)(FAR foc_routine_f32_t *h,
+                  FAR struct foc_routine_in_f32_s *in,
+                  FAR struct foc_routine_out_f32_s *out);
+
+  /* Run routine */
+
+  CODE int (*final)(FAR foc_routine_f32_t *h, FAR void *data);
+};
+
+/* FOC routine data */
+
+struct foc_routine_f32_s
+{
+  FAR struct foc_routine_ops_f32_s *ops;
+  FAR void                         *data;
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: foc_routine_init_f32
+ ****************************************************************************/
+
+int foc_routine_init_f32(FAR foc_routine_f32_t *r,
+                         FAR struct foc_routine_ops_f32_s *ops);
+
+/****************************************************************************
+ * Name: foc_routine_deinit_f32
+ ****************************************************************************/
+
+int foc_routine_deinit_f32(FAR foc_routine_f32_t *r);
+
+/****************************************************************************
+ * Name: foc_routine_cfg_f32
+ ****************************************************************************/
+
+int foc_routine_cfg_f32(FAR foc_routine_f32_t *r, FAR void *cfg);
+
+/****************************************************************************
+ * Name: foc_routine_run_f32
+ ****************************************************************************/
+
+int foc_routine_run_f32(FAR foc_routine_f32_t *r,
+                        FAR struct foc_routine_in_f32_s *in,
+                        FAR struct foc_routine_out_f32_s *out);
+
+/****************************************************************************
+ * Name: foc_routine_final_f32
+ ****************************************************************************/
+
+int foc_routine_final_f32(FAR foc_routine_f32_t *r, FAR void *data);
+
+#endif /* __INDUSTRY_FOC_FLOAT_FOC_ROUTINE_H */
diff --git a/industry/foc/Makefile b/industry/foc/Makefile
index 29b66bd..60f00ca 100644
--- a/industry/foc/Makefile
+++ b/industry/foc/Makefile
@@ -32,6 +32,7 @@ CSRCS += float/foc_ramp.c
 CSRCS += float/foc_handler.c
 CSRCS += float/foc_angle.c
 CSRCS += float/foc_velocity.c
+CSRCS += float/foc_routine.c
 ifeq ($(CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP),y)
 CSRCS += float/foc_openloop.c
 endif
@@ -61,6 +62,7 @@ CSRCS += fixed16/foc_ramp.c
 CSRCS += fixed16/foc_handler.c
 CSRCS += fixed16/foc_angle.c
 CSRCS += fixed16/foc_velocity.c
+CSRCS += fixed16/foc_routine.c
 ifeq ($(CONFIG_INDUSTRY_FOC_ANGLE_OPENLOOP),y)
 CSRCS += fixed16/foc_openloop.c
 endif
diff --git a/industry/foc/fixed16/foc_routine.c b/industry/foc/fixed16/foc_routine.c
new file mode 100644
index 0000000..bba1e61
--- /dev/null
+++ b/industry/foc/fixed16/foc_routine.c
@@ -0,0 +1,179 @@
+/****************************************************************************
+ * apps/industry/foc/fixed16/foc_routine.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+
+#include "industry/foc/foc_log.h"
+#include "industry/foc/fixed16/foc_routine.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: foc_routine_init_b16
+ *
+ * Description:
+ *   Initialize the FOC routine (fixed16)
+ *
+ * Input Parameter:
+ *   r   - pointer to FOC routine
+ *   ops - pointer to FOC routine operations
+ *
+ ****************************************************************************/
+
+int foc_routine_init_b16(FAR foc_routine_b16_t *r,
+                         FAR struct foc_routine_ops_b16_s *ops)
+{
+  int ret = OK;
+
+  DEBUGASSERT(r);
+  DEBUGASSERT(ops);
+
+  /* Routine ops */
+
+  DEBUGASSERT(ops->init);
+  DEBUGASSERT(ops->deinit);
+  DEBUGASSERT(ops->cfg);
+  DEBUGASSERT(ops->run);
+
+  /* Reset routine */
+
+  memset(r, 0, sizeof(foc_routine_b16_t));
+
+  /* Connect ops */
+
+  r->ops = ops;
+
+  /* Initialize routine */
+
+  ret = r->ops->init(r);
+  if (ret < 0)
+    {
+      FOCLIBERR("ERROR: ops->init failed %d\n", ret);
+      goto errout;
+    }
+
+errout:
+  return ret;
+}
+
+/****************************************************************************
+ * Name: foc_routine_deinit_b16
+ *
+ * Description:
+ *   De-initialize the FOC routine (fixed16)
+ *
+ * Input Parameter:
+ *   r - pointer to FOC routine
+ *
+ ****************************************************************************/
+
+int foc_routine_deinit_b16(FAR foc_routine_b16_t *r)
+{
+  int ret = OK;
+
+  DEBUGASSERT(r);
+
+  /* Deinitialize routine */
+
+  r->ops->deinit(r);
+
+  /* Reset data */
+
+  memset(r, 0, sizeof(foc_routine_b16_t));
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: foc_routine_cfg_b16
+ *
+ * Description:
+ *   Configure the FOC routine (fixed16)
+ *
+ * Input Parameter:
+ *   r   - pointer to FOC routine
+ *   cfg - pointer to routine configuration data
+ *
+ ****************************************************************************/
+
+int foc_routine_cfg_b16(FAR foc_routine_b16_t *r, FAR void *cfg)
+{
+  DEBUGASSERT(r);
+  DEBUGASSERT(cfg);
+
+  return r->ops->cfg(r, cfg);
+}
+
+/****************************************************************************
+ * Name: foc_routine_run_b16
+ *
+ * Description:
+ *   Run the FOC routine (fixed16)
+ *
+ * Input Parameter:
+ *   r   - pointer to FOC routine
+ *   in  - pointer to FOC routine input data
+ *   out - pointer to FOC routine output data
+ *
+ ****************************************************************************/
+
+int foc_routine_run_b16(FAR foc_routine_b16_t *r,
+                        FAR struct foc_routine_in_b16_s *in,
+                        FAR struct foc_routine_out_b16_s *out)
+{
+  DEBUGASSERT(r);
+  DEBUGASSERT(in);
+  DEBUGASSERT(out);
+
+  /* Run routine handler */
+
+  return r->ops->run(r, in, out);
+}
+
+/****************************************************************************
+ * Name: foc_routine_final_b16
+ *
+ * Description:
+ *   Finalize the FOC routine data (fixed16)
+ *
+ * Input Parameter:
+ *   r    - pointer to FOC routine
+ *   data - pointer to FOC routine final data
+ *
+ ****************************************************************************/
+
+int foc_routine_final_b16(FAR foc_routine_b16_t *r, FAR void *data)
+{
+  DEBUGASSERT(r);
+  DEBUGASSERT(data);
+
+  /* Finalize routine */
+
+  return r->ops->final(r, data);
+}
diff --git a/industry/foc/float/foc_routine.c b/industry/foc/float/foc_routine.c
new file mode 100644
index 0000000..767fb18
--- /dev/null
+++ b/industry/foc/float/foc_routine.c
@@ -0,0 +1,179 @@
+/****************************************************************************
+ * apps/industry/foc/float/foc_routine.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+
+#include "industry/foc/foc_log.h"
+#include "industry/foc/float/foc_routine.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: foc_routine_init_f32
+ *
+ * Description:
+ *   Initialize the FOC routine (float32)
+ *
+ * Input Parameter:
+ *   r   - pointer to FOC routine
+ *   ops - pointer to FOC routine operations
+ *
+ ****************************************************************************/
+
+int foc_routine_init_f32(FAR foc_routine_f32_t *r,
+                         FAR struct foc_routine_ops_f32_s *ops)
+{
+  int ret = OK;
+
+  DEBUGASSERT(r);
+  DEBUGASSERT(ops);
+
+  /* Routine ops */
+
+  DEBUGASSERT(ops->init);
+  DEBUGASSERT(ops->deinit);
+  DEBUGASSERT(ops->cfg);
+  DEBUGASSERT(ops->run);
+
+  /* Reset routine */
+
+  memset(r, 0, sizeof(foc_routine_f32_t));
+
+  /* Connect ops */
+
+  r->ops = ops;
+
+  /* Initialize routine */
+
+  ret = r->ops->init(r);
+  if (ret < 0)
+    {
+      FOCLIBERR("ERROR: ops->init failed %d\n", ret);
+      goto errout;
+    }
+
+errout:
+  return ret;
+}
+
+/****************************************************************************
+ * Name: foc_routine_deinit_f32
+ *
+ * Description:
+ *   De-initialize the FOC routine (float32)
+ *
+ * Input Parameter:
+ *   r - pointer to FOC routine
+ *
+ ****************************************************************************/
+
+int foc_routine_deinit_f32(FAR foc_routine_f32_t *r)
+{
+  int ret = OK;
+
+  DEBUGASSERT(r);
+
+  /* Deinitialize routine */
+
+  r->ops->deinit(r);
+
+  /* Reset data */
+
+  memset(r, 0, sizeof(foc_routine_f32_t));
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: foc_routine_cfg_f32
+ *
+ * Description:
+ *   Configure the FOC routine (float32)
+ *
+ * Input Parameter:
+ *   r   - pointer to FOC routine
+ *   cfg - pointer to routine configuration data
+ *
+ ****************************************************************************/
+
+int foc_routine_cfg_f32(FAR foc_routine_f32_t *r, FAR void *cfg)
+{
+  DEBUGASSERT(r);
+  DEBUGASSERT(cfg);
+
+  return r->ops->cfg(r, cfg);
+}
+
+/****************************************************************************
+ * Name: foc_routine_run_f32
+ *
+ * Description:
+ *   Run the FOC routine (float32)
+ *
+ * Input Parameter:
+ *   r   - pointer to FOC routine
+ *   in  - pointer to FOC routine input data
+ *   out - pointer to FOC routine output data
+ *
+ ****************************************************************************/
+
+int foc_routine_run_f32(FAR foc_routine_f32_t *r,
+                        FAR struct foc_routine_in_f32_s *in,
+                        FAR struct foc_routine_out_f32_s *out)
+{
+  DEBUGASSERT(r);
+  DEBUGASSERT(in);
+  DEBUGASSERT(out);
+
+  /* Run routine handler */
+
+  return r->ops->run(r, in, out);
+}
+
+/****************************************************************************
+ * Name: foc_routine_final_f32
+ *
+ * Description:
+ *   Finalize the FOC routine data (float32)
+ *
+ * Input Parameter:
+ *   r    - pointer to FOC routine
+ *   data - pointer to FOC routine final data
+ *
+ ****************************************************************************/
+
+int foc_routine_final_f32(FAR foc_routine_f32_t *r, FAR void *data)
+{
+  DEBUGASSERT(r);
+  DEBUGASSERT(data);
+
+  /* Finalize routine */
+
+  return r->ops->final(r, data);
+}