Skip to content

Commit

Permalink
osi: Initial commit of message handler and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tobifalk authored and cassava committed Mar 27, 2024
1 parent 1d6311a commit 1eb0a15
Show file tree
Hide file tree
Showing 23 changed files with 966 additions and 111 deletions.
1 change: 1 addition & 0 deletions optional/osi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
85 changes: 85 additions & 0 deletions optional/osi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)

project(cloe-osi LANGUAGES CXX)

include(GNUInstallDirs)
include(TargetLinting)

# Module -------------------------------------------------------------
find_package(cloe-models REQUIRED)
find_package(cloe-runtime REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Boost COMPONENTS headers REQUIRED)
find_package(open-simulation-interface REQUIRED)

message(STATUS "-> Building cloe-osi library.")
file(GLOB cloe-osi_PUBLIC_HEADERS "include/**/*.hpp")
add_library(cloe-osi
# find src -type f -name "*.cpp" \! -name "*_test.cpp"
src/osi/utility/osi_ground_truth.cpp
src/osi/utility/osi_omni_sensor.cpp
src/osi/utility/osi_transceiver_tcp.cpp
src/osi/utility/osi_utils.cpp

# For IDE integration
${cloe-osi_PUBLIC_HEADERS}
)
add_library(cloe::osi ALIAS cloe-osi)
set_target_properties(cloe-osi PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
VERSION ${CLOE_PROJECT_VERSION}
)
target_include_directories(cloe-osi
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_link_libraries(cloe-osi
PUBLIC
cloe::models
cloe::runtime
Boost::headers
Eigen3::Eigen
open-simulation-interface::open-simulation-interface
)

# Testing -------------------------------------------------------------
include(CTest)
if(BUILD_TESTING)
find_package(GTest REQUIRED)
include(GoogleTest)

add_executable(test-osi
# find src -type f -name "*_test.cpp"
src/osi/component/osi_sensor_test.cpp
src/osi/utility/osi_test.cpp
)
set_target_properties(test-osi PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(test-osi
PRIVATE
GTest::gtest
GTest::gtest_main
Boost::boost
cloe::runtime
cloe::models
cloe::osi
open-simulation-interface::open-simulation-interface
)
gtest_add_tests(TARGET test-osi)
endif()

# Installation -------------------------------------------------------
install(TARGETS cloe-osi
LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
45 changes: 45 additions & 0 deletions optional/osi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
PROJECT_ROOT := ../..
include ${PROJECT_ROOT}/Makefile.package

# -------------------------------------------------

ALL_VENDOR := $(wildcard vendor/*)
.PHONY: ${ALL_VENDOR}

WITHOUT_VENDOR :=
UNSELECT_VENDOR := ${WITHOUT_VENDOR}
WITH_VENDOR :=
SELECT_VENDOR := $(call uniq, $(filter-out ${UNSELECT_VENDOR}, ${ALL_VENDOR}) ${WITH_VENDOR})

vendor/open-simulation-interface-3.3.1: vendor/protoc

REGEX_TARGET := 's/(-vendor|-select)?-each//'
${SELECT_VENDOR}:
${MAKE} -C $@ $(shell echo ${MAKECMDGOALS} | sed -re ${REGEX_TARGET})

# Usage: $(call _make_target_rule, TARGET-NAME, MAKE-TARGET, HELP-DESCRIPTION, MAKE-ARGUMENTS)
define _make_target_rule
${1}:
$(call print_header, "Proceeding to $(call unquote, ${3})")
${MAKE} ${SUBMAKEFLAGS} PROJECT_ROOT=$(realpath ${PROJECT_ROOT}) ${4} ${2}
endef

# Usage: $(call _make_target_rules, TARGET-NAME, HELP-DESCRIPTION, HELP-CATEGORY, PACKAGE-DIRS)
define _make_target_rules
help::
$(call print_help_target, ${1}, ${2}, ${3})
$(call _make_target_rule,${1},${1}-each,${2})
${1}-each: ${4}
endef

# Usage: $(call make_vendor_target, TARGET-NAME, HELP-DESCRIPTION, HELP-CATEGORY)
define make_vendor_target
$(eval $(call _make_target_rules,${1},${2},${3},${SELECT_VENDOR}))
endef

help::
$(call print_help_section, "Available vendor targets")

$(call make_vendor_target, export-vendor, "export all vendor packages", "[conan-cache]")
$(call make_vendor_target, package-vendor, "create all vendor packages", "[conan-cache]")
$(call make_vendor_target, download-vendor, "download or build vendor packages", "[conan-cache]")
96 changes: 96 additions & 0 deletions optional/osi/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# mypy: ignore-errors
# pylint: skip-file

from pathlib import Path
from conan import ConanFile
from conan.tools import cmake, files, scm


class CloeOsi(ConanFile):
name = "cloe-osi"
url = "/~https://github.com/eclipse/cloe"
description = "OSI sensor component and utility functions for data conversion to Cloe"
license = "Apache-2.0"
settings = "os", "compiler", "build_type", "arch"
options = {
"shared": [True, False],
"fPIC": [True, False],
"pedantic": [True, False],
}
default_options = {
"shared": True,
"fPIC": True,
"pedantic": True,
}
generators = "CMakeDeps", "VirtualRunEnv"
no_copy_source = True
exports_sources = [
"include/*",
"src/*",
"CMakeLists.txt",
]

def set_version(self):
version_file = Path(self.recipe_folder) / "../../VERSION"
if version_file.exists():
self.version = files.load(self, version_file).strip()
else:
git = scm.Git(self, self.recipe_folder)
self.version = git.run("describe --dirty=-dirty")[1:]

def requirements(self):
self.requires(f"cloe-models/{self.version}@cloe/develop")
self.requires(f"cloe-runtime/{self.version}@cloe/develop")
self.requires("open-simulation-interface/3.3.1@cloe/stable")
self.requires("boost/[>=1.65.1]")
self.requires("eigen/3.4.0")

def configure(self):
self.options["open-simulation-interface"].shared = self.options.shared
self.options["open-simulation-interface"].fPIC = True

def build_requirements(self):
self.test_requires("gtest/1.13.0")

def layout(self):
cmake.cmake_layout(self)

def generate(self):
tc = cmake.CMakeToolchain(self)
tc.cache_variables["CMAKE_EXPORT_COMPILE_COMMANDS"] = True
tc.cache_variables["CLOE_PROJECT_VERSION"] = self.version
tc.cache_variables["TargetLintingExtended"] = self.options.pedantic
tc.generate()

def build(self):
cm = cmake.CMake(self)
if self.should_configure:
cm.configure()
if self.should_build:
cm.build()
if self.should_test:
cm.test()

def package(self):
cm = cmake.CMake(self)
if self.should_install:
cm.install()

def package_id(self):
self.info.requires["boost"].full_package_mode()
self.info.requires["open-simulation-interface"].full_package_mode()
del self.info.options.pedantic

def package_info(self):
self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_file_name", "cloe-osi")
self.cpp_info.set_property("cmake_target_name", "cloe::osi")
self.cpp_info.set_property("pkg_config_name", "cloe-osi")

# Make sure we can find the library, both in editable mode and in the
# normal package mode:
self.cpp_info.libdirs = [f"lib"]
if not self.in_local_cache:
self.cpp_info.libs = ["cloe-osi"]
else:
self.cpp_info.libs = files.collect_libs(self)
104 changes: 104 additions & 0 deletions optional/osi/include/osi/component/osi_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2022 Robert Bosch GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* \file cloe/component/osi_sensor.hpp
* \see cloe/component/osi.hpp
*/

#pragma once
#ifndef CLOE_COMPONENT_OSI_SENSOR_HPP_
#define CLOE_COMPONENT_OSI_SENSOR_HPP_

#include <memory> // for shared_ptr

#include "osi_groundtruth.pb.h" // for GroundTruth
#include "osi_sensordata.pb.h" // for SensorData
#include "osi_sensorview.pb.h" // for SensorView

#include <cloe/component.hpp> // for Component, Json

namespace cloe_osi {

class OsiSensor : public cloe::Component {
public:
using cloe::Component::Component;
OsiSensor() : Component("osi_sensor") {}
virtual ~OsiSensor() noexcept = default;

/**
* Return OSI data, if available.
*/

virtual std::shared_ptr<osi3::GroundTruth> ground_truth() = 0;

virtual std::shared_ptr<osi3::SensorView> sensor_view() = 0;

virtual std::shared_ptr<osi3::SensorData> sensor_data() = 0;

/**
* Writes JSON representation into j.
*/
cloe::Json active_state() const override {
return cloe::Json{
/*{"ground_truth", this->ground_truth()},
{"sensor_view", this->sensor_view()},
{"sensor_data", this->sensor_data()},*/
};
}
};

class NopOsiSensor : public OsiSensor {
public:
using OsiSensor::OsiSensor;
NopOsiSensor() : OsiSensor("nop_osi_sensor") {}
virtual ~NopOsiSensor() noexcept = default;

std::shared_ptr<osi3::GroundTruth> ground_truth() override { return ground_truth_; }

std::shared_ptr<osi3::SensorView> sensor_view() override { return sensor_view_; }

std::shared_ptr<osi3::SensorData> sensor_data() override { return sensor_data_; }

void set_ground_truth(const osi3::GroundTruth& gt) {
ground_truth_ = std::make_shared<osi3::GroundTruth>(gt);
}

void set_sensor_view(const osi3::SensorView& view) {
sensor_view_ = std::make_shared<osi3::SensorView>(view);
}

void set_sensor_data(const osi3::SensorData& data) {
sensor_data_ = std::make_shared<osi3::SensorData>(data);
}

void reset() override {
OsiSensor::reset();
ground_truth_.reset();
sensor_view_.reset();
sensor_data_.reset();
}

protected:
std::shared_ptr<osi3::GroundTruth> ground_truth_{nullptr};
std::shared_ptr<osi3::SensorView> sensor_view_{nullptr};
std::shared_ptr<osi3::SensorData> sensor_data_{nullptr};
};

} // namespace cloe_osi

#endif // CLOE_COMPONENT_OSI_SENSOR_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include <cloe/simulator.hpp> // for ModelError

#include "osi_utils.hpp" // for osi_require, ..
#include "osi/utility/osi_utils.hpp" // for osi_require, ..

namespace osii {

Expand All @@ -47,7 +47,7 @@ class OsiGroundTruth {
* Store address of the GroundTruth object belonging to the OSI message
* that is to be processed.
*/
void set(const osi3::GroundTruth& osi_gt) { gt_ptr_ = &osi_gt; }
void set(const osi3::GroundTruth& osi_gt);

const osi3::GroundTruth& get_gt() const {
if (!gt_ptr_) error();
Expand Down
Loading

0 comments on commit 1eb0a15

Please sign in to comment.