4444#endif
4545
4646#include < iostream>
47- #include < string>
47+ // #include <string>
4848#include < algorithm>
4949#include < cstdint>
5050#include < cmath>
51+ #include < chrono>
52+ #include < ctime>
53+ #include < iomanip>
54+ #include < sstream> // included for windows compatibility
5155
5256using namespace std ;
5357
5458#ifdef PRIVATE_LOG_PATH
5559ofstream COSMPDummySource::private_log_file;
5660#endif
5761
62+ #ifdef _WIN32
63+ std::string fileName = " C:/tmp/OSMPDummySource_flatbuf_timing" ;
64+ #else
65+ std::string fileName = " /tmp/OSMPDummySource_flatbuf_timing" ;
66+ #endif
67+
5868/*
5969 * ProtocolBuffer Accessors
6070 */
@@ -89,9 +99,11 @@ void encode_pointer_to_integer(const void* ptr,fmi2Integer& hi,fmi2Integer& lo)
8999 } base;
90100 unsigned long long address;
91101 } myaddr;
102+
92103 myaddr.address =reinterpret_cast <unsigned long long >(ptr);
93104 hi=myaddr.base .hi ;
94105 lo=myaddr.base .lo ;
106+
95107#elif PTRDIFF_MAX == INT32_MAX
96108 hi=0 ;
97109 lo=reinterpret_cast <int >(ptr);
@@ -100,13 +112,16 @@ void encode_pointer_to_integer(const void* ptr,fmi2Integer& hi,fmi2Integer& lo)
100112#endif
101113}
102114
103- void COSMPDummySource::set_fmi_sensor_view_out (const osi3::SensorView& data)
115+ // void COSMPDummySource::set_fmi_sensor_view_out(const osi3::SensorView& data)
116+ void COSMPDummySource::set_fmi_sensor_view_out ()
104117{
105- data.SerializeToString (currentBuffer);
106- encode_pointer_to_integer (currentBuffer->data (),integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX]);
107- integer_vars[FMI_INTEGER_SENSORVIEW_OUT_SIZE_IDX]=(fmi2Integer)currentBuffer->length ();
108- 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],currentBuffer->data ());
109- swap (currentBuffer,lastBuffer);
118+ // data.SerializeToString(currentBuffer);
119+ // encode_pointer_to_integer(currentBuffer->data(),integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX]);
120+ encode_pointer_to_integer (currentBuffer.data (), integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX], integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX]);
121+ integer_vars[FMI_INTEGER_SENSORVIEW_OUT_SIZE_IDX]=(fmi2Integer)currentBuffer.length ();
122+ 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],currentBuffer.data ());
123+ std::printf (" Providing %08X %08X, writing from %p ...\n " ,integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX],currentBuffer.data ());
124+ // swap(currentBuffer,lastBuffer);
110125}
111126
112127void COSMPDummySource::reset_fmi_sensor_view_out ()
@@ -186,8 +201,7 @@ void rotatePoint(double x, double y, double z,double yaw,double pitch,double rol
186201fmi2Status COSMPDummySource::doCalc (fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
187202{
188203 DEBUGBREAK ();
189-
190- osi3::SensorView currentOut;
204+ flatbuffers::FlatBufferBuilder builder (1024 );
191205 double time = currentCommunicationPoint+communicationStepSize;
192206
193207 normal_log (" OSI" ," Calculating SensorView at %f for %f (step size %f)" ,currentCommunicationPoint,time,communicationStepSize);
@@ -196,63 +210,93 @@ fmi2Status COSMPDummySource::doCalc(fmi2Real currentCommunicationPoint, fmi2Real
196210 static double source_y_offsets[10 ] = { 3.0 , 3.0 , 3.0 , 0.25 , 0 , -0.25 , -3.0 , -3.0 , -3.0 , -3.0 };
197211 static double source_x_offsets[10 ] = { 0.0 , 40.0 , 100.0 , 100.0 , 0.0 , 150.0 , 5.0 , 45.0 , 85.0 , 125.0 };
198212 static double source_x_speeds[10 ] = { 29.0 , 30.0 , 31.0 , 25.0 , 26.0 , 28.0 , 20.0 , 22.0 , 22.5 , 23.0 };
199- static osi3::MovingObject_VehicleClassification_Type source_veh_types[10 ] = {
200- osi3::MovingObject_VehicleClassification_Type_TYPE_MEDIUM_CAR,
201- osi3::MovingObject_VehicleClassification_Type_TYPE_SMALL_CAR,
202- osi3::MovingObject_VehicleClassification_Type_TYPE_COMPACT_CAR,
203- osi3::MovingObject_VehicleClassification_Type_TYPE_DELIVERY_VAN,
204- osi3::MovingObject_VehicleClassification_Type_TYPE_LUXURY_CAR,
205- osi3::MovingObject_VehicleClassification_Type_TYPE_MEDIUM_CAR,
206- osi3::MovingObject_VehicleClassification_Type_TYPE_COMPACT_CAR,
207- osi3::MovingObject_VehicleClassification_Type_TYPE_SMALL_CAR,
208- osi3::MovingObject_VehicleClassification_Type_TYPE_MOTORBIKE,
209- osi3::MovingObject_VehicleClassification_Type_TYPE_BUS };
210-
211- currentOut.Clear ();
212- currentOut.mutable_version ()->CopyFrom (osi3::InterfaceVersion::descriptor ()->file ()->options ().GetExtension (osi3::current_interface_version));
213- currentOut.mutable_sensor_id ()->set_value (10000 );
214- currentOut.mutable_host_vehicle_id ()->set_value (14 );
215- osi3::GroundTruth *currentGT = currentOut.mutable_global_ground_truth ();
216- currentOut.mutable_timestamp ()->set_seconds ((long long int )floor (time));
217- currentOut.mutable_timestamp ()->set_nanos ((int )((time - floor (time))*1000000000.0 ));
218- currentGT->mutable_timestamp ()->set_seconds ((long long int )floor (time));
219- currentGT->mutable_timestamp ()->set_nanos ((int )((time - floor (time))*1000000000.0 ));
220- currentGT->mutable_host_vehicle_id ()->set_value (14 );
221-
213+ static osi3::MovingObject_::VehicleClassification_::Type source_veh_types[10 ] = {
214+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_MEDIUM_CAR,
215+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_SMALL_CAR,
216+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_COMPACT_CAR,
217+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_DELIVERY_VAN,
218+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_LUXURY_CAR,
219+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_MEDIUM_CAR,
220+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_COMPACT_CAR,
221+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_SMALL_CAR,
222+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_MOTORBIKE,
223+ osi3::MovingObject_::VehicleClassification_::Type::TYPE_BUS };
224+
225+ std::vector<flatbuffers::Offset<osi3::MovingObject>> moving_object_vector;
222226 // Vehicles
223227 for (unsigned int i=0 ;i<10 ;i++) {
224- osi3::MovingObject *veh = currentGT->add_moving_object ();
225- veh->mutable_id ()->set_value (10 +i);
226- veh->set_type (osi3::MovingObject_Type_TYPE_VEHICLE);
227- auto vehclass = veh->mutable_vehicle_classification ();
228- vehclass->set_type (source_veh_types[i]);
229- auto vehlights = vehclass->mutable_light_state ();
230- vehlights->set_indicator_state (osi3::MovingObject_VehicleClassification_LightState_IndicatorState_INDICATOR_STATE_OFF);
231- vehlights->set_brake_light_state (osi3::MovingObject_VehicleClassification_LightState_BrakeLightState_BRAKE_LIGHT_STATE_OFF);
232- veh->mutable_base ()->mutable_dimension ()->set_height (1.5 );
233- veh->mutable_base ()->mutable_dimension ()->set_width (2.0 );
234- veh->mutable_base ()->mutable_dimension ()->set_length (5.0 );
235- veh->mutable_base ()->mutable_position ()->set_x (source_x_offsets[i]+time*source_x_speeds[i]);
236- veh->mutable_base ()->mutable_position ()->set_y (source_y_offsets[i]+sin (time/source_x_speeds[i])*0.25 );
237- veh->mutable_base ()->mutable_position ()->set_z (0.0 );
238- veh->mutable_base ()->mutable_velocity ()->set_x (source_x_speeds[i]);
239- veh->mutable_base ()->mutable_velocity ()->set_y (cos (time/source_x_speeds[i])*0.25 /source_x_speeds[i]);
240- veh->mutable_base ()->mutable_velocity ()->set_z (0.0 );
241- veh->mutable_base ()->mutable_acceleration ()->set_x (0.0 );
242- veh->mutable_base ()->mutable_acceleration ()->set_y (-sin (time/source_x_speeds[i])*0.25 /(source_x_speeds[i]*source_x_speeds[i]));
243- veh->mutable_base ()->mutable_acceleration ()->set_z (0.0 );
244- veh->mutable_base ()->mutable_orientation ()->set_pitch (0.0 );
245- veh->mutable_base ()->mutable_orientation ()->set_roll (0.0 );
246- veh->mutable_base ()->mutable_orientation ()->set_yaw (0.0 );
247- veh->mutable_base ()->mutable_orientation_rate ()->set_pitch (0.0 );
248- veh->mutable_base ()->mutable_orientation_rate ()->set_roll (0.0 );
249- veh->mutable_base ()->mutable_orientation_rate ()->set_yaw (0.0 );
250- normal_log (" OSI" ," GT: Adding Vehicle %d[%llu] Absolute Position: %f,%f,%f Velocity (%f,%f,%f)" ,i,veh->id ().value (),veh->base ().position ().x (),veh->base ().position ().y (),veh->base ().position ().z (),veh->base ().velocity ().x (),veh->base ().velocity ().y (),veh->base ().velocity ().z ());
251- }
252-
253- set_fmi_sensor_view_out (currentOut);
228+ osi3::IdentifierBuilder id_builder (builder);
229+ id_builder.add_value (10 +i);
230+ auto moving_obj_id = id_builder.Finish ();
231+
232+ osi3::MovingObject_::VehicleClassificationBuilder vehicle_classification_builder (builder);
233+ // vehicle_classification_builder.add_type(source_veh_types[i]); //todo: vehicle classifications are wrong in headers due to namespace conflict -> confused with moving object type
234+ auto vehicle_classification = vehicle_classification_builder.Finish ();
235+
236+ auto bbcenter_to_rear = osi3::CreateVector3d (builder, 1.0 , 0.0 , -0.3 );
237+ osi3::MovingObject_::VehicleAttributesBuilder vehicle_attributes_builder (builder);
238+ vehicle_attributes_builder.add_bbcenter_to_rear (bbcenter_to_rear);
239+ auto vehicle_attributes = vehicle_attributes_builder.Finish ();
240+
241+ auto dimension = osi3::CreateDimension3d (builder, 5.0 , 2.0 , 1.5 );
242+ auto position = osi3::CreateVector3d (builder, source_x_offsets[i]+time*source_x_speeds[i], source_y_offsets[i]+sin (time/source_x_speeds[i])*0.25 , 0.0 );
243+ auto velocity = osi3::CreateVector3d (builder, source_x_speeds[i], cos (time/source_x_speeds[i])*0.25 /source_x_speeds[i], 0.0 );
244+ auto acceleration = osi3::CreateVector3d (builder, 0.0 , -sin (time/source_x_speeds[i])*0.25 /(source_x_speeds[i]*source_x_speeds[i]), 0.0 );
245+ auto orientation = osi3::CreateOrientation3d (builder, 0.0 , 0.0 , 0.0 );
246+ auto orientation_rate = osi3::CreateOrientation3d (builder, 0.0 , 0.0 , 0.0 );
247+ osi3::BaseMovingBuilder base_moving_builder (builder);
248+ base_moving_builder.add_dimension (dimension);
249+ base_moving_builder.add_position (position);
250+ base_moving_builder.add_velocity (velocity);
251+ base_moving_builder.add_acceleration (acceleration);
252+ base_moving_builder.add_orientation (orientation);
253+ base_moving_builder.add_orientation_rate (orientation_rate);
254+ auto base_moving = base_moving_builder.Finish ();
255+
256+ osi3::MovingObjectBuilder moving_object_builder (builder);
257+ moving_object_builder.add_id (moving_obj_id);
258+ // moving_object_builder.add_type(osi3::MovingObject_::Type::TYPE_VEHICLE); //todo: vehicle types are wrong in headers due to namespace conflict -> stationary and moving are confused
259+ moving_object_builder.add_vehicle_classification (vehicle_classification);
260+ moving_object_builder.add_vehicle_attributes (vehicle_attributes);
261+ moving_object_builder.add_base (base_moving);
262+ auto current_moving_object = moving_object_builder.Finish ();
263+ moving_object_vector.push_back (current_moving_object);
264+
265+ auto moving_object = reinterpret_cast <osi3::MovingObject *>(builder.GetCurrentBufferPointer () + builder.GetSize () - current_moving_object.o );
266+ normal_log (" OSI" ," GT: Adding Vehicle %d[%llu] Absolute Position: %f,%f,%f Velocity (%f,%f,%f)" ,i,moving_object->id ()->value (),moving_object->base ()->position ()->x (),moving_object->base ()->position ()->y (),moving_object->base ()->position ()->z (),moving_object->base ()->velocity ()->x (),moving_object->base ()->velocity ()->y (),moving_object->base ()->velocity ()->z ());
267+ }
268+ auto moving_object_flatvector = builder.CreateVector (moving_object_vector);
269+
270+ auto timestamp = osi3::CreateTimestamp (builder, (int64_t )floor (time), (int )((time - floor (time))*1000000000.0 ));
271+ osi3::IdentifierBuilder host_vehicle_id_builder (builder);
272+ host_vehicle_id_builder.add_value (14 );
273+ auto host_vehicle_id = host_vehicle_id_builder.Finish ();
274+ osi3::GroundTruthBuilder ground_truth_builder (builder);
275+ ground_truth_builder.add_timestamp (timestamp);
276+ ground_truth_builder.add_host_vehicle_id (host_vehicle_id);
277+ ground_truth_builder.add_moving_object (moving_object_flatvector);
278+ auto ground_truth = ground_truth_builder.Finish ();
279+
280+ auto sensor_id = osi3::CreateIdentifier (builder, 10000 );
281+
282+ osi3::SensorViewBuilder sensor_view_builder (builder);
283+ // sensor_view_builder.add_version(osi3::InterfaceVersion::descriptor()->file()->options().GetExtension(osi3::current_interface_version)); //todo: the used Protobuf FileOptions do not exist in Flatbuffers
284+ sensor_view_builder.add_sensor_id (sensor_id);
285+ sensor_view_builder.add_host_vehicle_id (host_vehicle_id);
286+ sensor_view_builder.add_timestamp (timestamp);
287+ sensor_view_builder.add_global_ground_truth (ground_truth);
288+ auto sensor_view = sensor_view_builder.Finish ();
289+
290+ builder.Finish (sensor_view);
291+ auto uint8_buffer = builder.GetBufferPointer ();
292+ auto size = builder.GetSize ();
293+ std::string tmp_buffer (reinterpret_cast <char const *>(uint8_buffer), size);
294+ currentBuffer = tmp_buffer;
295+
296+ set_fmi_sensor_view_out ();
254297 set_fmi_valid (true );
255- set_fmi_count (currentGT->moving_object_size ());
298+ set_fmi_count ((int )moving_object_vector.size ());
299+
256300 return fmi2OK;
257301}
258302
@@ -280,8 +324,10 @@ COSMPDummySource::COSMPDummySource(fmi2String theinstanceName, fmi2Type thefmuTy
280324 visible(!!thevisible),
281325 loggingOn(!!theloggingOn)
282326{
283- currentBuffer = new string ();
284- lastBuffer = new string ();
327+ // currentBuffer = new string();
328+ // currentBuffer = new uint8_t();
329+ // lastBuffer = new string();
330+ // lastBuffer = new uint8_t();
285331 loggingCategories.clear ();
286332 loggingCategories.insert (" FMI" );
287333 loggingCategories.insert (" OSMP" );
@@ -290,8 +336,8 @@ COSMPDummySource::COSMPDummySource(fmi2String theinstanceName, fmi2Type thefmuTy
290336
291337COSMPDummySource::~COSMPDummySource ()
292338{
293- delete currentBuffer;
294- delete lastBuffer;
339+ // delete currentBuffer;
340+ // delete lastBuffer;
295341}
296342
297343fmi2Status COSMPDummySource::SetDebugLogging (fmi2Boolean theloggingOn, size_t nCategories, const fmi2String categories[])
@@ -557,6 +603,14 @@ extern "C" {
557603 FMI2_Export fmi2Status fmi2Terminate (fmi2Component c)
558604 {
559605 COSMPDummySource* myc = (COSMPDummySource*)c;
606+ std::ofstream logFile;
607+ logFile.open (fileName, std::ios_base::app);
608+ logFile << std::endl << " \t\t\t ]" << std::endl;
609+ logFile << " \t\t }" << std::endl;
610+ logFile << " \t ]" << std::endl;
611+ logFile << " }" << std::endl;
612+ logFile.close ();
613+
560614 return myc->Terminate ();
561615 }
562616
0 commit comments