fix some "magick numbers"

This commit is contained in:
eddyem 2019-09-30 17:10:40 +03:00
parent b4a8ed73c8
commit f34c50bbaf
5 changed files with 41 additions and 25 deletions

View File

@ -25,7 +25,6 @@
#include <linux/limits.h> // PATH_MAX #include <linux/limits.h> // PATH_MAX
#include "usefull_macros.h" // putlog #include "usefull_macros.h" // putlog
#include "cmdlnopts.h" // glob_pars #include "cmdlnopts.h" // glob_pars
#include "sens_place.h"
extern glob_pars *G; extern glob_pars *G;
@ -46,7 +45,7 @@ char *mkname(char *path, char *fname){
* @param Z (i) - 0 for upper and 1 for lower sensors (Z-coord) * @param Z (i) - 0 for upper and 1 for lower sensors (Z-coord)
* @return 1 if all OK * @return 1 if all OK
*/ */
static int formfile(char *fname, double data[2][8][8], int Z){ static int formfile(char *fname, double data[2][NCHANNEL_MAX+1][NCTRLR_MAX+1], int Z){
FILE *F = fopen(fname, "w"); FILE *F = fopen(fname, "w");
if(!F) return 0; if(!F) return 0;
for(int i = 1; i <= NCTRLR_MAX; ++i){ for(int i = 1; i <= NCTRLR_MAX; ++i){
@ -90,9 +89,9 @@ static void gnuplot(char *path, char *fname){
} }
} }
void plot(double data[2][8][8], char *savepath){ void plot(double data[2][NCHANNEL_MAX+1][NCTRLR_MAX+1], char *savepath){
/* /*
double dY[8][8]; // vertical gradients (top - bottom) double dY[NCH][NC]; // vertical gradients (top - bottom)
// calculate gradients // calculate gradients
for(int i = 1; i < 8; ++i){ for(int i = 1; i < 8; ++i){
for(int N = 0; N < 8; ++ N){ for(int N = 0; N < 8; ++ N){

View File

@ -24,6 +24,8 @@
#ifndef __GNUPLOT_H__ #ifndef __GNUPLOT_H__
#define __GNUPLOT_H__ #define __GNUPLOT_H__
void plot(double data[2][8][8], char *savepath); #include "sens_place.h"
void plot(double data[2][NCHANNEL_MAX+1][NCTRLR_MAX+1], char *savepath);
#endif // __GNUPLOT_H__ #endif // __GNUPLOT_H__

View File

@ -284,15 +284,15 @@ static void process_T(){
time_t tmeasmax = 0; time_t tmeasmax = 0;
double arr[128]; double arr[128];
// mean temperatures over 15 scans // mean temperatures over 15 scans
static double Tmean[2][8][8]; static double Tmean[2][NCHANNEL_MAX+1][NCTRLR_MAX+1];
static int Nmean; // amount of measurements for Tmean static int Nmean; // amount of measurements for Tmean
// get statistics // get statistics
poll_sensors(0); // poll N2 poll_sensors(0); // poll N2
// scan over controllers on mirror & calculate median // scan over controllers on mirror & calculate median
for(i = 1; i < 8; ++i){ for(i = 1; i <= NCTRLR_MAX; ++i){
if(poll_sensors(i)){ if(poll_sensors(i)){
int N, p; int N, p;
for(p = 0; p < 2; ++p) for(N = 0; N < 8; ++ N){ for(p = 0; p < 2; ++p) for(N = 0; N <= NCHANNEL_MAX; ++ N){
double T = t_last[p][N][i]; double T = t_last[p][N][i];
time_t t = tmeasured[p][N][i]; time_t t = tmeasured[p][N][i];
if(T > -100. && T < 100.){ if(T > -100. && T < 100.){
@ -309,8 +309,8 @@ static void process_T(){
// throw out all more than +-3degrC and calculate meanT // throw out all more than +-3degrC and calculate meanT
Num = 0; Num = 0;
double Tsum = 0.; double Tsum = 0.;
for(i = 1; i < 8; ++i){ for(i = 1; i <= NCTRLR_MAX; ++i){
for(p = 0; p < 2; ++p) for(N = 0; N < 8; ++ N){ for(p = 0; p < 2; ++p) for(N = 0; N <= NCHANNEL_MAX; ++N){
double T = t_last[p][N][i]; double T = t_last[p][N][i];
if(T > Ttop || T < Tbot || tmeasmax - tmeasured[p][N][i] > 1800){ if(T > Ttop || T < Tbot || tmeasmax - tmeasured[p][N][i] > 1800){
t_last[p][N][i] = -300.; t_last[p][N][i] = -300.;
@ -324,11 +324,11 @@ static void process_T(){
// make graphics // make graphics
if(G->savepath){ if(G->savepath){
if(++Nmean == GRAPHS_AMOUNT){ if(++Nmean == GRAPHS_AMOUNT){
for(i = 1; i < 8; ++i)for(p = 0; p < 2; ++p)for(N = 0; N < 8; ++ N){ for(i = 1; i <= NCTRLR_MAX; ++i)for(p = 0; p < 2; ++p)for(N = 0; N <= NCHANNEL_MAX; ++ N){
Tmean[p][N][i] /= Nmean; Tmean[p][N][i] /= Nmean;
} }
plot(Tmean, G->savepath); plot(Tmean, G->savepath);
memset(Tmean, 0, sizeof(double)*2*8*8); memset(Tmean, 0, sizeof(double)*2*(NCTRLR_MAX+1)*(NCHANNEL_MAX+1));
Nmean = 0; Nmean = 0;
} }
} }

View File

@ -27,9 +27,9 @@
#define BUFLEN 1024 #define BUFLEN 1024
// UNIX time of temperatures measurement: [Ngroup][Nsensor][Ncontroller] // UNIX time of temperatures measurement: [Ngroup][Nsensor][Ncontroller]
time_t tmeasured[2][8][8]; time_t tmeasured[2][NCHANNEL_MAX+1][NCTRLR_MAX+1];
// last temperatures read: [Ngroup][Nsensor][Ncontroller] // last temperatures read: [Ngroup][Nsensor][Ncontroller]
double t_last[2][8][8]; double t_last[2][NCHANNEL_MAX+1][NCTRLR_MAX+1];
/** /**
* read strings from terminal (ending with '\n') with timeout * read strings from terminal (ending with '\n') with timeout
@ -131,10 +131,11 @@ static int parse_answer(char *buf, int N){
//DBG("buf: %s", buf); //DBG("buf: %s", buf);
int v = getint(buf); int v = getint(buf);
//DBG("sensor #%d", v); //DBG("sensor #%d", v);
if(v < 0 || v > 81) return 0; //if(v < 0 || v > 81) return 0;
i = v/10; v -= i*10; i = v/10; v -= i*10;
if(i < 0 || i > NCTRLR_MAX) return 0;
//DBG("i=%d, v=%d", i,v); //DBG("i=%d, v=%d", i,v);
if((v & 1) != v) return 0; if((v & 1) != v) return 0; // should be only 0 or 1
if(*buf != '=' ) return 0; if(*buf != '=' ) return 0;
++buf; ++buf;
int T = getint(buf); int T = getint(buf);
@ -154,14 +155,26 @@ static int parse_answer(char *buf, int N){
* @return 0 if all OK * @return 0 if all OK
*/ */
static int send_cmd(int N, char cmd){ static int send_cmd(int N, char cmd){
if(N < 0 || N > 7) return 1; if(N < 0 || N > NCTRLR_MAX) return 1;
char buf[4] = {(char)N + '0', cmd, '\n', 0}; char buf[3] = {0};
int n = 3;
char *rtn; char *rtn;
if(N){ // CAN-bus
buf[0] = (char)N + '0';
buf[1] = cmd;
buf[2] = '\n';
}else{ // local command
n = 2;
buf[0] = cmd;
buf[1] = '\n';
}
// clear all incomint data // clear all incomint data
while(read_string()); while(read_string());
//DBG("send cmd %s", buf); DBG("send cmd %s", buf);
if(write_tty(buf, 3)) return 1; if(write_tty(buf, n)) return 1;
if(N == 0) return 0;
if((rtn = read_string())){ if((rtn = read_string())){
DBG("read_string: %s", rtn);
if(*rtn == cmd) return 0; if(*rtn == cmd) return 0;
} }
return 1; return 1;
@ -186,7 +199,7 @@ int poll_sensors(int N){
while(dtime() - t0 < T_POLLING_TMOUT && ngot < 16){ // timeout reached or got data from all while(dtime() - t0 < T_POLLING_TMOUT && ngot < 16){ // timeout reached or got data from all
if((ans = read_string())){ // parse new data if((ans = read_string())){ // parse new data
//DBG("got %s", ans); //DBG("got %s", ans);
if(*ans == 'T'){ // data from sensor if(*ans == CMD_MEASURE_T){ // data from sensor
//DBG("ptr: %s", ans); //DBG("ptr: %s", ans);
ngot += parse_answer(ans, N); ngot += parse_answer(ans, N);
} }
@ -205,8 +218,8 @@ int check_sensors(){
green(_("Check if there's a sensors...\n")); green(_("Check if there's a sensors...\n"));
int i, v, N, found = 0; int i, v, N, found = 0;
char *ans; char *ans;
for(N=0;N<8;++N)for(i=0;i<8;++i)for(v=0;v<2;++v) t_last[v][i][N] = -300.; // clear data for(N=0;N<=NCTRLR_MAX;++N)for(i=0;i<=NCHANNEL_MAX;++i)for(v=0;v<2;++v) t_last[v][i][N] = -300.; // clear data
for(i = 1; i < 8; ++i){ for(i = 1; i <= NCTRLR_MAX; ++i){
//red("i = %d\n", i); //red("i = %d\n", i);
double t0 = dtime(); double t0 = dtime();
while(dtime() - t0 < POLLING_TMOUT){ while(dtime() - t0 < POLLING_TMOUT){

View File

@ -22,6 +22,8 @@
#ifndef __TERM_H__ #ifndef __TERM_H__
#define __TERM_H__ #define __TERM_H__
#include "sens_place.h" // NCTRLR
#define FRAME_MAX_LENGTH (300) #define FRAME_MAX_LENGTH (300)
#define MAX_MEMORY_DUMP_SIZE (0x800 * 4) #define MAX_MEMORY_DUMP_SIZE (0x800 * 4)
// Terminal timeout (seconds) // Terminal timeout (seconds)
@ -43,8 +45,8 @@
#define CMD_MEASURE_LOCAL 't' #define CMD_MEASURE_LOCAL 't'
#define ANS_PONG "PONG" #define ANS_PONG "PONG"
extern time_t tmeasured[2][8][8]; extern time_t tmeasured[2][NCHANNEL_MAX+1][NCTRLR_MAX+1];
extern double t_last[2][8][8]; extern double t_last[2][NCHANNEL_MAX+1][NCTRLR_MAX+1];
// communication errors // communication errors
typedef enum{ typedef enum{