forked from DigitalDynamicsLab/fmu_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmu_host.cpp
147 lines (106 loc) · 4.59 KB
/
fmu_host.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// A very simple example that can be used as template project for
// a Chrono::Engine simulator with 3D view.
// =============================================================================
#include "FmuToolsImport.hpp"
#include <iostream>
std::string unzipped_fmu_folder = FMU_UNPACK_DIRECTORY;
//std::string unzipped_fmu_folder = FMU_MAIN_DIRECTORY; // for debug
int main(int argc, char* argv[]) {
FmuUnit my_fmu;
try {
//my_fmu.LoadUnzipped(unzipped_fmu_folder);
my_fmu.Load(FMU_FILENAME, FMU_UNPACK_DIRECTORY); // make sure the user has appropriate privileges to remove/create FMU_UNPACK_DIRECTORY
//my_fmu.Load(FMU_FILENAME); // will go in TEMP/_fmu_temp
my_fmu.BuildVariablesTree();
my_fmu.BuildVisualizersList(&my_fmu.tree_variables);
}catch (std::exception& my_exception) {
std::cout << "ERROR loading FMU: " << my_exception.what() << "\n";
}
std::cout << "FMU version: " << my_fmu._fmi2GetVersion() << "\n";
std::cout << "FMU platform: " << my_fmu._fmi2GetTypesPlatform() << "\n";
my_fmu.Instantiate("FmuComponent");
std::vector<std::string> categoriesVector = {"logAll"};
std::vector<const char*> categoriesArray;
for (const auto& category : categoriesVector) {
categoriesArray.push_back(category.c_str());
}
my_fmu._fmi2SetDebugLogging(my_fmu.component, fmi2True, categoriesVector.size(), categoriesArray.data());
double start_time = 0;
double stop_time = 2;
my_fmu._fmi2SetupExperiment(my_fmu.component,
fmi2False, // tolerance defined
0.0, // tolerance
start_time,
fmi2False, // use stop time
stop_time);
my_fmu._fmi2EnterInitializationMode(my_fmu.component);
//// play a bit with set/get:
//fmi2String m_str;
//unsigned int sref = 1;
//my_fmu._fmi2GetString(my_fmu.component, &sref, 1, &m_str);
//std::cout << "FMU variable 1 has value: " << m_str << "\n";
{
fmi2ValueReference valref = 8;
fmi2Real m_in = 15;
my_fmu._fmi2SetReal(my_fmu.component, &valref, 1, &m_in);
fmi2Real m_out;
my_fmu._fmi2GetReal(my_fmu.component, &valref, 1, &m_out);
std::cout << "m_out_: " << m_out << std::endl;
}
my_fmu._fmi2ExitInitializationMode(my_fmu.component);
// test a simulation loop:
double time = 0;
double dt = 0.001;
for (int i = 0; i<1000; ++i) {
my_fmu._fmi2DoStep(my_fmu.component, time, dt, fmi2True);
time +=dt;
}
fmi2Real val_real;
for (fmi2ValueReference valref = 1; valref<12; valref++){
my_fmu._fmi2GetReal(my_fmu.component, &valref, 1, &val_real);
std::cout << "REAL: valref: " << valref << " | val: " << val_real << std::endl;
}
fmi2Boolean val_bool;
for (fmi2ValueReference valref = 1; valref<2; valref++){
my_fmu._fmi2GetBoolean(my_fmu.component, &valref, 1, &val_bool);
std::cout << "BOOLEAN: valref: " << valref << " | val: " << val_bool << std::endl;
}
// Just some dumps for checking:
/*
//my_fmu.DumpTree(&my_fmu.tree_variables,0); // dump all tree
my_fmu.DumpTree(&my_fmu.tree_variables.children["body1"],0); // dump only one subtree
*/
/*
for (auto& i : my_fmu.visualizers) {
std::cout << "Visualizer: \n"
<< " r1 = " << i.pos_references[0] << "\n"
<< " r2 = " << i.pos_references[1] << "\n"
<< " r3 = " << i.pos_references[2] << "\n";
}
*/
/*
for (auto i : my_fmu.scalarVariables) {
std::cout << "Variable: \n"
<< " name = " << i.second.name << "\n"
<< " ref. = " << i.second.valueReference << "\n";
// << " desc. = " << i.second.description << "\n"
// << " init. = " << i.second.initial << "\n"
// << " caus. = " << i.second.causality << "\n"
// << " varb. = " << i.second.variability << "\n";
}
*/
//======================================================================
std::cout << "\n\n\n";
return 0;
}