Skip to content

Initial DummySource and DummySensor conversion to Flatbuffers #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
examples/build
examples/build*
12 changes: 9 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.5)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Modules/")
set(BUILD_FLATBUFFER ON CACHE BOOLEAN "Build flatbuffer versions of libraries")

# Set a default build type if none was specified
set(default_build_type "Release")
Expand All @@ -26,6 +27,11 @@ get_directory_property(OSI_VERSION_PATCH DIRECTORY open-simulation-interface DEF
set(OSIVERSION "${OSI_VERSION_MAJOR}.${OSI_VERSION_MINOR}.${OSI_VERSION_PATCH}")

include_directories( includes )
add_subdirectory( OSMPDummySensor )
add_subdirectory( OSMPDummySource )
add_subdirectory( OSMPCNetworkProxy )
if(BUILD_FLATBUFFER)
add_subdirectory( OSMPDummySensor_flat )
add_subdirectory( OSMPDummySource_flat )
else()
add_subdirectory( OSMPDummySensor )
add_subdirectory( OSMPDummySource )
add_subdirectory( OSMPCNetworkProxy )
endif()
111 changes: 111 additions & 0 deletions examples/OSMPDummySensor/OSMPDummySensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "OSMPDummySensor.h"

#define NO_LIDAR_REFLECTIONS

/*
* Debug Breaks
*
Expand Down Expand Up @@ -48,13 +50,25 @@
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream> //included for windows compatibility
#define M_PI 3.14159265358979323846264338327950288


using namespace std;

#ifdef PRIVATE_LOG_PATH
ofstream COSMPDummySensor::private_log_file;
#endif

#ifdef _WIN32
std::string fileName = "C:/tmp/OSMPDummySensor_Protobuf_timing";
#else
std::string fileName = "/tmp/OSMPDummySensor_Protobuf_timing";
#endif

/*
* ProtocolBuffer Accessors
*/
Expand Down Expand Up @@ -133,6 +147,7 @@ bool COSMPDummySensor::get_fmi_sensor_view_in(osi3::SensorView& data)
if (integer_vars[FMI_INTEGER_SENSORVIEW_IN_SIZE_IDX] > 0) {
void* buffer = decode_integer_to_pointer(integer_vars[FMI_INTEGER_SENSORVIEW_IN_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_IN_BASELO_IDX]);
normal_log("OSMP","Got %08X %08X, reading from %p ...",integer_vars[FMI_INTEGER_SENSORVIEW_IN_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_IN_BASELO_IDX],buffer);
std::printf("Got %08X %08X, reading from %p ...\n",integer_vars[FMI_INTEGER_SENSORVIEW_IN_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_IN_BASELO_IDX],buffer);
data.ParseFromArray(buffer,integer_vars[FMI_INTEGER_SENSORVIEW_IN_SIZE_IDX]);
return true;
} else {
Expand Down Expand Up @@ -263,7 +278,40 @@ fmi2Status COSMPDummySensor::doCalc(fmi2Real currentCommunicationPoint, fmi2Real
osi3::SensorData currentOut;
double time = currentCommunicationPoint+communicationStepSize;
normal_log("OSI","Calculating Sensor at %f for %f (step size %f)",currentCommunicationPoint,time,communicationStepSize);
auto startOSIDeserialize = std::chrono::duration_cast< std::chrono::microseconds >(std::chrono::system_clock::now().time_since_epoch());
if (get_fmi_sensor_view_in(currentIn)) {
auto stopOSIDeserialize = std::chrono::duration_cast< std::chrono::microseconds >(std::chrono::system_clock::now().time_since_epoch());
//// Lidar Detections
#ifndef NO_LIDAR_DETECTIONS
if (currentIn.lidar_sensor_view_size() > 0) {
auto lidar_sensor = currentOut.mutable_feature_data()->add_lidar_sensor();
double azimuth_fov = 360.0; // Azimuth angle FoV in °
int rays_per_beam_horizontal = 6; // horizontal super-sampling factor
double beam_step_azimuth = 0.2; // horizontal step-size per beam in degrees of VLP32 at 600 rpm (10 Hz) with VLP32's fixed firing_cycle of 55.296e^(-6) s
double beam_step_elevation = 0.3; // simplified equidistant beam spacing
double max_emitted_signal_strength_in_dB = 10 * std::log10(0.5); // maximal emitted signal strength in dB
int rays_per_layer = azimuth_fov/beam_step_azimuth*rays_per_beam_horizontal*0.8;
double const_distance = 10.0;
double speed_of_light = 299792458.0;
size_t num_reflections = currentIn.lidar_sensor_view(0).reflection_size();
int layer_idx = -1;
for (int reflection_idx = 0; reflection_idx < num_reflections; reflection_idx++) {
if ((reflection_idx % rays_per_layer) == 0) layer_idx++;
auto current_reflection = currentIn.lidar_sensor_view(0).reflection(reflection_idx);
if (reflection_idx % 18 == 0) { //18 times super-sampling
//todo: generate lidar detection
double distance = current_reflection.time_of_flight() * speed_of_light / 2;
double azimuth_deg = double(reflection_idx % rays_per_layer) * beam_step_azimuth;
double elevation_deg = layer_idx * beam_step_elevation - 5; //start at -5° for simplification
auto current_detection = lidar_sensor->add_detection();
current_detection->mutable_position()->set_distance(distance);
current_detection->mutable_position()->set_azimuth(azimuth_deg*M_PI/180);
current_detection->mutable_position()->set_elevation(elevation_deg*M_PI/180);
}
}
}
#endif
//// Moving Objects
double ego_x=0, ego_y=0, ego_z=0;
osi3::Identifier ego_id = currentIn.global_ground_truth().host_vehicle_id();
normal_log("OSI","Looking for EgoVehicle with ID: %llu",ego_id.value());
Expand Down Expand Up @@ -333,10 +381,66 @@ fmi2Status COSMPDummySensor::doCalc(fmi2Real currentCommunicationPoint, fmi2Real
}
});
normal_log("OSI","Mapped %d vehicles to output", i);

/* Serialize */
auto startOSISerialize = std::chrono::duration_cast< std::chrono::microseconds >(std::chrono::system_clock::now().time_since_epoch());
set_fmi_sensor_data_out(currentOut);
set_fmi_valid(true);
set_fmi_count(currentOut.moving_object_size());
auto stopOSISerialize = std::chrono::duration_cast< std::chrono::microseconds >(std::chrono::system_clock::now().time_since_epoch());

//Performance logging
std::ifstream f(fileName.c_str());
bool fileExists = f.is_open();
f.close();

std::ofstream logFile;
if(!fileExists) {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream time_string;
time_string << std::put_time(std::localtime(&in_time_t), "_%H-%M-%S.json");
fileName += time_string.str();
logFile.open (fileName, std::ios_base::app);
logFile << "{" << std::endl;
logFile << "\t\"Header\": {" << std::endl;
logFile << "\t\t\"OsiMessages\": [\"osi3::SensorView\", \"osi3::SensorData\"]," << std::endl;
logFile << "\t\t\"EventFields\": [\"EventId\", \"GlobalTime\", \"SimulationTime\", \"MessageId\", \"SizeValueReference\", \"MessageSize\"]," << std::endl;
logFile << "\t\t\"EventTypes\": [\"StartOSISerialize\", \"StopOSISerialize\", \"StartOSIDeserialize\", \"StopOSIDeserialize\"]," << std::endl;
logFile << "\t\t\"FormatVersion\": {" << std::endl;
logFile << "\t\t\t\"Major\": 1," << std::endl;
logFile << "\t\t\t\"Minor\": 0," << std::endl;
logFile << "\t\t\t\"Patch\": 0," << std::endl;
logFile << "\t\t\t\"PreRelease\": \"beta\"" << std::endl;
logFile << "\t\t}" << std::endl;
logFile << "\t}," << std::endl;
logFile << "\t\"Data\": [" << std::endl;
logFile << "\t\t{" << std::endl;
logFile << "\t\t\t\"Instance\": {" << std::endl;
logFile << "\t\t\t\t\"ModelIdentity\": " << "\"OSMPDummySensor Protobuf\"" << std::endl;
/*logFile << "\t\t\t\t\"ModelIdentity\": " << "\"OSMPDummySensor Protobuf\"" << "," << std::endl;
logFile << "\t\t\t\t\"OsiVersion\": {" << std::endl;
logFile << "\t\t\t\t\t\"version_major\": " << sensor_view_in->version()->version_major() << "," << std::endl;
logFile << "\t\t\t\t\t\"version_minor\": " << sensor_view_in->version()->version_minor() << "," << std::endl;
logFile << "\t\t\t\t\t\"version_patch\": " << sensor_view_in->version()->version_patch() << std::endl;*
logFile << "\t\t\t\t}" << std::endl;*/
logFile << "\t\t\t}," << std::endl;
logFile << "\t\t\t\"OsiEvents\": [" << std::endl;
} else {
logFile.open (fileName, std::ios_base::app);
}

if(fileExists) {
logFile << "," << std::endl;
}
size_t sensorDataSize = currentOut.ByteSize();
double osiSimTime = (double)currentOut.timestamp().seconds() + (float)currentOut.timestamp().nanos() * 0.000000001;
logFile << "\t\t\t\t[" << "2" << ", " << std::setprecision(16) << (double)startOSIDeserialize.count() << ", " << osiSimTime << ", " << "0" << ", " << "2" << ", " << 999 << "]," << std::endl; //todo: sensor view size
logFile << "\t\t\t\t[" << "3" << ", " << std::setprecision(16) << (double)stopOSIDeserialize.count() << ", " << osiSimTime << ", " << "0" << ", " << "2" << ", " << 999 << "]," << std::endl;
logFile << "\t\t\t\t[" << "0" << ", " << std::setprecision(16) << (double)startOSISerialize.count() << ", " << osiSimTime << ", " << "1" << ", " << "5" << ", " << sensorDataSize << "]," << std::endl;
logFile << "\t\t\t\t[" << "1" << ", " << std::setprecision(16) << (double)stopOSISerialize.count() << ", " << osiSimTime << ", " << "1" << ", " << "5" << ", " << sensorDataSize << "]";
logFile.close();

} else {
/* We have no valid input, so no valid output */
normal_log("OSI","No valid input, therefore providing no valid output.");
Expand Down Expand Up @@ -661,6 +765,13 @@ extern "C" {
FMI2_Export fmi2Status fmi2Terminate(fmi2Component c)
{
COSMPDummySensor* myc = (COSMPDummySensor*)c;
std::ofstream logFile;
logFile.open(fileName, std::ios_base::app);
logFile << std::endl << "\t\t\t]" << std::endl;
logFile << "\t\t}" << std::endl;
logFile << "\t]" << std::endl;
logFile << "}" << std::endl;
logFile.close();
return myc->Terminate();
}

Expand Down
67 changes: 67 additions & 0 deletions examples/OSMPDummySensor_flat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
cmake_minimum_required(VERSION 3.5)
project(OSMPDummySensor)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(LINK_WITH_SHARED_OSI ON CACHE BOOL "Link FMU with shared OSI library instead of statically linking")
set(PUBLIC_LOGGING OFF CACHE BOOL "Enable logging via FMI logger")
set(PRIVATE_LOGGING OFF CACHE BOOL "Enable private logging to file")
if(WIN32)
set(PRIVATE_LOG_PATH "C:/TEMP/OSMPDummySensorLog.log" CACHE FILEPATH "Path to write private log file to")
else()
set(PRIVATE_LOG_PATH "/tmp/OSMPDummySensorLog.log" CACHE FILEPATH "Path to write private log file to")
endif()
if(PRIVATE_LOGGING)
file(TO_NATIVE_PATH ${PRIVATE_LOG_PATH} PRIVATE_LOG_PATH_NATIVE)
string(REPLACE "\\" "\\\\" PRIVATE_LOG_PATH ${PRIVATE_LOG_PATH_NATIVE})
endif()
set(VERBOSE_FMI_LOGGING OFF CACHE BOOL "Enable detailed FMI function logging")
set(DEBUG_BREAKS OFF CACHE BOOL "Enable debugger traps for debug builds of FMU")

string(TIMESTAMP FMUTIMESTAMP UTC)
string(MD5 FMUGUID modelDescription.in.xml)
configure_file(modelDescription.in.xml modelDescription.xml @ONLY)
configure_file(OSMPDummySensorConfig.in.h OSMPDummySensorConfig.h)

find_package(Protobuf 3.0.0 REQUIRED)
add_library(OSMPDummySensor SHARED OSMPDummySensor.cpp)
set_target_properties(OSMPDummySensor PROPERTIES PREFIX "")
target_compile_definitions(OSMPDummySensor PRIVATE "FMU_SHARED_OBJECT")
if(LINK_WITH_SHARED_OSI)
target_link_libraries(OSMPDummySensor open_simulation_interface_fbs)
else()
target_link_libraries(OSMPDummySensor open_simulation_interface_pic)
endif()
include_directories("${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/include")

if(WIN32)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(FMI_BINARIES_PLATFORM "win64")
else()
set(FMI_BINARIES_PLATFORM "win32")
endif()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(FMI_BINARIES_PLATFORM "linux64")
else()
set(FMI_BINARIES_PLATFORM "linux32")
endif()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(FMI_BINARIES_PLATFORM "darwin64")
else()
set(FMI_BINARIES_PLATFORM "darwin32")
endif()
endif()

add_custom_command(TARGET OSMPDummySensor
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/buildfmu"
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources"
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/binaries/${FMI_BINARIES_PLATFORM}"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/modelDescription.xml" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/OSMPDummySensor.cpp" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/OSMPDummySensor.h" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/OSMPDummySensorConfig.h" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources/OSMPDummySensorConfig.h"
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:OSMPDummySensor> $<$<PLATFORM_ID:Windows>:$<$<CONFIG:Debug>:$<TARGET_PDB_FILE:OSMPDummySensor>>> "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/binaries/${FMI_BINARIES_PLATFORM}"
COMMAND ${CMAKE_COMMAND} -E chdir "${CMAKE_CURRENT_BINARY_DIR}/buildfmu" ${CMAKE_COMMAND} -E tar "cfv" "../OSMPDummySensor.fmu" --format=zip "modelDescription.xml" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/sources" "${CMAKE_CURRENT_BINARY_DIR}/buildfmu/binaries/${FMI_BINARIES_PLATFORM}")
Loading