diff --git a/FC_InferfaceBoard/Analog_Out.ino b/FC_InferfaceBoard/Analog_Out.ino index 30a4895..15d5c14 100644 --- a/FC_InferfaceBoard/Analog_Out.ino +++ b/FC_InferfaceBoard/Analog_Out.ino @@ -1,65 +1,52 @@ void AO_setup(int i, int Rng) { - DFRobot_GP8403 AO; + DFRobot_GP8403* AO; bool needBegin = true; bool needRng = true; - int twinRng = 0; - if(i%2){ // Odd - twinRng = RngAO[i-1]; - }else{ // Even - twinRng = RngAO[i+1]; - } - - needBegin = (twinRng == 0); - - if(Rng > twinRng){ - needRng = true; - }else{ - needRng = false; - } + needBegin = (RngAO[i] == 0); if(i <= 1){ - AO = AO_0; + AO = &AO_0; }else if(i <= 3){ - AO = AO_2; + AO = &AO_2; }else if(i <= 5){ - AO = AO_4; + AO = &AO_4; }else if(i <= 7){ - AO = AO_6; + AO = &AO_6; }else if(i <= 9){ - AO = AO_8; + AO = &AO_8; }else if(i <= 11){ - AO = AO_10; + AO = &AO_10; }else if(i <= 13){ - AO = AO_12; + AO = &AO_12; }else if(i <= 15){ - AO = AO_14; + AO = &AO_14; } if(needBegin){ // If not begin yet - Serial.println("AO init begin"); - while(AO.begin()!=0); - Serial.println("AO init succeed"); - AO.setDACOutVoltage(0, 0); - AO.setDACOutVoltage(0, 1); - delay(20); - AO.store(); + Serial.println("AO_"+ String(i) + ": initialization Begin"); + while(AO->begin()!=0); + AO->setDACOutVoltage(0, 2); + delay(5); + AO->store(); } - if(needRng){ - if(Rng == 10){ - AO.setDACOutRange(AO.eOutputRange10V); - }else{ - AO.setDACOutRange(AO.eOutputRange5V); - } + + if(Rng == 10){ + AO->setDACOutRange(AO->eOutputRange10V); + Serial.println("AO_"+ String(i) + ": Initialized, Set Range 10V"); + }else{ + AO->setDACOutRange(AO->eOutputRange5V); + Serial.println("AO_"+ String(i) + ": Initialized, Set Range 5V"); } + RngAO[i] = Rng; - Serial.println("AO FIN"); + RngAO[i+1] = Rng; } void AO_Write(int i, int volt) { - DFRobot_GP8403 AO; + DFRobot_GP8403* AO; int ch; if(i%2){ // Odd @@ -69,22 +56,22 @@ void AO_Write(int i, int volt) { } if(i <= 1){ - AO = AO_0; + AO = &AO_0; }else if(i <= 3){ - AO = AO_2; + AO = &AO_2; }else if(i <= 5){ - AO = AO_4; + AO = &AO_4; }else if(i <= 7){ - AO = AO_6; + AO = &AO_6; }else if(i <= 9){ - AO = AO_8; + AO = &AO_8; }else if(i <= 11){ - AO = AO_10; + AO = &AO_10; }else if(i <= 13){ - AO = AO_12; + AO = &AO_12; }else if(i <= 15){ - AO = AO_14; + AO = &AO_14; } - AO.setDACOutVoltage(volt, ch); + AO->setDACOutVoltage(volt, ch); } \ No newline at end of file diff --git a/FC_InferfaceBoard/DFRobot_GP8403.h b/FC_InferfaceBoard/DFRobot_GP8403.h index 2fedc87..e9483aa 100644 --- a/FC_InferfaceBoard/DFRobot_GP8403.h +++ b/FC_InferfaceBoard/DFRobot_GP8403.h @@ -31,7 +31,7 @@ #define DBG(...) #endif #define GP8302_CONFIG_CURRENT_REG 0x02 -#define OUTPUT_RANGE 0X00 +#define OUTPUT_RANGE 0X01 class DFRobot_GP8403 { public: diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.h b/FC_InferfaceBoard/FC_InferfaceBoard.h new file mode 100644 index 0000000..4c825a5 --- /dev/null +++ b/FC_InferfaceBoard/FC_InferfaceBoard.h @@ -0,0 +1,57 @@ +//Processes +String Prcss_ALL_Read(); +String Prcss_DO_Write(unsigned int data[], int dataSize); +String Prcss_DI_Read(); +String Prcss_AI_Read(); +String Prcss_AO_Write(unsigned int data[], int dataSize); +String Prcss_PV_Read(); +String Prcss_SV_Read(); +String Prcss_SV_Write(unsigned int data[], int dataSize); +String Prcss_AT_Write(bool onOff); +String Prcss_RngAO(unsigned int data[], int dataSize); +String Prcss_ChMC9(unsigned int data[], int dataSize); + +//Ethernet +void Ethernet_setup(); +void webReponse(); +String demuxCMD(String command, String* rightPart); +int demuxNum(String rightPart, unsigned int data[]); + +//RS485 +void RS485_setup(); +void send_485(); +int recieve_485(); +void recieve_485_0(); + +//GPIO +void GPIO_setup(); +void read_analog(); +void read_digital(); + +//MC9 +void setupMC9_1(int i, int data); +String msg_MC9_PV(int addr); +String msg_MC9_SV(int addr); +int saveMC9(String message); +int timeoutMC9(); +int parseMC9(const String& message, int& addr, String& mode, int data[8], int& crc); +String sumMC9(String input); + +//Utils +int write_buff_c(char* buff, char c); +int write_buff(char* buff, String str); +int write_buff_first(char* buff, String str); +String read_buff(char* buff); + +// Periodics +void Periodic_run(); +void timer_10ms(); + +// Analog_Out +void AO_setup(int i, int Rng); +void AO_Write(int i, int volt); + + + +void timer_10ms(); +int write_buff_c(char* buff, char c); \ No newline at end of file diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.ino b/FC_InferfaceBoard/FC_InferfaceBoard.ino index 12a8e54..eda1643 100644 --- a/FC_InferfaceBoard/FC_InferfaceBoard.ino +++ b/FC_InferfaceBoard/FC_InferfaceBoard.ino @@ -5,6 +5,8 @@ #include "Wire.h" // I2C #include "DFRobot_GP8403.h" +#include "FC_InferfaceBoard.h" + #define CR "\r" #define FIN "\n" #define CRLF "\r\n" @@ -80,11 +82,27 @@ int Values_DI; int Size_AI = 16; int Values_AI[16]; -bool rcv_10_PV; +int RS485_1_Addr[2] = {0,0}; +bool RS485_1_Rcv_PV[2] = {false, false}; +bool RS485_1_Rcv_SV[2] = {false, false}; +int RS485_1_Rcv_size = 8; +int RS485_1_Values_PV[8*2]; +int RS485_1_Values_SV[8*2]; + +/* +int RS485_2_Addr[2]; +bool RS485_2_Rcv_PV[2]; +bool RS485_2_Rcv_SV[2]; +int RS485_2_Rcv_size = 8*2; +int RS485_2_Values_PV[8*2]; +int RS485_2_Values_SV[8*2]; +*/ + +bool rcv_10_PV = true; int Size_PV = 8; int Values_10_PV[8]; -bool rcv_10_SV; +bool rcv_10_SV = true; int Size_SV = 8; int Values_10_SV[8]; @@ -186,13 +204,16 @@ void loop() { }else if(cmd=="PV?"){ client.print(Prcss_PV_Read()); - }else if(cmd=="ATon!"){ + } + /* + else if(cmd=="ATon!"){ client.print(Prcss_AT_Write(true)); }else if(cmd=="AToff!"){ client.print(Prcss_AT_Write(false)); } + */ // Init else if(cmd=="State?"){ @@ -202,7 +223,7 @@ void loop() { }else if(cmd=="ChMC9!"){ dataSize = demuxNum(cmdData, data); - //client.print(Prcss_ChMC9(data, dataSize)); + client.print(Prcss_ChMC9(data, dataSize)); } diff --git a/FC_InferfaceBoard/GP8403.ino b/FC_InferfaceBoard/GP8403.ino index 171499f..1b5f616 100644 --- a/FC_InferfaceBoard/GP8403.ino +++ b/FC_InferfaceBoard/GP8403.ino @@ -1,6 +1,4 @@ - - DFRobot_GP8403::DFRobot_GP8403(TwoWire *pWire,uint8_t addr) { _pWire = pWire; diff --git a/FC_InferfaceBoard/MC9.ino b/FC_InferfaceBoard/MC9.ino index fceb7d4..5a1093d 100644 --- a/FC_InferfaceBoard/MC9.ino +++ b/FC_InferfaceBoard/MC9.ino @@ -1,28 +1,61 @@ const int MC9_CH[] = {1000, 1008, 1016, 1024, 1100, 1108, 1116, 1124}; -#define MC9_10_PV "10DRS,08,0001CB" -#define MC9_10_SV "10DRS,08,0011CC" - // 10DWR,02,0302,0001,0501,0001 // Auto Tune. 0302(Ch Sel) 0001(Ch.1) 0501(AT) 0001(On, Off=0000) +void setupMC9_1(int i, int data){ + RS485_1_Addr[i] = data; +} + +String msg_MC9_PV(int addr){ + String message = ""; + + message += String(addr); + message += "DRS,08,0001"; + message += sumMC9(message); + message = "" + message; + + return message; +} + +String msg_MC9_SV(int addr){ + String message = ""; + + message += String(addr); + message += "DRS,08,0011"; + message += sumMC9(message); + message = "" + message; + + return message; +} + int saveMC9(String message){ - int addr; + int addr, idx; String mode; int data[8]; int crc; if (parseMC9(message, addr, mode, data, crc)) { - if(latest_sent_msg == MC9_10_PV){ - rcv_10_PV = true; - for(int i = 0 ; i < Size_PV ; i++){ - Values_10_PV[i] = data[i]; + for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s + if(RS485_1_Addr[i] == addr){ + idx = i; + break; + }else{ + return -1; + } + } + + if(latest_sent_msg.indexOf("DRS,08,0001") != -1){ // if PV CMD + //rcv_10_PV = true; + for(int i = 0 ; i < RS485_1_Rcv_size ; i++){ + RS485_1_Values_PV[i + idx*8] = data[i]; } } - if(latest_sent_msg == MC9_10_SV){ - rcv_10_SV = true; - for(int i = 0 ; i < Size_SV ; i++){ - Values_10_SV[i] = data[i]; + //if(latest_sent_msg == msg_MC9_SV(10)){ + if(latest_sent_msg.indexOf("DRS,08,0011") != -1){ // if SV CMD + //rcv_10_SV = true; + for(int i = 0 ; i < RS485_1_Rcv_size ; i++){ + RS485_1_Values_SV[i + idx*8] = data[i]; } } return 1; @@ -37,17 +70,19 @@ int timeoutMC9(){ String mode; int data[8]; int crc; + + /* if(latest_sent_msg == MC9_10_PV){ - rcv_10_PV = false; + //rcv_10_PV = false; } if(latest_sent_msg == MC9_10_SV){ - rcv_10_SV = false; + //rcv_10_SV = false; }else { Serial.println("error 485 read"); return 0; } - + */ return 1; } diff --git a/FC_InferfaceBoard/Processes.ino b/FC_InferfaceBoard/Processes.ino index 58ea9d7..d799a03 100644 --- a/FC_InferfaceBoard/Processes.ino +++ b/FC_InferfaceBoard/Processes.ino @@ -20,9 +20,9 @@ String Prcss_ALL_Read(){ str += "PV10?:"; if(rcv_10_PV){ - for (int i = 0; i < Size_PV; i++) { + for (int i = 0; i < RS485_1_Rcv_size*2; i++) { char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 - sprintf(formattedNumber, "%04X", Values_10_PV[i]); // 4자리로 고정된 형식의 문자열 생성 + sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i]); // 4자리로 고정된 형식의 문자열 생성 str += formattedNumber; // 형식화된 문자열 추가 str += ','; } @@ -33,9 +33,9 @@ String Prcss_ALL_Read(){ str += "SV10?:"; if(rcv_10_SV){ - for (int i = 0; i < Size_SV; i++) { + for (int i = 0; i < RS485_1_Rcv_size*2; i++) { char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 - sprintf(formattedNumber, "%04X", Values_10_SV[i]); // 4자리로 고정된 형식의 문자열 생성 + sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i]); // 4자리로 고정된 형식의 문자열 생성 str += formattedNumber; // 형식화된 문자열 추가 str += ','; } @@ -92,15 +92,15 @@ String Prcss_AI_Read(){ String Prcss_AO_Write(unsigned int data[], int dataSize){ String str = "AO!:"; - int t = millis(); + //int t = millis(); int d = 2; for(int i=0 ; i data[i+1]) ? data[i] : data[i+1]; + }else{ + rng = data[i]; + } + AO_setup(i, rng); delay(d); + i++; // every 0 ch, not 1 ch. } str += RcvOK; return str + FIN; @@ -205,9 +212,9 @@ String Prcss_RngAO(unsigned int data[], int dataSize){ String Prcss_ChMC9(unsigned int data[], int dataSize){ String str = "ChMC9!:"; - int d = 2; + int d = 20; for(int i=0 ; i 25){ // Timeout = periodic(20ms) x 25 = 500 msec Serial.println("485 not responced... (Timeout)"); - if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){ + //if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){ + + /* + if((latest_sent_msg == msg_MC9_PV(10)) || (latest_sent_msg == msg_MC9_SV(10))){ timeoutMC9(); } - + */ latest_sent_msg = ""; Wait_485 = false; Wait_485_cnt = 0; numOf485--; - return 0; + return -1; } } // Receive pv data @@ -66,20 +69,22 @@ int recieve_485(){ String message = read_buff(Buff_485_Rd); if(message != ""){ numOf485--; - if((latest_sent_msg == MC9_10_PV) ||(latest_sent_msg == MC9_10_SV)){ + //if((latest_sent_msg == msg_MC9_PV(10)) || (latest_sent_msg == msg_MC9_SV(10))){ + if(latest_sent_msg.indexOf("DRS") != -1){ saveMC9(message); } else{ - Serial.print("485 rcv : "); + Serial.print("----- 485 rcv : "); Serial.println(message); } - Wait_485 = false; Wait_485_cnt = 0; } - return 1; + + return 0; } + /* For Test from PC */ void recieve_485_0(){ diff --git a/FC_InferfaceBoard/periodics.ino b/FC_InferfaceBoard/periodics.ino index 5613d25..2672126 100644 --- a/FC_InferfaceBoard/periodics.ino +++ b/FC_InferfaceBoard/periodics.ino @@ -26,19 +26,36 @@ void Periodic_run(){ T_500ms = false; } if(T_1000ms){ - write_buff(Buff_485_Wr, MC9_10_PV); // 10 DRS num 8 from ch1 PV + //write_buff(Buff_485_Wr, MC9_10_PV); // 10 DRS num 8 from ch1 PV + for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s + if(RS485_1_Addr[i] > 0){ // If there is address + write_buff(Buff_485_Wr, msg_MC9_PV(RS485_1_Addr[i])); // write buff to send PV Req. + } + } + //write_buff(Buff_485_Wr, msg_MC9_PV(10)); + T_1000ms = false; } if(T_2000ms){ - Serial.print("----- remain 485 buff : "); // For - Serial.println(numOf485); // Debugging + if(numOf485 > 5){ + Serial.print("----- remain 485 buff : "); // For + Serial.println(numOf485); // Debugging + } T_2000ms = false; } if(T_5000ms){ - write_buff(Buff_485_Wr, MC9_10_SV); // 10 DRS num 8 from ch1 SV + //write_buff(Buff_485_Wr, MC9_10_SV); // 10 DRS num 8 from ch1 SV + for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s + if(RS485_1_Addr[i] > 0){ // If there is address + write_buff(Buff_485_Wr, msg_MC9_SV(RS485_1_Addr[i])); // write buff to send SV Req. + } + } + //write_buff(Buff_485_Wr, msg_MC9_SV(10)); + T_5000ms = false; } } + void timer_10ms(){ msCnt += 10; diff --git a/Labview/IF_CMD.vi b/Labview/IF_CMD.vi index 2574a01..11b1a76 100644 Binary files a/Labview/IF_CMD.vi and b/Labview/IF_CMD.vi differ diff --git a/Labview/IF_Loop.vi b/Labview/IF_Loop.vi index 8b4f3b6..8c18b9a 100644 Binary files a/Labview/IF_Loop.vi and b/Labview/IF_Loop.vi differ