diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6214c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,80 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ diff --git a/KSM-Line-2.2.tpr b/KSM-Line-2.2.tpr new file mode 100644 index 0000000..4211a13 --- /dev/null +++ b/KSM-Line-2.2.tpr @@ -0,0 +1,165 @@ +[project] +version=1 +platform=TPP2W(G2) +src_lib_ver=2_01_00 +name=KSM-Line-2.0 +output=KSM-Line-2.0.tpc +debug=on +defines= /dLCD_TYPE=SOLOMON_SSD1963 /dOBJECT_LCD=LCD_ENABLED /dOBJECT_FD=FD_ENABLED /dOBJECT_KP=KP_ENABLED /dOBJECT_WLN=WLN_ENABLED +transport=udp_broadcast +[address1] +platform=SIMULATOR +transport=udp_broadcast +address=0.36.119.82.182.139 +[address2] +platform=TPP3W(G2) +transport=udp_broadcast +address=0.48.72.100.118.81 +[address3] +platform=TPP2W +transport=udp_broadcast +address=0.48.72.100.118.81 +[address4] +platform=TPP2W(G2) +transport=udp_broadcast +address=0.36.119.82.182.139 +[file1] +path=main.tc +type=c +format= +condition= +locked=no +location= +[file2] +path=global.th +type=cheader +format= +condition= +locked=no +location= +[file3] +path=tpp2.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file4] +path=tpp3.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file5] +path=simulator.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file6] +path=command.tc +type=c +format=65001 +condition= +locked=no +location=project +[file7] +path=net.tc +type=c +format=65001 +condition= +locked=no +location=project +[file8] +path=net.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file9] +path=sdf.txt +type=text +format=65001 +condition= +locked=no +location=project +[file10] +path=config_work.tc +type=c +format=65001 +condition= +locked=no +location=project +[file11] +path=config_work.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file12] +path=braker.tc +type=c +format=65001 +condition= +locked=no +location=project +[file13] +path=braker.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file14] +path=sensors.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file15] +path=sensors.tc +type=c +format=65001 +condition= +locked=no +location=project +[file16] +path=relay.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file17] +path=relay.tc +type=c +format=65001 +condition= +locked=no +location=project +[file18] +path=command.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file19] +path=encoder.th +type=cheader +format=65001 +condition= +locked=no +location=project +[file20] +path=encoder.tc +type=c +format=65001 +condition= +locked=no +location=project diff --git a/KSM-Line-2.2.tprusr b/KSM-Line-2.2.tprusr new file mode 100644 index 0000000..de41c3b --- /dev/null +++ b/KSM-Line-2.2.tprusr @@ -0,0 +1,101 @@ +[project] +watch=works[i].Count, portVector, works[i].WorkPort +[file1] +line=105 +cursor=1677 +opened=yes +breakpoints= +[file2] +line=78 +cursor=409 +opened=yes +breakpoints= +[file3] +line=0 +cursor=248 +opened=active +breakpoints= +[file4] +line=0 +cursor=125 +opened=yes +breakpoints= +[file5] +line=0 +cursor=161 +opened=yes +breakpoints= +[file6] +line=18 +cursor=422 +opened=yes +breakpoints= +[file7] +line=18 +cursor=916 +opened=no +breakpoints= +[file8] +line=0 +cursor=172 +opened=no +breakpoints= +[file9] +line=0 +cursor=0 +opened=no +[file10] +line=4 +cursor=1099 +opened=no +breakpoints= +[file11] +line=0 +cursor=299 +opened=no +breakpoints= +[file12] +line=0 +cursor=360 +opened=yes +breakpoints= +[file13] +line=0 +cursor=55 +opened=yes +breakpoints= +[file14] +line=0 +cursor=93 +opened=no +breakpoints= +[file15] +line=12 +cursor=541 +opened=yes +breakpoints= +[file16] +line=0 +cursor=18 +opened=no +breakpoints= +[file17] +line=0 +cursor=20 +opened=yes +breakpoints= +[file18] +line=0 +cursor=55 +opened=yes +breakpoints= +[file19] +line=0 +cursor=101 +opened=yes +breakpoints= +[file20] +line=18 +cursor=2430 +opened=yes +breakpoints=24 diff --git a/braker.tc b/braker.tc new file mode 100644 index 0000000..655d703 --- /dev/null +++ b/braker.tc @@ -0,0 +1,108 @@ +#include "global.th" +#include "sensors.th" +#include "braker.th" +#include "relay.th" + +extern Configuration config; + +BrakerProcess brakers[BRAKER_COUNT]; + +void brakers_init(){ + + for (int num = 0;num 3){ + d("ERROR: Big braker number - set "+str(number)+" but max "+ str(BRAKER_COUNT)); + } + unsigned char num = number -1; + config.BRAKERS[num].Enable = true; + config.BRAKERS[num].SensPort = sensor; + config.BRAKERS[num].DropPort = drop; + config.BRAKERS[num].Relay = relay; + config.BRAKERS[num].Timeout = timeout; + brakers[num].config = &config.BRAKERS[num]; + brakers[num].State = false; + brakers[num].OnTime = 0; + brakers[num].Fifo=""; + if (config.BRAKERS[num].Enable){ + #if PLATFORM_ID != SIMULATOR + io.num = INPIN[config.BRAKERS[num].SensPort-1]; + io.enabled=false; + io.num = INPIN[config.BRAKERS[num].DropPort-1]; + io.enabled=false; + io.num = OUTPIN[config.BRAKERS[num].Relay-1]; + io.enabled=true; + #endif + } +} + + +/// ** +/// Обработка команды для бракиратора - G1 или B1. Где G|B статус сигнала (Good|Bad), а цифра - номер бракиратора +/// ** +void braker_set(string cmd){ + if (len(cmd)!=2) + d("Неверный формат команды = '"+cmd+"'"); + string type = left(cmd,1); + unsigned char num = val(right(cmd,1)); + brakers[num-1].Fifo += type; + d("Бракиратор #"+str(num)+" сигнал ["+type+"]"); + +} + +void braker_proc(unsigned char number) +{ + unsigned char num = number-1; + bool en = brakers[num].config->Enable; + if (brakers[num].config->Enable==true){ + unsigned long timeout = brakers[num].config->Timeout; + if (brakers[num].State){ // проверка на выключение + if (brakers[num].OnTime + timeout < sys.timercountms){ // Обработка таймаута + relay_set(brakers[num].config->Relay,false); + brakers[num].State = false; + d("Бракиратор #"+str(number)+" выключен по таймауту ["+str(sys.timercountms - brakers[num].OnTime)+"/"+str(timeout)+"]ms"); + brakers[num].OnTime = 0; + }else{ // проверка статуса порта + if (getPinState(brakers[num].config->DropPort)){ + relay_set(brakers[num].config->Relay,false); + brakers[num].State = false; + d("Бракиратор #"+str(number)+" выключен по сигналу сброса через "+str(sys.timercountms - brakers[num].OnTime)+"ms"); + brakers[num].OnTime = 0; + } + } + }else{ // проверка на сработку + if (pinChange(brakers[num].config->SensPort) && getPinStateS(brakers[num].config->SensPort,true)){ + if (braker_get_current(num) == "B"){ + relay_set(brakers[num].config->Relay,true); + brakers[num].State = true; + brakers[num].OnTime = sys.timercountms; + d("Бракиратор #"+str(number)+" включен по сигналу сенсора"); + } + } + } + + } +} + +string braker_get_current(unsigned char num){ + string c = left(brakers[num].Fifo,1); + int l = len(brakers[num].Fifo); + brakers[num].Fifo = right(brakers[num].Fifo,l-1); + d("Бракиратор #"+str(num)+": Получено текущее значение = ["+c+"]"); + return c; +} diff --git a/braker.th b/braker.th new file mode 100644 index 0000000..aa4842c --- /dev/null +++ b/braker.th @@ -0,0 +1,12 @@ +#include "global.th" +#ifndef BRAKER + #define BRAKER 1 + #message "brakers" + +void brakers_init(); +void braker_init(unsigned char number, unsigned char sensor, unsigned char drop, unsigned char relay, unsigned long timeout); +void braker_set(string cmd); +void braker_proc(unsigned char number); +string braker_get_current(unsigned char num); + +#endif \ No newline at end of file diff --git a/command.tbs b/command.tbs new file mode 100644 index 0000000..e69de29 diff --git a/command.tc b/command.tc new file mode 100644 index 0000000..f26298d --- /dev/null +++ b/command.tc @@ -0,0 +1,135 @@ +#include "global.th" +#include "command.th" +#include "config_work.th" +#include "braker.th" +#include "net.th" +#include "encoder.th" + +static string cmdbuffer = ""; +void on_sock_inband() // обработка поступивших команд +{ + cmdbuffer += sock.getinband(); + unsigned char position = instr(1,cmdbuffer,chr(sock.endchar),1); + while (position != 0){ + string cmdline = left(cmdbuffer,position); + cmdbuffer = right(cmdbuffer,len(cmdbuffer)-position); + d("{cmd} line: "+cmdline); + unsigned char cmdlen = instr(1,cmdline,chr('='),1); + if (cmdlen==0) + cmdlen = instr(1,cmdline,chr('&'),1); + string cmd = left(cmdline,cmdlen-1); + string params = mid(cmdline,cmdlen+1,position-cmdlen-1); + d("{cmd}: "+cmd); + d("{cmd}.params: "+params); + if (cmd =="systeminfo"){ + cmd_replay(cmd_systeminfo()); + }else if (cmd == "reset" || cmd == "reboot"){ +// cmd_replay(cmd_reset()); + cmd_replay("{cmd} Rebooting..."); + cmd_reset(); + + }else if (cmd == "ip"){ + cmd_replay(cmd_ip(params)); + }else if (cmd == "setbraker"){ + cmd_replay(cmd_setbraker(params)); + }else if (cmd == "setencoder"){ + cmd_replay(cmd_setencoder(params)); + }else if (cmd == "enc_calibrate_start"){ + cmd_replay(cmd_enc_calibre(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); + } +} + +void cmd_replay(string message){ + sock.setsendinband(chr(sock.escchar)+" "+message+chr(sock.endchar)); +} + +string cmd_setencoder(string params){ + return "Запущен режим калибровки энкодера"; +} +string cmd_enc_calibre(string params){ + return "Остановлен режим калибровки энкодера. Среднее значение: " + str(enc_calibrate_result()); +} +string cmd_systeminfo(){ + return "{ platform='"+ PLATFORM+"', version='" + VERSION + "', uptime=" + str(sys.timercountms)+" }"; +} + +void cmd_reset() +{ + d("{cmd} Rebooting..."); + unsigned int w = sys.timercount; + while (sys.timercount - w < 2){ + doevents; + } + sock.close(); + w = sys.timercount; + while (sys.timercount-w < 2){ + doevents; + } + sys.reboot(); +} +string cmd_ip(string params) +{ + string result=""; + string vip = validate_id(params); + if (vip != params){ + return "{cmd}.ip - Invalid IP: [ "+params+" ]"; + } + + unsigned char x = stor.setdata(ddval(vip),0); + doevents; + if (x == 4){ + config.IP = vip; + config_save(); + result = ("{cmd}.ip set to [ " + ddstr(stor.getdata(1,4))+" ] after reboot"); + d(result); + doevents; + } + else + { + sock.setsendinband(chr(sock.escchar)+" ERROR STORE IP "+chr(sock.endchar)); + doevents; + } + return result; +} +string cmd_setbraker(string 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); + } + num = vals[0]; +// BrakerConfig c = config.BRAKERS[num-1]; +// c.Enable = val(vals[1]) > 0; +// string rrrr = right(params,len(params)-n+1); +// if (c.Enable){ +// c.Relay= vals[1]; +// c.SensPort= vals[2]; +// c.DropPort= vals[3]; +// 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); + 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."; + }else{ + message = "OK: Num="+str(num)+" DISABLED"; + } + d(message); + return message; +} + diff --git a/command.th b/command.th new file mode 100644 index 0000000..d7d9fdc --- /dev/null +++ b/command.th @@ -0,0 +1,8 @@ +void on_sock_inband(); +void cmd_replay(string message); +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); diff --git a/config_work.tc b/config_work.tc new file mode 100644 index 0000000..2bbfff2 --- /dev/null +++ b/config_work.tc @@ -0,0 +1,197 @@ +#include "global.th" +#include "config_work.th" +Configuration config; + + + +string get_parameter(string param){ + romfile.open("sdf.txt"); + int str_start = 1; + unsigned long pos,pos2; + pos = romfile.find(str_start,param+"=",1); + pos +=len(param+"="); + romfile.pointer32=pos; + pos2 = romfile.find(pos,chr(13),1); + return romfile.getdata(pos2-pos); +} + +string get_parameter_from_file(string param,string filename){ + fd.filenum = 2; + fd.open(filename); + fd.setpointer(1); + unsigned long pos,pos2; + pos=fd.find(1,param+"=",1,FORWARD,1,PL_FD_FIND_EQUAL); + if (pos == 0){ + fd.close(); + return ""; + } + fd.setpointer(pos+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); + d("|<"+param+"='"+r+"'"+chr(13)); + fd.close(); + return r; +} +bool format_flash(int flash_size, int file_count){ + return (fd.format(config.FLASH_SIZE,config.FLASH_FILES) == PL_FD_STATUS_OK); +} + + + +void config_load(){ + d("load config"); + 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>>"+sb); + for (int i = 0; i0){ + 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); + } + + } + for (int i=0;i0){ + 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); + + } + } + d("after write:file size:" +str(fd.filesize)); + fd.close(); + } +} +#define DEF_IP "192.168.123.19" +//#define DEF_IP "10.0.50.232" + +void config_init(){ + config_create(); + int init= stor.getdata(5,1); + switch (init){ + case 3: + config.STORE_LOCATON =FLASH; + config.FLASH_SIZE = 1024; + config.FLASH_FILES = 10; + break; + case 6: + config.STORE_LOCATON = EEPROM; + break; + default: + config.STORE_LOCATON = NONE; + + break; + } + + config.IP = ddstr(stor.getdata(0,4)); + if (config.IP != validate_id(config.IP)) + config.IP= DEF_IP; + +// config.BRAKER_COUNT = 3; +// config.STORE_LOCATON = FLASH; +// config.FLASH_SIZE = 1024; +// config.FLASH_FILES = 10; +// config.STORE_BRAKER = false; + + if (config.STORE_LOCATON == FLASH){ + if (fd.mount() !=PL_FD_STATUS_OK){ + d("formating"); + if (format_flash(config.FLASH_SIZE,config.FLASH_FILES)) + { + d("format complete"); + fd.mount(); + }else{ + d("format error"); + } + + + + } + fd.filenum = 1; + if (!fd.open("config") || fd.filesize==0){ + d("initial write config"); + fd.create("config"); + config_write(config); + } + + config_load(); + } +} + +void config_save(){ + config_write(config); +} + +void config_create(){ + config.IP= "0.0.0.0"; + config.STORE_LOCATON = NONE; + config.FLASH_SIZE = 0; + config.FLASH_FILES = 0; + config.STORE_BRAKER = false; + BrakerConfig c; + c.Enable = false; + c.SensPort = 0; + c.DropPort = 0; + c.Relay = 0; + c.Timeout = 0; + config.BRAKERS[0] = c; + config.BRAKERS[1] = c; + config.BRAKERS[2] = c; + + config.EncoderCfg.encoderPort = 4; + config.EncoderCfg.encoderMode = 1; + config.EncoderCfg.encoderInterval = 10; + config.EncoderCfg.workedPorts = "12"; + +} \ No newline at end of file diff --git a/config_work.th b/config_work.th new file mode 100644 index 0000000..4d4ff7a --- /dev/null +++ b/config_work.th @@ -0,0 +1,11 @@ +#include "global.th" + + +string get_parameter(string param); +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_init(); +void config_create(); diff --git a/encoder.tc b/encoder.tc new file mode 100644 index 0000000..c853089 --- /dev/null +++ b/encoder.tc @@ -0,0 +1,98 @@ +#include "global.th"; +#include "sensors.th"; + +#define ENCWORKER 5 +EncoderProcess works[ENCWORKER]; +string vector[4]; +char portVector[SENSORS]; +int calibrate_mode = 0; + + +void encoder_init(){ + for (int i = 0; i< SENSORS;i++){ + portVector[i]=-1; + } + int intnum = ((int)(config.EncoderCfg.encoderPort/4)); + for (char i = 0; i < ENCWORKER; i++){ + works[i].Enabled = false; + works[i].config = &config.EncoderCfg; + works[i].State = false; + works[i].Count = config.EncoderCfg.encoderInterval/2; + works[i].WorkPort=0; + string ports = config.EncoderCfg.workedPorts; + if (iencoderInterval; + long diff = interval /5; + + if (works[portVector[port]].Count >= (interval/2 - diff) && works[portVector[port]].Count <= (interval/2+diff)){ + works[portVector[port]].State = true; + } + return works[portVector[port]].State; + } + else{ + return false; + } +} + +void on_io_int(unsigned char linestate){ + string workers = vector[linestate-1]; + for (int i = 0; i < len(workers);i++){ + if (works[i].Enabled){ + works[i].Count++; + if (works[i].Count>works[i].config->encoderInterval) + { + works[i].Count = 0; + works[i].State = false; + } + d("count ["+str(i)+"] :"+str(works[i].Count)); + } + } + +} +/// Блок калибровки + +void enc_calibrate_mode(int mode){ + switch (mode){ + case 0: // Остановка калибровки + calibrate_mode = 0; + break; + case 1: // Калибровка интервала бутылок + calibrate_mode = 1; + break; + case 2: // Калибровка интервала датчиков + calibrate_mode = 2; + break; + + default: + calibrate_mode = 0; + d("Установка режима калибровки энкодера - неверный режим: "+ str(mode)); + break; + } + +} + + +int enc_calibrate_result(){ + + return 0; +} \ No newline at end of file diff --git a/encoder.th b/encoder.th new file mode 100644 index 0000000..b411e7b --- /dev/null +++ b/encoder.th @@ -0,0 +1,3 @@ +void encoder_init(); +bool getEncEnabled(unsigned char port,bool value); +int enc_calibrate_result(); \ No newline at end of file diff --git a/global.th b/global.th new file mode 100644 index 0000000..b206def --- /dev/null +++ b/global.th @@ -0,0 +1,99 @@ +//DEFINES------------------------------------------------------------- + +#ifndef GLOBAL + +#ifndef GLOBAL + #message "link global inside" + #define GLOBAL +#else + #message "link global not defined" +#endif + +#define VERSION "2.0b" +#define MAX_FIFO_SIZE 20 +#define BRAKER_COUNT 3 + + +//INCLUDES------------------------------------------------------------ +#if PLATFORM_ID==TPP2WG2 + #include "tpp2.th" +#endif +#if PLATFORM_ID==TPP3WG2 + #include "tpp3.th" +#endif +#if PLATFORM_ID==SIMULATOR + #include "simulator.th" +#endif + +void d(string mess); + + + + +enum STORELOCATION{ + NONE, + EEPROM, + FLASH +}; + +typedef struct BrakerConfig{ + bool Enable; + unsigned char SensPort; + unsigned char DropPort; + unsigned char Relay; + unsigned long Timeout; +}; + +typedef struct BounceConfig{ + bool Enable; + unsigned char Port; + unsigned int Timeout; +}; + +typedef struct BrakerProcess{ + BrakerConfig *config; + bool State; + unsigned long OnTime; + string Fifo; +}; + +typedef struct EncoderConfig{ + unsigned char encoderPort; + unsigned char encoderMode; + unsigned long encoderInterval; + string workedPorts; +}; + +typedef struct EncoderProcess{ + bool Enabled; + EncoderConfig *config; + bool State; + unsigned long Count; + unsigned char WorkPort; +}; +typedef struct Configuration{ + string IP; + STORELOCATION STORE_LOCATON; + unsigned int FLASH_SIZE; + unsigned char FLASH_FILES; + bool STORE_BRAKER; + BrakerConfig BRAKERS[BRAKER_COUNT]; + BounceConfig BOUNCE[SENSORS]; + EncoderConfig EncoderCfg; +}; + +//#include "config_work.th" +#include "net.th" +//#include "braker.th" + +#endif + +//DECLARATIONS-------------------------------------------------------- + + +extern Configuration config; + +extern unsigned char state[SENSORS]; +extern BrakerProcess brakers[BRAKER_COUNT]; + + diff --git a/main.tc b/main.tc new file mode 100644 index 0000000..a2cfee0 --- /dev/null +++ b/main.tc @@ -0,0 +1,130 @@ +#include "global.th" +#include "sensors.th" +#include "relay.th" +#include "net.th" +#include "encoder.th" + +//==================================================================== +void d(string mess){ + sys.debugprint(mess+chr(13)); + int c = sock.num; + sock.num = 2; + 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" + + +void on_sys_init(){ + +pat.play("R-R---~",PL_PAT_CANINT); + + config_init(); + net_init(); + sensor_init(); + relay_init(); + brakers_init(); +// encoder_init(); + + +// fd.filenum = 2; +// fd.open("config"); +// fd.setpointer(1); +// string data = fd.getdata(250); +// sys.debugprint(data+chr(13)); +// fd.close(); + +// config.STORE_LOCATON = FLASH; +// config.IP= net.ip; +// config.FLASH_SIZE = 1024; +// config.FLASH_FILES = 10; +// config.STORE_BRAKER = true; +// config.BRAKERS[0].Enable = true; +// config.BRAKERS[0].SensPort = 1; +// config.BRAKERS[0].DropPort = 2; +// config.BRAKERS[0].Relay = 1; +// config.BRAKERS[0].Timeout = 300; +// config_write(config); + + d(""); + d("Запуск системы"); + d(""); + d(config.IP); + + d("sys.version: "+sys.version); +// if (left(sys.serialnum,1)==chr(255)){ +// sys.setserialnum("TEST SERIAL NUMBER SETTING 0123456789-abcdefghijklmnopqrstuvwxyz"); +// } + d("sys.serialnum: "+sys.serialnum); + d(PLATFORM ); + d("fd.availableflashspace / fd.totalsize: "+str(fd.availableflashspace)+"/"+str(fd.totalsize)); + d("sys.totalbuffpages: "+ str(sys.totalbuffpages)); + d("sys.freebuffpages: "+str(sys.freebuffpages)); + + d("config.IP="+config.IP); +// if (rtc.running){ +// unsigned int day,min; +// unsigned char second; +// rtc.getdata(day,min,second); +// if (day == 0) +// rtc.setdata(daycount(18,10,25),mincount(19,58),1); +// } + sys.onsystimerperiod = 1 ; + net_start(); + + 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; + pat.play("G-R-R---~",PL_PAT_CANINT); +} + + + +void on_sys_timer() +{ + + for (int i=1; i<=3; i++){ + braker_proc(i); + } + // sensors process + for (int i=1; i <= SENSORS; i++){ + if (pinChange(i)){ + bool state = getPinStateS(i,true); + //if (getEncEnabled(i,state)) + send_sensor(i,state); + } + } + + +#if PLATFORM_ID != SIMULATOR + sock.num = 0; + io.num=PL_IO_NUM_47; + io.lineset(PL_IO_NUM_48,sock.statesimple != PL_SSTS_EST); + io.state=LOW; + io.state=HIGH; + sock.num = 1; + io.lineset(PL_IO_NUM_48,sock.statesimple != PL_SSTS_EST); + io.state=LOW; + io.state=HIGH; + sock.num = 2; + io.lineset(PL_IO_NUM_48,sock.statesimple != PL_SSTS_EST); + io.state=LOW; + io.state=HIGH; + sock.num = 3; + io.lineset(PL_IO_NUM_48,sock.statesimple != PL_SSTS_EST); + io.state=LOW; + io.state=HIGH; + io.lineset(PL_IO_NUM_48,HIGH); + io.state=LOW; + io.state=HIGH; +#endif +} \ No newline at end of file diff --git a/net.tbs b/net.tbs new file mode 100644 index 0000000..e69de29 diff --git a/net.tc b/net.tc new file mode 100644 index 0000000..e12692e --- /dev/null +++ b/net.tc @@ -0,0 +1,143 @@ +#include "net.th" +#include "relay.th" +#include "config_work.th" +#include "braker.th" + +int num_sdf = 0; +#define IP_STR "IP=" + +string buff[4]; + +void (*net_handlers[4])(string); + +void net_openSocket(unsigned char socket,unsigned int port, bool cmd, void (*handler)(string)){ + + d("Сеть ["+str(socket)+"]: Открытие "+ (cmd? "коммандного ":"") +"порта #"+str(port)); + sock.num = socket; + //net_handlers[socket] = handler; + sock.protocol = PL_SOCK_PROTOCOL_TCP; + sock.localportlist = port; + sock.rxbuffrq(1); + sock.txbuffrq(1); + if (cmd){ + sock.cmdbuffrq(2); + sock.rplbuffrq(2); + sock.tx2buffrq(5); + sock.inbandcommands= YES; + sock.escchar = '@'; + sock.endchar = '&'; + } + sys.buffalloc(); + sock.reconmode = PL_SOCK_RECONMODE_1; + sock.inconmode = PL_SOCK_INCONMODE_ANY_IP_ANY_PORT; + +} +void net_init(){ + +// string ip = get_parameter("IP"); + net.ip=validate_id(config.IP); + d("Network initialized"); +} + +void net_start(){ + + net_openSocket(0,1000,false);//, &net_sensor_handle); // sensors + net_openSocket(1,999,false);//, &net_relay_handle); // relay + net_openSocket(2,998,false);//, &net_diag_handle); // diag + net_openSocket(3,997,true);//, &net_config_handle); // config +} + +string validate_id(string ip){ + unsigned char x; + string _ip = ddval(ip); + + // first number can't be 0 or >223 + x = asc(left(_ip,1)); + if (x==0 || x>223){ + insert(_ip,1,chr(1)); + } + + // last number can't 0 or 255 + x = asc(right(_ip,1)); + if (x == 0 || x == 255){ + insert(_ip,4,chr(1)); + } + return ddstr(_ip); +} + +void add_buff(int num, string message){ + buff[num]+= message; +} + +string get_line(int num){ + int pos; + string line; + pos = instr(1,buff[num],chr(13)+chr(10),1); + if (pos>0){ + line = left(buff[num],pos-1); + buff[num] = right(buff[num],(len(buff[num])-pos)-1); + }else + line = ""; + return line; +} + +string relay_buff; +void net_relay_handle(string message){ + add_buff(0, message); + string line = get_line(0); + while (len(line)>0){ + string relay; + if (len(line)==3) + relay = left(line,2); + else + relay = left(line,1); + int state = val(right(line,1)); + if (relay=="G" || relay == "B") + braker_set(line); + else + relay_set(val(relay),state>0); + line = get_line(0); + } + + +} + +void net_diag_handle(string message){ + +} + +void net_config_handle(string message){ + +} + +void net_sensor_handle(string message){ + +} + +void send_sensor(int number, bool state){ + sock.num = 0; + if (sock.statesimple == PL_SSTS_EST){ + sock.setdata(str(number)+(state?"1":"0")+chr(13)+chr(10)); + sock.send(); + } +} +void on_sock_data_arrival() +{ + switch (sock.num){ + case 0: + net_sensor_handle(sock.getdata(sock.rxlen)); + break; + case 1: + net_relay_handle(sock.getdata(sock.rxlen)); + break; + case 2: + net_diag_handle(sock.getdata(sock.rxlen)); + break; + case 3: + //net_config_handle(sock.getdata(sock.rxlen)); + net_relay_handle(sock.getdata(sock.rxlen)); + break; + default: + sock.getdata(sock.rxlen); + } +} \ No newline at end of file diff --git a/net.th b/net.th new file mode 100644 index 0000000..6deb665 --- /dev/null +++ b/net.th @@ -0,0 +1,9 @@ +void net_init(); +void net_start(); +string validate_id(string ip); +//void net_openSocket(unsigned char socket,unsigned int port, bool cmd, void *handler); +void net_relay_handle(string message); +void net_diag_handle(string message); +void net_config_handle(string message); +void net_sensor_handle(string message); +void send_sensor(int number, bool state); \ No newline at end of file diff --git a/relay.tc b/relay.tc new file mode 100644 index 0000000..cc08f4c --- /dev/null +++ b/relay.tc @@ -0,0 +1,20 @@ +#include "global.th" + +void relay_init(){ +#if PLATFORM_ID != SIMULATOR + for (int i=1; i++; i< RELAYS){ + io.num = OUTPIN[i-1]; + io.enabled = true; + } + d("Relays initialized"); +#endif + +} + +void relay_set(int number, bool state) +{ +#if PLATFORM_ID != SIMULATOR + io.lineset(OUTPIN[number-1],!state ); +#endif + d("Relay "+str(number)+" is "+ (state? "ON":"OFF")); +} \ No newline at end of file diff --git a/relay.th b/relay.th new file mode 100644 index 0000000..be5408f --- /dev/null +++ b/relay.th @@ -0,0 +1,2 @@ +void relay_init(); +void relay_set(int number, bool state); \ No newline at end of file diff --git a/sdf.txt b/sdf.txt new file mode 100644 index 0000000..cc115a8 --- /dev/null +++ b/sdf.txt @@ -0,0 +1,18 @@ +# settings + +# default IP address +IP=10.0.50.232 + +# Location for store configuration. Can be FLASH or EEPROM +STORE_LOCATON=FLASH + +# Flash drive size for storing settings in sectors. total size = (FLASH_SIZE * 264) bytes +# For auto upgrade software FLASH_SIZE must be more (firmware size + application size)/264+50 +# example: 229376+14336 = 243712 / 264 = 924 + 50 = FLASH_SIZE = 1064 +FLASH_SIZE=50 + +# Maximum number of files +FLASH_FILES=10 + +# Store brakers configuration in memory +STORE_BRAKER=1 diff --git a/sensors.tc b/sensors.tc new file mode 100644 index 0000000..3d15ff7 --- /dev/null +++ b/sensors.tc @@ -0,0 +1,38 @@ +#include "global.th" + +bool pinState[SENSORS] = {false}; + +void sensor_init(){ +#if PLATFORM_ID != SIMULATOR + for (int i=1; i++; i< SENSORS){ + io.num = INPIN[i-1]; + io.enabled = false; + } + d("Sensors initialized"); +#endif +} + +bool getPinStateS(unsigned char pinNumber, bool save){ + bool state = false; +#if PLATFORM_ID != SIMULATOR + io.num = INPIN[pinNumber-1]; + state = 1 - io.state; + if (save) pinState[pinNumber-1] = state; +#endif + return state; +} +bool getPinState(unsigned char pinNumber) +{ + return getPinStateS(pinNumber,false); +} + +bool pinChange(unsigned char pinNumber){ + bool state = false; +#if PLATFORM_ID != SIMULATOR +// state = YES - io.lineget(INPIN[pinNumber-1]); + io.num = INPIN[pinNumber-1]; + io.enabled = NO; + state = 1 - io.state; +#endif + return pinState[pinNumber-1]!=state; +} \ No newline at end of file diff --git a/sensors.th b/sensors.th new file mode 100644 index 0000000..0d5a5fd --- /dev/null +++ b/sensors.th @@ -0,0 +1,5 @@ +void sensor_init(); +bool getPinStateS(unsigned char pinNumber, bool save); +bool getPinState(unsigned char pinNumber); +bool pinChange(unsigned char pinNumber); + diff --git a/simulator.tc b/simulator.tc new file mode 100644 index 0000000..e69de29 diff --git a/simulator.th b/simulator.th new file mode 100644 index 0000000..2872702 --- /dev/null +++ b/simulator.th @@ -0,0 +1,27 @@ +#ifndef PLATFORM + +#message "Platform SIMULATOR" + +#define PLATFORM "SIMULATOR" +#define SENSORS 8 +#define RELAYS 6 + enum INPIN { + S1 = 9, + S2 = 8, + S3 = 0, + S4 = 16, + S5 = 11, + S6 = 10, + S7 = 1, + S8 = 17 + }; + enum OUTPIN { + R1 = 13, + R2 = 12, + R3 = 15, + R4 = 14, + R5 = 32, + R6 = 33 + }; + +#endif \ No newline at end of file diff --git a/tpp2.th b/tpp2.th new file mode 100644 index 0000000..c0ea1f4 --- /dev/null +++ b/tpp2.th @@ -0,0 +1,23 @@ +#message "link TPP2" + +#ifndef global + #message "link global not defined" +#endif + +#ifndef PLATFORM + + #message "Platform TPP2" + + #define PLATFORM "TPP2" + #define SENSORS 8 + #define RELAYS 6 + + static unsigned char INPIN[SENSORS] = { + 9, 8, 0, 16, 11, 10, 1, 17 + }; + + static unsigned char OUTPIN[RELAYS] = { + 13, 12, 15, 14, 32, 33 + }; + +#endif \ No newline at end of file diff --git a/tpp3.th b/tpp3.th new file mode 100644 index 0000000..8397024 --- /dev/null +++ b/tpp3.th @@ -0,0 +1,18 @@ +#ifndef PLATFORM + +#message "Platform TPP3" + +#define PLATFORM "TPP3" +#define SENSORS 16 +#define RELAYS 16 + +static unsigned char INPIN[SENSORS] = { + 9, 8, 0, 16, 32, 33, 4, 20, + 11, 10, 1, 17, 34, 35, 5, 21 +}; +static unsigned char OUTPIN[RELAYS] = { + 13, 12, 36, 37, 15, 14, 38, 39, + 24, 25, 28, 29, 40, 41, 54, 55 +}; + +#endif \ No newline at end of file