From 5834e232635308888247318dc506ec378ab26dac Mon Sep 17 00:00:00 2001 From: Changwoo Park Date: Tue, 8 Aug 2023 09:18:37 +0900 Subject: [PATCH] =?UTF-8?q?[Periodics][Init]=20-=20Periodics=20=20=20?= =?UTF-8?q?=E3=84=B4=20Analog=20read:=20=200=20to=209=20pin,=20100ms=20x?= =?UTF-8?q?=2050=20samples=20=3D=20500ms=20time=20window=20-=20Analog=20re?= =?UTF-8?q?ad=20value=20=20=20=E3=84=B4=20=ED=8F=89=EA=B7=A0=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EB=B3=B5=EC=88=98=EC=9D=98=20Analog=20rea?= =?UTF-8?q?d=20data=EB=A5=BC=20=EB=A7=81=20=ED=98=95=ED=83=9C=EB=A1=9C=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20(=EA=B8=B0=EC=A1=B4=EC=97=90=EB=8A=94=20?= =?UTF-8?q?=EB=B0=B0=EC=97=B4=20=EC=9E=AC=EC=A0=95=EB=A0=AC,=20=EC=97=B0?= =?UTF-8?q?=EC=82=B0=EC=86=8D=EB=8F=84=20=ED=96=A5=EC=83=81=20=ED=95=84?= =?UTF-8?q?=EC=9A=94)=20=20=20=E3=84=B4=20=ED=8F=89=EA=B7=A0=EC=97=90=20?= =?UTF-8?q?=EC=9D=98=ED=95=9C=20=EC=A0=95=EB=B0=80=EB=8F=84=20=ED=96=A5?= =?UTF-8?q?=EC=83=81=EC=9C=BC=EB=A1=9C=20floating=20=EC=97=B0=EC=82=B0=20?= =?UTF-8?q?=ED=9B=84=20factor=20=EA=B3=B1=ED=95=98=EC=97=AC=20=EC=86=A1?= =?UTF-8?q?=EC=8B=A0=20-=20MC9=20=20=20=E3=84=B4=20not=20responsed=20?= =?UTF-8?q?=EB=94=94=EB=B2=84=EA=B7=B8=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=A3=BC=EC=84=9D=20(=EC=B6=94=ED=9B=84=20=EB=94=94=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=EB=AA=A8=EB=93=9C=20=EA=B0=9C=EB=B0=9C=20=EC=98=88?= =?UTF-8?q?=EC=A0=95)=20-=20Init=20=20=20=E3=84=B4=20AO,=20MC9=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EB=B0=A9=EC=A7=80?= =?UTF-8?q?=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EB=A6=AC=ED=84=B4=20=EA=B0=92?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FC_InterfaceBoard/FC_InterfaceBoard.h | 18 ++++++++++--- FC_InterfaceBoard/FC_InterfaceBoard.ino | 7 ++--- FC_InterfaceBoard/GPIO.ino | 6 +++++ FC_InterfaceBoard/MC9.ino | 5 ++-- FC_InterfaceBoard/Periodics.ino | 34 +++++++++++++++++++------ FC_InterfaceBoard/Processes.ino | 21 ++++++++++----- FC_InterfaceBoard/RS485.ino | 2 +- 7 files changed, 68 insertions(+), 25 deletions(-) diff --git a/FC_InterfaceBoard/FC_InterfaceBoard.h b/FC_InterfaceBoard/FC_InterfaceBoard.h index d3aeb81..a1ed167 100644 --- a/FC_InterfaceBoard/FC_InterfaceBoard.h +++ b/FC_InterfaceBoard/FC_InterfaceBoard.h @@ -3,9 +3,19 @@ #include // Ethernet2 v1.0.4 #include "GP8403.h" +#define AI_AVG_SIZE 50 +#define AI_RANGE 10 + +/* ---------- Init ---------- */ +bool Init_AO = false; +bool Init_MC9 = false; + /* ---------- Periodics ---------- */ // Flags bool T_1ms = false; +bool T_2ms = false; +bool T_4ms = false; +bool T_5ms = false; bool T_10ms = false; bool T_20ms = false; bool T_50ms = false; @@ -19,7 +29,6 @@ bool T_5000ms = false; // Datas int msCnt = 0; unsigned long timer = 0; -#define AI_AVG_SIZE 20 /* ---------- Arduino Mega 2560 Basic IO ---------- */ // Pins @@ -32,11 +41,12 @@ const int AiPin[] = { A0, A1, A2, A3, A4, A5, A6, A7, // Data Storages int Size_DO = 16; int Size_DI = 8; -int Size_AI = 16; +int Size_AI = AI_RANGE; +int index_AI = 0; int Values_DI; // digit values to a number -int Values_AI[16]; -int Recent_AI[16][AI_AVG_SIZE]; +int Values_AI[AI_RANGE]; +int Recent_AI[AI_RANGE][AI_AVG_SIZE]; /* ---------- I2C (Analog Output, DAC) ---------- */ // Addresses diff --git a/FC_InterfaceBoard/FC_InterfaceBoard.ino b/FC_InterfaceBoard/FC_InterfaceBoard.ino index 83e195b..7d1923d 100644 --- a/FC_InterfaceBoard/FC_InterfaceBoard.ino +++ b/FC_InterfaceBoard/FC_InterfaceBoard.ino @@ -1,8 +1,6 @@ #include "FC_InterfaceBoard.h" - - #define CR "\r" #define FIN "\n" #define CRLF "\r\n" @@ -13,7 +11,7 @@ String IdeSerial; // for 485_0 void setup() { - // put your setup code here, to run once: + // Serial setup (Debug) Serial.begin(9600); Serial.println("Start!"); @@ -98,13 +96,16 @@ void loop() { } // Init else if(cmd=="State?"){ + client.print(cmd + " " + RcvOK + FIN); }else if(cmd=="RngAO!"){ dataSize = demuxNum(cmdData, data); client.print(Prcss_RngAO(data, dataSize)); + Init_AO = true; }else if(cmd=="ChMC9_1!"){ dataSize = demuxNum(cmdData, data); client.print(Prcss_ChMC9(data, dataSize)); + Init_MC9 = true; } // Extra diff --git a/FC_InterfaceBoard/GPIO.ino b/FC_InterfaceBoard/GPIO.ino index 886ef3c..f71a574 100644 --- a/FC_InterfaceBoard/GPIO.ino +++ b/FC_InterfaceBoard/GPIO.ino @@ -15,11 +15,17 @@ void read_analog(){ for(i = 0 ; i < Size_AI ; i++){ Values_AI[i] = analogRead(AiPin[i]); + /* for(int j = 0; j < AI_AVG_SIZE - 1; j++) { Recent_AI[i][j] = Recent_AI[i][j + 1]; } Recent_AI[i][AI_AVG_SIZE - 1] = Values_AI[i]; + */ + // 현재 인덱스 위치에 값을 저장 + Recent_AI[i][index_AI] = Values_AI[i]; } + // 인덱스를 증가시키고, 필요한 경우 순환 + index_AI = (index_AI + 1) % AI_AVG_SIZE; } void read_digital(){ diff --git a/FC_InterfaceBoard/MC9.ino b/FC_InterfaceBoard/MC9.ino index d4364c6..c08efc1 100644 --- a/FC_InterfaceBoard/MC9.ino +++ b/FC_InterfaceBoard/MC9.ino @@ -225,7 +225,7 @@ int timeoutMC9(){ msg = latest_sent_msg.substring(1); sscanf(msg.c_str(), "%2d%3s", &addr, &dummy); - Serial.print("485 not responced... (Timeout) Req msg : " + latest_sent_msg); + //230721 Serial.print("485 not responced... (Timeout) Req msg : " + latest_sent_msg); if (latest_sent_msg.indexOf("DRS") != -1) { for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s @@ -235,7 +235,7 @@ int timeoutMC9(){ break; } } - + /* 230721 // If not matched, return error if(idx < 0){ Serial.println(); @@ -253,6 +253,7 @@ int timeoutMC9(){ Serial.print(" >> SV @"); Serial.println(idx); } + */ return 0; } else { diff --git a/FC_InterfaceBoard/Periodics.ino b/FC_InterfaceBoard/Periodics.ino index 88f76b5..b5866ac 100644 --- a/FC_InterfaceBoard/Periodics.ino +++ b/FC_InterfaceBoard/Periodics.ino @@ -1,22 +1,31 @@ void Periodic_run(){ if(T_1ms){ - // Read Arduino AI - read_analog(); + // read_analog(); T_1ms = false; } - if(T_10ms){ + if(T_2ms){ + //read_analog(); // runtime of 1.xxx ms @8ch + T_2ms = false; + } + if(T_4ms){ //read_analog(); + T_4ms = false; + } + if(T_5ms){ + //read_analog(); // runtime of 2.048012 ms @16ch + T_5ms = false; + } + if(T_10ms){ + read_analog(); T_10ms = false; } if(T_20ms){ - // send and recieve RS485 send_485(); recieve_485(); T_20ms = false; } if(T_50ms){ - // Read Arduino DI read_digital(); T_50ms = false; } @@ -29,8 +38,8 @@ void Periodic_run(){ T_200ms = false; } if(T_500ms){ - // RS485 recieve (for developing) - recieve_485_0(); + + recieve_485_0(); // RS485 recieve (for developing) T_500ms = false; } if(T_1000ms){ @@ -43,7 +52,7 @@ void Periodic_run(){ T_1000ms = false; } if(T_2000ms){ - if(numOf485 > 0){ + if(numOf485 > 10){ Serial.print("----- remain 485 buff : "); // For Serial.println(numOf485); // Debugging } @@ -64,6 +73,15 @@ void timer_1ms(){ msCnt += 1; T_1ms = true; + if (msCnt % 2 == 0){ + T_2ms = true; + } + if (msCnt % 4 == 0){ + T_4ms = true; + } + if (msCnt % 5 == 0){ + T_5ms = true; + } if (msCnt % 10 == 0){ T_10ms = true; } diff --git a/FC_InterfaceBoard/Processes.ino b/FC_InterfaceBoard/Processes.ino index d614bdc..860acb2 100644 --- a/FC_InterfaceBoard/Processes.ino +++ b/FC_InterfaceBoard/Processes.ino @@ -1,23 +1,27 @@ String Prcss_ALL_Read(){ String str = ""; + char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + float factor = 1.0f / AI_AVG_SIZE; str += "AI?:"; for (int i = 0; i < Size_AI; i++) { - int sum_AI = 0; + float sum_AI = 0; for(int j = 0; j < AI_AVG_SIZE; j++) { - sum_AI += Recent_AI[i][j]; + sum_AI += (float) Recent_AI[i][j] * factor; } - char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + sum_AI *= 50.0f; // convert the sum to an integer representation, Transfer data max(0xFFFF): 65,535. Each data max(10bit ADC): 1024. 1024 x 50 = 51,200 + unsigned int integer_part = (unsigned int)sum_AI; + //sprintf(formattedNumber, "%04X", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성 - sprintf(formattedNumber, "%04X", sum_AI); // 4자리로 고정된 형식의 문자열 생성 + sprintf(formattedNumber, "%04X", integer_part); // 4자리로 고정된 형식의 문자열 생성 str += formattedNumber; // 형식화된 문자열 추가 str += ','; } str += RcvOK; str += "DI?:"; - char formattedNumber[5]; + sprintf(formattedNumber, "%04X", Values_DI); str += formattedNumber; str += ','; @@ -25,7 +29,6 @@ String Prcss_ALL_Read(){ str += "PV_1?:"; for (int i = 0; i < RS485_1_Rcv_size*2; i++) { - char formattedNumber[5]; int rcv = int(RS485_1_Rcv_PV[i/RS485_1_Rcv_size]); sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i] * rcv); @@ -37,7 +40,6 @@ String Prcss_ALL_Read(){ str += "SV_1?:"; for (int i = 0; i < RS485_1_Rcv_size*2; i++) { - char formattedNumber[5]; int rcv = int(RS485_1_Rcv_SV[i/RS485_1_Rcv_size]); sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i] * rcv); @@ -46,6 +48,11 @@ String Prcss_ALL_Read(){ } str += RcvOK; + str += "INIT?:"; + sprintf(formattedNumber, "%04X", ((unsigned int)(Init_AO & Init_MC9))); + str += formattedNumber; + str += ','; + str += RcvOK; return str + FIN; } diff --git a/FC_InterfaceBoard/RS485.ino b/FC_InterfaceBoard/RS485.ino index 4ea020c..8c93e22 100644 --- a/FC_InterfaceBoard/RS485.ino +++ b/FC_InterfaceBoard/RS485.ino @@ -43,7 +43,7 @@ int recieve_485(){ // Timeout code if(Wait_485_1){ Wait_485_1_cnt++; - if(Wait_485_1_cnt > 25){ // Timeout = periodic(20ms) x 25 = 500 msec + if(Wait_485_1_cnt > 25){ // Timeout = periodic(20ms) x 20 = 400 msec //Serial.println("485 not responced... (Timeout)"); timeoutMC9(); //if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){