examples/foc: add a simple character based interface to interact with the app

This commit is contained in:
raiden00pl 2021-12-07 17:12:58 +01:00 committed by Xiang Xiao
parent 87d8494a6e
commit 0577bd2c33
3 changed files with 237 additions and 3 deletions

View File

@ -218,6 +218,11 @@ config EXAMPLES_FOC_SETPOINT_ADC
select EXAMPLES_FOC_HAVE_ADC select EXAMPLES_FOC_HAVE_ADC
select EXAMPLES_FOC_HAVE_SETPOINT_VAR 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 endchoice # FOC setpoint interface
config EXAMPLES_FOC_HAVE_SETPOINT_VAR config EXAMPLES_FOC_HAVE_SETPOINT_VAR
@ -269,6 +274,20 @@ config EXAMPLES_FOC_BUTTON_DEVPATH
endif 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 endmenu # FOC user input
menu "FOC controller parameters" menu "FOC controller parameters"

View File

@ -131,7 +131,8 @@
/* Setpoint source must be specified */ /* Setpoint source must be specified */
#if !defined(CONFIG_EXAMPLES_FOC_SETPOINT_CONST) && \ #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 # error
#endif #endif
@ -150,6 +151,12 @@
# endif # endif
#endif #endif
/* CHARCTRL setpoint control */
#ifdef CONFIG_EXAMPLES_FOC_SETPOINT_CHAR
# define SETPOINT_ADC_SCALE (1 / 1000.0f)
#endif
/* VBUS source must be specified */ /* VBUS source must be specified */
#if !defined(CONFIG_EXAMPLES_FOC_VBUS_CONST) && \ #if !defined(CONFIG_EXAMPLES_FOC_VBUS_CONST) && \

View File

@ -49,8 +49,9 @@
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
#if defined(CONFIG_EXAMPLES_FOC_HAVE_ADC) || \ #if defined(CONFIG_EXAMPLES_FOC_HAVE_ADC) || \
defined(CONFIG_EXAMPLES_FOC_HAVE_BUTTON) defined(CONFIG_EXAMPLES_FOC_HAVE_BUTTON) || \
defined(CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL)
# define FOC_HAVE_INTF # define FOC_HAVE_INTF
#endif #endif
@ -91,6 +92,15 @@ struct foc_intf_adc_s
}; };
#endif #endif
#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
/* Character control interface data */
struct foc_intf_chc_s
{
int fd;
};
#endif
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
@ -120,6 +130,12 @@ static struct foc_intf_btn_s g_btn_intf;
static struct foc_intf_adc_s g_adc_intf; static struct foc_intf_adc_s g_adc_intf;
#endif #endif
#ifdef CONFIG_EXAMPLES_FOC_HAVE_CHARCTRL
/* Character control interface data */
static struct foc_intf_chc_s g_chc_intf;
#endif
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -367,6 +383,165 @@ errout:
} }
#endif #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 * Public Functions
****************************************************************************/ ****************************************************************************/
@ -401,6 +576,17 @@ int foc_intf_init(void)
} }
#endif #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 #ifdef FOC_HAVE_INTF
errout: errout:
#endif #endif
@ -438,6 +624,17 @@ int foc_intf_deinit(void)
} }
#endif #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 #ifdef FOC_HAVE_INTF
errout: errout:
#endif #endif
@ -455,6 +652,17 @@ int foc_intf_update(FAR struct foc_intf_data_s *data)
DEBUGASSERT(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 #ifdef CONFIG_EXAMPLES_FOC_HAVE_BUTTON
/* Update button */ /* Update button */