[Periodics][Init]

- Periodics
  ㄴ Analog read:  0 to 9 pin, 100ms x 50 samples = 500ms time window
- Analog read value
  ㄴ 평균을 위한 복수의 Analog read data를 링 형태로 저장 (기존에는 배열 재정렬, 연산속도 향상 필요)
  ㄴ 평균에 의한 정밀도 향상으로 floating 연산 후 factor 곱하여 송신
- MC9
  ㄴ not responsed 디버그 메시지 주석 (추후 디버그 모드 개발 예정)
- Init
  ㄴ AO, MC9 중복 초기화 방지를 위한 리턴 값 추가
main
Changwoo Park 1 year ago
parent da4fc697af
commit 5834e23263

@ -3,9 +3,19 @@
#include <Ethernet2.h> // Ethernet2 v1.0.4 #include <Ethernet2.h> // Ethernet2 v1.0.4
#include "GP8403.h" #include "GP8403.h"
#define AI_AVG_SIZE 50
#define AI_RANGE 10
/* ---------- Init ---------- */
bool Init_AO = false;
bool Init_MC9 = false;
/* ---------- Periodics ---------- */ /* ---------- Periodics ---------- */
// Flags // Flags
bool T_1ms = false; bool T_1ms = false;
bool T_2ms = false;
bool T_4ms = false;
bool T_5ms = false;
bool T_10ms = false; bool T_10ms = false;
bool T_20ms = false; bool T_20ms = false;
bool T_50ms = false; bool T_50ms = false;
@ -19,7 +29,6 @@ bool T_5000ms = false;
// Datas // Datas
int msCnt = 0; int msCnt = 0;
unsigned long timer = 0; unsigned long timer = 0;
#define AI_AVG_SIZE 20
/* ---------- Arduino Mega 2560 Basic IO ---------- */ /* ---------- Arduino Mega 2560 Basic IO ---------- */
// Pins // Pins
@ -32,11 +41,12 @@ const int AiPin[] = { A0, A1, A2, A3, A4, A5, A6, A7,
// Data Storages // Data Storages
int Size_DO = 16; int Size_DO = 16;
int Size_DI = 8; 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_DI; // digit values to a number
int Values_AI[16]; int Values_AI[AI_RANGE];
int Recent_AI[16][AI_AVG_SIZE]; int Recent_AI[AI_RANGE][AI_AVG_SIZE];
/* ---------- I2C (Analog Output, DAC) ---------- */ /* ---------- I2C (Analog Output, DAC) ---------- */
// Addresses // Addresses

@ -1,8 +1,6 @@
#include "FC_InterfaceBoard.h" #include "FC_InterfaceBoard.h"
#define CR "\r" #define CR "\r"
#define FIN "\n" #define FIN "\n"
#define CRLF "\r\n" #define CRLF "\r\n"
@ -13,7 +11,7 @@
String IdeSerial; // for 485_0 String IdeSerial; // for 485_0
void setup() { void setup() {
// put your setup code here, to run once: // Serial setup (Debug)
Serial.begin(9600); Serial.begin(9600);
Serial.println("Start!"); Serial.println("Start!");
@ -98,13 +96,16 @@ void loop() {
} }
// Init // Init
else if(cmd=="State?"){ else if(cmd=="State?"){
client.print(cmd + " " + RcvOK + FIN);
}else if(cmd=="RngAO!"){ }else if(cmd=="RngAO!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_RngAO(data, dataSize)); client.print(Prcss_RngAO(data, dataSize));
Init_AO = true;
}else if(cmd=="ChMC9_1!"){ }else if(cmd=="ChMC9_1!"){
dataSize = demuxNum(cmdData, data); dataSize = demuxNum(cmdData, data);
client.print(Prcss_ChMC9(data, dataSize)); client.print(Prcss_ChMC9(data, dataSize));
Init_MC9 = true;
} }
// Extra // Extra

@ -15,11 +15,17 @@ void read_analog(){
for(i = 0 ; i < Size_AI ; i++){ for(i = 0 ; i < Size_AI ; i++){
Values_AI[i] = analogRead(AiPin[i]); Values_AI[i] = analogRead(AiPin[i]);
/*
for(int j = 0; j < AI_AVG_SIZE - 1; j++) { for(int j = 0; j < AI_AVG_SIZE - 1; j++) {
Recent_AI[i][j] = Recent_AI[i][j + 1]; Recent_AI[i][j] = Recent_AI[i][j + 1];
} }
Recent_AI[i][AI_AVG_SIZE - 1] = Values_AI[i]; 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(){ void read_digital(){

@ -225,7 +225,7 @@ int timeoutMC9(){
msg = latest_sent_msg.substring(1); msg = latest_sent_msg.substring(1);
sscanf(msg.c_str(), "%2d%3s", &addr, &dummy); 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) { if (latest_sent_msg.indexOf("DRS") != -1) {
for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s for(int i=0 ; i < 2 ; i++){ // RS485_1 has two rooms for two MC9s
@ -235,7 +235,7 @@ int timeoutMC9(){
break; break;
} }
} }
/* 230721
// If not matched, return error // If not matched, return error
if(idx < 0){ if(idx < 0){
Serial.println(); Serial.println();
@ -253,6 +253,7 @@ int timeoutMC9(){
Serial.print(" >> SV @"); Serial.print(" >> SV @");
Serial.println(idx); Serial.println(idx);
} }
*/
return 0; return 0;
} else { } else {

@ -1,22 +1,31 @@
void Periodic_run(){ void Periodic_run(){
if(T_1ms){ if(T_1ms){
// Read Arduino AI // read_analog();
read_analog();
T_1ms = false; 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(); //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; T_10ms = false;
} }
if(T_20ms){ if(T_20ms){
// send and recieve RS485
send_485(); send_485();
recieve_485(); recieve_485();
T_20ms = false; T_20ms = false;
} }
if(T_50ms){ if(T_50ms){
// Read Arduino DI
read_digital(); read_digital();
T_50ms = false; T_50ms = false;
} }
@ -29,8 +38,8 @@ void Periodic_run(){
T_200ms = false; T_200ms = false;
} }
if(T_500ms){ if(T_500ms){
// RS485 recieve (for developing)
recieve_485_0(); recieve_485_0(); // RS485 recieve (for developing)
T_500ms = false; T_500ms = false;
} }
if(T_1000ms){ if(T_1000ms){
@ -43,7 +52,7 @@ void Periodic_run(){
T_1000ms = false; T_1000ms = false;
} }
if(T_2000ms){ if(T_2000ms){
if(numOf485 > 0){ if(numOf485 > 10){
Serial.print("----- remain 485 buff : "); // For Serial.print("----- remain 485 buff : "); // For
Serial.println(numOf485); // Debugging Serial.println(numOf485); // Debugging
} }
@ -64,6 +73,15 @@ void timer_1ms(){
msCnt += 1; msCnt += 1;
T_1ms = true; 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){ if (msCnt % 10 == 0){
T_10ms = true; T_10ms = true;
} }

@ -1,23 +1,27 @@
String Prcss_ALL_Read(){ String Prcss_ALL_Read(){
String str = ""; String str = "";
char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자
float factor = 1.0f / AI_AVG_SIZE;
str += "AI?:"; str += "AI?:";
for (int i = 0; i < Size_AI; i++) { 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++) { 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", Values_AI[i]); // 4자리로 고정된 형식의 문자열 생성
sprintf(formattedNumber, "%04X", sum_AI); // 4자리로 고정된 형식의 문자열 생성 sprintf(formattedNumber, "%04X", integer_part); // 4자리로 고정된 형식의 문자열 생성
str += formattedNumber; // 형식화된 문자열 추가 str += formattedNumber; // 형식화된 문자열 추가
str += ','; str += ',';
} }
str += RcvOK; str += RcvOK;
str += "DI?:"; str += "DI?:";
char formattedNumber[5];
sprintf(formattedNumber, "%04X", Values_DI); sprintf(formattedNumber, "%04X", Values_DI);
str += formattedNumber; str += formattedNumber;
str += ','; str += ',';
@ -25,7 +29,6 @@ String Prcss_ALL_Read(){
str += "PV_1?:"; str += "PV_1?:";
for (int i = 0; i < RS485_1_Rcv_size*2; i++) { 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]); int rcv = int(RS485_1_Rcv_PV[i/RS485_1_Rcv_size]);
sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i] * rcv); sprintf(formattedNumber, "%04X", RS485_1_Values_PV[i] * rcv);
@ -37,7 +40,6 @@ String Prcss_ALL_Read(){
str += "SV_1?:"; str += "SV_1?:";
for (int i = 0; i < RS485_1_Rcv_size*2; i++) { 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]); int rcv = int(RS485_1_Rcv_SV[i/RS485_1_Rcv_size]);
sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i] * rcv); sprintf(formattedNumber, "%04X", RS485_1_Values_SV[i] * rcv);
@ -46,6 +48,11 @@ String Prcss_ALL_Read(){
} }
str += RcvOK; str += RcvOK;
str += "INIT?:";
sprintf(formattedNumber, "%04X", ((unsigned int)(Init_AO & Init_MC9)));
str += formattedNumber;
str += ',';
str += RcvOK;
return str + FIN; return str + FIN;
} }

@ -43,7 +43,7 @@ int recieve_485(){
// Timeout code // Timeout code
if(Wait_485_1){ if(Wait_485_1){
Wait_485_1_cnt++; 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)"); //Serial.println("485 not responced... (Timeout)");
timeoutMC9(); timeoutMC9();
//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)){

Loading…
Cancel
Save