Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/foc: add real time data capture with nxscope lib #1765

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions examples/foc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -421,5 +421,92 @@ config EXAMPLES_FOC_RUN_DISABLE
bool "FOC Disable FOC motor controller"
default n

config EXAMPLES_FOC_NXSCOPE
bool "FOC nxscope support"
depends on LOGGING_NXSCOPE
select LOGGING_NXSCOPE_DISABLE_PUTLOCK
default n
---help---
This option enables a controller real-time data capture with
the NxScope library.

if EXAMPLES_FOC_NXSCOPE

config EXAMPLES_FOC_NXSCOPE_SERIAL
bool "FOC nxscope on serial port"
select LOGGING_NXSCOPE_INTF_SERIAL
default y

if EXAMPLES_FOC_NXSCOPE_SERIAL

config EXAMPLES_FOC_NXSCOPE_SERIAL_PATH
string "FOC nxscope serial path"
default "/dev/ttyS0"

config EXAMPLES_FOC_NXSCOPE_SERIAL_BAUD
int "FOC nxscope serial baud"
default 115200

endif # EXAMPLES_FOC_NXSCOPE_SERIAL

config EXAMPLES_FOC_NXSCOPE_STREAMBUF_LEN
int "FOC nxscope stream buffer length"
default 512

config EXAMPLES_FOC_NXSCOPE_RXBUF_LEN
int "FOC nxscope RX buffer length"
default 64

config EXAMPLES_FOC_NXSCOPE_RXPADDING
int "FOC nxscope RX padding"
default 0

config EXAMPLES_FOC_NXSCOPE_CHANNELS
int "FOC nxscope channels"
default 0

config EXAMPLES_FOC_NXSCOPE_PRESCALER
int "FOC nxscope prescaler"
default 1
---help---
This option allows you to reduce the frequency of adding samples to
the NxScope buffer.

config EXAMPLES_FOC_NXSCOPE_CFG
hex "FOC nxscope configuration"
default 0x00000000
---help---
Each bit defines the controller state variable that can be captured.
Look at foc_nxscope.h for bits definitions.

config EXAMPLES_FOC_NXSCOPE_START
bool "FOC nxscope start frame sync"
default n
---help---
If this option is set, the controller will be waiting for the start
frame from a NxScope master device. This allows us to capture
controller data from the very beginning of its operation.

config EXAMPLES_FOC_NXSCOPE_THREAD
bool "FOC nxscope uses separate thread"
default n
---help---
Use a separate thread for NxScope communication.

if EXAMPLES_FOC_NXSCOPE_THREAD

config EXAMPLES_FOC_NXSCOPE_PRIO
int "FOC nxscope thread priority"
default 100

config EXAMPLES_FOC_NXSCOPE_STACKSIZE
int "FOC nxscope thread stack size"
default 2048

endif # EXAMPLES_FOC_NXSCOPE_THREAD

endif # EXAMPLES_FOC_NXSCOPE

endif # EXAMPLES_FOC


6 changes: 6 additions & 0 deletions examples/foc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ ifeq ($(CONFIG_INDUSTRY_FOC_FLOAT),y)
CSRCS += foc_float_thr.c foc_motor_f32.c
endif

# NxScope support

ifeq ($(CONFIG_EXAMPLES_FOC_NXSCOPE),y)
CSRCS += foc_nxscope.c
endif

include $(APPDIR)/Application.mk
98 changes: 98 additions & 0 deletions examples/foc/foc_fixed16_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "industry/foc/foc_utils.h"
#include "industry/foc/foc_common.h"

#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
# include "logging/nxscope/nxscope.h"
#endif

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
Expand Down Expand Up @@ -173,6 +177,91 @@ static int foc_state_print(FAR struct foc_motor_b16_s *motor)
}
#endif

#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
/****************************************************************************
* Name: foc_fixed16_nxscope
****************************************************************************/

static void foc_fixed16_nxscope(FAR struct foc_nxscope_s *nxs,
FAR struct foc_motor_b16_s *motor,
FAR struct foc_device_s *dev)
{
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG != 0)
FAR b16_t *ptr = NULL;
int i = nxs->ch_per_inst * motor->envp->id;
#endif

nxscope_lock(&nxs->nxs);

#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_IABC)
ptr = (FAR b16_t *)&motor->foc_state.curr;
nxscope_put_vb16(&nxs->nxs, i++, ptr, CONFIG_MOTOR_FOC_PHASES);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_IDQ)
ptr = (FAR b16_t *)&motor->foc_state.idq;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_IAB)
ptr = (FAR b16_t *)&motor->foc_state.iab;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VABC)
ptr = (FAR b16_t *)&motor->foc_state.volt;
nxscope_put_vb16(&nxs->nxs, i++, ptr, CONFIG_MOTOR_FOC_PHASES);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VDQ)
ptr = (FAR b16_t *)&motor->foc_state.vdq;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VAB)
ptr = (FAR b16_t *)&motor->foc_state.vab;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_AEL)
ptr = (FAR b16_t *)&motor->angle_el;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 1);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_AM)
ptr = (FAR b16_t *)&motor->angle_m;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 1);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VEL)
# warning not supported yet
i++;
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VM)
# warning not supported yet
i++;
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VBUS)
ptr = (FAR b16_t *)&motor->vbus;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 1);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_SPTORQ)
ptr = (FAR b16_t *)&motor->torq;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 3);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_SPVEL)
ptr = (FAR b16_t *)&motor->vel;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 3);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_SPPOS)
ptr = (FAR b16_t *)&motor->pos;
nxscope_put_vb16(&nxs->nxs, i++, ptr, 3);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_DQREF)
ptr = (FAR b16_t *)&motor->dq_ref;
nxscope_put_vb16_t(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VDQCOMP)
ptr = (FAR b16_t *)&motor->vdq_comp;
nxscope_put_vb16_t(&nxs->nxs, i++, ptr, 2);
#endif

nxscope_unlock(&nxs->nxs);
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -355,6 +444,15 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
goto errout;
}

#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
/* Capture nxscope samples */

if (time % CONFIG_EXAMPLES_FOC_NXSCOPE_PRESCALER == 0)
{
foc_fixed16_nxscope(envp->nxs, &motor, &dev);
}
#endif

/* Terminate control thread */

if (motor.ctrl_state == FOC_CTRL_STATE_TERMINATE)
Expand Down
98 changes: 98 additions & 0 deletions examples/foc/foc_float_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "industry/foc/foc_utils.h"
#include "industry/foc/foc_common.h"

#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
# include "logging/nxscope/nxscope.h"
#endif

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
Expand Down Expand Up @@ -174,6 +178,91 @@ static int foc_state_print(FAR struct foc_motor_f32_s *motor)
}
#endif

#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
/****************************************************************************
* Name: foc_float_nxscope
****************************************************************************/

static void foc_float_nxscope(FAR struct foc_nxscope_s *nxs,
FAR struct foc_motor_f32_s *motor,
FAR struct foc_device_s *dev)
{
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG != 0)
FAR float *ptr = NULL;
int i = nxs->ch_per_inst * motor->envp->id;
#endif

nxscope_lock(&nxs->nxs);

#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_IABC)
ptr = (FAR float *)&motor->foc_state.curr;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, CONFIG_MOTOR_FOC_PHASES);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_IDQ)
ptr = (FAR float *)&motor->foc_state.idq;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_IAB)
ptr = (FAR float *)&motor->foc_state.iab;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VABC)
ptr = (FAR float *)&motor->foc_state.volt;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, CONFIG_MOTOR_FOC_PHASES);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VDQ)
ptr = (FAR float *)&motor->foc_state.vdq;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VAB)
ptr = (FAR float *)&motor->foc_state.vab;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_AEL)
ptr = (FAR float *)&motor->angle_el;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 1);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_AM)
ptr = (FAR float *)&motor->angle_m;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 1);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VEL)
# warning not supported yet
i++;
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VM)
# warning not supported yet
i++;
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VBUS)
ptr = (FAR float *)&motor->vbus;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 1);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_SPTORQ)
ptr = (FAR float *)&motor->torq;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 3);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_SPVEL)
ptr = (FAR float *)&motor->vel;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 3);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_SPPOS)
ptr = (FAR float *)&motor->pos;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 3);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_DQREF)
ptr = (FAR float *)&motor->dq_ref;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 2);
#endif
#if (CONFIG_EXAMPLES_FOC_NXSCOPE_CFG & FOC_NXSCOPE_VDQCOMP)
ptr = (FAR float *)&motor->vdq_comp;
nxscope_put_vfloat(&nxs->nxs, i++, ptr, 2);
#endif

nxscope_unlock(&nxs->nxs);
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -339,6 +428,15 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
}
#endif

#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
/* Capture nxscope samples */

if (time % CONFIG_EXAMPLES_FOC_NXSCOPE_PRESCALER == 0)
{
foc_float_nxscope(envp->nxs, &motor, &dev);
}
#endif

#ifdef CONFIG_EXAMPLES_FOC_STATE_USE_MODEL_PMSM
/* Feed FOC model with data */

Expand Down
Loading