From a0253be32dc33e4cbce118e791a6f6a0825bc070 Mon Sep 17 00:00:00 2001 From: luoyimei Date: Fri, 1 Dec 2023 18:00:57 +0800 Subject: [PATCH] update detect_task.c --- demos/ascend310b/apps/openamp/detect_task.c | 112 +++++++++++++++++--- 1 file changed, 95 insertions(+), 17 deletions(-) diff --git a/demos/ascend310b/apps/openamp/detect_task.c b/demos/ascend310b/apps/openamp/detect_task.c index 4217f63b..3ecd5e6e 100644 --- a/demos/ascend310b/apps/openamp/detect_task.c +++ b/demos/ascend310b/apps/openamp/detect_task.c @@ -15,13 +15,14 @@ #define BROADCAST_IP "10.132.100.255" // 所在局域网的广播地址 #define BROADCAST_PORT 60000 -#define DECTECT_TIMER_INTERVAL 1000 -#define TIMEOUT_US ((DECTECT_TIMER_INTERVAL - 50) * 1000) -#define MAX_TIMEOUT_CNT 3 +#define DECTECT_TIMER_INTERVAL 10 +#define TIMEOUT_US ((DECTECT_TIMER_INTERVAL - 2) * 1000) +#define MAX_SENDTIMEOUT_CNT 3 +#define MAX_RECVTIMEOUT_CNT 4 #define TARGET_POSTION_BEGIN 15000000 -#define TARGET_POSTION_B 30000000 +#define TARGET_POSTION_B 60000000 #define TARGET_POSTION_C 10000000 -#define TARGET_POSTION_D 20000000 +#define TARGET_POSTION_D 40000000 #define TARGET_POSTION_END 0xFFFFFFFF int g_serverSocket, g_clientSocket; struct sockaddr_in g_LocalServerAddr, g_RemoteServerAddr; @@ -45,6 +46,8 @@ struct communicationMsgInfo g_localMsg = {0}; #endif #endif +// #define SWITCH_TEST + extern U32 PRT_Printf(const char *format, ...); extern int PRT_ProxyIoctl(int fd, unsigned long request, void *arg, size_t len); int CreateOrResumeDemoTask(); @@ -80,7 +83,7 @@ uintptr_t GetRandNum() int UDPSendMsgWithBuf(char *sendBuf) { int ret; - benchmark_timer_initialize(); + // benchmark_timer_initialize(); ret = sendto(g_clientSocket, sendBuf, strlen(sendBuf), 0, (struct sockaddr*)&g_RemoteServerAddr, sizeof(struct sockaddr_in)); return ret; @@ -109,13 +112,13 @@ int blockLocalMsg(struct communicationMsgInfo *remoteMsg) void changeLocalState(local_state_t changestate){ char *localState[] = {"SLAVE_CONNECTED", "SLAVE_DISCONNECTED", "MASTER_CONNECTED", "MASTER_DISCONNECTED"}; - PRT_Printf("%s to %s\n", localState[g_localState], localState[changestate]); + PRT_Printf("%s\n", localState[changestate]); g_localState = changestate; } void selectTask(struct communicationMsgInfo *remoteMsg) { // g_recvMsgState = KEEP_ALIVE; - if(g_sendMsgErr > MAX_TIMEOUT_CNT){ // 本端设备有问题,保持备状态 + if(g_sendMsgErr > 0){ // 本端设备有问题,保持备状态,防止还未收到消息就把自己设成主 return; } if (g_waitMsgCount == 0) { @@ -142,8 +145,14 @@ void selectTask(struct communicationMsgInfo *remoteMsg) } int ret; if (g_localMsg.isLocalMaster) { +#ifdef SWITCH_TEST + uintptr_t swichTime = benchmark_timer_read(); + PRT_Printf("swichTime:%llu.\n", swichTime); +#endif ret = CreateOrResumeDemoTask(); if (ret != OS_OK) { + EthercatKoControl(0); + g_localMsg.isLocalMaster =FALSE; PRT_Printf("S_M CreateOrResumeDemoTask fail.\n"); } } else { @@ -172,7 +181,7 @@ void keepAlive(struct communicationMsgInfo *remoteMsg) void detectFail(int recvMsgLen) { // 1、本机为主才会走到该分支 - if (g_sendMsgErr >= MAX_TIMEOUT_CNT) { + if (g_sendMsgErr >= MAX_SENDTIMEOUT_CNT) { g_localMsg.isLocalMaster = FALSE; SuspendDemoTask(); changeLocalState(SLAVE_DISCONNECTED); // 本端有问题,降备 @@ -189,8 +198,14 @@ void RecvMsgProc(char *recvBuf, int recvMsgLen) // 1、解析数据,处理对端消息 struct communicationMsgInfo remoteMsg = { 0 }; if(recvMsgLen != sizeof(communicationMsgInfo)) { +#ifdef SWITCH_TEST + if(recvMsgLen == sizeof(g_cycle)){ + PRT_Printf("remoteTime:%llu.\n", (uintptr_t)&recvBuf); + benchmark_timer_initialize(); + } +#endif // PRT_Printf("[Error]: recvMsglen error, g_waitMsgCount = %d\n", g_waitMsgCount); - if (g_waitMsgCount >= MAX_TIMEOUT_CNT || g_sendMsgErr >= MAX_TIMEOUT_CNT) { + if (g_waitMsgCount >= MAX_RECVTIMEOUT_CNT || g_sendMsgErr >= MAX_SENDTIMEOUT_CNT) { // PRT_Printf("three time.\n"); if(g_localMsg.isLocalMaster) { changeLocalState(MASTER_DISCONNECTED); @@ -342,7 +357,7 @@ int SocketInit(void) void DectectTimerCallBack(TimerHandle tmrHandle, U32 arg1, U32 arg2, U32 arg3, U32 arg4) { - int ret = 0; + int ret = -1; #ifdef PERF_TEST #ifdef DEVICE_1 char sendmsg[] = "UDPAAA"; @@ -360,19 +375,67 @@ void DectectTimerCallBack(TimerHandle tmrHandle, U32 arg1, U32 arg2, U32 arg3, U } #endif #else +#ifdef SWITCH_TEST + if(g_localMsg.isLocalMaster) { + static int sendMsgCnt = 0; + if(sendMsgCnt == 10000) { + PRT_Printf("cycle:%llu.\n", g_cycle); + sendto(g_clientSocket, (void *)&g_cycle, sizeof(g_cycle), 0, (struct sockaddr*)&g_RemoteServerAddr, + sizeof(struct sockaddr_in)); + sendMsgCnt++; + return; + } else if(sendMsgCnt > 10000){ + if (g_waitMsgCount < MAX_RECVTIMEOUT_CNT) { + g_waitMsgCount++; + return; + // PRT_Printf("sendto failed. CNT:%d\n", g_sendMsgErr); + } + return; + } + sendMsgCnt++; + } ret = UDPSendMsg(); if (ret != 0) { - if (g_sendMsgErr <= MAX_TIMEOUT_CNT) { + if (g_sendMsgErr < MAX_SENDTIMEOUT_CNT) { g_sendMsgErr++; } + // PRT_Printf("sendto failed. CNT:%d\n", g_sendMsgErr); + return; + } + if (g_sendMsgErr > 0) { + g_sendMsgErr--; + return; + } + if (g_waitMsgCount < MAX_RECVTIMEOUT_CNT) + { + g_waitMsgCount++; + if (g_waitMsgCount > 2) { + PRT_Printf("waitCount %d\n", g_waitMsgCount); + } + return; + } + // PRT_Printf("waitCount %d\n", g_waitMsgCount); +#else + ret = UDPSendMsg(); + if (ret != 0) { + if (g_sendMsgErr < MAX_SENDTIMEOUT_CNT) { + g_sendMsgErr++; + return; + } PRT_Printf("sendto failed. CNT:%d\n", g_sendMsgErr); return; } - g_sendMsgErr = 0; - if (g_waitMsgCount <= MAX_TIMEOUT_CNT) { + if (g_sendMsgErr > 0) { + g_sendMsgErr--; + return; + } + if (g_waitMsgCount < MAX_RECVTIMEOUT_CNT) + { g_waitMsgCount++; + return; } PRT_Printf("waitCount %d\n", g_waitMsgCount); +#endif #endif return; } @@ -488,14 +551,30 @@ void TargetPostionUpdate() void DemoTask() { + static int tsmpCnt = 0; + static int demoCnt = 0; int isReachTarget = FALSE; while (1) { + demoCnt++; + tsmpCnt++; + + if (demoCnt == 100) { + PRT_Printf("-------DemoTask------------.\n "); + demoCnt = 0; + } + PRT_TaskDelay(10); // 对应wait_rest_of_period,时间要确认下 if (!(g_localMsg.isLocalMaster)) { - PRT_Printf("demo not master.\n "); + if (tsmpCnt == 100) { + PRT_Printf("demo not master.\n "); + tsmpCnt = 0; + } continue; } isReachTarget = EthercatControl(g_localMsg.deviceStatus, g_localMsg.isLocalMaster); - PRT_Printf("S:%d M:%d R:%d.\n ", g_localMsg.deviceStatus, g_localMsg.isLocalMaster, isReachTarget); + if (tsmpCnt == 100) { + PRT_Printf("S:%d M:%d R:%d.\n ", g_localMsg.deviceStatus, g_localMsg.isLocalMaster, isReachTarget); + tsmpCnt = 0; + } if (isReachTarget) { TargetPostionUpdate(); if (g_localMsg.deviceStatus == TARGET_POSTION_END) { @@ -503,7 +582,6 @@ void DemoTask() } isReachTarget = FALSE; } - PRT_TaskDelay(10); // 对应wait_rest_of_period,时间要确认下 } } -- Gitee