examples/foc: add support for control loop performance measurements

also format CMakeLists.txt
This commit is contained in:
raiden00pl 2023-10-20 16:31:27 +02:00 committed by Xiang Xiao
parent 73ab7dedd4
commit 9ba7092000
9 changed files with 233 additions and 32 deletions

View File

@ -1,22 +1,22 @@
############################################################################ # ##############################################################################
# apps/examples/foc/CMakeLists.txt # apps/examples/foc/CMakeLists.txt
# #
# Licensed to the Apache Software Foundation (ASF) under one or more # Licensed to the Apache Software Foundation (ASF) under one or more contributor
# contributor license agreements. See the NOTICE file distributed with # license agreements. See the NOTICE file distributed with this work for
# this work for additional information regarding copyright ownership. The # additional information regarding copyright ownership. The ASF licenses this
# ASF licenses this file to you under the Apache License, Version 2.0 (the # file to you under the Apache License, Version 2.0 (the "License"); you may not
# "License"); you may not use this file except in compliance with the # use this file except in compliance with the License. You may obtain a copy of
# License. You may obtain a copy of the License at # the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations under
# under the License. # the License.
# #
############################################################################ # ##############################################################################
if(CONFIG_EXAMPLES_FOC) if(CONFIG_EXAMPLES_FOC)
nuttx_add_application( nuttx_add_application(
@ -29,34 +29,36 @@ if(CONFIG_EXAMPLES_FOC)
PRIORITY PRIORITY
${CONFIG_EXAMPLES_FOC_PRIORITY}) ${CONFIG_EXAMPLES_FOC_PRIORITY})
set(CSRCS set(CSRCS foc_device.c foc_mq.c foc_thr.c foc_intf.c)
foc_device.c
foc_mq.c
foc_thr.c
foc_intf.c)
if(CONFIG_BUILTIN) if(CONFIG_BUILTIN)
list(APPEND CSRCS foc_parseargs.c) list(APPEND CSRCS foc_parseargs.c)
endif() endif()
# fixed16 support # fixed16 support
if(CONFIG_INDUSTRY_FOC_FIXED16) if(CONFIG_INDUSTRY_FOC_FIXED16)
list(APPEND CSRCS foc_fixed16_thr.c foc_motor_b16.c) list(APPEND CSRCS foc_fixed16_thr.c foc_motor_b16.c)
endif() endif()
# float32 support # float32 support
if(CONFIG_INDUSTRY_FOC_FLOAT) if(CONFIG_INDUSTRY_FOC_FLOAT)
list(APPEND CSRCS foc_float_thr.c foc_motor_f32.c) list(APPEND CSRCS foc_float_thr.c foc_motor_f32.c)
endif() endif()
# NxScope support # NxScope support
if(CONFIG_EXAMPLES_FOC_NXSCOPE) if(CONFIG_EXAMPLES_FOC_NXSCOPE)
list(APPEND CSRCS foc_nxscope.c) list(APPEND CSRCS foc_nxscope.c)
endif() endif()
target_sources(apps PRIVATE ${CSRCS}) # perf support
if(CONFIG_EXAMPLES_FOC_PERF)
list(APPEND CSRCS foc_perf.c)
endif()
target_sources(apps PRIVATE ${CSRCS})
endif() endif()

View File

@ -43,6 +43,10 @@ config EXAMPLES_FOC_VERBOSE
default 1 default 1
range 0 2 range 0 2
config EXAMPLES_FOC_PERF
bool "Enable performance meassurements"
default n
choice choice
prompt "FOC current controller selection" prompt "FOC current controller selection"
default EXAMPLES_FOC_CONTROL_PI default EXAMPLES_FOC_CONTROL_PI

View File

@ -56,4 +56,10 @@ ifeq ($(CONFIG_EXAMPLES_FOC_NXSCOPE),y)
CSRCS += foc_nxscope.c CSRCS += foc_nxscope.c
endif endif
# perf support
ifeq ($(CONFIG_EXAMPLES_FOC_PERF),y)
CSRCS += foc_perf.c
endif
include $(APPDIR)/Application.mk include $(APPDIR)/Application.mk

View File

@ -90,6 +90,12 @@ int foc_device_init(FAR struct foc_device_s *dev, int id)
goto errout; goto errout;
} }
#ifdef CONFIG_EXAMPLES_FOC_PERF
/* Initialize perf */
foc_perf_init(&dev->perf);
#endif
errout: errout:
return ret; return ret;
} }
@ -138,6 +144,15 @@ int foc_device_start(FAR struct foc_device_s *dev, bool state)
PRINTFV("ERROR: foc_dev_start failed %d!\n", ret); PRINTFV("ERROR: foc_dev_start failed %d!\n", ret);
goto errout; goto errout;
} }
#ifdef CONFIG_EXAMPLES_FOC_PERF
/* Skip this cycle in stats. When the dev is started, many components
* are initialized, which significantly increases the cycle time and
* disturbs the statistics.
*/
foc_perf_skip(&dev->perf);
#endif
} }
else else
{ {
@ -172,6 +187,10 @@ int foc_dev_state_get(FAR struct foc_device_s *dev)
goto errout; goto errout;
} }
#ifdef CONFIG_EXAMPLES_FOC_PERF
foc_perf_start(&dev->perf);
#endif
errout: errout:
return ret; return ret;
} }
@ -195,6 +214,10 @@ int foc_dev_params_set(FAR struct foc_device_s *dev)
goto errout; goto errout;
} }
#ifdef CONFIG_EXAMPLES_FOC_PERF
foc_perf_end(&dev->perf);
#endif
errout: errout:
return ret; return ret;
} }

View File

@ -27,6 +27,8 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include "foc_perf.h"
#include "industry/foc/foc_utils.h" #include "industry/foc/foc_utils.h"
/**************************************************************************** /****************************************************************************
@ -41,6 +43,9 @@ struct foc_device_s
struct foc_info_s info; /* FOC dev info */ struct foc_info_s info; /* FOC dev info */
struct foc_state_s state; /* FOC dev state */ struct foc_state_s state; /* FOC dev state */
struct foc_params_s params; /* FOC dev params */ struct foc_params_s params; /* FOC dev params */
#ifdef CONFIG_EXAMPLES_FOC_PERF
struct foc_perf_s perf; /* FOC dev perf */
#endif
}; };
/**************************************************************************** /****************************************************************************

View File

@ -498,6 +498,13 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
/* Increase counter */ /* Increase counter */
motor.time += 1; motor.time += 1;
#ifdef CONFIG_EXAMPLES_FOC_PERF
if (dev.perf.max_changed)
{
PRINTF_PERF("max=%" PRId32 "\n", dev.perf.max);
}
#endif
} }
errout: errout:

View File

@ -511,6 +511,13 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
/* Increase counter */ /* Increase counter */
motor.time += 1; motor.time += 1;
#ifdef CONFIG_EXAMPLES_FOC_PERF
if (dev.perf.max_changed)
{
PRINTF_PERF("max=%" PRId32 "\n", dev.perf.max);
}
#endif
} }
errout: errout:

90
examples/foc/foc_perf.c Normal file
View File

@ -0,0 +1,90 @@
/****************************************************************************
* apps/examples/foc/foc_perf.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 <stdbool.h>
#include <string.h>
#include <nuttx/clock.h>
#include "foc_perf.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: foc_perf_init
****************************************************************************/
int foc_perf_init(struct foc_perf_s *p)
{
memset(p, 0, sizeof(struct foc_perf_s));
return OK;
}
/****************************************************************************
* Name: foc_perf_start
****************************************************************************/
void foc_perf_start(struct foc_perf_s *p)
{
p->now = perf_gettime();
}
/****************************************************************************
* Name: foc_perf_skip
****************************************************************************/
void foc_perf_skip(struct foc_perf_s *p)
{
p->skip = true;
}
/****************************************************************************
* Name: foc_perf_end
****************************************************************************/
void foc_perf_end(struct foc_perf_s *p)
{
p->now = perf_gettime() - p->now;
p->max_changed = false;
if (p->skip == false)
{
if (p->now > p->max)
{
p->max = p->now;
p->max_changed = true;
}
}
/* Reset skip flag */
p->skip = false;
}

57
examples/foc/foc_perf.h Normal file
View File

@ -0,0 +1,57 @@
/****************************************************************************
* apps/examples/foc/foc_perf.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 __APPS_EXAMPLES_FOC_FOC_PERF_H
#define __APPS_EXAMPLES_FOC_FOC_PERF_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define PRINTF_PERF(format, ...) printf(format, ##__VA_ARGS__)
/****************************************************************************
* Public Type Definition
****************************************************************************/
struct foc_perf_s
{
bool max_changed;
bool skip;
uint32_t max;
uint32_t now;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int foc_perf_init(struct foc_perf_s *p);
void foc_perf_start(struct foc_perf_s *p);
void foc_perf_skip(struct foc_perf_s *p);
void foc_perf_end(struct foc_perf_s *p);
#endif /* __APPS_EXAMPLES_FOC_FOC_PERF_H */