onboardapi  9.10.0
by Rheinmetall
Examples

To use the onboardapi, download the binaries for the respective architecture.

The following code provides a complete implementation example on how the API is used. Note, however, that this would normally be done in two separate applications, each using the relevant client or service interface structure.

#include <iostream>
#include <chrono>
#include <thread>
#include <onboardapi/api/CoordinateFrame.hpp>
// Receive data from the service
struct CallbacksFromService : M_CoordinateFrame::IClient {
// Receiving from the connected service interface instance is achieved
// by implementing its methods and inspecting the method parameters.
std::cout << "Received ReportCoordinateFrameSettings" << std::endl;
}
void ReportCoordinateTransformations(std::string const&, M_Common::CoordinateTransformationListType const&, bool) override {}
void ReportPositions(std::string const&, M_Common::PositionListType const&, bool) override {}
void ReportAttitudes(std::string const&, M_Common::AttitudeListType const&, bool) override {}
void ReportLinearVelocities(std::string const&, M_Common::LinearVelocityListType const&, bool) override {}
void ReportAngularVelocities(std::string const&, M_Common::AngularVelocityListType const&, bool) override {}
void ReportLinearAccelerations(std::string const&, M_Common::LinearAccelerationListType const&, bool) override {}
void ReportAngularAccelerations(std::string const&, M_Common::AngularAccelerationListType const&, bool) override {}
};
// Receive data from clients.
struct CallbacksFromClients : M_CoordinateFrame::IService {
// Receiving from the connected client interface instance is achieved
// by implementing its methods and inspecting the method parameters.
void NotifyCoordinateTransformation(std::string const&, M_Common::CoordinateTransformationType const&) override {
std::cout << "Received NotifyCoordinateTransformation" << std::endl;
}
void NotifyPosition(std::string const&, M_Common::PositionType const&) override {}
void NotifyAttitude(std::string const&, M_Common::AttitudeType const&) override {}
void NotifyLinearVelocity(std::string const&, M_Common::LinearVelocityType const&) override {}
void NotifyAngularVelocity(std::string const&, M_Common::AngularVelocityType const&) override {}
};
// This merely demonstrates actual usage of the above interface
// implementations. Note, however, that in reality, two sides of an interface
// will always reside in different applications.
int main() {
std::cout << "Start Application ..." << std::endl;
const int domainId = 1;
const std::string serviceName = "TheCoolServiceName";
try { // all calls could fail because of accessing remote resources
// Create the service
CallbacksFromClients callbacksFromClients;
M_CoordinateFrame::Service service = M_CoordinateFrame::Service::create(domainId, serviceName, callbacksFromClients);
// Create a client
CallbacksFromService callbacksFromService;
M_CoordinateFrame::Client client = M_CoordinateFrame::Client::create(domainId, serviceName, callbacksFromService);
// Send data to the clients
service->ReportCoordinateFrameSettings(M_CoordinateFrame::CoordinateFrameSettingsType());
// Send data to the service
client->NotifyCoordinateTransformation(std::string(), M_Common::CoordinateTransformationType());
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
new oo-api
Definition: CoordinateFrame.hpp:251
static std::enable_if< std::is_same< T, int >::value &&std::is_same< IClientDerived, std::vector< ddkit::callback::Base * > >::value, Client >::type create(T domain, std::string serviceName, IClientDerived &callbacks)
Create dds-readers + writers. remove on Client leaving scope (overridden methods are auto-detected),...
Definition: CoordinateFrame.hpp:1068
Definition: CoordinateFrame.hpp:71
virtual void ReportCoordinateTransformations(std::string const &KeyId, M_Common::CoordinateTransformationListType const &TransformationList, bool IsRemoved)
Definition: CoordinateFrame.cpp:156
virtual void ReportLinearVelocities(std::string const &KeyId, M_Common::LinearVelocityListType const &LinearVelocityList, bool IsRemoved)
Definition: CoordinateFrame.cpp:207
virtual void ReportAttitudes(std::string const &KeyId, M_Common::AttitudeListType const &AttitudeList, bool IsRemoved)
Definition: CoordinateFrame.cpp:190
virtual void ReportCoordinateFrameSettings(M_CoordinateFrame::CoordinateFrameSettingsType const &Settings)
Definition: CoordinateFrame.cpp:126
virtual void ReportCoordinateFrameStatus(M_CoordinateFrame::CoordinateFrameStatusType const &Status, bool IsRemoved)
Definition: CoordinateFrame.cpp:139
virtual void ReportPositions(std::string const &KeyId, M_Common::PositionListType const &PositionList, bool IsRemoved)
Definition: CoordinateFrame.cpp:173
virtual void ReportAngularAccelerations(std::string const &KeyId, M_Common::AngularAccelerationListType const &AngularAccelerationList, bool IsRemoved)
Definition: CoordinateFrame.cpp:258
virtual void ReportLinearAccelerations(std::string const &KeyId, M_Common::LinearAccelerationListType const &LinearAccelerationList, bool IsRemoved)
Definition: CoordinateFrame.cpp:241
virtual void ReportAngularVelocities(std::string const &KeyId, M_Common::AngularVelocityListType const &AngularVelocityList, bool IsRemoved)
Definition: CoordinateFrame.cpp:224
Definition: CoordinateFrame.hpp:53
virtual void NotifyAngularVelocity(std::string const &KeyId, M_Common::AngularVelocityType const &AngularVelocity)
Definition: CoordinateFrame.cpp:110
virtual void NotifyPosition(std::string const &KeyId, M_Common::PositionType const &Position)
Definition: CoordinateFrame.cpp:71
virtual void NotifyAttitude(std::string const &KeyId, M_Common::AttitudeType const &Attitude)
Definition: CoordinateFrame.cpp:84
virtual void NotifyLinearVelocity(std::string const &KeyId, M_Common::LinearVelocityType const &LinearVelocity)
Definition: CoordinateFrame.cpp:97
virtual void NotifyCoordinateTransformation(std::string const &KeyId, M_Common::CoordinateTransformationType const &Transformation)
Definition: CoordinateFrame.cpp:58
new oo-api
Definition: CoordinateFrame.hpp:65
static std::enable_if< std::is_same< T, int >::value &&std::is_same< IServiceDerived, std::vector< ddkit::callback::Base * > >::value, Service >::type create(T domain, std::string serviceName, IServiceDerived &callbacks)
Create dds-readers + writers. remove on Service leaving scope (overridden methods are auto-detected),...
Definition: CoordinateFrame.hpp:639
std::vector< M_Common::AngularVelocityType > AngularVelocityListType
List of AngularVelocityType.
Definition: Common.hpp:2728
std::vector< M_Common::AttitudeType > AttitudeListType
List of AttitudeType.
Definition: Common.hpp:2292
std::vector< M_Common::CoordinateTransformationType > CoordinateTransformationListType
List of CoordinateTransformationType.
Definition: Common.hpp:3063
std::vector< M_Common::LinearVelocityType > LinearVelocityListType
List of LinearVelocityType.
Definition: Common.hpp:2601
std::vector< M_Common::PositionType > PositionListType
List of PositionType.
Definition: Common.hpp:2158
std::vector< M_Common::LinearAccelerationType > LinearAccelerationListType
List of LinearAccelerationType.
Definition: Common.hpp:2856
std::vector< M_Common::AngularAccelerationType > AngularAccelerationListType
List of AngualarAccelerationType.
Definition: Common.hpp:2983
Contains angular velocity.
Definition: Common.hpp:2660
Definition: Common.hpp:2219
Contains the transformation data between two specified coordinate frames.
Definition: Common.hpp:2988
Definition: Common.hpp:2518
Definition: Common.hpp:2080
Contains the settings of the coordinate frame service.
Definition: CoordinateFrame.hpp:134
Contains coordinate frame status data.
Definition: CoordinateFrame.hpp:205

Lambda Callbacks

The following code provides a complete implementation example on how the lambda callbacks are used.

#include <iostream>
#include <chrono>
#include <thread>
#include <onboardapi/api/CoordinateFrame.hpp>
int main() {
std::cout << "Start Application ..." << std::endl;
const int domainId = 1;
const std::string serviceName = "TheCoolServiceName";
try {
// Create the callbacks
std::vector<ddkit::callback::Base*> callbacks = {
[](std::string const& KeyId, M_Common::CoordinateTransformationType const& Transformation) {
std::cout << "Received NotifyCoordinateTransformation" << std::endl;
}
),
ddkit::callback::Callback(&M_CoordinateFrame::IService::NotifyPosition,
[](std::string const& KeyId, M_Common::PositionType const& Position) {
std::cout << "Received NotifyPosition" << std::endl;
}
)
};
// Create the service
M_CoordinateFrame::Service service = M_CoordinateFrame::Service::create(domainId, serviceName, callbacks);
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}

CMake-Integration

cmake_minimum_required(VERSION 3.18)
project(onboardapi_helloworld)
list(APPEND CMAKE_MODULE_PATH "/path/to/onboardapi_package_folder/cmake")
find_package(onboardapi REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC onboardapi::onboardapi)
Definition: Serialization.hpp:95

Optional

The following code demonstrates the use of ddkit::utils::types::Optional, which is a wrapper providing util functions.

It should be used for safe access to all *OptionalTypes, which consist of a vector with one element.

#include <iostream>
#include <vector>
#include <ddkit/utils/optional.hpp>
int main() {
std::vector<int> vec = {8};
ddkit::utils::types::Optional<int> opt(vec);
std::cout << opt.value() << std::endl; // Output current value (8)
opt.set(3); // Override value (3)
std::cout << std::boolalpha << opt << std::endl; // Output if optional has a value (true)
opt.reset(); // Clear optional and remove value
std::cout << std::boolalpha << opt << std::endl; // Output if optional has a value (false)
std::cout << opt.value_or(0) << std::endl; // Return default value if optional has no value (0)
opt = 9; // Override value (9)
std::cout << std::boolalpha << opt << std::endl; // Output if optional has a value (True)
std::cout << opt.value() << std::endl; // Output current value (9)
return 0;
}

Global Exception Handler

Uncaught global exceptions are being intercepted by ddkit::utils::exceptions::GlobalHandler.

The class offers a registry for callbacks templated by exception type:

#include "ddkit/utils/Exceptions.hpp"
int main() {
ddkit::utils::exceptions::GlobalHandler::registerHandler<std::exception>([](std::exception& exception){
ddkit::log::error() << "Uncaught exception" << typeid(exception).name() << "derived from std::exception:" << exception.what();
});
//ddkit::utils::exceptions::GlobalHandler::registerHandler<Custom::ExceptionType>([](Custom::ExceptionType& exception){
// ddkit::log::error() << "Uncaught exception" << typeid(exception).name() << "derived from Custom::ExceptionType:" << exception.what();
//});
ddkit::utils::exceptions::GlobalHandler::registerHandler([](std::exception_ptr){
ddkit::log::error() << "std::terminate triggered, uncaught exception occured!";
});
// throw Custom::Exception("foobar");
throw std::logic_error("I'm an example");
return 0;
}

CallbackInfo

Additional information about the current callback are available using ddkit::actors::CallbackInfo.

The following code demonstrates the use of it. Notice, that it has to be used inside the callback function!

#include <ddkit/actors/CallbackInfo.hpp>
struct CallbacksFromClients : M_CoordinateFrame::IService {
void NotifyCoordinateTransformation(std::string const&, M_Common::CoordinateTransformationType const&) override {
auto callbackInfo = ddkit::actors::CallbackInfo::current();
std::cout << callbackInfo << std::endl;
}
};