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
The following commit(s) were added to refs/heads/master by this push:
new 0577bd2 examples/foc: add a simple character based interface to
interact with the app
0577bd2 is described below
commit 0577bd2c339fbb8ff3e43038a806ec9ba9441fb2
Author: raiden00pl <[email protected]>
AuthorDate: Tue Dec 7 17:12:58 2021 +0100
examples/foc: add a simple character based interface to interact with the
app
---
examples/foc/Kconfig | 19 +++++
examples/foc/foc_cfg.h | 9 +-
examples/foc/foc_intf.c | 212 +++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 237 insertions(+), 3 deletions(-)
diff --git a/examples/foc/Kconfig b/examples/foc/Kconfig
index b8a69ee..f16e92e 100644
--- a/examples/foc/Kconfig
+++ b/examples/foc/Kconfig
@@ -218,6 +218,11 @@ config EXAMPLES_FOC_SETPOINT_ADC
select EXAMPLES_FOC_HAVE_ADC
select EXAMPLES_FOC_HAVE_SETPOINT_VAR
+config EXAMPLES_FOC_SETPOINT_CHAR
+ bool "Use character interface to control setpoint"
+ select EXAMPLES_FOC_HAVE_CHARCTRL
+ select EXAMPLES_FOC_HAVE_SETPOINT_VAR
+
endchoice # FOC setpoint interface
config EXAMPLES_FOC_HAVE_SETPOINT_VAR
@@ -269,6 +274,20 @@ config EXAMPLES_FOC_BUTTON_DEVPATH
endif
+config EXAMPLES_FOC_HAVE_CHARCTRL
+ bool "FOC character control interface support"
+ default n
+ ---help---
+ Use simple character commands to interact with the app
+
+if EXAMPLES_FOC_HAVE_CHARCTRL
+
+config EXAMPLES_FOC_CHAR_SETPOINT_STEP
+ int "FOC character control setpoint step [x1000]"
+ default 0
+
+endif
+
endmenu # FOC user input
menu "FOC controller parameters"
diff --git a/examples/foc/foc_cfg.h b/examples/foc/foc_cfg.h
index 25cee0e..8aae491 100644
--- a/examples/foc/foc_cfg.h
+++ b/examples/foc/foc_cfg.h
@@ -131,7 +131,8 @@
/* Setpoint source must be specified */
#if !defined(CONFIG_EXAMPLES_FOC_SETPOINT_CONST) && \
- !defined(CONFIG_EXAMPLES_FOC_SETPOINT_ADC)
+ !defined(CONFIG_EXAMPLES_FOC_SETPOINT_ADC) && \
+ !defined(CONFIG_EXAMPLES_FOC_SETPOINT_CHAR)
# error
#endif
@@ -150,6 +151,12 @@
# endif
#endif
+/* CHARCTRL setpoint control */
+
+#ifdef CONFIG_EXAMPLES_FOC_SETPOINT_CHAR
+# define SETPOINT_ADC_SCALE (1 / 1000.0f)
+#endif
+
/* VBUS source must be specified */
#if !defined(CONFIG_EXAMPLES_FOC_VBUS_CONST) && \
diff --git a/examples/foc/foc_intf.c b/examples/foc/foc_intf.c
index 7bc0581..45edfa2 100644
--- a/examples/foc/foc_intf.c
+++ b/examples/foc/foc_intf.c
@@ -49,8 +49,9 @@
* Pre-processor Definitions
****************************************************************************/
-#if defined(CONFIG_EXAMPLES_FOC_HAVE_ADC) || \
- defined(CONFIG_EXAMPLES_FOC_HAVE_BUTTON)
+#if defined(CONFIG_EXAMPLES_FOC_HAVE_ADC) || \
+ defined(CONFIG_EXAMPLES_FOC_HAVE_BUTTON) || \
+ defined(CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL)
# define FOC_HAVE_INTF
#endif
@@ -91,6 +92,15 @@ struct foc_intf_adc_s
};
#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
+/* Character control interface data */
+
+struct foc_intf_chc_s
+{
+ int fd;
+};
+#endif
+
/****************************************************************************
* Private Data
****************************************************************************/
@@ -120,6 +130,12 @@ static struct foc_intf_btn_s g_btn_intf;
static struct foc_intf_adc_s g_adc_intf;
#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
+/* Character control interface data */
+
+static struct foc_intf_chc_s g_chc_intf;
+#endif
+
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -367,6 +383,165 @@ errout:
}
#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
+/****************************************************************************
+ * Name: foc_charctrl_help
+ ****************************************************************************/
+
+static void foc_charctrl_help(void)
+{
+ PRINTF("\nCHARCTRL commands:\n"
+ " h - print this message\n"
+ " u - increase setpoint\n"
+ " d - decrease setpoint\n"
+ " q - quit\n"
+ " 1..4 - change example state\n\n");
+}
+
+/****************************************************************************
+ * Name: foc_charctrl_init
+ ****************************************************************************/
+
+static int foc_charctrl_init(FAR struct foc_intf_chc_s *intf)
+{
+ int ret = 0;
+
+ DEBUGASSERT(intf);
+
+ /* Character control interface on STDIN */
+
+ intf->fd = 0;
+
+ /* Print help msg */
+
+ PRINTF("\nCHARCTRL mode enabled\n");
+ foc_charctrl_help();
+
+ /* Configure input to not blocking */
+
+ fcntl(intf->fd, F_SETFL, O_NONBLOCK);
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: foc_charctrl_deinit
+ ****************************************************************************/
+
+static int foc_charctrl_deinit(FAR struct foc_intf_chc_s *intf)
+{
+ DEBUGASSERT(intf);
+
+ if (intf->fd > 0)
+ {
+ close(intf->fd);
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: foc_charctrl_update
+ ****************************************************************************/
+
+static int foc_charctrl_update(FAR struct foc_intf_chc_s *intf,
+ FAR struct foc_intf_data_s *data)
+{
+ char c = 0;
+ int ret = OK;
+
+ DEBUGASSERT(intf);
+ DEBUGASSERT(data);
+
+ ret = read(intf->fd, &c, 1);
+ if (ret < 0)
+ {
+ if (errno != EAGAIN)
+ {
+ PRINTF("ERROR: read char failed %d\n", errno);
+ ret = -errno;
+ goto errout;
+ }
+ else
+ {
+ ret = OK;
+ }
+ }
+ else
+ {
+ switch (c)
+ {
+#ifdef CONFIG_EXAMPLES_FOC_SETPOINT_CHAR
+ case 'u':
+ {
+ data->sp_raw += CONFIG_EXAMPLES_FOC_CHAR_SETPOINT_STEP;
+
+ if (data->sp_raw > CONFIG_EXAMPLES_FOC_SETPOINT_MAX)
+ {
+ data->sp_raw = CONFIG_EXAMPLES_FOC_SETPOINT_MAX;
+ }
+
+ PRINTF(">> sp=%" PRIu32 "\n", data->sp_raw);
+ data->sp_update = true;
+
+ break;
+ }
+
+ case 'd':
+ {
+ data->sp_raw -= CONFIG_EXAMPLES_FOC_CHAR_SETPOINT_STEP;
+
+ if (data->sp_raw < 0)
+ {
+ data->sp_raw = 0;
+ }
+
+ PRINTF(">> sp=%" PRIu32 "\n", data->sp_raw);
+ data->sp_update = true;
+
+ break;
+ }
+#endif /* CONFIG_EXAMPLES_FOC_SETPOINT_CHAR */
+
+ case 'h':
+ {
+ foc_charctrl_help();
+
+ break;
+ }
+
+ case 'q':
+ {
+ PRINTF(">> QUIT\n");
+ data->terminate = true;
+ break;
+ }
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ {
+ data->state = c - '1' + 1;
+ PRINTF(">> state=%" PRIu32 "\n", data->state);
+ data->state_update = true;
+
+ break;
+ }
+
+ default:
+ {
+ PRINTF("ERROR: invalid cmd=%d\n", c);
+ break;
+ }
+ }
+ }
+
+errout:
+ return ret;
+}
+#endif
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -401,6 +576,17 @@ int foc_intf_init(void)
}
#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
+ /* Initialize character control interface */
+
+ ret = foc_charctrl_init(&g_chc_intf);
+ if (ret < 0)
+ {
+ PRINTF("ERROR: failed to initialize char interface %d\n", ret);
+ goto errout;
+ }
+#endif
+
#ifdef FOC_HAVE_INTF
errout:
#endif
@@ -438,6 +624,17 @@ int foc_intf_deinit(void)
}
#endif
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
+ /* De-initialize character interface */
+
+ ret = foc_charctrl_deinit(&g_chc_intf);
+ if (ret < 0)
+ {
+ PRINTF("ERROR: foc_charctrl_deinit failed %d\n", ret);
+ goto errout;
+ }
+#endif
+
#ifdef FOC_HAVE_INTF
errout:
#endif
@@ -455,6 +652,17 @@ int foc_intf_update(FAR struct foc_intf_data_s *data)
DEBUGASSERT(data);
+#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
+ /* Update charctrl */
+
+ ret = foc_charctrl_update(&g_chc_intf, data);
+ if (ret < 0)
+ {
+ PRINTF("ERROR: foc_charctrl_update failed: %d\n", ret);
+ goto errout;
+ }
+#endif
+
#ifdef CONFIG_EXAMPLES_FOC_HAVE_BUTTON
/* Update button */