Created
April 28, 2022 13:00
-
-
Save tasosxak/d3cdb95eb1c73ae0047621d818a83d0a to your computer and use it in GitHub Desktop.
FMI Master with runtime monitor in C++ (DEPLOY_TEMPLATE.cpp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <dlfcn.h> | |
#include <string> | |
#include <cstring> | |
#include <fmi2Functions.h> | |
#include <fmi2FunctionTypes.h> | |
#include <iostream> | |
#include <jni.h> | |
using namespace std; | |
int main(int argc, char **argv) { | |
/* Create JVM instance in order to load scala code */ | |
JavaVM *jvm; /* denotes a Java VM */ | |
JNIEnv *env; //<!-- same as yours | |
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */ | |
JavaVMOption* options = new JavaVMOption[1]; | |
options[0].optionString = "-Djava.class.path=/usr/lib/java:/home/siemens/dejavu/dir/dejavu.jar:/usr/share/scala-2.11/lib/scala-library.jar :/home/siemens/dejavu/out/examples/speedlimit/TraceMonitor.jar"; | |
vm_args.version = JNI_VERSION_1_6; | |
vm_args.nOptions = 1; | |
vm_args.options = options; | |
vm_args.ignoreUnrecognized = false; | |
/* load and initialize a Java VM, return a JNI interface | |
* pointer in env */ | |
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); | |
// First get the class that contains the method you need to call | |
jclass tracemonitor_class = env->FindClass("TraceMonitor"); | |
// Get the method that you want to call | |
jmethodID main_method = env->GetStaticMethodID(tracemonitor_class, "eval", "(Ljava/lang/String;)Z"); | |
int ret = EXIT_SUCCESS; | |
fmi2Real tstart = 0.0; | |
fmi2Real tcur = tstart; | |
fmi2Real hstep = 0.2; | |
fmi2Real tend = 7.0; | |
void *handle; | |
fmi2InstantiateTYPE* (fmi2Instantiate); | |
fmi2EnterInitializationModeTYPE* (fmi2EnterInitializationMode); | |
fmi2ExitInitializationModeTYPE* (fmi2ExitInitializationMode); | |
fmi2SetRealTYPE* (fmi2SetReal); | |
fmi2GetRealTYPE* (fmi2GetReal); | |
fmi2DoStepTYPE* (fmi2DoStep); | |
void (*set_inverted_map)(int,string); | |
char *error; | |
handle = dlopen ("bip_example/binaries/linux64/bip_example.so", RTLD_LAZY); | |
if (!handle) { | |
fprintf (stderr, "%s\n", dlerror()); | |
exit(1); | |
} | |
dlerror(); /* Clear any existing error */ | |
fmi2Instantiate = (fmi2InstantiateTYPE*) dlsym(handle, "fmi2Instantiate"); | |
fmi2EnterInitializationMode = (fmi2EnterInitializationModeTYPE*) dlsym(handle, "fmi2EnterInitializationMode"); | |
fmi2ExitInitializationMode = (fmi2ExitInitializationModeTYPE*) dlsym(handle, "fmi2ExitInitializationMode"); | |
fmi2SetReal = (fmi2SetRealTYPE*) dlsym(handle, "fmi2SetReal"); | |
fmi2GetReal = (fmi2GetRealTYPE*) dlsym(handle, "fmi2GetReal"); | |
fmi2DoStep = (fmi2DoStepTYPE*) dlsym(handle, "fmi2DoStep"); | |
set_inverted_map = (void (*)(int, std::string)) dlsym(handle, "set_inverted_map"); | |
if ((error = dlerror()) != NULL) { | |
fprintf (stderr, "%s\n", error); | |
exit(1); | |
} | |
cout<<"CALL fmi2Instantiate...\n"; | |
fmi2Component c = (*fmi2Instantiate)("prModel", fmi2CoSimulation , "prModel", "resource/location", NULL, true, true); | |
(*set_inverted_map)(100, "ROOT.environment._id__inputIndex"); | |
//(*set_inverted_map)(101, "ROOT.environment._id__time"); | |
(*set_inverted_map)(102, "ROOT.camera._id__inputIndex"); | |
(*set_inverted_map)(103, "ROOT.perception._id__inputIndex"); | |
(*set_inverted_map)(104, "ROOT.perception._id__result"); | |
(*set_inverted_map)(105, "ROOT.controller._id__speed"); | |
(*set_inverted_map)(106, "ROOT.controller._id__speedLimit"); | |
(*set_inverted_map)(107, "ROOT.controller._id__deltaSpeed"); | |
(*set_inverted_map)(108, "ROOT.controller._id__zero"); | |
(*set_inverted_map)(109, "ROOT.brake._id__deltaSpeed"); | |
(*set_inverted_map)(110, "ROOT.throttle._id__deltaSpeed"); | |
(*set_inverted_map)(111, "ROOT.speedSensor._id__speed"); | |
(*set_inverted_map)(112, "ROOT.speedSensor._id__deltaSpeed"); | |
const unsigned int vr[] = {100,/*101,*/ 102,103,104,105,106,107,108,109,110,111,112}; | |
fmi2Real values[] = {1,/*10,*/ 0,0,0,0,0,0,0,0,0,100,0}; | |
cout<<"CALL fmi2SetReal...\n"; | |
(*fmi2SetReal)(c, vr, 12, values); | |
cout<<"CALL fmi2EnterInitializationMode...\n"; | |
(*fmi2EnterInitializationMode)(c); | |
cout<<"CALL fmi2ExitInitializationMode...\n"; | |
(*fmi2ExitInitializationMode)(c); | |
tcur = tstart; | |
while(tcur<tend){ | |
fmi2Boolean newStep = fmi2True; | |
fmi2Real val[12]; | |
cout<<"tcur: "<<tcur<<'\n'; | |
//cout<<"CALL fmi2DoStep...\n"; | |
fmi2DoStep(c, tcur,hstep, newStep); | |
//cout<<"CALL fmi2GetReal...\n"; | |
fmi2GetReal(c, vr, 12, val); | |
cout<<"Speed:"<<val[4]<<endl; | |
cout<<"Speed Limit:"<<val[5]<<endl; | |
tcur += hstep; | |
/* SEND EVENT TO RUNTIME MONITOR */ | |
string nativeString; | |
nativeString+="speed,"; | |
nativeString+=to_string((int)(val[5]-val[4])); | |
/* call scala function 'eval' for the evaluation of the property */ | |
env->CallStaticBooleanMethod(tracemonitor_class, main_method, env->NewStringUTF(nativeString.c_str())); | |
} | |
cout<<"Simulation finished.\n"; | |
dlclose(handle); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I defined a property for the vehicle in order to keep the speed lower or equal than speed limit of the current traffic sign.
The log of the Master + Runtime monitor: