2021-03-03 19:48:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/examples/foc/foc_main.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 <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2021-05-24 11:41:49 +02:00
|
|
|
#include <assert.h>
|
2021-03-03 19:48:19 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/boardctl.h>
|
|
|
|
|
|
|
|
#include "foc_mq.h"
|
|
|
|
#include "foc_thr.h"
|
2021-12-04 17:34:58 +01:00
|
|
|
#include "foc_cfg.h"
|
2021-03-03 19:48:19 +01:00
|
|
|
#include "foc_debug.h"
|
2021-10-30 11:18:00 +02:00
|
|
|
#include "foc_parseargs.h"
|
2021-12-05 16:19:15 +01:00
|
|
|
#include "foc_intf.h"
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
#include "industry/foc/foc_common.h"
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
|
|
|
|
# include "foc_nxscope.h"
|
|
|
|
#endif
|
|
|
|
|
2021-03-03 19:48:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Main loop sleep */
|
|
|
|
|
|
|
|
#define MAIN_LOOP_USLEEP (200000)
|
|
|
|
|
2022-01-05 16:34:05 +01:00
|
|
|
/* Enabled instances default state */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2022-01-05 16:34:05 +01:00
|
|
|
#define INST_EN_DEFAULT (0xff)
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2022-10-22 18:01:06 +02:00
|
|
|
* Private Data
|
2021-03-03 19:48:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
/* Default configuration */
|
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
struct args_s g_args =
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2022-10-22 18:01:06 +02:00
|
|
|
.time = CONFIG_EXAMPLES_FOC_TIME_DEFAULT,
|
2022-10-22 18:06:06 +02:00
|
|
|
.state = CONFIG_EXAMPLES_FOC_STATE_INIT,
|
|
|
|
.en = INST_EN_DEFAULT,
|
|
|
|
.cfg =
|
|
|
|
{
|
|
|
|
.fmode = CONFIG_EXAMPLES_FOC_FMODE,
|
|
|
|
.mmode = CONFIG_EXAMPLES_FOC_MMODE,
|
2021-10-30 14:27:32 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
|
2022-10-22 18:06:06 +02:00
|
|
|
.qparam = CONFIG_EXAMPLES_FOC_OPENLOOP_Q,
|
2021-10-30 14:27:32 +02:00
|
|
|
#endif
|
2022-02-16 14:03:16 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_CONTROL_PI
|
2022-10-22 18:06:06 +02:00
|
|
|
.foc_pi_kp = CONFIG_EXAMPLES_FOC_IDQ_KP,
|
|
|
|
.foc_pi_ki = CONFIG_EXAMPLES_FOC_IDQ_KI,
|
2022-02-16 14:03:16 +01:00
|
|
|
#endif
|
2021-12-05 16:19:15 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_SETPOINT_CONST
|
2022-10-22 18:01:06 +02:00
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
|
2022-10-22 18:06:06 +02:00
|
|
|
.torqmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
|
2022-10-22 18:01:06 +02:00
|
|
|
# endif
|
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
|
2022-10-22 18:06:06 +02:00
|
|
|
.velmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
|
2022-10-22 18:01:06 +02:00
|
|
|
# endif
|
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
|
2022-10-22 18:06:06 +02:00
|
|
|
.posmax = CONFIG_EXAMPLES_FOC_SETPOINT_CONST_VALUE,
|
2022-10-22 18:01:06 +02:00
|
|
|
# endif
|
2021-10-31 20:30:37 +01:00
|
|
|
#else
|
2022-10-22 18:01:06 +02:00
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_HAVE_TORQ
|
2022-10-22 18:06:06 +02:00
|
|
|
.torqmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
|
2022-10-22 18:01:06 +02:00
|
|
|
# endif
|
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_HAVE_VEL
|
2022-10-22 18:06:06 +02:00
|
|
|
.velmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
|
2022-10-22 18:01:06 +02:00
|
|
|
# endif
|
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_HAVE_POS
|
2022-10-22 18:06:06 +02:00
|
|
|
.posmax = CONFIG_EXAMPLES_FOC_SETPOINT_MAX,
|
2022-10-22 18:01:06 +02:00
|
|
|
# endif
|
2022-10-22 19:41:34 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_HAVE_IDENT
|
|
|
|
.ident_res_ki = CONFIG_EXAMPLES_FOC_IDENT_RES_KI,
|
|
|
|
.ident_res_curr = CONFIG_EXAMPLES_FOC_IDENT_RES_CURRENT,
|
|
|
|
.ident_res_sec = CONFIG_EXAMPLES_FOC_IDENT_RES_SEC,
|
|
|
|
.ident_ind_volt = CONFIG_EXAMPLES_FOC_IDENT_IND_VOLTAGE,
|
|
|
|
.ident_ind_sec = CONFIG_EXAMPLES_FOC_IDENT_IND_SEC,
|
2021-03-03 19:48:19 +01:00
|
|
|
#endif
|
2022-10-22 18:06:06 +02:00
|
|
|
}
|
2022-10-22 18:01:06 +02:00
|
|
|
};
|
2021-10-30 13:53:32 +02:00
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
/* Start allowed at defaule */
|
|
|
|
|
|
|
|
static bool g_start_allowed = true;
|
|
|
|
static pthread_mutex_t g_start_allowed_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foc_mq_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int foc_mq_send(mqd_t mqd, uint8_t msg, FAR void *data)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t buffer[5];
|
|
|
|
uint32_t tmp = 0;
|
|
|
|
|
|
|
|
DEBUGASSERT(data);
|
|
|
|
|
|
|
|
/* Data max 4B */
|
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
tmp = *((FAR uint32_t *)data);
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
buffer[0] = msg;
|
|
|
|
buffer[1] = ((tmp & 0x000000ff) >> 0);
|
|
|
|
buffer[2] = ((tmp & 0x0000ff00) >> 8);
|
|
|
|
buffer[3] = ((tmp & 0x00ff0000) >> 16);
|
|
|
|
buffer[4] = ((tmp & 0xff000000) >> 24);
|
|
|
|
|
|
|
|
ret = mq_send(mqd, (FAR char *)buffer, 5, 42);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("foc_main: mq_send failed %d\n", errno);
|
|
|
|
ret = -errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foc_vbus_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int foc_vbus_send(mqd_t mqd, uint32_t vbus)
|
|
|
|
{
|
|
|
|
return foc_mq_send(mqd, CONTROL_MQ_MSG_VBUS, (FAR void *)&vbus);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-10-30 13:53:32 +02:00
|
|
|
* Name: foc_setpoint_send
|
2021-03-03 19:48:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-10-30 13:53:32 +02:00
|
|
|
static int foc_setpoint_send(mqd_t mqd, uint32_t setpoint)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2021-10-30 13:53:32 +02:00
|
|
|
return foc_mq_send(mqd, CONTROL_MQ_MSG_SETPOINT, (FAR void *)&setpoint);
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foc_state_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int foc_state_send(mqd_t mqd, uint32_t state)
|
|
|
|
{
|
|
|
|
return foc_mq_send(mqd, CONTROL_MQ_MSG_APPSTATE, (FAR void *)&state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foc_start_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int foc_start_send(mqd_t mqd)
|
|
|
|
{
|
|
|
|
int tmp = 0;
|
|
|
|
return foc_mq_send(mqd, CONTROL_MQ_MSG_START, (FAR void *)&tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foc_kill_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int foc_kill_send(mqd_t mqd)
|
|
|
|
{
|
|
|
|
int tmp = 0;
|
|
|
|
return foc_mq_send(mqd, CONTROL_MQ_MSG_KILL, (FAR void *)&tmp);
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE_START
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foc_nxscope_cb_start
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int foc_nxscope_cb_start(FAR void *priv, bool start)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&g_start_allowed_lock);
|
|
|
|
g_start_allowed = start;
|
|
|
|
pthread_mutex_unlock(&g_start_allowed_lock);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-03-03 19:48:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct foc_ctrl_env_s foc[CONFIG_MOTOR_FOC_INST];
|
|
|
|
pthread_t threads[CONFIG_MOTOR_FOC_INST];
|
|
|
|
mqd_t mqd[CONFIG_MOTOR_FOC_INST];
|
2021-12-05 16:19:15 +01:00
|
|
|
struct foc_intf_data_s data;
|
2023-05-05 13:15:36 +02:00
|
|
|
uint32_t thrs_active = 0;
|
2021-03-03 19:48:19 +01:00
|
|
|
int ret = OK;
|
|
|
|
int i = 0;
|
|
|
|
int time = 0;
|
2023-05-17 12:31:25 +02:00
|
|
|
bool startallowed = false;
|
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
|
|
|
|
struct foc_nxscope_s nxs;
|
|
|
|
#endif
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
/* Reset some data */
|
|
|
|
|
|
|
|
memset(mqd, 0, sizeof(mqd_t) * CONFIG_MOTOR_FOC_INST);
|
|
|
|
memset(foc, 0, sizeof(struct foc_ctrl_env_s) * CONFIG_MOTOR_FOC_INST);
|
|
|
|
memset(threads, 0, sizeof(pthread_t) * CONFIG_MOTOR_FOC_INST);
|
2021-12-05 16:19:15 +01:00
|
|
|
memset(&data, 0, sizeof(struct foc_intf_data_s));
|
2023-05-17 12:31:25 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
|
|
|
|
memset(&nxs, 0, sizeof(struct foc_nxscope_s));
|
|
|
|
#endif
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_BUILTIN
|
|
|
|
/* Parse the command line */
|
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
parse_args(&g_args, argc, argv);
|
2021-03-03 19:48:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Validate arguments */
|
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
ret = validate_args(&g_args);
|
2021-03-03 19:48:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: validate args failed\n");
|
2023-05-17 12:31:25 +02:00
|
|
|
goto errout_no_nxscope;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_ARCHINIT
|
|
|
|
/* Perform architecture-specific initialization (if configured) */
|
|
|
|
|
|
|
|
boardctl(BOARDIOC_INIT, 0);
|
|
|
|
|
|
|
|
# ifdef CONFIG_BOARDCTL_FINALINIT
|
|
|
|
/* Perform architecture-specific final-initialization (if configured) */
|
|
|
|
|
|
|
|
boardctl(BOARDIOC_FINALINIT, 0);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRINTF("\nStart foc_main application!\n\n");
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
|
|
|
|
|
|
|
|
# ifdef CONFIG_EXAMPLES_FOC_NXSCOPE_START
|
|
|
|
/* Wait for nxscope */
|
|
|
|
|
|
|
|
g_start_allowed = false;
|
|
|
|
|
|
|
|
/* Connect start callback */
|
|
|
|
|
|
|
|
nxs.cb.start = foc_nxscope_cb_start;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/* Initialize nxscope */
|
|
|
|
|
|
|
|
ret = foc_nxscope_init(&nxs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: failed to initialize nxscope %d\n", ret);
|
|
|
|
goto errout_no_threads;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-05 15:57:42 +01:00
|
|
|
/* Initialize threads */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-12-05 15:57:42 +01:00
|
|
|
ret = foc_threads_init();
|
|
|
|
if (ret < 0)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2021-12-05 15:57:42 +01:00
|
|
|
PRINTF("ERROR: failed to initialize threads %d\n", ret);
|
2023-05-05 11:57:21 +02:00
|
|
|
goto errout_no_intf;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
/* Initialize control interface */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
ret = foc_intf_init();
|
|
|
|
if (ret < 0)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2021-12-05 16:19:15 +01:00
|
|
|
PRINTF("ERROR: failed to initialize control interface %d\n", ret);
|
2021-03-03 19:48:19 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialzie FOC controllers */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
|
|
|
{
|
|
|
|
/* Get configuration */
|
|
|
|
|
2022-10-22 18:15:23 +02:00
|
|
|
foc[i].cfg = &g_args.cfg;
|
2023-05-17 12:31:25 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
|
|
|
|
foc[i].nxs = &nxs;
|
|
|
|
#endif
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
if (g_args.en & (1 << i))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
/* Initialize controller thread if enabled */
|
|
|
|
|
2021-12-05 15:57:42 +01:00
|
|
|
ret = foc_ctrlthr_init(&foc[i], i, &mqd[i], &threads[i]);
|
2021-03-03 19:48:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-12-05 15:57:42 +01:00
|
|
|
PRINTF("ERROR: foc_ctrlthr_init failed %d!\n", ret);
|
2021-03-03 19:48:19 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait some time to finish all controllers initialziation */
|
|
|
|
|
|
|
|
usleep(10000);
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
/* Initial update for VBUS and SETPOINT */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_FOC_VBUS_ADC
|
2021-12-05 16:19:15 +01:00
|
|
|
data.vbus_update = true;
|
|
|
|
data.vbus_raw = VBUS_CONST_VALUE;
|
2021-03-03 19:48:19 +01:00
|
|
|
#endif
|
2021-10-30 13:53:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_FOC_SETPOINT_ADC
|
2021-12-05 16:19:15 +01:00
|
|
|
data.sp_update = true;
|
|
|
|
data.sp_raw = 1;
|
2021-03-03 19:48:19 +01:00
|
|
|
#endif
|
2021-12-05 16:19:15 +01:00
|
|
|
data.state_update = true;
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
/* Controller state */
|
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
data.state = g_args.state;
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
/* Auxliary control loop */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
while (data.terminate != true)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
PRINTFV("foc_main loop %d\n", time);
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_FOC_NXSCOPE) && \
|
|
|
|
!defined(CONFIG_EXAMPLES_FOC_NXSCOPE_THREAD)
|
|
|
|
foc_nxscope_work(&nxs);
|
|
|
|
#endif
|
|
|
|
|
2023-05-05 13:15:36 +02:00
|
|
|
/* Get active control threads */
|
|
|
|
|
|
|
|
thrs_active = foc_threads_get();
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
/* Update control interface */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
ret = foc_intf_update(&data);
|
2021-03-03 19:48:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-12-05 16:19:15 +01:00
|
|
|
PRINTF("ERROR: foc_intf_update failed: %d\n", ret);
|
|
|
|
goto errout;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 1. Update VBUS */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
if (data.vbus_update == true)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
|
|
|
{
|
2023-05-05 13:15:36 +02:00
|
|
|
if ((g_args.en & (1 << i)) && (thrs_active & (1 << i)))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
PRINTFV("Send vbus to %d\n", i);
|
|
|
|
|
|
|
|
/* Send VBUS to thread */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
ret = foc_vbus_send(mqd[i], data.vbus_raw);
|
2021-03-03 19:48:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: foc_vbus_send failed %d\n", ret);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset flag */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
data.vbus_update = false;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Update motor state */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
if (data.state_update == true)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
|
|
|
{
|
2023-05-05 13:15:36 +02:00
|
|
|
if ((g_args.en & (1 << i)) && (thrs_active & (1 << i)))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2021-12-05 16:19:15 +01:00
|
|
|
PRINTFV("Send state %" PRIu32 " to %d\n", data.state, i);
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
/* Send STATE to thread */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
ret = foc_state_send(mqd[i], data.state);
|
2021-03-03 19:48:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: foc_state_send failed %d\n", ret);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset flag */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
data.state_update = false;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 3. Update motor velocity */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
if (data.sp_update == true)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
|
|
|
{
|
2023-05-05 13:15:36 +02:00
|
|
|
if ((g_args.en & (1 << i)) && (thrs_active & (1 << i)))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2021-12-05 16:19:15 +01:00
|
|
|
PRINTFV("Send setpoint = %" PRIu32 "to %d\n",
|
|
|
|
data.sp_raw, i);
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-10-30 13:53:32 +02:00
|
|
|
/* Send setpoint to threads */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
ret = foc_setpoint_send(mqd[i], data.sp_raw);
|
2021-03-03 19:48:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-10-30 13:53:32 +02:00
|
|
|
PRINTF("ERROR: foc_setpoint_send failed %d\n", ret);
|
2021-03-03 19:48:19 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset flag */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
data.sp_update = false;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. One time start */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
if (data.started == false)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2023-05-17 12:31:25 +02:00
|
|
|
/* Is start allowed now ? */
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_start_allowed_lock);
|
|
|
|
startallowed = g_start_allowed;
|
|
|
|
pthread_mutex_unlock(&g_start_allowed_lock);
|
|
|
|
|
|
|
|
if (startallowed)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2023-05-17 12:31:25 +02:00
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2023-05-17 12:31:25 +02:00
|
|
|
if ((g_args.en & (1 << i)) && (thrs_active & (1 << i)))
|
|
|
|
{
|
|
|
|
PRINTFV("Send start to %d\n", i);
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
/* Send START to threads */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
ret = foc_start_send(mqd[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: foc_start_send failed %d\n", ret);
|
|
|
|
goto errout;
|
|
|
|
}
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
/* Set flag */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
data.started = true;
|
|
|
|
}
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle run time */
|
|
|
|
|
|
|
|
time += 1;
|
|
|
|
|
2022-10-22 18:01:06 +02:00
|
|
|
if (g_args.time != -1)
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
2022-10-22 18:01:06 +02:00
|
|
|
if (time >= (g_args.time * (1000000 / MAIN_LOOP_USLEEP)))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
/* Exit loop */
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
data.terminate = true;
|
2021-03-03 19:48:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 17:46:03 +01:00
|
|
|
/* Terminate main loop if threads terminated */
|
|
|
|
|
|
|
|
if (foc_threads_terminated() == true)
|
|
|
|
{
|
2021-12-05 16:19:15 +01:00
|
|
|
data.terminate = true;
|
2021-11-06 17:46:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 19:48:19 +01:00
|
|
|
usleep(MAIN_LOOP_USLEEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
|
2023-05-05 11:57:21 +02:00
|
|
|
/* De-initialize control interface */
|
|
|
|
|
|
|
|
ret = foc_intf_deinit();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: foc_inf_deinit failed %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_no_intf:
|
|
|
|
|
2021-03-03 19:48:19 +01:00
|
|
|
/* Stop FOC control threads */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
|
|
|
{
|
2023-05-05 13:15:36 +02:00
|
|
|
/* Only for active threads */
|
|
|
|
|
|
|
|
thrs_active = foc_threads_get();
|
|
|
|
|
|
|
|
if ((g_args.en & (1 << i)) && (thrs_active & (1 << i)))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
if (mqd[i] != (mqd_t)-1)
|
|
|
|
{
|
|
|
|
/* Stop thread message */
|
|
|
|
|
|
|
|
ret = foc_kill_send(mqd[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: foc_kill_send failed %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
/* Wait for threads termination */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
while (foc_threads_terminated() == false)
|
|
|
|
{
|
|
|
|
usleep(100000);
|
|
|
|
}
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2021-12-05 16:19:15 +01:00
|
|
|
/* De-initialize all mq */
|
2021-03-03 19:48:19 +01:00
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_MOTOR_FOC_INST; i += 1)
|
|
|
|
{
|
2022-10-22 18:01:06 +02:00
|
|
|
if (g_args.en & (1 << i))
|
2021-03-03 19:48:19 +01:00
|
|
|
{
|
|
|
|
/* Close FOC control thread queue */
|
|
|
|
|
|
|
|
if (mqd[i] != (mqd_t)-1)
|
|
|
|
{
|
|
|
|
mq_close(mqd[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 11:57:21 +02:00
|
|
|
/* De-initialize control threads */
|
2021-12-05 16:19:15 +01:00
|
|
|
|
2021-12-05 15:57:42 +01:00
|
|
|
foc_threads_deinit();
|
2021-03-03 19:48:19 +01:00
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_FOC_NXSCOPE
|
2023-05-05 11:57:21 +02:00
|
|
|
errout_no_threads:
|
|
|
|
|
2023-05-17 12:31:25 +02:00
|
|
|
/* De-initialize NxScope */
|
|
|
|
|
|
|
|
foc_nxscope_deinit(&nxs);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
errout_no_nxscope:
|
2021-03-03 19:48:19 +01:00
|
|
|
PRINTF("foc_main exit\n");
|
|
|
|
return 0;
|
|
|
|
}
|