ADD: added Sensor virtual PArent class

This commit is contained in:
Henry Winkel
2023-01-18 13:41:15 +01:00
parent f07a07b8d8
commit 193336e7fb
9 changed files with 413 additions and 5 deletions

View File

@@ -58,7 +58,8 @@ add_library(SimCore STATIC
include/SimCore/UtilFunctions.hpp
src/SimCore/UtilFunctions.cpp
include/SimCore/Sensor.hpp
src/SimCore/Sensor.cpp
)
target_link_libraries(SimCore
@@ -131,6 +132,10 @@ IF (${TEST_SIMCORE_LIBRARY})
target_link_libraries(test_UtilFunctions Catch2::Catch2 SimCore loguru)
catch_discover_tests(test_UtilFunctions)
add_executable(test_SensorClass tests/test_SensorClass.cpp)
target_link_libraries(test_SensorClass Catch2::Catch2 SimCore loguru)
catch_discover_tests(test_SensorClass)
ENDIF()

View File

@@ -0,0 +1,76 @@
#pragma once
#include "SimCore/Messages/Track.hpp"
#include <WHISPER/InternalUDPService.hpp>
#include <SimCore/Identifier.hpp>
#include <WHISPER/threadSafeQueue.hpp>
#include <WHISPER/Messages/Message.hpp>
#include <SimCore/SimCore.hpp>
#include <SimCore/Identifier.hpp>
#include <SimCore/Position.hpp>
#include <memory>
#include <thread>
namespace SimCore {
class Sensor {
public:
Sensor(SimCore::Identifier OwnID, SimCore::Identifier ParentID, SimCore::SensorKinds SensorKind,std::uint32_t GroundTruthPort, std::uint32_t ParentPort,std::string ParentIPAddress);
~Sensor();
void start();
void stop();
protected:
std::shared_ptr<WHISPER::threadSafeQueue<SimCore::Track>> incommingTrackMessages = nullptr;
std::shared_ptr<WHISPER::threadSafeQueue<WHISPER::Message>> incommingGroundThruthMessages = nullptr;
std::shared_ptr<WHISPER::threadSafeQueue<WHISPER::Message>> outgoingGroundThruthMessages = nullptr;
std::shared_ptr<WHISPER::threadSafeQueue<WHISPER::Message>> incommingParentMessages = nullptr;
std::shared_ptr<WHISPER::threadSafeQueue<WHISPER::Message>> outgoingParentMessages = nullptr;
virtual void specificSensorCalculations() = 0;
virtual void specificReloadCharacteristicts() = 0;
private:
SimCore::Identifier OwnID_;
SimCore::Identifier ParentID_;
SimCore::SensorKinds SensorKind_;
std::uint32_t GroundTruthPort_;
std::uint32_t ParentPort_;
std::string ParentIPAddress_;
std::shared_ptr<WHISPER::InternalUDPService> GroundTruthUDPService_ = nullptr;
std::shared_ptr<WHISPER::InternalUDPService> ParentUDPService_ = nullptr;
void receivingData();
void sendingData();
void SensorCalculations();
void ReloadCharacteristicts();
std::atomic<bool> stopReceivingGroundThruth = false;
std::atomic<bool> ReceivingGroundThruthIsRunnung = false;
std::atomic<bool> stopsendCalculatedData = false;
std::atomic<bool> sendCalculatedDataIsRunnung = false;
std::atomic<bool> stopCalculationData = false;
std::atomic<bool> CalculationIsRunnung = false;
std::thread receiveGroundTruthThread;
std::thread sendCalculatedDataThread;
std::thread sensorCalculationThread;
std::shared_ptr<Position> position_ = nullptr;
};
}

View File

@@ -37,4 +37,12 @@ enum ContactEnvironment : std::uint8_t{
};
enum SensorKinds : std::uint32_t {
RADAR = 1,
ELOKA,
SONAR,
VISUAL
};
}

View File

@@ -2,6 +2,7 @@
#include <vector>
#include <string>
#include <sstream>
#include <netdb.h>
@@ -12,9 +13,13 @@ namespace SimCore {
{
public:
static std::vector<std::string> explode(std::string const & s, char delim);
static std::string implode(const std::vector<std::string> &v , char delim);
static bool isNumber(const std::string& s);
static void check_host_name(int hostname);
static void check_host_entry(struct hostent * hostentry);
static void IP_formatter(char *IPbuffer);
static std::string getOwnIP();
};

182
src/SimCore/Sensor.cpp Normal file
View File

@@ -0,0 +1,182 @@
#include "SimCore/Messages/Track.hpp"
#include "SimCore/UtilFunctions.hpp"
#include "WHISPER/Messages/Message.hpp"
#include <SimCore/Sensor.hpp>
#include <memory>
namespace SimCore {
Sensor::Sensor(SimCore::Identifier OwnID, SimCore::Identifier ParentID, SimCore::SensorKinds SensorKind,std::uint32_t GroundTruthPort, std::uint32_t ParentPort,std::string ParentIPAddress):
OwnID_(OwnID),ParentID_(ParentID),SensorKind_(SensorKind),GroundTruthPort_(GroundTruthPort),ParentPort_(ParentPort),ParentIPAddress_(ParentIPAddress)
{
std::string ownIP = SimCore::UtilFunctions::getOwnIP();
auto ip = SimCore::UtilFunctions::explode(ownIP, '.');
ip[3] = "255";
LOG_S(INFO)<<SimCore::UtilFunctions::implode(ip,'.');
incommingGroundThruthMessages = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
outgoingGroundThruthMessages = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
incommingParentMessages = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
outgoingParentMessages = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
incommingTrackMessages = std::make_shared<WHISPER::threadSafeQueue<SimCore::Track>>();
GroundTruthUDPService_ = std::make_shared<WHISPER::InternalUDPService>(OwnID.getNumber(),WHISPER::SENSOR,8000,SimCore::UtilFunctions::implode(ip,'.'),ownIP);
ParentUDPService_ = std::make_shared<WHISPER::InternalUDPService>(OwnID.getNumber(),WHISPER::SENSOR,ParentPort,ParentIPAddress_,ownIP);
ReloadCharacteristicts();
};
Sensor::~Sensor(){
this->stop();
while (!this->incommingGroundThruthMessages.unique()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
this->incommingGroundThruthMessages.reset();
while (!this->outgoingGroundThruthMessages.unique()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
this->outgoingGroundThruthMessages.reset();
GroundTruthUDPService_->disconnect();
while (!this->GroundTruthUDPService_.unique()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
this->GroundTruthUDPService_.reset();
ParentUDPService_->disconnect();
while (!this->ParentUDPService_.unique()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
this->ParentUDPService_.reset();
}
void Sensor::start(){
stopReceivingGroundThruth = false;
receiveGroundTruthThread = std::thread(&Sensor::receivingData,this);
// stopsendCalculatedData = false;
// sendCalculatedDataThread = std::thread(&Sensor::sendingData,this);
// stopCalculationData = false;
// sensorCalculationThread = std::thread(&Sensor::SensorCalculations,this);
}
void Sensor::stop() {
while (ReceivingGroundThruthIsRunnung == true ) {
stopReceivingGroundThruth = true;
LOG_S(INFO)<<"waiting for receiving thread thread";
if (receiveGroundTruthThread.joinable() == true ) {
receiveGroundTruthThread.join();
}
}
while (sendCalculatedDataIsRunnung == true ) {
stopsendCalculatedData = true;
LOG_S(INFO)<<"waiting for receiving thread thread";
if (sendCalculatedDataThread.joinable() == true ) {
sendCalculatedDataThread.join();
}
}
while (CalculationIsRunnung == true ) {
stopCalculationData = true;
LOG_S(INFO)<<"waiting for receiving thread thread";
if (sensorCalculationThread.joinable() == true ) {
sensorCalculationThread.join();
}
}
}
void Sensor::receivingData()
{
this->ReceivingGroundThruthIsRunnung = true;
// GroundTruthUDPService_->connect(incommingGroundThruthMessages);
// GroundTruthUDPService_->subscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]);
// while (stopReceivingGroundThruth == false) {
// if (incommingGroundThruthMessages->size() > 0) {
// WHISPER::Message msg;
// incommingGroundThruthMessages->get(msg);
// if (msg.msgType_ == WHISPER::MsgType::RAW_TRACK) {
// auto Track = SimCore::Track(msg.serialize());
// incommingTrackMessages->addElement(Track);
// }
// }
// if (outgoingGroundThruthMessages->size() > 0) {
// WHISPER::Message msg;
// outgoingGroundThruthMessages->get(msg);
// GroundTruthUDPService_->publish(msg.serialize(), WHISPER::MsgTopicsMap[(WHISPER::MsgTopics)msg.topic_]);
// }
// }
// GroundTruthUDPService_->disconnect();
this->ReceivingGroundThruthIsRunnung = false;
}
void Sensor::sendingData()
{
this->sendCalculatedDataIsRunnung = true;
ParentUDPService_->connect(incommingParentMessages);
ParentUDPService_->subscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::COMMANDS]);
while (stopsendCalculatedData == false) {
if (incommingParentMessages->size() > 0) {
WHISPER::Message msg;
incommingGroundThruthMessages->get(msg);
LOG_S(INFO)<< "Message received from Parent is" << msg.msgType_;
}
if (outgoingParentMessages->size() > 0) {
WHISPER::Message msg;
outgoingParentMessages->get(msg);
ParentUDPService_->publish(msg.serialize(), WHISPER::MsgTopicsMap[(WHISPER::MsgTopics)msg.topic_]);
}
}
this->sendCalculatedDataIsRunnung = false;
}
void Sensor::SensorCalculations()
{
CalculationIsRunnung = true;
specificSensorCalculations();
while (!stopCalculationData) {
specificSensorCalculations();
}
CalculationIsRunnung = false;
}
void Sensor::ReloadCharacteristicts()
{
LOG_S(INFO)<<"loading specs";
// specificReloadCharacteristicts();
}
}

View File

@@ -1,4 +1,8 @@
#include <SimCore/UtilFunctions.hpp>
#include <arpa/inet.h>
#include <netdb.h>
#include <string>
#include <unistd.h>
namespace SimCore {
@@ -16,6 +20,22 @@ namespace SimCore {
return result;
}
std::string UtilFunctions::implode(const std::vector<std::string> &v , char delim)
{
std::string ret;
for(const auto &s : v)
{
if(!ret.empty())
{
ret += delim;
}
ret += s;
}
return ret;
}
bool UtilFunctions::isNumber(const std::string& s)
{
@@ -23,4 +43,38 @@ namespace SimCore {
s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
}
void UtilFunctions::check_host_name(int hostname) { //This function returns host name for local computer
if (hostname == -1) {
perror("gethostname");
exit(1);
}
}
void UtilFunctions::check_host_entry(struct hostent * hostentry) { //find host info from host name
if (hostentry == NULL){
perror("gethostbyname");
exit(1);
}
}
void IP_formatter(char *IPbuffer) { //convert IP string to dotted decimal format
if (NULL == IPbuffer) {
perror("inet_ntoa");
exit(1);
}
}
std::string UtilFunctions::getOwnIP(){
char host[256];
char *IP;
struct hostent *host_entry;
int hostname;
hostname = gethostname(host, sizeof(host)); //find the host name
check_host_name(hostname);
host_entry = gethostbyname(host); //find host information
check_host_entry(host_entry);
IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0])); //Convert into IP string
return IP;
};
}

View File

@@ -0,0 +1,67 @@
#include "SimCore/Identifier.hpp"
#include <SimCore/SimCore.hpp>
#include <memory>
#include <thread>
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <SimCore/Sensor.hpp>
// SimCore::Identifier OwnID, SimCore::Identifier ParentID, SimCore::SensorKinds SensorKind,std::uint32_t GroundTruthPort, std::uint32_t ParentPort,std::string ParentIPAddress
class Radar : public SimCore::Sensor
{
public:
Radar(SimCore::Identifier OwnID,
SimCore::Identifier ParentID,
SimCore::SensorKinds SensorKind,
std::uint32_t GroundTruthPort,
std::uint32_t ParentPort,
std::string ParentIPAddress,
std::string radarType):SimCore::Sensor(OwnID, ParentID, SensorKind, GroundTruthPort, ParentPort, ParentIPAddress),radarType_(radarType)
{
}
private:
std::string radarType_;
void specificSensorCalculations() override
{
LOG_S(INFO)<<"calculating";
};
void specificReloadCharacteristicts() override
{
LOG_S(INFO)<<"loading specifications";
};
};
SCENARIO("Testing the SimCore Sensor")
{
GIVEN("different Attributes for a Track in different forms")
{
SimCore::Identifier IDParent(0,1,false);
SimCore::Identifier IDRadar(1,1,false);
Radar Radar(IDRadar,IDParent,SimCore::SensorKinds::RADAR,8000,8001,"127.0.0.1","APAR");
Radar.start();
WHEN("constructing Track Object with data")
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
THEN("check if Track attributes are correct")
{
// REQUIRE(testOperator == true);
} //THEN
} // WHEN
} // GIVEN
} //SCENARIO

View File

@@ -2,6 +2,7 @@
#include <string>
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <loguru.hpp>
@@ -14,6 +15,14 @@ SCENARIO("Testing the SimCorePositionClass")
std::string s1 = "hello.world.guy";
std::string s2 = "my;Name;is;henry";
std::string ownIP = SimCore::UtilFunctions::getOwnIP();
LOG_S(INFO)<<ownIP;
auto ip = SimCore::UtilFunctions::explode(ownIP, '.');
ip[3] = "255";
LOG_S(INFO)<< ip[0]+"."+ip[1]+"."+ip[2]+"."+ip[3];
LOG_S(INFO)<<SimCore::UtilFunctions::implode(ip, '.');
WHEN("constructing Position Object with data")
{
@@ -29,6 +38,8 @@ SCENARIO("Testing the SimCorePositionClass")
REQUIRE(v2.size() == 4);
REQUIRE(v2[1].compare("Name") == 0);
REQUIRE(ownIP.size() > 0);
REQUIRE(ip.size() == 4);