Commit 219de194 authored by Claes Sjofors's avatar Claes Sjofors

MPC MLP controller fix

parent 59b5832c
...@@ -39,8 +39,10 @@ ...@@ -39,8 +39,10 @@
#include "co_math.h" #include "co_math.h"
#include "co_time.h" #include "co_time.h"
#include "co_cdh.h"
#include "co_dcli.h" #include "co_dcli.h"
#include "rt_plc_bcomp.h" #include "rt_plc_bcomp.h"
#include "rt_plc_msg.h"
/*_* /*_*
RunTimeCounterFo RunTimeCounterFo
...@@ -1507,13 +1509,12 @@ int mpc_mlp_import(const char *file, mlp_sCtx *mlp) ...@@ -1507,13 +1509,12 @@ int mpc_mlp_import(const char *file, mlp_sCtx *mlp)
dcli_translate_filename(fname, file); dcli_translate_filename(fname, file);
fp = fopen(fname, "r"); fp = fopen(fname, "r");
if (!fp) if (!fp)
return 0; return PLC__FILE;
while (dcli_read_line(line, sizeof(line), fp)) { while (dcli_read_line(line, sizeof(line), fp)) {
if (strncmp(line, "Layers ", 7) == 0) { if (strncmp(line, "Layers ", 7) == 0) {
if (sscanf(&line[7], "%d", &mlp->layers) != 1) { if (sscanf(&line[7], "%d", &mlp->layers) != 1) {
printf("Syntax error\n"); return PLC__FILESYNTAX;
return 0;
} }
} }
else if (strncmp(line, "LayerSizes ", 11) == 0) { else if (strncmp(line, "LayerSizes ", 11) == 0) {
...@@ -1522,13 +1523,11 @@ int mpc_mlp_import(const char *file, mlp_sCtx *mlp) ...@@ -1522,13 +1523,11 @@ int mpc_mlp_import(const char *file, mlp_sCtx *mlp)
for (i = 0; i < mlp->layers; i++) { for (i = 0; i < mlp->layers; i++) {
s = strchr(s, ' '); s = strchr(s, ' ');
if (!s) { if (!s) {
printf("Syntax error\n"); return PLC__FILESYNTAX;
return 0;
} }
s++; s++;
if (sscanf(s, "%d", &mlp->layer_sizes[i]) != 1) { if (sscanf(s, "%d", &mlp->layer_sizes[i]) != 1) {
printf("Syntax error\n"); return PLC__FILESYNTAX;
return 0;
} }
} }
} }
...@@ -1593,7 +1592,7 @@ int mpc_mlp_import(const char *file, mlp_sCtx *mlp) ...@@ -1593,7 +1592,7 @@ int mpc_mlp_import(const char *file, mlp_sCtx *mlp)
for (i = 0; i < mlp->layers - 1; i++) { for (i = 0; i < mlp->layers - 1; i++) {
mlp->h[i] = (double *)calloc(mlp->layer_sizes[i+1], sizeof(double)); mlp->h[i] = (double *)calloc(mlp->layer_sizes[i+1], sizeof(double));
} }
return 1; return PLC__SUCCESS;
} }
float mpc_mlpacc_model(plc_sThread* tp, pwr_sClass_CompMPC_MLP_Fo *o, float mpc_mlpacc_model(plc_sThread* tp, pwr_sClass_CompMPC_MLP_Fo *o,
...@@ -1776,13 +1775,32 @@ void CompMPC_MLP_Fo_init(pwr_sClass_CompMPC_MLP_Fo* o) ...@@ -1776,13 +1775,32 @@ void CompMPC_MLP_Fo_init(pwr_sClass_CompMPC_MLP_Fo* o)
o->ModelP = (mlp_tCtx)calloc(1, sizeof(mlp_sCtx)); o->ModelP = (mlp_tCtx)calloc(1, sizeof(mlp_sCtx));
co->Status = mpc_mlp_import(co->ModelFile, (mlp_tCtx)o->ModelP); co->Status = mpc_mlp_import(co->ModelFile, (mlp_tCtx)o->ModelP);
if (EVEN(co->Status)) if (EVEN(co->Status)) {
char astr[200];
cdh_ArefToString(astr, sizeof(astr), &o->PlcConnect, 1);
errh_Error("CompMPC_MLP initialization error, %s, %m", astr, co->Status);
return; return;
}
co->Layers = ((mlp_tCtx)o->ModelP)->layers; co->Layers = ((mlp_tCtx)o->ModelP)->layers;
for (i = 0; i < MIN(co->Layers, sizeof(co->LayerSizes)/sizeof(co->LayerSizes[0])); i++) for (i = 0; i < MIN(co->Layers, sizeof(co->LayerSizes)/sizeof(co->LayerSizes[0])); i++)
co->LayerSizes[i] = ((mlp_tCtx)o->ModelP)->layer_sizes[i]; co->LayerSizes[i] = ((mlp_tCtx)o->ModelP)->layer_sizes[i];
switch (((mlp_tCtx)o->ModelP)->activation) {
case mlp_eActivation_Tanh:
strcpy(co->Activation, "tanh");
break;
case mlp_eActivation_Identity:
strcpy(co->Activation, "identity");
break;
case mlp_eActivation_Relu:
strcpy(co->Activation, "relu");
break;
case mlp_eActivation_Logistic:
strcpy(co->Activation, "logistic");
break;
default:
strcpy(co->Activation, "unknown");
}
((mlp_tCtx)o->ModelP)->inputs = (double *)calloc(((mlp_tCtx)o->ModelP)->layer_sizes[0], ((mlp_tCtx)o->ModelP)->inputs = (double *)calloc(((mlp_tCtx)o->ModelP)->layer_sizes[0],
sizeof(double)); sizeof(double));
} }
...@@ -1798,7 +1816,7 @@ void CompMPC_MLP_Fo_exec(plc_sThread* tp, pwr_sClass_CompMPC_MLP_Fo* o) ...@@ -1798,7 +1816,7 @@ void CompMPC_MLP_Fo_exec(plc_sThread* tp, pwr_sClass_CompMPC_MLP_Fo* o)
int size; int size;
pwr_sClass_CompMPC_MLP *co = (pwr_sClass_CompMPC_MLP *)o->PlcConnectP; pwr_sClass_CompMPC_MLP *co = (pwr_sClass_CompMPC_MLP *)o->PlcConnectP;
if (!co) if (!co || co->Status == PLC__FILE || co->Status == PLC__FILESYNTAX)
return; return;
if (((mlp_tCtx)o->ModelP)->outlist == 0) { if (((mlp_tCtx)o->ModelP)->outlist == 0) {
......
...@@ -64,5 +64,7 @@ void CompCurvePolValueFo_exec( ...@@ -64,5 +64,7 @@ void CompCurvePolValueFo_exec(
plc_sThread* tp, pwr_sClass_CompCurvePolValueFo* o); plc_sThread* tp, pwr_sClass_CompCurvePolValueFo* o);
void CompMPC_Fo_init(pwr_sClass_CompMPC_Fo* o); void CompMPC_Fo_init(pwr_sClass_CompMPC_Fo* o);
void CompMPC_Fo_exec(plc_sThread* tp, pwr_sClass_CompMPC_Fo* o); void CompMPC_Fo_exec(plc_sThread* tp, pwr_sClass_CompMPC_Fo* o);
void CompMPC_MLP_Fo_init(pwr_sClass_CompMPC_MLP_Fo* o);
void CompMPC_MLP_Fo_exec(plc_sThread* tp, pwr_sClass_CompMPC_MLP_Fo* o);
#endif #endif
...@@ -74,4 +74,6 @@ ioread <IO stalled, io read error> /fatal ...@@ -74,4 +74,6 @@ ioread <IO stalled, io read error> /fatal
iowrite <IO stalled, io write error> /fatal iowrite <IO stalled, io write error> /fatal
reduinit <Redundancy init error> /error reduinit <Redundancy init error> /error
reduconfig <Redundancy not configured> /error reduconfig <Redundancy not configured> /error
filesyntax <Syntax error in file> /error
file <No such file> /error
.end .end
...@@ -137,6 +137,7 @@ palette NavigatorPalette ...@@ -137,6 +137,7 @@ palette NavigatorPalette
class CompModeImc class CompModeImc
class CompModePID class CompModePID
class CompMPC class CompMPC
class CompMPC_MLP
class CompOnOffBurner class CompOnOffBurner
class CompOnOffZone class CompOnOffZone
class CompPosit class CompPosit
...@@ -1264,6 +1265,7 @@ palette PlcEditorPalette ...@@ -1264,6 +1265,7 @@ palette PlcEditorPalette
class CompModePID_Fo class CompModePID_Fo
class CompPID_Fo class CompPID_Fo
class CompMPC_Fo class CompMPC_Fo
class CompMPC_MLP_Fo
class CompModeImc_Fo class CompModeImc_Fo
class CompImc_Fo class CompImc_Fo
class CompOnOffBurnerFo class CompOnOffBurnerFo
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment