forked from esmini/esmini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesmini.cpp
More file actions
855 lines (751 loc) · 27.4 KB
/
Copy pathesmini.cpp
File metadata and controls
855 lines (751 loc) · 27.4 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//
// Copyright 2016 -- 2018 PMSF IT Consulting Pierre R. Mai
// Copyright 2023 BMW AG
// SPDX-License-Identifier: MPL-2.0
//
#include "esminiLib.hpp"
#include "osi_common.pb.h"
#include "osi_object.pb.h"
#include "osi_groundtruth.pb.h"
#include "osi_trafficupdate.pb.h"
#include "esmini.h"
/*
* Debug Breaks
*
* If you define DEBUG_BREAKS the FMU will automatically break
* into an attached Debugger on all major computation functions.
* Note that the FMU is likely to break all environments if no
* Debugger is actually attached when the breaks are triggered.
*/
#if defined(DEBUG_BREAKS) && !defined(NDEBUG)
#if defined(__has_builtin) && !defined(__ibmxl__)
#if __has_builtin(__builtin_debugtrap)
#define DEBUGBREAK() __builtin_debugtrap()
#elif __has_builtin(__debugbreak)
#define DEBUGBREAK() __debugbreak()
#endif
#endif
#if !defined(DEBUGBREAK)
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
#include <intrin.h>
#define DEBUGBREAK() __debugbreak()
#else
#include <signal.h>
#if defined(SIGTRAP)
#define DEBUGBREAK() raise(SIGTRAP)
#else
#define DEBUGBREAK() raise(SIGABRT)
#endif
#endif
#endif
#else
#define DEBUGBREAK()
#endif
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdint>
#include <cmath>
using namespace std;
#ifdef PRIVATE_LOG_PATH
ofstream COSMPDummySource::private_log_file;
#endif
/*
* ProtocolBuffer Accessors
*/
void* decode_integer_to_pointer(fmi2Integer hi, fmi2Integer lo)
{
#if PTRDIFF_MAX == INT64_MAX
union addrconv
{
struct
{
int lo;
int hi;
} base;
unsigned long long address;
} myaddr;
myaddr.base.lo = lo;
myaddr.base.hi = hi;
return reinterpret_cast<void*>(myaddr.address);
#elif PTRDIFF_MAX == INT32_MAX
return reinterpret_cast<void*>(lo);
#else
#error "Cannot determine 32bit or 64bit environment!"
#endif
}
void encode_pointer_to_integer(const void* ptr, fmi2Integer& hi, fmi2Integer& lo)
{
#if PTRDIFF_MAX == INT64_MAX
union addrconv
{
struct
{
int lo;
int hi;
} base;
unsigned long long address;
} myaddr;
myaddr.address = reinterpret_cast<unsigned long long>(ptr);
hi = myaddr.base.hi;
lo = myaddr.base.lo;
#elif PTRDIFF_MAX == INT32_MAX
hi = 0;
lo = reinterpret_cast<int>(ptr);
#else
#error "Cannot determine 32bit or 64bit environment!"
#endif
}
bool EsminiOsiSource::get_fmi_traffic_update_in(osi3::TrafficUpdate& data)
{
if (integer_vars[FMI_INTEGER_TRAFFICUPDATE_IN_SIZE_IDX] > 0)
{
void* buffer =
decode_integer_to_pointer(integer_vars[FMI_INTEGER_TRAFFICUPDATE_IN_BASEHI_IDX], integer_vars[FMI_INTEGER_TRAFFICUPDATE_IN_BASELO_IDX]);
normal_log("OSMP",
"Got %08X %08X, reading from %p ...",
integer_vars[FMI_INTEGER_TRAFFICUPDATE_IN_BASEHI_IDX],
integer_vars[FMI_INTEGER_TRAFFICUPDATE_IN_BASELO_IDX],
buffer);
data.ParseFromArray(buffer, integer_vars[FMI_INTEGER_TRAFFICUPDATE_IN_SIZE_IDX]);
return true;
}
else
{
return false;
}
}
void EsminiOsiSource::set_fmi_sensor_view_out(const osi3::SensorView& data)
{
data.SerializeToString(currentBufferSVOut);
encode_pointer_to_integer(currentBufferSVOut->data(),
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX]);
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_SIZE_IDX] = (fmi2Integer)currentBufferSVOut->length();
normal_log("OSMP",
"Providing %08X %08X, writing from %p ...",
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX],
currentBufferSVOut->data());
swap(currentBufferSVOut, lastBufferSVOut);
}
void EsminiOsiSource::reset_fmi_sensor_view_out()
{
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_SIZE_IDX] = 0;
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX] = 0;
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX] = 0;
}
void EsminiOsiSource::set_fmi_traffic_command_out(const osi3::TrafficCommand& data)
{
data.SerializeToString(currentBufferTCOut);
encode_pointer_to_integer(currentBufferTCOut->data(),
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_BASEHI_IDX],
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_BASELO_IDX]);
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_SIZE_IDX] = (fmi2Integer)currentBufferTCOut->length();
normal_log("OSMP",
"Providing %08X %08X, writing from %p ...",
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_BASEHI_IDX],
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_BASELO_IDX],
currentBufferTCOut->data());
swap(currentBufferTCOut, lastBufferTCOut);
}
void EsminiOsiSource::reset_fmi_traffic_command_out()
{
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_SIZE_IDX] = 0;
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_BASEHI_IDX] = 0;
integer_vars[FMI_INTEGER_TRAFFICCOMMAND_OUT_BASELO_IDX] = 0;
}
/*
* Actual Core Content
*/
fmi2Status EsminiOsiSource::doInit()
{
DEBUGBREAK();
/* Booleans */
for (int i = 0; i < FMI_BOOLEAN_VARS; i++)
boolean_vars[i] = fmi2False;
/* Integers */
for (int i = 0; i < FMI_INTEGER_VARS; i++)
integer_vars[i] = 0;
/* Reals */
for (int i = 0; i < FMI_REAL_VARS; i++)
real_vars[i] = 0.0;
/* Strings */
for (int i = 0; i < FMI_STRING_VARS; i++)
string_vars[i] = "";
return fmi2OK;
}
fmi2Status EsminiOsiSource::doStart(fmi2Boolean toleranceDefined,
fmi2Real tolerance,
fmi2Real startTime,
fmi2Boolean stopTimeDefined,
fmi2Real stopTime)
{
DEBUGBREAK();
fmiSlaveTerminated = false;
return fmi2OK;
}
fmi2Status EsminiOsiSource::doEnterInitializationMode()
{
DEBUGBREAK();
return fmi2OK;
}
fmi2Status EsminiOsiSource::doExitInitializationMode()
{
DEBUGBREAK();
std::string esmini_args = fmi_esmini_args();
if (esmini_args.empty())
{
const std::string xosc_path = fmi_xosc_path();
if (xosc_path.empty())
{
std::cerr << "No OpenScenario file selected!" << std::endl;
return fmi2Error;
}
if (SE_Init(xosc_path.c_str(), 0, fmi_use_viewer(), 0, 0) != 0)
{
std::cerr << "Failed to initialize the scenario" << std::endl;
return fmi2Error;
}
}
else
{
std::vector<std::string> args = {"esmini(lib)"};
size_t start = 0, end = 0;
while ((end = esmini_args.find(' ', start)) != size_t(-1))
{
args.push_back(esmini_args.substr(start, end - start));
start = end + 1;
}
args.push_back(esmini_args.substr(start).c_str());
int argc = static_cast<int>(args.size());
const char* argv[argc];
for (int i = 0; i < argc; i++)
{
argv[i] = args.at(i).c_str();
}
if (SE_InitWithArgs(argc, argv) != 0)
{
std::cerr << "Failed to initialize the scenario" << std::endl;
return fmi2Error;
}
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::doCalc(fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
{
DEBUGBREAK();
// Handle OSI TrafficUpdate input
osi3::TrafficUpdate traffic_update;
if (get_fmi_traffic_update_in(traffic_update))
{
for (const auto& obj : traffic_update.update())
{
const int obj_id = (int)obj.id().value();
SE_ScenarioObjectState vehicleState;
SE_GetObjectState(obj_id, &vehicleState);
if (obj.base().has_orientation())
{
vehicleState.h = (float)obj.base().orientation().yaw();
}
if (obj.base().has_position())
{
vehicleState.x =
obj.base().position().x() - vehicleState.centerOffsetX * cos(vehicleState.h) - vehicleState.centerOffsetY * sin(vehicleState.h);
vehicleState.y =
obj.base().position().y() - vehicleState.centerOffsetX * sin(vehicleState.h) - vehicleState.centerOffsetY * cos(vehicleState.h);
}
SE_ReportObjectPosXYH(obj_id, vehicleState.x, vehicleState.y, vehicleState.h);
if (obj.base().has_velocity())
{
SE_ReportObjectVel(obj_id, obj.base().velocity().x(), obj.base().velocity().y(), obj.base().velocity().z());
}
}
}
else
{
normal_log("OSMP", "No TrafficUpdate received.");
}
SE_SetOSIStaticReportMode(SE_OSIStaticReportMode::API);
// Run simulation step
if (SE_StepDT((float)communicationStepSize) != 0)
{
std::cerr << "Failed run simulation step" << std::endl;
return fmi2Error;
}
const auto* se_osi_ground_truth = reinterpret_cast<const osi3::GroundTruth*>(SE_GetOSIGroundTruthRaw()); // Fetch OSI struct
osi3::SensorView currentOut;
currentOut.Clear();
currentOut.mutable_sensor_id()->set_value(0);
currentOut.mutable_version()->CopyFrom(se_osi_ground_truth->version());
currentOut.mutable_host_vehicle_id()->set_value(se_osi_ground_truth->host_vehicle_id().value());
double const time = currentCommunicationPoint + communicationStepSize;
currentOut.mutable_timestamp()->set_seconds((long long int)floor(time));
const double sec_to_nanos = 1000000000.0;
currentOut.mutable_timestamp()->set_nanos((int)((time - floor(time)) * sec_to_nanos));
osi3::GroundTruth* currentGT = currentOut.mutable_global_ground_truth();
currentGT->CopyFrom(*se_osi_ground_truth);
set_fmi_sensor_view_out(currentOut);
// Handle OSI TrafficCommand output
const auto* traffic_command =
reinterpret_cast<const osi3::TrafficCommand*>(SE_GetOSITrafficCommandRaw()); // Fetch OSI struct (via pointer, no copying of data)
set_fmi_traffic_command_out(*traffic_command);
set_fmi_valid(1);
if (SE_GetQuitFlag() > 0) {
normal_log("OSMP", ("esmini terminated with flag " + std::to_string(SE_GetQuitFlag()) + ". Terminating agent!").c_str());
fmiSlaveTerminated = true;
return fmi2Discard;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::doTerm()
{
DEBUGBREAK();
return fmi2OK;
}
void EsminiOsiSource::doFree()
{
DEBUGBREAK();
}
/*
* Generic C++ Wrapper Code
*/
EsminiOsiSource::EsminiOsiSource(fmi2String theinstanceName,
fmi2Type thefmuType,
fmi2String thefmuGUID,
fmi2String thefmuResourceLocation,
const fmi2CallbackFunctions* thefunctions,
fmi2Boolean thevisible,
fmi2Boolean theloggingOn)
: instanceName(theinstanceName),
fmuType(thefmuType),
fmuGUID(thefmuGUID),
fmuResourceLocation(thefmuResourceLocation),
functions(*thefunctions),
visible(!!thevisible),
loggingOn(!!theloggingOn)
{
currentBufferSVOut = new string();
currentBufferTCOut = new string();
lastBufferSVOut = new string();
lastBufferTCOut = new string();
loggingCategories.clear();
loggingCategories.insert("FMI");
loggingCategories.insert("OSMP");
loggingCategories.insert("OSI");
}
EsminiOsiSource::~EsminiOsiSource()
{
delete currentBufferSVOut;
delete currentBufferTCOut;
delete lastBufferSVOut;
delete lastBufferTCOut;
}
fmi2Status EsminiOsiSource::SetDebugLogging(fmi2Boolean theloggingOn, size_t nCategories, const fmi2String categories[])
{
fmi_verbose_log("fmi2SetDebugLogging(%s)", theloggingOn ? "true" : "false");
loggingOn = theloggingOn ? true : false;
if (categories && (nCategories > 0))
{
loggingCategories.clear();
for (size_t i = 0; i < nCategories; i++)
{
if (0 == strcmp(categories[i], "FMI"))
loggingCategories.insert("FMI");
else if (0 == strcmp(categories[i], "OSMP"))
loggingCategories.insert("OSMP");
else if (0 == strcmp(categories[i], "OSI"))
loggingCategories.insert("OSI");
}
}
else
{
loggingCategories.clear();
loggingCategories.insert("FMI");
loggingCategories.insert("OSMP");
loggingCategories.insert("OSI");
}
return fmi2OK;
}
fmi2Component EsminiOsiSource::Instantiate(fmi2String instanceName,
fmi2Type fmuType,
fmi2String fmuGUID,
fmi2String fmuResourceLocation,
const fmi2CallbackFunctions* functions,
fmi2Boolean visible,
fmi2Boolean loggingOn)
{
EsminiOsiSource* myc = new EsminiOsiSource(instanceName, fmuType, fmuGUID, fmuResourceLocation, functions, visible, loggingOn);
if (myc == NULL)
{
fmi_verbose_log_global("fmi2Instantiate(\"%s\",%d,\"%s\",\"%s\",\"%s\",%d,%d) = NULL (alloc failure)",
instanceName,
fmuType,
fmuGUID,
(fmuResourceLocation != NULL) ? fmuResourceLocation : "<NULL>",
"FUNCTIONS",
visible,
loggingOn);
return NULL;
}
if (myc->doInit() != fmi2OK)
{
fmi_verbose_log_global("fmi2Instantiate(\"%s\",%d,\"%s\",\"%s\",\"%s\",%d,%d) = NULL (doInit failure)",
instanceName,
fmuType,
fmuGUID,
(fmuResourceLocation != NULL) ? fmuResourceLocation : "<NULL>",
"FUNCTIONS",
visible,
loggingOn);
delete myc;
return NULL;
}
else
{
fmi_verbose_log_global("fmi2Instantiate(\"%s\",%d,\"%s\",\"%s\",\"%s\",%d,%d) = %p",
instanceName,
fmuType,
fmuGUID,
(fmuResourceLocation != NULL) ? fmuResourceLocation : "<NULL>",
"FUNCTIONS",
visible,
loggingOn,
myc);
return (fmi2Component)myc;
}
}
fmi2Status EsminiOsiSource::SetupExperiment(fmi2Boolean toleranceDefined,
fmi2Real tolerance,
fmi2Real startTime,
fmi2Boolean stopTimeDefined,
fmi2Real stopTime)
{
fmi_verbose_log("fmi2SetupExperiment(%d,%g,%g,%d,%g)", toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
return doStart(toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
}
fmi2Status EsminiOsiSource::EnterInitializationMode()
{
fmi_verbose_log("fmi2EnterInitializationMode()");
return doEnterInitializationMode();
}
fmi2Status EsminiOsiSource::ExitInitializationMode()
{
fmi_verbose_log("fmi2ExitInitializationMode()");
return doExitInitializationMode();
}
fmi2Status EsminiOsiSource::DoStep(fmi2Real currentCommunicationPoint,
fmi2Real communicationStepSize,
fmi2Boolean noSetFMUStatePriorToCurrentPointfmi2Component)
{
fmi_verbose_log("fmi2DoStep(%g,%g,%d)", currentCommunicationPoint, communicationStepSize, noSetFMUStatePriorToCurrentPointfmi2Component);
return doCalc(currentCommunicationPoint, communicationStepSize, noSetFMUStatePriorToCurrentPointfmi2Component);
}
fmi2Status EsminiOsiSource::Terminate()
{
fmi_verbose_log("fmi2Terminate()");
return doTerm();
}
fmi2Status EsminiOsiSource::Reset()
{
fmi_verbose_log("fmi2Reset()");
fmiSlaveTerminated = false;
doFree();
return doInit();
}
void EsminiOsiSource::FreeInstance()
{
fmi_verbose_log("fmi2FreeInstance()");
doFree();
}
fmi2Status EsminiOsiSource::GetReal(const fmi2ValueReference vr[], size_t nvr, fmi2Real value[])
{
fmi_verbose_log("fmi2GetReal(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_REAL_VARS)
value[i] = real_vars[vr[i]];
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::GetInteger(const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[])
{
fmi_verbose_log("fmi2GetInteger(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_INTEGER_VARS)
value[i] = integer_vars[vr[i]];
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::GetBoolean(const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[])
{
fmi_verbose_log("fmi2GetBoolean(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_BOOLEAN_VARS)
value[i] = boolean_vars[vr[i]];
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::GetString(const fmi2ValueReference vr[], size_t nvr, fmi2String value[])
{
fmi_verbose_log("fmi2GetString(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_STRING_VARS)
value[i] = string_vars[vr[i]].c_str();
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::SetReal(const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[])
{
fmi_verbose_log("fmi2SetReal(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_REAL_VARS)
real_vars[vr[i]] = value[i];
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::SetInteger(const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[])
{
fmi_verbose_log("fmi2SetInteger(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_INTEGER_VARS)
integer_vars[vr[i]] = value[i];
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::SetBoolean(const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[])
{
fmi_verbose_log("fmi2SetBoolean(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_BOOLEAN_VARS)
boolean_vars[vr[i]] = value[i];
else
return fmi2Error;
}
return fmi2OK;
}
fmi2Status EsminiOsiSource::SetString(const fmi2ValueReference vr[], size_t nvr, const fmi2String value[])
{
fmi_verbose_log("fmi2SetString(...)");
for (size_t i = 0; i < nvr; i++)
{
if (vr[i] < FMI_STRING_VARS)
string_vars[vr[i]] = value[i];
else
return fmi2Error;
}
return fmi2OK;
}
/*
* FMI 2.0 Co-Simulation Interface API
*/
extern "C"
{
FMI2_Export const char* fmi2GetTypesPlatform()
{
return fmi2TypesPlatform;
}
FMI2_Export const char* fmi2GetVersion()
{
return fmi2Version;
}
FMI2_Export fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories, const fmi2String categories[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->SetDebugLogging(loggingOn, nCategories, categories);
}
/*
* Functions for Co-Simulation
*/
FMI2_Export fmi2Component fmi2Instantiate(fmi2String instanceName,
fmi2Type fmuType,
fmi2String fmuGUID,
fmi2String fmuResourceLocation,
const fmi2CallbackFunctions* functions,
fmi2Boolean visible,
fmi2Boolean loggingOn)
{
return EsminiOsiSource::Instantiate(instanceName, fmuType, fmuGUID, fmuResourceLocation, functions, visible, loggingOn);
}
FMI2_Export fmi2Status fmi2SetupExperiment(fmi2Component c,
fmi2Boolean toleranceDefined,
fmi2Real tolerance,
fmi2Real startTime,
fmi2Boolean stopTimeDefined,
fmi2Real stopTime)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->SetupExperiment(toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
}
FMI2_Export fmi2Status fmi2EnterInitializationMode(fmi2Component c)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->EnterInitializationMode();
}
FMI2_Export fmi2Status fmi2ExitInitializationMode(fmi2Component c)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->ExitInitializationMode();
}
FMI2_Export fmi2Status fmi2DoStep(fmi2Component c,
fmi2Real currentCommunicationPoint,
fmi2Real communicationStepSize,
fmi2Boolean noSetFMUStatePriorToCurrentPointfmi2Component)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->DoStep(currentCommunicationPoint, communicationStepSize, noSetFMUStatePriorToCurrentPointfmi2Component);
}
FMI2_Export fmi2Status fmi2Terminate(fmi2Component c)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->Terminate();
}
FMI2_Export fmi2Status fmi2Reset(fmi2Component c)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->Reset();
}
FMI2_Export void fmi2FreeInstance(fmi2Component c)
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
myc->FreeInstance();
delete myc;
}
/*
* Data Exchange Functions
*/
FMI2_Export fmi2Status fmi2GetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Real value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->GetReal(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2GetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->GetInteger(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2GetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->GetBoolean(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2GetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2String value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->GetString(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2SetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->SetReal(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2SetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->SetInteger(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2SetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->SetBoolean(vr, nvr, value);
}
FMI2_Export fmi2Status fmi2SetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2String value[])
{
EsminiOsiSource* myc = (EsminiOsiSource*)c;
return myc->SetString(vr, nvr, value);
}
/*
* Unsupported Features (FMUState, Derivatives, Async DoStep, Status Enquiries)
*/
FMI2_Export fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate* FMUstate)
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate FMUstate)
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate* FMUstate)
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate FMUstate, size_t* size)
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte serializedState[], size_t size)
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte serializedState[], size_t size, fmi2FMUstate* FMUstate)
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2GetDirectionalDerivative(fmi2Component c,
const fmi2ValueReference vUnknown_ref[],
size_t nUnknown,
const fmi2ValueReference vKnown_ref[],
size_t nKnown,
const fmi2Real dvKnown[],
fmi2Real dvUnknown[])
{
return fmi2Error;
}
FMI2_Export fmi2Status
fmi2SetRealInputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], const fmi2Real value[])
{
return fmi2Error;
}
FMI2_Export fmi2Status
fmi2GetRealOutputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], fmi2Real value[])
{
return fmi2Error;
}
FMI2_Export fmi2Status fmi2CancelStep(fmi2Component c)
{
return fmi2OK;
}
FMI2_Export fmi2Status fmi2GetStatus(fmi2Component c, const fmi2StatusKind s, fmi2Status* value)
{
return fmi2Discard;
}
FMI2_Export fmi2Status fmi2GetRealStatus(fmi2Component c, const fmi2StatusKind s, fmi2Real* value)
{
return fmi2Discard;
}
FMI2_Export fmi2Status fmi2GetIntegerStatus(fmi2Component c, const fmi2StatusKind s, fmi2Integer* value)
{
return fmi2Discard;
}
FMI2_Export fmi2Status fmi2GetBooleanStatus(fmi2Component c, const fmi2StatusKind s, fmi2Boolean* value)
{
if (s == fmi2StatusKind::fmi2Terminated) {
EsminiOsiSource* myc = (EsminiOsiSource*)c;
*value = myc->get_slave_terminated() ? fmi2True : fmi2False;
return fmi2OK;
}
return fmi2Discard;
}
FMI2_Export fmi2Status fmi2GetStringStatus(fmi2Component c, const fmi2StatusKind s, fmi2String* value)
{
return fmi2Discard;
}
}