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)){