From ef28f3057a28bfc9d845829a22fdfaaac7d5fb4c Mon Sep 17 00:00:00 2001 From: wlyu Date: Thu, 31 Mar 2022 11:27:16 +0800 Subject: [PATCH] optimize plc motor test and fix the lwip mutex and sem error --- .../control_app/opcua_demo/opcua_demo.c | 1 - .../control_app/plc_demo/plc_control_demo.c | 118 +++++++++++++----- .../plc/interoperability/opcua/open62541.c | 12 ++ .../resources/ethernet/LwIP/arch/sys_arch.c | 9 +- .../resources/ethernet/LwIP/arch/sys_arch.h | 4 +- .../XiZi/tool/shell/letter-shell/shell.c | 1 - 6 files changed, 107 insertions(+), 38 deletions(-) diff --git a/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c b/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c index eb4962eef..6cabf185c 100755 --- a/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c +++ b/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c @@ -21,7 +21,6 @@ #include #include #include "board.h" -#include #include "open62541.h" #include "ua_api.h" diff --git a/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c b/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c index 0df64375f..fed4063bf 100755 --- a/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c +++ b/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c @@ -30,12 +30,23 @@ struct PlcChannel plc_demo_ch; struct PlcDriver plc_demo_drv; struct PlcDevice plc_demo_dev; +int plc_test_flag = 0; + PlcCtrlParamType plc_ctrl_param; UA_NodeId test_nodeid = {4, UA_NODEIDTYPE_NUMERIC, 5}; /******************************************************************************/ +void PlcDelay(int sec) +{ + volatile uint32_t i = 0; + for (i = 0; i < 100000000 * sec; ++i) + { + __asm("NOP"); /* delay */ + } +} + // get NodeId from str void PlcGetTestNodeId(char *str, UA_NodeId *id) { @@ -149,7 +160,7 @@ void PlcReadUATask(void* arg) ops->close(&plc_demo_dev); } -void PlcReadTest(int argc, char* argv[]) +void PlcReadTestShell(int argc, char* argv[]) { static char node_str[UA_NODE_LEN]; memset(node_str, 0, sizeof(node_str)); @@ -163,7 +174,7 @@ void PlcReadTest(int argc, char* argv[]) } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), - PlcRead, PlcReadTest, Read PLC); + PlcRead, PlcReadTestShell, Read PLC); void PlcWriteUATask(void* arg) { @@ -192,7 +203,7 @@ void PlcWriteUATask(void* arg) ops->close(&plc_demo_dev); } -void PlcWriteTest(int argc, char* argv[]) +void PlcWriteTestShell(int argc, char* argv[]) { static char node_str[UA_NODE_LEN]; static char val_param[UA_NODE_LEN]; @@ -214,32 +225,38 @@ void PlcWriteTest(int argc, char* argv[]) } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), - PlcWrite, PlcWriteTest, Read PLC); + PlcWrite, PlcWriteTestShell, Read PLC); // test motor -// 清零 -//PlcWrite n4,2 0b -//PlcWrite n4,3 0b -//PlcWrite n4,4 0b -//PlcWrite n4,5 0b +// clear parameter +// PlcWrite n4,2 0b +// PlcWrite n4,3 0b +// PlcWrite n4,4 0b +// PlcWrite n4,5 0b // -// 使能 -//PlcWrite n4,2 1b +// enable +// PlcWrite n4,2 1b // -// 设转速 -//PlcWrite n4,7 50 +// set rotate speed +// PlcWrite n4,3 50 // -// 正转 -//PlcWrite n4,4 1b +// positive turn +// PlcWrite n4,4 1b // -// 反转 -//PlcWrite n4,5 1b +// reversal turn +// PlcWrite n4,5 1b +static int plc_test_speed = 50; +static int plc_test_dir = 1; // direction positive: 1 reversal: 0 void PlcMotorTestTask(void* arg) { - char *test_target[] = {"n4,2", "n4,3", "n4,4", "n4,5"}; - char *test_cmd[] = {"1b", "50", "1b", "1b"}; + //support node id + char *test_nodeid[] = {"n4,2", "n4,3", "n4,4", "n4,5", "n4,7"}; + // enable -> speed -> positive dir or inversal dir -> stop -> enable + char test_sort[] = {0, 4, 2, 1, 0}; + char test_cmd[][4] = {"1b", "50", "1b", "1b", "0b"}; + char *test_notice[] = {"Enable Motor", "Set Speed", "Set Forward", "Set Reverse", "Stop Motor"}; int ret = 0; struct PlcOps* ops = NULL; @@ -258,37 +275,80 @@ void PlcMotorTestTask(void* arg) UaParamType* ua_ptr = plc_demo_dev.priv_data; - for(int i = 0; i < 4; i++) + // initialize step + for(int i = 0; i < 5; i++) { - UA_NodeId id; - PlcGetTestNodeId(test_target[i], &id); - memcpy(&ua_ptr->ua_id, &id, sizeof(id)); + plc_print("###\n### Clear %s\n###\n", test_notice[i]); + PlcGetTestNodeId(test_nodeid[i], &ua_ptr->ua_id); ret = ops->write(&plc_demo_dev, "0b", PLC_BUF_SIZE); if(EOK != ret) { plc_print("plc: [%s] %d write failed\n", __func__, __LINE__); } + PlcDelay(1); } - for(int i = 0; i < 4; i++) + if(plc_test_speed != 50) { - UA_NodeId id; - PlcGetTestNodeId(test_target[i], &id); - memcpy(&ua_ptr->ua_id, &id, sizeof(id)); + snprintf(test_cmd[1], 4, "%d", plc_test_speed); + } + + if(plc_test_dir == 0) // if not postive, next running + test_sort[2] = 3; + + for(int i = 0; i < sizeof(test_sort)/sizeof(test_sort[0]); i++) + { + PlcGetTestNodeId(test_nodeid[test_sort[i]], &ua_ptr->ua_id); + plc_print("###\n### %s\n###\n", test_notice[i]); ret = ops->write(&plc_demo_dev, test_cmd[i], PLC_BUF_SIZE); if(EOK != ret) { plc_print("plc: [%s] %d write failed\n", __func__, __LINE__); } + PlcDelay(1); + if(i == 2) // postive + { + PlcDelay(10); + } } ops->close(&plc_demo_dev); + plc_test_flag = 0; } -void PlcMotorTest(int argc, char* argv[]) +// get parameter from +void PlcGetMotorParam(char *str) { + static char node_str[UA_NODE_LEN]; + memset(node_str, 0, sizeof(node_str)); + + plc_print("plc: arg %s\n", str); + + sscanf(str, "speed=%d", &plc_test_speed); + sscanf(str, "dir=%d", &plc_test_dir); + plc_print("speed is %d\n", plc_test_speed); + plc_print("dir is %d\n", plc_test_dir); +} + +void PlcMotorTestShell(int argc, char* argv[]) +{ + if(plc_test_flag) + { + plc_print("PLC Motor testing!\n"); + return; + } + plc_test_flag = 1; + + if(argc > 1) + { + for(int i = 0; i < argc; i++) + { + PlcGetMotorParam(argv[i]); + } + } + sys_thread_new("plc motor", PlcMotorTestTask, NULL, PLC_STACK_SIZE, PLC_TASK_PRIO); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), - PlcMotorTest, PlcMotorTest, Run motor); + PlcMotorTest, PlcMotorTestShell, Run motor); diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c b/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c index c5140a9f9..c4bba7b04 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c @@ -23,6 +23,18 @@ * @date 2021.12.15 */ +/************************************************* +File name: open62541.c +Description: Support OPCUA protocol +Others: take https://github.com/open62541/open62541.git +History: +1. Date: 2021-12-15 +Author: AIIT XUOS Lab +Modification: +1. added debug +2. fixed bug to free twice when receiveResponse timeout +*************************************************/ + #ifndef UA_DYNAMIC_LINKING_EXPORT # define UA_DYNAMIC_LINKING_EXPORT # define MDNSD_DYNAMIC_LINKING diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c index d9fe5b640..aed1bda45 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c @@ -184,7 +184,7 @@ sys_sem_free(sys_sem_t *sem) int sys_sem_valid(sys_sem_t *sem) { - return (*sem >= SYS_SEM_NULL); + return (*sem > SYS_SEM_NULL); } void @@ -212,7 +212,7 @@ u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) if(KSemaphoreObtain(*sem, wait_time) == EOK) - return ((CurrentTicksGain()-start_tick)*MS_PER_SYSTICK_F407); + return ((CurrentTicksGain()-start_tick)*MS_PER_SYSTICK); else return SYS_ARCH_TIMEOUT; } @@ -226,7 +226,7 @@ void sys_sem_signal(sys_sem_t *sem) err_t sys_mutex_new(sys_mutex_t *mutex) { *mutex = KMutexCreate(); - if(*mutex >= 0) + if(*mutex > SYS_MRTEX_NULL) return ERR_OK; else { @@ -344,7 +344,7 @@ u32_t sys_arch_mbox_fetch(sys_mbox_t *q, void **msg, u32_t timeout) wait_time = timeout; if(KMsgQueueRecv(*q, &(*msg), sizeof(void *), wait_time) == EOK) - return ((CurrentTicksGain()-start_tick)*MS_PER_SYSTICK_F407); + return ((CurrentTicksGain()-start_tick)*MS_PER_SYSTICK); else{ *msg = NULL; return SYS_ARCH_TIMEOUT; @@ -446,7 +446,6 @@ void lwip_input_thread(void *param) /* Poll the driver, get any outstanding frames */ ethernetif_input(net); sys_check_timeouts(); /* Handle all system timeouts for all core protocols */ - KPrintf(""); } } } diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.h b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.h index 64ae8e826..bea4e6943 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.h +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.h @@ -62,7 +62,7 @@ /* USER CODE END 0 */ #define SYS_MBOX_NULL -1 -#define SYS_SEM_NULL 0 +#define SYS_SEM_NULL -1 #define SYS_MRTEX_NULL SYS_SEM_NULL typedef int32 sys_sem_t; @@ -71,7 +71,7 @@ typedef int32 sys_mbox_t; typedef int32 sys_thread_t; typedef x_base sys_prot_t; -#define MS_PER_SYSTICK_F407 (1000 / TICK_PER_SECOND) +#define MS_PER_SYSTICK (1000 / TICK_PER_SECOND) //debug rtos with IRQ //#define FSL_RTOS_XIUOS diff --git a/Ubiquitous/XiZi/tool/shell/letter-shell/shell.c b/Ubiquitous/XiZi/tool/shell/letter-shell/shell.c index 7cab44a65..6a923ece8 100644 --- a/Ubiquitous/XiZi/tool/shell/letter-shell/shell.c +++ b/Ubiquitous/XiZi/tool/shell/letter-shell/shell.c @@ -1652,7 +1652,6 @@ void shellTask(void *param) shellHandler(shell, data[i]); } } - KPrintf(""); } }