Тестовая сборка с сохранением параметров 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]
platform=SIMULATOR
transport=udp_broadcast
address=0.36.119.82.182.139
address=0.48.72.100.118.81
[address2]
platform=TPP3W(G2)
transport=udp_broadcast

View File

@ -8,10 +8,11 @@ extern Configuration config;
BrakerProcess brakers[BRAKER_COUNT];
void brakers_init(){
config.STORE_BRAKER = false;
for (int num = 0;num <BRAKER_COUNT; num++){
brakers[num].config = &config.BRAKERS[num];
if (config.BRAKERS[num].Enable){
config.STORE_BRAKER = true;
#if PLATFORM_ID != SIMULATOR
io.num = INPIN[config.BRAKERS[num].SensPort-1];
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].Fifo="";
if (config.BRAKERS[num].Enable){
config.STORE_BRAKER = true;
#if PLATFORM_ID != SIMULATOR
io.num = INPIN[config.BRAKERS[num].SensPort-1];
io.enabled=false;

View File

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

View File

@ -1,16 +1,13 @@
void on_sock_inband();
void cmd_replay(string message);
string cmd_debug();
string cmd_system();
string cmd_encoder();
string cmd_systeminfo();
void cmd_reset();
string cmd_ip(string params);
string cmd_setbraker(string params);
string cmd_setencoder(string params);
string cmd_enc_calibre(string params);
string cmd_setbraker();
string cmd_setencoder();
string cmd_enc_calibre();
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);
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.
//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
#include "global.th";
// // sanity checks and initialization
// if (!orig || !rep)
// return null;
// len_rep = len(rep);
// if (len_rep == 0)
// return null; // empty rep causes infinite loop during count
// if (!with)
// with = "";
// len_with = len(with);
int arrayLength(string *a){
int i=0;
for (i=0;i<255;i++){
if (len(a[i])==0)
break;
}
return i-1;
}
// // count the number of replacements needed
// ins = orig;
// for (count = 0; tmp = instr(ins, rep); ++count) {
// ins = tmp + len_rep;
// }
int indexOfArray(string *a,string search){
int r = -1;
int i=0;
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)
// return null;
char tolower(char c){
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
// // 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;
//}
//#endif

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 "config_work.th"
#include "common.th"
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+"=");
string r = fd.getdata(pos2-pos);
d("|<"+param+"='"+r+"'"+chr(13));
//d("|<"+param+"='"+r+"'"+chr(13));
fd.close();
return r;
}
@ -44,73 +45,138 @@ void config_load(){
if (config.STORE_LOCATON == FLASH){
fd.filenum=1;
fd.open("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("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");
string s =fd.getdata(200);
while (len(s)>0){
d(s);
s=fd.getdata(200);
}
//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){
if (conf.STORE_LOCATON == FLASH){
fd.filenum = 1;
fd.open("config");
d("before write:file size:" +str(fd.filesize));
fd.setpointer(1);
char *get_config( char *array){
string sb="";
string r = chr(13);
sb += "IP="+conf.IP+r;
string t[] = {"NONE","EEPROM","FLASH"};
sb += "STORE_LOCATON="+ t[conf.STORE_LOCATON]+r;
sb += "FLASH_SIZE=" + str(conf.FLASH_SIZE)+r;
sb += "FLASH_FILES=" + str(conf.FLASH_FILES)+r;
sb += "STORE_BRAKER=" + str(conf.STORE_BRAKER)+r;
fd.filenum = 1;
fd.setdata(sb);
d("|>>>"+sb);
string r = chr(13)+chr(10);
sb += "ip="+config.IP+r;
string t[] = {"none","eeprom","flash"};
sb += "store="+ t[config.STORE_LOCATON]+r;
if (config.STORE_LOCATON == FLASH){
sb += "fs=" + str(config.FLASH_SIZE)+r;
sb += "ff=" + str(config.FLASH_FILES)+r;
}
//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++){
string b = "";
if (conf.BRAKERS[i].SensPort>0){
BrakerConfig c = conf.BRAKERS[i];
b+="BRAKER["+str(i)+"].ENABLE=" +str(c.Enable)+r;
b+="BRAKER["+str(i)+"].SENSPORT=" +str(c.SensPort)+r;
b+="BRAKER["+str(i)+"].DROPPORT=" +str(c.DropPort)+r;
b+="BRAKER["+str(i)+"].RELAY=" +str(c.Relay)+r;
b+="BRAKER["+str(i)+"].TIMEOUT=" +str(c.Timeout)+r;
fd.filenum = 1;
fd.setdata(b);
d("|>>>"+b);
if (config.BRAKERS[i].Enable){
config.STORE_BRAKER = true;
BrakerConfig c = config.BRAKERS[i];
b+="br["+str(i)+"].e=" +str(c.Enable)+r;
b+="br["+str(i)+"].s=" +str(c.SensPort)+r;
b+="br["+str(i)+"].d=" +str(c.DropPort)+r;
b+="br["+str(i)+"].r=" +str(c.Relay)+r;
b+="br["+str(i)+"].t=" +str(c.Timeout)+r;
//d("|>>>"+b);
}
sb+=b;
}
for (int i=0;i<SENSORS;i++){
// d("write bounce" + chr(i));
string b = "";
if (conf.BOUNCE[i].Port>0){
b+="BOUNCE["+str(i)+"].ENABLE="+ str(conf.BOUNCE[i].Enable);
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);
sb += "sb=" + str(config.STORE_BRAKER)+r;
d(sb);
memcpy(array,sb,len(sb));
array+=len(sb);
return array;
}
}
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"
@ -118,12 +184,18 @@ void config_write(Configuration conf){
void config_init(){
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){
case 3:
config.STORE_LOCATON =FLASH;
config.FLASH_SIZE = 1024;
config.FLASH_FILES = 10;
break;
case 6:
config.STORE_LOCATON = EEPROM;
@ -154,15 +226,15 @@ void config_init(){
}else{
d("format error");
}
}
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");
fd.create("config");
config_write(config);
config_write();
}
config_load();
@ -170,7 +242,7 @@ void config_init(){
}
void config_save(){
config_write(config);
config_write();
}
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);
void config_save();
void config_load();
void config_write(Configuration conf);
void config_write();
void config_init();
void config_create();
char *get_config( char *array);

View File

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

View File

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

42
main.tc
View File

@ -3,7 +3,18 @@
#include "relay.th"
#include "net.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){
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 "braker.th"
@ -27,12 +50,24 @@ void on_sys_init(){
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();
net_init();
sensor_init();
relay_init();
brakers_init();
// encoder_init();
encoder_init();
// fd.filenum = 2;
@ -80,13 +115,14 @@ pat.play("RG~",PL_PAT_CANINT);
// }
sys.onsystimerperiod = 1 ;
net_start();
#if PLATFORM_ID != SIMULATOR
io.num=PL_IO_NUM_46;
io.enabled=YES;
io.num=PL_IO_NUM_47;
io.enabled=YES;
io.num=PL_IO_NUM_48;
io.enabled=YES;
#endif
pat.play("---G-R-R~",PL_PAT_CANINT);
}
@ -94,6 +130,7 @@ pat.play("RG~",PL_PAT_CANINT);
void on_sys_timer()
{
t("sys_start");
if (!inEncCalibrate()){
for (int i=1; i<=3; i++){
braker_proc(i);
@ -141,4 +178,5 @@ if (!inEncCalibrate()){
io.state=LOW;
io.state=HIGH;
#endif
t("sys_end");
}

3
net.tc
View File

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

View File

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