Тестовая сборка с сохранением параметров 1902121725

This commit is contained in:
Иван Мокротоваров 2019-02-12 17:25:44 +03:00
parent fa3bd10033
commit 5904a3045a
13 changed files with 467 additions and 231 deletions

View File

@ -10,7 +10,7 @@ transport=udp_broadcast
[address1] [address1]
platform=SIMULATOR platform=SIMULATOR
transport=udp_broadcast transport=udp_broadcast
address=0.36.119.82.182.139 address=0.48.72.100.118.81
[address2] [address2]
platform=TPP3W(G2) platform=TPP3W(G2)
transport=udp_broadcast transport=udp_broadcast

View File

@ -8,10 +8,11 @@ extern Configuration config;
BrakerProcess brakers[BRAKER_COUNT]; BrakerProcess brakers[BRAKER_COUNT];
void brakers_init(){ void brakers_init(){
config.STORE_BRAKER = false;
for (int num = 0;num <BRAKER_COUNT; num++){ for (int num = 0;num <BRAKER_COUNT; num++){
brakers[num].config = &config.BRAKERS[num]; brakers[num].config = &config.BRAKERS[num];
if (config.BRAKERS[num].Enable){ if (config.BRAKERS[num].Enable){
config.STORE_BRAKER = true;
#if PLATFORM_ID != SIMULATOR #if PLATFORM_ID != SIMULATOR
io.num = INPIN[config.BRAKERS[num].SensPort-1]; io.num = INPIN[config.BRAKERS[num].SensPort-1];
io.enabled=false; io.enabled=false;
@ -40,6 +41,7 @@ void braker_init(unsigned char number, unsigned char sensor, unsigned char drop,
brakers[num].OnTime = 0; brakers[num].OnTime = 0;
brakers[num].Fifo=""; brakers[num].Fifo="";
if (config.BRAKERS[num].Enable){ if (config.BRAKERS[num].Enable){
config.STORE_BRAKER = true;
#if PLATFORM_ID != SIMULATOR #if PLATFORM_ID != SIMULATOR
io.num = INPIN[config.BRAKERS[num].SensPort-1]; io.num = INPIN[config.BRAKERS[num].SensPort-1];
io.enabled=false; io.enabled=false;

View File

@ -4,16 +4,36 @@
#include "braker.th" #include "braker.th"
#include "net.th" #include "net.th"
#include "encoder.th" #include "encoder.th"
#include "common.th"
static string debug_commands[]= {
static string commands[]= { "debug_enc_level", // 0
"enc_set_windows", "debug_enc_state", // 1
"debug_enc_level", "debug_relay_reply", // 2
"debug_relay_reply", "debug_relay_show", // 3
"debug_sensors", "debug_sensors", // 4
"debug_showtime", "debug_showtime", // 5
"debug_enc_state" "" // Разделитель. не удалять
}; };
static string enc_commands[]= {
"enc_set_window", // 0
"enc_calibrate_start", // 1
"enc_calibrate_end", // 2
"" // Разделитель. не удалять
};
static string sys_commands[]= {
"systeminfo", // 0
"ip", // 1
"reset","reboot", // 2
"setbraker", // 3
"setencoder", // 4
"setconfig", // 5
"getconfig", // 6
"",
"" // Разделитель. не удалять
};
static string cmdbuffer = ""; static string cmdbuffer = "";
string cmd; string cmd;
string params; string params;
@ -32,16 +52,20 @@ void on_sock_inband() // обработка поступивших команд
cmdlen = instr(1,cmdline,chr('&'),1); cmdlen = instr(1,cmdline,chr('&'),1);
cmd = left(cmdline,cmdlen-1); cmd = left(cmdline,cmdlen-1);
params = mid(cmdline,cmdlen+1,position-cmdlen-1); params = mid(cmdline,cmdlen+1,position-cmdlen-1);
getParams(params);
if (left(cmd,3)=="enc"){
cmd_replay(cmd_encoder());
}else if(left(cmd,5)=="debug"){
cmd_replay(cmd_debug());
}else {
cmd_replay(cmd_system());
}
/*
// d("{cmd}: "+cmd); // d("{cmd}: "+cmd);
// d("{cmd}.params: "+params); // d("{cmd}.params: "+params);
if (cmd =="systeminfo"){ if (cmd == "setbraker"){
cmd_replay(cmd_systeminfo());
}else if (cmd == "reset" || cmd == "reboot"){
cmd_replay("{cmd} Rebooting...");
cmd_reset();
}else if (cmd == "ip"){
cmd_replay(cmd_ip(params));
}else if (cmd == "setbraker"){
cmd_replay(cmd_setbraker(params)); cmd_replay(cmd_setbraker(params));
}else if (cmd == "setencoder"){ }else if (cmd == "setencoder"){
cmd_replay(cmd_setencoder(params)); cmd_replay(cmd_setencoder(params));
@ -49,107 +73,183 @@ void on_sock_inband() // обработка поступивших команд
cmd_replay(cmd_enc_calibre(params)); cmd_replay(cmd_enc_calibre(params));
}else if (cmd == "enc_set_windows"){ }else if (cmd == "enc_set_windows"){
cmd_replay(cmd_enc_set_windows(params)); cmd_replay(cmd_enc_set_windows(params));
}else if (cmd == "debug_enc_level"){
cmd_replay(cmd_debug_enc_level(params));
}else if (cmd == "debug_enc_state"){
cmd_replay(cmd_debug_enc_state(params));
}else if (cmd == "debug_relay_reply"){
cmd_replay(cmd_debug_relay_reply(params));
}else if (cmd == "debug_relay_show"){
cmd_replay(cmd_debug_relay_show(params));
}else if (cmd == "debug_sensors"){
cmd_replay(cmd_debug_sensors(params));
}else if (cmd == "debug_showtime"){
cmd_replay(cmd_debug_showtime(params));
}else if (cmd == "enc_calibrate_end"){ }else if (cmd == "enc_calibrate_end"){
cmd_replay(cmd_enc_calibre("0")); cmd_replay(cmd_enc_calibre("0"));
}else { }else {
d("{cmd}: Unknown command: "+cmdline); d("{cmd}: Unknown command: "+cmdline);
cmd_replay("{cmd}: Unknown command - ["+cmdline+"]"); cmd_replay("{cmd}: Unknown command - ["+cmdline+"]");
} }*/
position = instr(1,cmdbuffer,chr(sock.endchar),1); position = instr(1,cmdbuffer,chr(sock.endchar),1);
} }
} }
string cmd_debug_sensors(string params){ string cmd_system(){
getParams(params); string result;
config.debug.SensorsLook = val(pars[0])>0; bool save = true;
return "Debug sensors SET="+(config.debug.SensorsLook? "TRUE":"FALSE"); int i = indexOfArray(sys_commands,cmd);
if (i >= 0){
switch (i){
case 0: // systeminfo
result = cmd_systeminfo();
save = false;
break;
case 1: // ip
result = cmd_ip(params);
break;
case 2: // reset
case 3: // reboot
cmd_replay("{cmd} Rebooting...");
cmd_reset();
save = false;
break;
case 4: // setbraker
result = cmd_setbraker();
break;
case 5: // setencoder
result = cmd_setencoder();
break;
case 6: // setconfig
string r;
string s;
unsigned char x;
string p = toLower1(pars[0]);
if (p=="off"||p=="none"){
s=chr(255);
stor.setdata(s,5);
config.STORE_LOCATON = NONE;
save=false;
fd.mount();
fd.delete("config");
}else if(p=="flash"){
s=chr(3);
stor.setdata(s,5);
config.STORE_LOCATON = FLASH;
config.FLASH_SIZE = 1024;
config.FLASH_FILES = 10;
}else if(p=="eprom"){
s=chr(6);
stor.setdata(s,5);
config.STORE_LOCATON = EEPROM;
}
break;
case 7: // getconfig
save = false;
char strconfig[1000];
char *ptr = get_config(strconfig);
char *out = strconfig;
int length = ptr - out;
d(str(length));
while (out < ptr){
string s;
char *ss = s;
if (out+100 <ptr){
memcpy(ss,out,100);
*(ss-2)=100;
}else{
memcpy(ss,out,ptr-out);
*(ss-2)=ptr-out;
}
d(s);
out+=100;
}
break;
default:
result = "{cmd}: Unknown command: "+cmd;
}
if (save) config_save();
}else
result = "{cmd}: Unknown command: "+cmd;
return result;
} }
string cmd_debug_relay_reply(string params){ string cmd_debug(){
getParams(params); string result;
config.debug.RelayReply = val(pars[0])>0; int i = indexOfArray(debug_commands,cmd);
return "Debug relay reply SET="+(config.debug.RelayReply? "TRUE":"FALSE"); if (i >= 0){
} switch (i){
string cmd_debug_relay_show(string params){ case 0: // debug_enc_level
getParams(params); config.debug.EncoderDebug= val(pars[0]);
config.debug.RelayShow = val(pars[0])>0; result = "Debug encoder level SET="+str(config.debug.EncoderDebug);
return "Debug relay show SET="+(config.debug.RelayShow? "TRUE":"FALSE"); break;
} case 1: // debug_enc_state
string cmd_debug_enc_level(string params){ config.debug.EncoderStateMode= val(pars[0]);
getParams(params); result = "Debug encoder work with state SET="+str(config.debug.EncoderStateMode);
config.debug.EncoderDebug= val(pars[0]); break;
return "Debug encoder level SET="+str(config.debug.EncoderDebug); case 2: // debug_relay_reply
} config.debug.RelayReply = val(pars[0])>0;
string cmd_debug_enc_state(string params){ result = "Debug relay reply SET="+(config.debug.RelayReply? "TRUE":"FALSE");
getParams(params); break;
config.debug.EncoderStateMode= val(pars[0]); case 3: // debug_relay_show
return "Debug encoder work with state SET="+str(config.debug.EncoderStateMode); config.debug.RelayShow = val(pars[0])>0;
} result = "Debug relay show SET="+(config.debug.RelayShow? "TRUE":"FALSE");
string cmd_debug_showtime(string params){ break;
getParams(params); case 4: // debug_sensors
config.debug.ShowTime = val(pars[0])>0; config.debug.SensorsLook = val(pars[0])>0;
return "Debug show time SET="+(config.debug.ShowTime? "TRUE":"FALSE"); result = "Debug sensors SET="+(config.debug.SensorsLook? "TRUE":"FALSE");
break;
case 5: // debug_showtime
config.debug.ShowTime = val(pars[0])>0;
result = "Debug show time SET="+(config.debug.ShowTime? "TRUE":"FALSE");
break;
default:
result = "{cmd}: Unknown command: "+cmd;
}
}else
result = "{cmd}: Unknown command: "+cmd;
return result;
} }
string cmd_enc_set_windows(string params){ string cmd_encoder(){
getParams(params); string result;
config.EncoderCfg.windowWidth = val(pars[0]); bool save = true;
return "Encoder window SET="+str(config.EncoderCfg.windowWidth); int i = indexOfArray(enc_commands,cmd);
if (i >= 0){
switch (i){
case 0: // enc_set_window
config.EncoderCfg.windowWidth = val(pars[0]);
result = "Encoder window SET="+str(config.EncoderCfg.windowWidth);
save = true;
break;
case 1: // "enc_calibrate_start", // 1
case 2: // "enc_calibrate_end", // 2
result = cmd_enc_calibre();
break;
default:
result = "{cmd}: Unknown command: "+cmd;
break;
}
}else
result = "{cmd}: Unknown command: "+cmd;
if (save) config_save();
return result;
} }
void cmd_replay(string message){ void cmd_replay(string message){
d(message); d(message);
sock.setsendinband(chr(sock.escchar)+" "+message+chr(sock.endchar)+chr(13)+chr(10)); sock.setsendinband(chr(sock.escchar)+" "+message+chr(sock.endchar)+chr(13)+chr(10));
} }
string cmd_setencoder(string params){ string cmd_setencoder(){
string message; string message;
// setencoder=encoderpin,mode,interval,sensorspinoverdot config.EncoderCfg.encoderPort = pars[0];
// setencoder=4,1,50,1.2 config.EncoderCfg.encoderMode = pars[1];
d("params: "+params); config.EncoderCfg.encoderInterval = pars[2];
unsigned char n, p, num ; config.EncoderCfg.workedPorts = pars[par_num];
unsigned long t; config_save();
string vals = "";
n = 1;
p = instr(1,params,",",1);
while (p>0){
vals+=chr(val(mid(params,n,n-p)));
n = p+1;
p = instr(n,params,",",1);
}
string port = mid(params,n,len(params)-n+1);
d("port: "+port);
string ports;
n = 1;
p = instr(1,port,".",1);
while (p>0){
ports+=chr(val(mid(port,n,n-p)));
n = p+1;
p = instr(n,port,".",1);
}
ports += chr(val(mid(port,n,len(port)-n+1)));
config.EncoderCfg.encoderPort = vals[0];
config.EncoderCfg.encoderMode = vals[1];
config.EncoderCfg.encoderInterval = vals[2];
config.EncoderCfg.workedPorts = ports;
message = "Энкодер настроен: Порт: "+str(vals[0])+ ", Режим: "+str(vals[1])+ ", Интервал: "+str(vals[2])+ ", Рабочие порты: "+port;
encoder_init(); encoder_init();
return message; return "Энкодер настроен: Порт: "+str(pars[0])+ ", Режим: "+str(pars[1])+ ", Интервал: "+str(pars[2])+ ", Рабочие порты: "+pars[par_num];
} }
string cmd_enc_calibre(string params){ string cmd_enc_calibre(){
int mode = val(left(params,1)); int mode = val(pars[0]);
enc_calibrate_mode(mode); enc_calibrate_mode(mode);
if (mode >0){ if (mode >0){
return "Запушена калибровка энкодера в режиме "+str(mode); return "Запушена калибровка энкодера в режиме "+str(mode);
@ -157,7 +257,7 @@ string cmd_enc_calibre(string params){
return "Остановлен режим калибровки энкодера. Среднее значение: " + str(enc_calibrate_result()); return "Остановлен режим калибровки энкодера. Среднее значение: " + str(enc_calibrate_result());
} }
string cmd_systeminfo(){ string cmd_systeminfo(){
return "{ platform='"+ PLATFORM+"', version='" + VERSION + "', uptime=" + str(sys.timercountms)+" }"; return "platform='"+ PLATFORM+"', version='" + VERSION + "', uptime=" + str(sys.timercountms)+"";
} }
void cmd_reset() void cmd_reset()
@ -198,7 +298,7 @@ string cmd_ip(string params)
} }
return result; return result;
} }
string cmd_setbraker(string params) string cmd_setbraker()
{ {
unsigned char n, p, num ; unsigned char n, p, num ;
unsigned long t; unsigned long t;
@ -210,9 +310,12 @@ string cmd_setbraker(string params)
n = p+1; n = p+1;
p = instr(n,params,",",1); p = instr(n,params,",",1);
} }
num = vals[0]; num = pars[0];
// BrakerConfig c = config.BRAKERS[num-1]; if (toLower1(pars[1])=="off"){
// c.Enable = val(vals[1]) > 0; config.BRAKERS[num-1].Enable = false;
brakers_init();
return "OK: Braker "+str(num)+" now OFF";
}
// string rrrr = right(params,len(params)-n+1); // string rrrr = right(params,len(params)-n+1);
// if (c.Enable){ // if (c.Enable){
// c.Relay= vals[1]; // c.Relay= vals[1];
@ -221,11 +324,11 @@ string cmd_setbraker(string params)
// c.Timeout= (strtof(right(params,len(params)-n+1))*1000); // c.Timeout= (strtof(right(params,len(params)-n+1))*1000);
// } // }
// //braker_init(unsigned char number, unsigned char sensor, unsigned char drop, unsigned char relay, unsigned long timeout); // //braker_init(unsigned char number, unsigned char sensor, unsigned char drop, unsigned char relay, unsigned long timeout);
unsigned long timeout = (strtof(right(params,len(params)-n+1))*1000); float timeout = (strtof(pars[par_num])*1000);
braker_init(num,vals[2],vals[3],vals[1],timeout); braker_init(num,val(pars[2]),val(vals[3]),val(vals[1]),timeout);
string message; string message;
if (vals[1]>0){ if (val(pars[1])>0){
message = "OK: Num="+str(num)+", Rel="+str(vals[1])+", Sens="+str(vals[2])+", Drop="+str(vals[3])+", Timeout="+ ftostr(timeout,FTOSTR_MODE_PLAIN,4)+" sec."; message = "OK: Num="+str(num)+", Rel="+pars[1]+", Sens="+pars[2]+", Drop="+pars[3]+", Timeout="+ ftostr(timeout/1000,FTOSTR_MODE_PLAIN,4)+" sec.";
}else{ }else{
message = "OK: Num="+str(num)+" DISABLED"; message = "OK: Num="+str(num)+" DISABLED";
} }
@ -236,6 +339,9 @@ string cmd_setbraker(string params)
void getParams(string params){ void getParams(string params){
//d("params: "+params); //d("params: "+params);
for (int i=0;i<10;i++){
pars[i]="";
}
unsigned char n, p, num ; unsigned char n, p, num ;
unsigned long t; unsigned long t;
string vals = ""; string vals = "";
@ -243,7 +349,7 @@ void getParams(string params){
n = 1; n = 1;
p = instr(1,params,",",1); p = instr(1,params,",",1);
while (p>0){ while (p>0){
pars[par_num] = mid(params,n,n-p); pars[par_num] = mid(params,n,p-n);
n = p+1; n = p+1;
par_num++; par_num++;
p = instr(n,params,",",1); p = instr(n,params,",",1);

View File

@ -1,16 +1,13 @@
void on_sock_inband(); void on_sock_inband();
void cmd_replay(string message); void cmd_replay(string message);
string cmd_debug();
string cmd_system();
string cmd_encoder();
string cmd_systeminfo(); string cmd_systeminfo();
void cmd_reset(); void cmd_reset();
string cmd_ip(string params); string cmd_ip(string params);
string cmd_setbraker(string params); string cmd_setbraker();
string cmd_setencoder(string params); string cmd_setencoder();
string cmd_enc_calibre(string params); string cmd_enc_calibre();
string cmd_enc_set_windows(string params); string cmd_enc_set_windows(string params);
string cmd_debug_enc_level(string params);
string cmd_debug_enc_state(string params);
string cmd_debug_sensors(string params);
string cmd_debug_showtime(string params);
void getParams(string params); void getParams(string params);
string cmd_debug_relay_reply(string params);
string cmd_debug_relay_show(string params);

View File

@ -1,46 +1,52 @@
//// You must free the result if result is non-NULL. #include "global.th";
//char *str_replace(char *orig, char *rep, char *with) {
// char *result; // the return string
// char *ins; // the next insert point
// char *tmp; // varies
// int len_rep; // length of rep (the string to remove)
// int len_with; // length of with (the string to replace rep with)
// int len_front; // distance between rep and end of last rep
// int count; // number of replacements
// // sanity checks and initialization int arrayLength(string *a){
// if (!orig || !rep) int i=0;
// return null; for (i=0;i<255;i++){
// len_rep = len(rep); if (len(a[i])==0)
// if (len_rep == 0) break;
// return null; // empty rep causes infinite loop during count }
// if (!with) return i-1;
// with = ""; }
// len_with = len(with);
// // count the number of replacements needed int indexOfArray(string *a,string search){
// ins = orig; int r = -1;
// for (count = 0; tmp = instr(ins, rep); ++count) { int i=0;
// ins = tmp + len_rep; for (i=0;i<255;i++){
// } if (len(a[i])==0){
r=-1;
break;
}
else{
if (a[i] == search){
r = i;
break;
}
}
}
return r;
}
// tmp = result = malloc(len(orig) + (len_with - len_rep) * count + 1); void *memcpy(void *dst,void *src,char n){
int i;
for( i = 0; i < n; i++ )
((unsigned char*)dst)[i] = ((unsigned char*)src)[i];
return dst;
}
// if (!result) char tolower(char c){
// return null; char o = c;
if(c>=65 && c<=90){
c=c+32 ;
o =(char) c;
}
return (char) o;
}
string toLower1(string st){
for (int i=0;i<len(st);i++){
st[i] = tolower(st[i]);
}
return st;
}
// // first time through the loop, all the variable are set correctly //#endif
// // from here on,
// // tmp points to the end of the result string
// // ins points to the next occurrence of rep in orig
// // orig points to the remainder of orig after "end of rep"
// while (count--) {
// ins = instr(orig, rep);
// len_front = ins - orig;
// tmp = mid(tmp, orig, len_front) + len_front;
// tmp = strcpy(tmp, with) + len_with;
// orig += len_front + len_rep; // move to next "end of rep"
// }
// strcpy(tmp, orig);
// return result;
//}

View File

@ -0,0 +1,10 @@
#ifndef global
#define global
int arrayLength(string *a);
int indexOfArray(string *a,string search);
void *memcpy(void *dst,char *src,char n);
char tolover(char c);
string toLower1(string st);
#endif

View File

@ -1,5 +1,6 @@
#include "global.th" #include "global.th"
#include "config_work.th" #include "config_work.th"
#include "common.th"
Configuration config; Configuration config;
@ -29,7 +30,7 @@ string get_parameter_from_file(string param,string filename){
pos2 = fd.find(pos+len(param),chr(13),1,FORWARD,1,PL_FD_FIND_EQUAL)-len(param+"="); pos2 = fd.find(pos+len(param),chr(13),1,FORWARD,1,PL_FD_FIND_EQUAL)-len(param+"=");
string r = fd.getdata(pos2-pos); string r = fd.getdata(pos2-pos);
d("|<"+param+"='"+r+"'"+chr(13)); //d("|<"+param+"='"+r+"'"+chr(13));
fd.close(); fd.close();
return r; return r;
} }
@ -44,73 +45,138 @@ void config_load(){
if (config.STORE_LOCATON == FLASH){ if (config.STORE_LOCATON == FLASH){
fd.filenum=1; fd.filenum=1;
fd.open("config"); fd.open("config");
d("config file size:" +str(fd.filesize)); string s =fd.getdata(200);
fd.close(); while (len(s)>0){
config.IP= get_parameter_from_file("IP","config"); d(s);
config.STORE_LOCATON = get_parameter_from_file("STORE_LOCATON","config"); s=fd.getdata(200);
config.FLASH_SIZE = get_parameter_from_file("FLASH_SIZE","config");
config.FLASH_FILES = get_parameter_from_file("FLASH_FILES","config");
config.STORE_BRAKER = get_parameter_from_file("STORE_BRAKER","config");
for (int i = 0; i<BRAKER_COUNT;i++){
BrakerConfig c = config.BRAKERS[i];
c.Enable = get_parameter_from_file("BRAKER["+str(i)+"].ENABLE","config");
c.SensPort= get_parameter_from_file("BRAKER["+str(i)+"].SENSPORT","config");
c.DropPort=get_parameter_from_file("BRAKER["+str(i)+"].DROPPORT","config");
c.Relay=get_parameter_from_file("BRAKER["+str(i)+"].RELAY","config");
c.Timeout=get_parameter_from_file("BRAKER["+str(i)+"].TIMEOUT","config");
} }
//d("config file size:" +str(fd.filesize));
fd.close();
config.IP= get_parameter_from_file("ip","config");
//config.STORE_LOCATON = get_parameter_from_file("STORE_LOCATON","config");
//config.FLASH_SIZE = get_parameter_from_file("FLASH_SIZE","config");
//config.FLASH_FILES = get_parameter_from_file("FLASH_FILES","config");
config.STORE_BRAKER = get_parameter_from_file("sb","config");
config.STORE_ENCODER = get_parameter_from_file("se","config");
if (config.STORE_ENCODER){
config.EncoderCfg.encoderPort = get_parameter_from_file("en.p","config");
config.EncoderCfg.encoderMode = get_parameter_from_file("en.m","config");
config.EncoderCfg.encoderInterval= get_parameter_from_file("en.int","config");
config.EncoderCfg.workedPorts = get_parameter_from_file("en.wp","config");
config.EncoderCfg.windowWidth = get_parameter_from_file("en.wn","config");
config.EncoderCfg.reinitInterval = get_parameter_from_file("en.ri","config");
}
//if (config.STORE_BRAKER){
for (int i = 0; i<BRAKER_COUNT;i++){
BrakerConfig c = config.BRAKERS[i];
if (len(get_parameter_from_file("br["+str(i)+"].e","config"))>0){
c.Enable = get_parameter_from_file("br["+str(i)+"].e","config");
c.SensPort= get_parameter_from_file("br["+str(i)+"].s","config");
c.DropPort=get_parameter_from_file("br["+str(i)+"].d","config");
c.Relay=get_parameter_from_file("br["+str(i)+"].r","config");
c.Timeout=get_parameter_from_file("br["+str(i)+"].t","config");
}
}
//}
char arr[1024];
get_config(arr);
} }
} }
void config_write(Configuration conf){ char *get_config( char *array){
if (conf.STORE_LOCATON == FLASH){
fd.filenum = 1;
fd.open("config");
d("before write:file size:" +str(fd.filesize));
fd.setpointer(1);
string sb=""; string sb="";
string r = chr(13); string r = chr(13)+chr(10);
sb += "IP="+conf.IP+r; sb += "ip="+config.IP+r;
string t[] = {"NONE","EEPROM","FLASH"}; string t[] = {"none","eeprom","flash"};
sb += "STORE_LOCATON="+ t[conf.STORE_LOCATON]+r; sb += "store="+ t[config.STORE_LOCATON]+r;
sb += "FLASH_SIZE=" + str(conf.FLASH_SIZE)+r; if (config.STORE_LOCATON == FLASH){
sb += "FLASH_FILES=" + str(conf.FLASH_FILES)+r; sb += "fs=" + str(config.FLASH_SIZE)+r;
sb += "STORE_BRAKER=" + str(conf.STORE_BRAKER)+r; sb += "ff=" + str(config.FLASH_FILES)+r;
fd.filenum = 1; }
fd.setdata(sb); //d("|>>>"+sb);
d("|>>>"+sb); memcpy(array,sb,len(sb));
array+=len(sb);
sb="";
if (config.EncoderCfg.encoderPort>0){
config.STORE_ENCODER = true;
sb += "en.p="+str(config.EncoderCfg.encoderPort)+r;
sb += "en.m="+str(config.EncoderCfg.encoderMode)+r;
sb += "en.int="+str(config.EncoderCfg.encoderInterval)+r;
sb += "en.wp="+config.EncoderCfg.workedPorts+r;
sb += "en.wn="+str(config.EncoderCfg.windowWidth)+r;
sb += "en.ri="+str(config.EncoderCfg.reinitInterval)+r;
}
sb += "se=" + str(config.STORE_ENCODER)+r;
d(sb);
memcpy(array,sb,len(sb));
array+=len(sb);
sb="";
for (int i = 0; i<BRAKER_COUNT;i++){ for (int i = 0; i<BRAKER_COUNT;i++){
string b = ""; string b = "";
if (conf.BRAKERS[i].SensPort>0){ if (config.BRAKERS[i].Enable){
BrakerConfig c = conf.BRAKERS[i]; config.STORE_BRAKER = true;
b+="BRAKER["+str(i)+"].ENABLE=" +str(c.Enable)+r; BrakerConfig c = config.BRAKERS[i];
b+="BRAKER["+str(i)+"].SENSPORT=" +str(c.SensPort)+r; b+="br["+str(i)+"].e=" +str(c.Enable)+r;
b+="BRAKER["+str(i)+"].DROPPORT=" +str(c.DropPort)+r; b+="br["+str(i)+"].s=" +str(c.SensPort)+r;
b+="BRAKER["+str(i)+"].RELAY=" +str(c.Relay)+r; b+="br["+str(i)+"].d=" +str(c.DropPort)+r;
b+="BRAKER["+str(i)+"].TIMEOUT=" +str(c.Timeout)+r; b+="br["+str(i)+"].r=" +str(c.Relay)+r;
fd.filenum = 1; b+="br["+str(i)+"].t=" +str(c.Timeout)+r;
fd.setdata(b); //d("|>>>"+b);
d("|>>>"+b);
} }
sb+=b;
} }
for (int i=0;i<SENSORS;i++){ sb += "sb=" + str(config.STORE_BRAKER)+r;
// d("write bounce" + chr(i)); d(sb);
string b = ""; memcpy(array,sb,len(sb));
if (conf.BOUNCE[i].Port>0){ array+=len(sb);
b+="BOUNCE["+str(i)+"].ENABLE="+ str(conf.BOUNCE[i].Enable); return array;
b+="BOUNCE["+str(i)+"].PORT="+ str(conf.BOUNCE[i].Port);
b+="BOUNCE["+str(i)+"].TIMEOUT="+ str(conf.BOUNCE[i].Timeout);
fd.filenum = 1;
fd.setdata(b);
d("|>>>"+b);
} }
void config_write(){
if (config.STORE_LOCATON == NONE){
fd.filenum = 1;
fd.delete("config");
}
if (config.STORE_LOCATON == FLASH){
fd.mount();
fd.filenum = 1;
//d("flash size = "+ stri(fd.capacity));
if (fd.open("config") == PL_FD_STATUS_NOT_FOUND)
fd.create("config");
if (fd.open("config") == PL_FD_STATUS_OK){
//d("before write:file size:" +str(fd.filesize));
fd.setpointer(0);
char prep[1000];
char *ptr = get_config(prep);
char *out = prep;
int length = ptr - out;
while (out < ptr){
string s;
char *ss = s;
if (out+100 <ptr){
memcpy(ss,out,100);
*(ss-2)=100;
}else{
memcpy(ss,out,ptr-out);
*(ss-2)=ptr-out;
}
fd.setdata(s);
fd.flush();
out+=100;
}
//d("after write:file size:" +str(fd.filesize));
fd.close();
}else{
d("Error open file 'config' from flash");
} }
d("after write:file size:" +str(fd.filesize));
fd.close();
} }
} }
#define DEF_IP "192.168.123.19" #define DEF_IP "192.168.123.19"
@ -118,12 +184,18 @@ void config_write(Configuration conf){
void config_init(){ void config_init(){
config_create(); config_create();
int init= stor.getdata(5,1); //unsigned char x=stor.base;
//stor.base = 5;
string in =stor.getdata(5,1);
unsigned char init= in[0];
// stor.base = x;
switch (init){ switch (init){
case 3: case 3:
config.STORE_LOCATON =FLASH; config.STORE_LOCATON =FLASH;
config.FLASH_SIZE = 1024; config.FLASH_SIZE = 1024;
config.FLASH_FILES = 10; config.FLASH_FILES = 10;
break; break;
case 6: case 6:
config.STORE_LOCATON = EEPROM; config.STORE_LOCATON = EEPROM;
@ -154,15 +226,15 @@ void config_init(){
}else{ }else{
d("format error"); d("format error");
} }
} }
fd.filenum = 1; fd.filenum = 1;
if (!fd.open("config") || fd.filesize==0){ int f = fd.open("config") ;
int s = fd.filesize;
d("f="+stri(f)+" ,size="+stri(s));
if (f>0 || s==0){
d("initial write config"); d("initial write config");
fd.create("config"); fd.create("config");
config_write(config); config_write();
} }
config_load(); config_load();
@ -170,7 +242,7 @@ void config_init(){
} }
void config_save(){ void config_save(){
config_write(config); config_write();
} }
void config_create(){ void config_create(){

View File

@ -6,6 +6,7 @@ string get_parameter_from_file(string param,string filename);
bool format_flash(int flash_size, int file_count); bool format_flash(int flash_size, int file_count);
void config_save(); void config_save();
void config_load(); void config_load();
void config_write(Configuration conf); void config_write();
void config_init(); void config_init();
void config_create(); void config_create();
char *get_config( char *array);

View File

@ -14,6 +14,7 @@ bool inited = false;
int center; int center;
void encoder_init(){ void encoder_init(){
config.STORE_ENCODER = false;
for (int i = 0; i< SENSORS;i++){ for (int i = 0; i< SENSORS;i++){
portVector[i]=-1; portVector[i]=-1;
} }
@ -33,14 +34,14 @@ void encoder_init(){
portVector[works[i].WorkPort] = i; portVector[works[i].WorkPort] = i;
works[i].Enabled = true; works[i].Enabled = true;
vector[intnum-1]+=chr(i); vector[intnum-1]+=chr(i);
config.STORE_ENCODER = true;
} }
} }
inited = true; inited = true;
#if PLATFORM_ID != SIMULATOR #if PLATFORM_ID != SIMULATOR
io.intnum = intnum-1; io.intnum = intnum-1;
io.intenabled = YES; if (config.STORE_ENCODER) io.intenabled = YES;
#endif #endif
} }
bool inEncCalibrate(){ bool inEncCalibrate(){
@ -55,7 +56,7 @@ bool getEncEnabled(unsigned char port,bool value){
return false; return false;
if (portVector[port]==-1) if (portVector[port]==-1)
return true; return true;
t("enc_start");
if (works[portVector[port]].Enabled && works[portVector[port]].State == false && value){ if (works[portVector[port]].Enabled && works[portVector[port]].State == false && value){
long interval = works[portVector[port]].config->encoderInterval; long interval = works[portVector[port]].config->encoderInterval;
long diff = interval * config.EncoderCfg.windowWidth / 200; long diff = interval * config.EncoderCfg.windowWidth / 200;
@ -86,24 +87,26 @@ bool getEncEnabled(unsigned char port,bool value){
}else{ }else{
if (config.debug.EncoderDebug>2) d(str(port)+" не попал о окно "+str(works[portVector[port]].Count)); if (config.debug.EncoderDebug>2) d(str(port)+" не попал о окно "+str(works[portVector[port]].Count));
} }
t("enc_end");
return works[portVector[port]].State; return works[portVector[port]].State;
} }
else{ else{
if (works[portVector[port]].Enabled && works[portVector[port]].State == true && value){ if (works[portVector[port]].Enabled && works[portVector[port]].State == true && value){
if (config.debug.EncoderDebug>1) d(str(port)+" Повторное срабатывание "+str(works[portVector[port]].Count)); if (config.debug.EncoderDebug>1) d(str(port)+" Повторное срабатывание "+str(works[portVector[port]].Count));
} }
t("enc_end");
return false; return false;
} }
} }
void on_io_int(unsigned char linestate){ void on_io_int(unsigned char linestate){
t("enc_int_start");
string workers = vector[linestate-1]; string workers = vector[linestate-1];
if(calibrate_mode>0){ if(calibrate_mode>0){
for (int i=0;i<len(config.EncoderCfg.workedPorts);i++){ for (int i=0;i<len(config.EncoderCfg.workedPorts);i++){
unsigned char port = config.EncoderCfg.workedPorts[i]; unsigned char port = config.EncoderCfg.workedPorts[i];
if (pinChange(port) && getPinStateS(port,true)==true) if (pinChange(port) && getPinStateS(port,true)==true)
{ {
if (z[i]>=0) if (z[i]>=0)
{ {
calibrate_buff[i][z[i]]=calibrate_count[i]; calibrate_buff[i][z[i]]=calibrate_count[i];
@ -114,12 +117,10 @@ void on_io_int(unsigned char linestate){
calibrate_count[i]=0; calibrate_count[i]=0;
if (z[i]>9) if (z[i]>9)
z[i]=0; z[i]=0;
} }
else else
calibrate_count[i]++; calibrate_count[i]++;
} }
return;
} }
else{ else{
for (int i = 0; i < ENCWORKER;i++){ for (int i = 0; i < ENCWORKER;i++){
@ -138,6 +139,7 @@ void on_io_int(unsigned char linestate){
} }
} }
} }
t("enc_int_end");
} }
/// Блок калибровки /// Блок калибровки
@ -152,7 +154,6 @@ void enc_calibrate_mode(int mode){
case 2: // Калибровка интервала датчиков case 2: // Калибровка интервала датчиков
calibrate_mode = 2; calibrate_mode = 2;
break; break;
default: default:
calibrate_mode = 0; calibrate_mode = 0;
d("Установка режима калибровки энкодера - неверный режим: "+ str(mode)); d("Установка режима калибровки энкодера - неверный режим: "+ str(mode));

View File

@ -9,7 +9,7 @@
#message "link global not defined" #message "link global not defined"
#endif #endif
#define VERSION "2.0b_1902011712" #define VERSION "2.0b_1902121725"
#define MAX_FIFO_SIZE 20 #define MAX_FIFO_SIZE 20
#define BRAKER_COUNT 3 #define BRAKER_COUNT 3
@ -26,6 +26,7 @@
#endif #endif
void d(string mess); void d(string mess);
void t(string mess);
@ -91,6 +92,7 @@ typedef struct Configuration{
unsigned int FLASH_SIZE; unsigned int FLASH_SIZE;
unsigned char FLASH_FILES; unsigned char FLASH_FILES;
bool STORE_BRAKER; bool STORE_BRAKER;
bool STORE_ENCODER;
BrakerConfig BRAKERS[BRAKER_COUNT]; BrakerConfig BRAKERS[BRAKER_COUNT];
BounceConfig BOUNCE[SENSORS]; BounceConfig BOUNCE[SENSORS];
EncoderConfig EncoderCfg; EncoderConfig EncoderCfg;
@ -122,3 +124,4 @@ extern BrakerProcess brakers[BRAKER_COUNT];

42
main.tc
View File

@ -3,7 +3,18 @@
#include "relay.th" #include "relay.th"
#include "net.th" #include "net.th"
#include "encoder.th" #include "encoder.th"
#include "common.th"
static string debug_commands[]= {
"debug_enc_level",
"debug_enc_state",
"debug_relay_reply",
"debug_relay_show",
"debug_sensors",
"debug_showtime",
"" // Разделитель. не удалять
};
//==================================================================== //====================================================================
void d(string mess){ void d(string mess){
if (config.debug.ShowTime) if (config.debug.ShowTime)
@ -19,6 +30,18 @@ void d(string mess){
} }
void t(string mess){
mess = lstr(sys.timercountms)+":\t"+mess;
int c = sock.num;
sock.num = 4;
if (sock.statesimple == PL_SSTS_EST){
sock.setdata(mess+chr(13)+chr(10));
sock.send();
}
sock.num =c;
}
#include "config_work.th" #include "config_work.th"
#include "braker.th" #include "braker.th"
@ -27,12 +50,24 @@ void on_sys_init(){
pat.play("RG~",PL_PAT_CANINT); pat.play("RG~",PL_PAT_CANINT);
d(arrayLength(debug_commands));
// for (int i=0;i<255;i++){
// if (len(debug_commands[i])>0){
// d(str(i)+" - "+debug_commands[i]);
// }else{
// break;
// }
// }
config_init(); config_init();
net_init(); net_init();
sensor_init(); sensor_init();
relay_init(); relay_init();
brakers_init(); brakers_init();
// encoder_init(); encoder_init();
// fd.filenum = 2; // fd.filenum = 2;
@ -80,13 +115,14 @@ pat.play("RG~",PL_PAT_CANINT);
// } // }
sys.onsystimerperiod = 1 ; sys.onsystimerperiod = 1 ;
net_start(); net_start();
#if PLATFORM_ID != SIMULATOR
io.num=PL_IO_NUM_46; io.num=PL_IO_NUM_46;
io.enabled=YES; io.enabled=YES;
io.num=PL_IO_NUM_47; io.num=PL_IO_NUM_47;
io.enabled=YES; io.enabled=YES;
io.num=PL_IO_NUM_48; io.num=PL_IO_NUM_48;
io.enabled=YES; io.enabled=YES;
#endif
pat.play("---G-R-R~",PL_PAT_CANINT); pat.play("---G-R-R~",PL_PAT_CANINT);
} }
@ -94,6 +130,7 @@ pat.play("RG~",PL_PAT_CANINT);
void on_sys_timer() void on_sys_timer()
{ {
t("sys_start");
if (!inEncCalibrate()){ if (!inEncCalibrate()){
for (int i=1; i<=3; i++){ for (int i=1; i<=3; i++){
braker_proc(i); braker_proc(i);
@ -141,4 +178,5 @@ if (!inEncCalibrate()){
io.state=LOW; io.state=LOW;
io.state=HIGH; io.state=HIGH;
#endif #endif
t("sys_end");
} }

3
net.tc
View File

@ -4,7 +4,7 @@
#include "braker.th" #include "braker.th"
int num_sdf = 0; int num_sdf = 0;
#define IP_STR "IP=" #define IP_STR "ip="
string buff[4]; string buff[4];
@ -45,6 +45,7 @@ void net_start(){
net_openSocket(1,999,false);//, &net_relay_handle); // relay net_openSocket(1,999,false);//, &net_relay_handle); // relay
net_openSocket(2,998,true);//, &net_diag_handle); // diag net_openSocket(2,998,true);//, &net_diag_handle); // diag
net_openSocket(3,997,true);//, &net_config_handle); // config net_openSocket(3,997,true);//, &net_config_handle); // config
net_openSocket(4,996,false);//, &net_config_handle); // config
} }
string validate_id(string ip){ string validate_id(string ip){

View File

@ -7,8 +7,7 @@ void relay_init(){
io.enabled = true; io.enabled = true;
} }
d("Relays initialized"); d("Relays initialized");
#endif #endif
} }
void relay_set(int number, bool state) void relay_set(int number, bool state)