From 16a7626183a6045f9068c8d5ca9f125b3454c8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= Date: Sat, 20 May 2017 11:57:59 +0200 Subject: [PATCH] Handle dassl better in opc-ua --- .../c/simulation/solver/embedded_server.c | 9 +++++---- .../c/simulation/solver/embedded_server.h | 4 ++-- .../c/simulation/solver/perform_simulation.c | 14 ++++++++++---- .../c/simulation/solver/solver_main.c | 2 +- SimulationRuntime/opc/ua/omc_opc_ua.c | 17 ++++++++--------- SimulationRuntime/opc/ua/omc_opc_ua.h | 4 ++-- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/SimulationRuntime/c/simulation/solver/embedded_server.c b/SimulationRuntime/c/simulation/solver/embedded_server.c index 4287c40a0e8..0cbe65ce377 100644 --- a/SimulationRuntime/c/simulation/solver/embedded_server.c +++ b/SimulationRuntime/c/simulation/solver/embedded_server.c @@ -42,7 +42,7 @@ #define DLL_EXT ".so" #endif -void* no_embedded_server_init(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep) +void* no_embedded_server_init(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port) { return NULL; } @@ -51,15 +51,16 @@ void no_embedded_server_deinit(void *handle) { } -void no_embedded_server_update(void *handle, double tout) +int no_embedded_server_update(void *handle, double tout) { + return 0; } -void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep) = no_embedded_server_init; +void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port) = no_embedded_server_init; void (*embedded_server_deinit)(void*) = no_embedded_server_deinit; // Tells the embedded server that a simulation step has passed; the server // can read/write values from/to the simulator -void (*embedded_server_update)(void*, double tout) = no_embedded_server_update; +int (*embedded_server_update)(void*, double tout) = no_embedded_server_update; void* embedded_server_load_functions(const char *server_name) { diff --git a/SimulationRuntime/c/simulation/solver/embedded_server.h b/SimulationRuntime/c/simulation/solver/embedded_server.h index 9ab4bebdfc8..f8385492b7d 100644 --- a/SimulationRuntime/c/simulation/solver/embedded_server.h +++ b/SimulationRuntime/c/simulation/solver/embedded_server.h @@ -37,12 +37,12 @@ extern "C" { #endif -extern void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep); +extern void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port); extern void (*embedded_server_deinit)(void *handle); /* Tells the embedded server that a simulation step has passed; the server * can read/write values from/to the simulator */ -extern void (*embedded_server_update)(void *handle, double tout); +extern int (*embedded_server_update)(void *handle, double tout); /* Give the filename or generic name to use for loading an embedded server */ extern void* embedded_server_load_functions(const char *name); extern void embedded_server_unload_functions(void *dllHandle); diff --git a/SimulationRuntime/c/simulation/solver/perform_simulation.c b/SimulationRuntime/c/simulation/solver/perform_simulation.c index 46b88737b8e..368e1a5f9d1 100644 --- a/SimulationRuntime/c/simulation/solver/perform_simulation.c +++ b/SimulationRuntime/c/simulation/solver/perform_simulation.c @@ -183,7 +183,7 @@ static void fmtInit(DATA* data, MEASURE_TIME* mt) } } -static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, int didEventStep) +static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, SOLVER_INFO* solverInfo) { if(mt->fmtReal) { @@ -218,11 +218,17 @@ static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, } /* prevent emit if noEventEmit flag is used, if it's an event */ - if ((omc_flag[FLAG_NOEVENTEMIT] && didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT]) { + if ((omc_flag[FLAG_NOEVENTEMIT] && solverInfo->didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT]) { sim_result.emit(&sim_result, data, threadData); } #if !defined(OMC_MINIMAL_RUNTIME) - embedded_server_update(data->embeddedServerState, data->localData[0]->timeValue); + if (embedded_server_update(data->embeddedServerState, data->localData[0]->timeValue)) { + solverInfo->didEventStep = 1; + overwriteOldSimulationData(data); + storePreValues(data); // Maybe?? + storeOldValues(data); // Maybe?? + sim_result.emit(&sim_result, data, threadData); + } if (data->real_time_sync.enabled) { double time = data->localData[0]->timeValue; int64_t res = rt_ext_tp_sync_nanosec(&data->real_time_sync.clock, (uint64_t) (data->real_time_sync.scaling*(time-data->real_time_sync.time)*1e9)); @@ -455,7 +461,7 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_ syncStep = simulationUpdate(data, threadData, solverInfo); retry = 0; /* reset retry */ - fmtEmitStep(data, threadData, &fmt, solverInfo->didEventStep); + fmtEmitStep(data, threadData, &fmt, solverInfo); saveIntegratorStats(solverInfo); checkSimulationTerminated(data, solverInfo); diff --git a/SimulationRuntime/c/simulation/solver/solver_main.c b/SimulationRuntime/c/simulation/solver/solver_main.c index 9b109181191..092a3b4db01 100644 --- a/SimulationRuntime/c/simulation/solver/solver_main.c +++ b/SimulationRuntime/c/simulation/solver/solver_main.c @@ -705,7 +705,7 @@ int solver_main(DATA* data, threadData_t *threadData, const char* init_initMetho } } } - data->embeddedServerState = embedded_server_init(data, data->localData[0]->timeValue, solverInfo.currentStepSize, argv_0, omc_real_time_sync_update, port, &solverInfo.didEventStep); + data->embeddedServerState = embedded_server_init(data, data->localData[0]->timeValue, solverInfo.currentStepSize, argv_0, omc_real_time_sync_update, port); #endif if(0 == retVal) { retVal = -1; diff --git a/SimulationRuntime/opc/ua/omc_opc_ua.c b/SimulationRuntime/opc/ua/omc_opc_ua.c index 150062ced6c..808ee0192b6 100644 --- a/SimulationRuntime/opc/ua/omc_opc_ua.c +++ b/SimulationRuntime/opc/ua/omc_opc_ua.c @@ -58,7 +58,6 @@ typedef struct { int *boolValsInputIndex; int reinitStateFlag; int *stateWasUpdatedFlag; - int *didEventStep; double *updatedStates; double real_time_sync_scaling; void (*omc_real_time_sync_update)(DATA *data, double scaling); @@ -449,7 +448,7 @@ static inline omc_opc_ua_state* addAliasVars(omc_opc_ua_state *state, var_kind_t } return state; } -void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep) +void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port) { MODEL_DATA *modelData = data->modelData; omc_opc_ua_state *state = (omc_opc_ua_state*) malloc(sizeof(omc_opc_ua_state)); @@ -467,7 +466,6 @@ void* omc_embedded_server_init(DATA *data, double t, double step, const char *ar state->server_running = 1; state->time = t; state->omc_real_time_sync_update = omc_real_time_sync_update; - state->didEventStep = didEventStep; pthread_cond_init(&state->cond_pause, NULL); pthread_mutex_init(&state->mutex_pause, NULL); @@ -620,13 +618,15 @@ void omc_embedded_server_deinit(void *state_vp) free(state); } -void omc_embedded_server_update(void *state_vp, double t) +int omc_embedded_server_update(void *state_vp, double t) { omc_opc_ua_state *state = (omc_opc_ua_state*) state_vp; - int i, realIndex=0, boolIndex=0; + int i, realIndex=0, boolIndex=0, res=0; DATA *data = state->data; MODEL_DATA *modelData = data->modelData; + waitForStep(state); + pthread_rwlock_wrlock(&state->rwlock); state->time = t; @@ -639,12 +639,12 @@ void omc_embedded_server_update(void *state_vp, double t) } if (state->gotNewInput) { - *state->didEventStep = 1; /* Trigger an event in the solver, restarting it */ + res = 1; /* Trigger an event in the solver, restarting it */ memcpy(data->simulationInfo->inputVars, state->inputVarsBackup, modelData->nInputVars * sizeof(double)); } if (state->reinitStateFlag) { - *state->didEventStep = 1; /* Trigger an event in the solver, restarting it */ + res = 1; /* Trigger an event in the solver, restarting it */ for (i = 0; i < modelData->nStates; i++) { if (state->stateWasUpdatedFlag[i]) { state->stateWasUpdatedFlag[i] = 0; @@ -654,6 +654,5 @@ void omc_embedded_server_update(void *state_vp, double t) } pthread_rwlock_unlock(&state->rwlock); - - waitForStep(state); + return res; } diff --git a/SimulationRuntime/opc/ua/omc_opc_ua.h b/SimulationRuntime/opc/ua/omc_opc_ua.h index e571d0e1831..9b162b5d171 100644 --- a/SimulationRuntime/opc/ua/omc_opc_ua.h +++ b/SimulationRuntime/opc/ua/omc_opc_ua.h @@ -47,9 +47,9 @@ #endif #endif -OPC_UA_EXPORT void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep); +OPC_UA_EXPORT void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port); OPC_UA_EXPORT void omc_embedded_server_deinit(void*); -OPC_UA_EXPORT void omc_embedded_server_update(void*, double t); +OPC_UA_EXPORT int omc_embedded_server_update(void*, double t); #define OMC_OPC_NODEID_STEP 10000 #define OMC_OPC_NODEID_RUN 10001