[AO][MC9]

- AO
  ㄴ LV에서 Range 제어 (활성화)
  ㄴ 오류 수정 : Range 레지스터 주소가 0x00으로 잘못 되어 있는 것 수정 ->0x01

- MC9
  ㄴ LV에서 Addr 제어 (활성화)
  ㄴ RS485_1에서 2개 사용. RS485_2 에서 추후 2개 사용하도록 확장 필요
  ㄴ 단, SV Write 부분은 추가 업데이트 필요

- 기타 제어 확인
- 디버깅을 위해 FC_InferfaceBoard.h 추가 (함수 전처리 안되어있으면 해당 부분으로 오류 검출됨)
  ㄴ 파일명 오타 확인. 추후 수정예정.
main
Changwoo Park 1 year ago
parent 1ef1e47cc1
commit 24723b0e74

@ -1,65 +1,52 @@
void AO_setup(int i, int Rng) { void AO_setup(int i, int Rng) {
DFRobot_GP8403 AO; DFRobot_GP8403* AO;
bool needBegin = true; bool needBegin = true;
bool needRng = true; bool needRng = true;
int twinRng = 0;
if(i%2){ // Odd needBegin = (RngAO[i] == 0);
twinRng = RngAO[i-1];
}else{ // Even
twinRng = RngAO[i+1];
}
needBegin = (twinRng == 0);
if(Rng > twinRng){
needRng = true;
}else{
needRng = false;
}
if(i <= 1){ if(i <= 1){
AO = AO_0; AO = &AO_0;
}else if(i <= 3){ }else if(i <= 3){
AO = AO_2; AO = &AO_2;
}else if(i <= 5){ }else if(i <= 5){
AO = AO_4; AO = &AO_4;
}else if(i <= 7){ }else if(i <= 7){
AO = AO_6; AO = &AO_6;
}else if(i <= 9){ }else if(i <= 9){
AO = AO_8; AO = &AO_8;
}else if(i <= 11){ }else if(i <= 11){
AO = AO_10; AO = &AO_10;
}else if(i <= 13){ }else if(i <= 13){
AO = AO_12; AO = &AO_12;
}else if(i <= 15){ }else if(i <= 15){
AO = AO_14; AO = &AO_14;
} }
if(needBegin){ // If not begin yet if(needBegin){ // If not begin yet
Serial.println("AO init begin"); Serial.println("AO_"+ String(i) + ": initialization Begin");
while(AO.begin()!=0); while(AO->begin()!=0);
Serial.println("AO init succeed"); AO->setDACOutVoltage(0, 2);
AO.setDACOutVoltage(0, 0); delay(5);
AO.setDACOutVoltage(0, 1); AO->store();
delay(20);
AO.store();
} }
if(needRng){
if(Rng == 10){ if(Rng == 10){
AO.setDACOutRange(AO.eOutputRange10V); AO->setDACOutRange(AO->eOutputRange10V);
}else{ Serial.println("AO_"+ String(i) + ": Initialized, Set Range 10V");
AO.setDACOutRange(AO.eOutputRange5V); }else{
} AO->setDACOutRange(AO->eOutputRange5V);
Serial.println("AO_"+ String(i) + ": Initialized, Set Range 5V");
} }
RngAO[i] = Rng; RngAO[i] = Rng;
Serial.println("AO FIN"); RngAO[i+1] = Rng;
} }
void AO_Write(int i, int volt) { void AO_Write(int i, int volt) {
DFRobot_GP8403 AO; DFRobot_GP8403* AO;
int ch; int ch;
if(i%2){ // Odd if(i%2){ // Odd
@ -69,22 +56,22 @@ void AO_Write(int i, int volt) {
} }
if(i <= 1){ if(i <= 1){
AO = AO_0; AO = &AO_0;
}else if(i <= 3){ }else if(i <= 3){
AO = AO_2; AO = &AO_2;
}else if(i <= 5){ }else if(i <= 5){
AO = AO_4; AO = &AO_4;
}else if(i <= 7){ }else if(i <= 7){
AO = AO_6; AO = &AO_6;
}else if(i <= 9){ }else if(i <= 9){
AO = AO_8; AO = &AO_8;
}else if(i <= 11){ }else if(i <= 11){
AO = AO_10; AO = &AO_10;
}else if(i <= 13){ }else if(i <= 13){
AO = AO_12; AO = &AO_12;
}else if(i <= 15){ }else if(i <= 15){
AO = AO_14; AO = &AO_14;
} }
AO.setDACOutVoltage(volt, ch); AO->setDACOutVoltage(volt, ch);
} }

@ -31,7 +31,7 @@
#define DBG(...) #define DBG(...)
#endif #endif
#define GP8302_CONFIG_CURRENT_REG 0x02 #define GP8302_CONFIG_CURRENT_REG 0x02
#define OUTPUT_RANGE 0X00 #define OUTPUT_RANGE 0X01
class DFRobot_GP8403 class DFRobot_GP8403
{ {
public: public:

@ -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);

@ -5,6 +5,8 @@
#include "Wire.h" // I2C #include "Wire.h" // I2C
#include "DFRobot_GP8403.h" #include "DFRobot_GP8403.h"
#include "FC_InferfaceBoard.h"
#define CR "\r" #define CR "\r"
#define FIN "\n" #define FIN "\n"
#define CRLF "\r\n" #define CRLF "\r\n"
@ -80,11 +82,27 @@ int Values_DI;
int Size_AI = 16; int Size_AI = 16;
int Values_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 Size_PV = 8;
int Values_10_PV[8]; int Values_10_PV[8];
bool rcv_10_SV; bool rcv_10_SV = true;
int Size_SV = 8; int Size_SV = 8;
int Values_10_SV[8]; int Values_10_SV[8];
@ -186,13 +204,16 @@ void loop() {
}else if(cmd=="PV?"){ }else if(cmd=="PV?"){
client.print(Prcss_PV_Read()); client.print(Prcss_PV_Read());
}else if(cmd=="ATon!"){ }
/*
else if(cmd=="ATon!"){
client.print(Prcss_AT_Write(true)); client.print(Prcss_AT_Write(true));
}else if(cmd=="AToff!"){ }else if(cmd=="AToff!"){
client.print(Prcss_AT_Write(false)); client.print(Prcss_AT_Write(false));
} }
*/
// Init // Init
else if(cmd=="State?"){ else if(cmd=="State?"){
@ -202,7 +223,7 @@ void loop() {
}else if(cmd=="ChMC9!"){ }else if(cmd=="ChMC9!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
//client.print(Prcss_ChMC9(data, dataSize)); client.print(Prcss_ChMC9(data, dataSize));
} }

@ -1,6 +1,4 @@
DFRobot_GP8403::DFRobot_GP8403(TwoWire *pWire,uint8_t addr) DFRobot_GP8403::DFRobot_GP8403(TwoWire *pWire,uint8_t addr)
{ {
_pWire = pWire; _pWire = pWire;

@ -1,28 +1,61 @@
const int MC9_CH[] = {1000, 1008, 1016, 1024, 1100, 1108, 1116, 1124}; 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 // 10DWR,02,0302,0001,0501,0001
// Auto Tune. 0302(Ch Sel) 0001(Ch.1) 0501(AT) 0001(On, Off=0000) // 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 saveMC9(String message){
int addr; int addr, idx;
String mode; String mode;
int data[8]; int data[8];
int crc; int crc;
if (parseMC9(message, addr, mode, data, crc)) { if (parseMC9(message, addr, mode, data, crc)) {
if(latest_sent_msg == MC9_10_PV){ for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s
rcv_10_PV = true; if(RS485_1_Addr[i] == addr){
for(int i = 0 ; i < Size_PV ; i++){ idx = i;
Values_10_PV[i] = data[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){ //if(latest_sent_msg == msg_MC9_SV(10)){
rcv_10_SV = true; if(latest_sent_msg.indexOf("DRS,08,0011") != -1){ // if SV CMD
for(int i = 0 ; i < Size_SV ; i++){ //rcv_10_SV = true;
Values_10_SV[i] = data[i]; for(int i = 0 ; i < RS485_1_Rcv_size ; i++){
RS485_1_Values_SV[i + idx*8] = data[i];
} }
} }
return 1; return 1;
@ -37,17 +70,19 @@ int timeoutMC9(){
String mode; String mode;
int data[8]; int data[8];
int crc; int crc;
/*
if(latest_sent_msg == MC9_10_PV){ if(latest_sent_msg == MC9_10_PV){
rcv_10_PV = false; //rcv_10_PV = false;
} }
if(latest_sent_msg == MC9_10_SV){ if(latest_sent_msg == MC9_10_SV){
rcv_10_SV = false; //rcv_10_SV = false;
}else { }else {
Serial.println("error 485 read"); Serial.println("error 485 read");
return 0; return 0;
} }
*/
return 1; return 1;
} }

@ -20,9 +20,9 @@ String Prcss_ALL_Read(){
str += "PV10?:"; str += "PV10?:";
if(rcv_10_PV){ 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자리 숫자 + 널 종료 문자 char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
sprintf(formattedNumber, "%04X", Values_10_PV[i]); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i]); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber; // 형식화된 문자열 추가
str += ','; str += ',';
} }
@ -33,9 +33,9 @@ String Prcss_ALL_Read(){
str += "SV10?:"; str += "SV10?:";
if(rcv_10_SV){ 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자리 숫자 + 널 종료 문자 char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
sprintf(formattedNumber, "%04X", Values_10_SV[i]); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i]); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber; // 형식화된 문자열 추가
str += ','; str += ',';
} }
@ -92,15 +92,15 @@ String Prcss_AI_Read(){
String Prcss_AO_Write(unsigned int data[], int dataSize){ String Prcss_AO_Write(unsigned int data[], int dataSize){
String str = "AO!:"; String str = "AO!:";
int t = millis(); //int t = millis();
int d = 2; int d = 2;
for(int i=0 ; i<dataSize ; i++){ for(int i=0 ; i<dataSize ; i++){
AO_Write(i, data[i]); AO_Write(i, data[i]);
} }
t = millis()-t; //t = millis()-t;
Serial.println(t); //Serial.println(t);
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
@ -194,10 +194,17 @@ String Prcss_AT_Write(bool onOff){
String Prcss_RngAO(unsigned int data[], int dataSize){ String Prcss_RngAO(unsigned int data[], int dataSize){
String str = "RngAO!:"; String str = "RngAO!:";
int d = 2; int rng = 0;
int d = 20;
for(int i=0 ; i<dataSize ; i++){ for(int i=0 ; i<dataSize ; i++){
AO_setup(i, data[i]); if(i+1 < dataSize){
rng = (data[i] > data[i+1]) ? data[i] : data[i+1];
}else{
rng = data[i];
}
AO_setup(i, rng);
delay(d); delay(d);
i++; // every 0 ch, not 1 ch.
} }
str += RcvOK; str += RcvOK;
return str + FIN; return str + FIN;
@ -205,9 +212,9 @@ String Prcss_RngAO(unsigned int data[], int dataSize){
String Prcss_ChMC9(unsigned int data[], int dataSize){ String Prcss_ChMC9(unsigned int data[], int dataSize){
String str = "ChMC9!:"; String str = "ChMC9!:";
int d = 2; int d = 20;
for(int i=0 ; i<dataSize ; i++){ for(int i=0 ; i<dataSize ; i++){
AO_setup(i, data[i]); setupMC9_1(i, data[i]);
delay(d); delay(d);
} }
str += RcvOK; str += RcvOK;

@ -44,17 +44,20 @@ int recieve_485(){
if(Wait_485_cnt > 25){ // Timeout = periodic(20ms) x 25 = 500 msec if(Wait_485_cnt > 25){ // Timeout = periodic(20ms) x 25 = 500 msec
Serial.println("485 not responced... (Timeout)"); 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(); timeoutMC9();
} }
*/
latest_sent_msg = ""; latest_sent_msg = "";
Wait_485 = false; Wait_485 = false;
Wait_485_cnt = 0; Wait_485_cnt = 0;
numOf485--; numOf485--;
return 0; return -1;
} }
} }
// Receive pv data // Receive pv data
@ -66,20 +69,22 @@ int recieve_485(){
String message = read_buff(Buff_485_Rd); String message = read_buff(Buff_485_Rd);
if(message != ""){ if(message != ""){
numOf485--; 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); saveMC9(message);
} }
else{ else{
Serial.print("485 rcv : "); Serial.print("----- 485 rcv : ");
Serial.println(message); Serial.println(message);
} }
Wait_485 = false; Wait_485 = false;
Wait_485_cnt = 0; Wait_485_cnt = 0;
} }
return 1;
return 0;
} }
/* For Test from PC */ /* For Test from PC */
void recieve_485_0(){ void recieve_485_0(){

@ -26,19 +26,36 @@ void Periodic_run(){
T_500ms = false; T_500ms = false;
} }
if(T_1000ms){ 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; T_1000ms = false;
} }
if(T_2000ms){ if(T_2000ms){
Serial.print("----- remain 485 buff : "); // For if(numOf485 > 5){
Serial.println(numOf485); // Debugging Serial.print("----- remain 485 buff : "); // For
Serial.println(numOf485); // Debugging
}
T_2000ms = false; T_2000ms = false;
} }
if(T_5000ms){ 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; T_5000ms = false;
} }
} }
void timer_10ms(){ void timer_10ms(){
msCnt += 10; msCnt += 10;

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save