ADD: added new version of entity base class

This commit is contained in:
Henry Winkel
2023-07-05 17:58:59 +02:00
parent bbbd5b96df
commit 64f37e59f5
3 changed files with 103 additions and 153 deletions

View File

@@ -2,8 +2,10 @@
#include "DirectCommunicationServer.hpp"
#include "Entities/Movement.hpp" #include "Entities/Movement.hpp"
#include "SimCore/Messages/GroundThruthTrack.hpp" #include "SimCore/Messages/GroundThruthTrack.hpp"
#include "SimCore/Messages/SimTrack.hpp"
#include "SimCore/Messages/Track.hpp" #include "SimCore/Messages/Track.hpp"
#include "SimCore/Orientation.hpp" #include "SimCore/Orientation.hpp"
#include "SimCore/SafeMap.hpp" #include "SimCore/SafeMap.hpp"
@@ -53,62 +55,61 @@ namespace Entities {
SimCore::Identifier ParentID, SimCore::Identifier ParentID,
SimCore::EntityKind EntityKind, SimCore::EntityKind EntityKind,
std::uint32_t GroundTruthPort, std::uint32_t GroundTruthPort,
std::uint32_t CommandPort, ushort CommandPort);
std::string CommandIPAddress);
~Entity(); ~Entity();
void start(); void start();
void stop(); void stop();
void setPosition(SimCore::Position);
void setSpeed(double speed);
void setCourse(double course);
void setPitch( double pitch);
protected: protected:
std::shared_ptr<WHISPER::threadSafeQueue<WHISPER::Message>> incommingCommandMessages = nullptr;
std::shared_ptr<WHISPER::threadSafeQueue<WHISPER::Message>> outgoingCommandMessages = nullptr;
virtual void specificPhysicsCalculations(std::chrono::milliseconds::rep duration) = 0; virtual void specificPhysicsCalculations(std::chrono::milliseconds::rep duration) = 0;
virtual void specificReloadCharacteristicts() = 0; virtual void specificReloadCharacteristicts() = 0;
std::unique_ptr<Entities::Movement> Movement_ = nullptr;
protected: protected:
std::shared_ptr<SimCore::SimTrack> OwnShipTrack = nullptr;
std::string EntityName_; std::string EntityName_;
SimCore::GroundTruthTrack ownTrack_;
SimCore::Identifier ParentID_; SimCore::Identifier ParentID_;
SimCore::EntityKind EntityKind_; SimCore::EntityKind EntityKind_;
std::uint32_t GroundTruthPort_;
std::uint32_t CommandPort_; ushort MovemntWorkerPort_;
std::string CommandIPAddress_;
private: private:
std::vector<std::thread> threads; std::vector<std::thread> threads;
std::atomic<bool> stopMainLoop = false;
std::atomic<bool> stopCommandWorker = false; void MainLoop();
std::atomic<bool> stopSensorWorker = false;
std::atomic<bool> stopTrackWorker = false;
std::atomic<bool> stopPhysicsWorker = false; void startMovementWorker();
std::atomic<bool> MovementWorkerStarted = false;
std::shared_ptr<DirectCommunication::DirectCommunicationServer> MovemtServer_ = nullptr;
std::atomic<bool> physicsIsRunning = false;
void CommandWorker();
void SensorWorker();
void TrackWorker();
void physicsWorker();
void startSensor(); void startSensor();
std::shared_ptr<WHISPER::InternalUDPSender> GroundTruthUDPSender_ = nullptr;
std::shared_ptr<std::list<Entities::SensorClientData>> SensorStore_; std::shared_ptr<std::list<Entities::SensorClientData>> SensorStore_;

View File

@@ -1,4 +1,6 @@
#include "DirectCommunicationServer.hpp"
#include "Entities/Movement.hpp" #include "Entities/Movement.hpp"
#include "SimCore/Messages/SimTrack.hpp"
#include "WHISPER/InternalUDPListener.hpp" #include "WHISPER/InternalUDPListener.hpp"
#include "WHISPER/InternalUDPSender.hpp" #include "WHISPER/InternalUDPSender.hpp"
#include "WHISPER/Messages/Message.hpp" #include "WHISPER/Messages/Message.hpp"
@@ -14,7 +16,7 @@
#define calculationPeriode 200 #define __MOVEMENT_SERVER_PORT__ 5556
namespace Entities namespace Entities
{ {
@@ -25,16 +27,17 @@ namespace Entities
SimCore::Identifier ParentID, SimCore::Identifier ParentID,
SimCore::EntityKind EntityKind, SimCore::EntityKind EntityKind,
std::uint32_t GroundTruthPort, std::uint32_t GroundTruthPort,
std::uint32_t CommandPort, ushort CommandPort):
std::string CommandIPAddress):
EntityName_(EnttityName), EntityName_(EnttityName),
ownTrack_(OwnType, OwnID, SimCore::TrackKind::GROUND_TRUTH_TRACK),
ParentID_(ParentID), ParentID_(ParentID),
EntityKind_(EntityKind), EntityKind_(EntityKind)
GroundTruthPort_(GroundTruthPort),
CommandPort_(CommandPort),
CommandIPAddress_(CommandIPAddress)
{ {
OwnShipTrack = std::make_shared<SimCore::SimTrack>(OwnID, OwnType, EntityKind);
MovemtServer_ = std::make_shared<DirectCommunication::DirectCommunicationServer>(__MOVEMENT_SERVER_PORT__);
} }
@@ -44,35 +47,42 @@ namespace Entities
stop(); stop();
} }
void Entity::setPosition(SimCore::Position pos)
{
OwnShipTrack->setPosition(pos);
}
void Entity::setSpeed(double val)
{
OwnShipTrack->Speed.setValue(val);
}
void Entity::setCourse(double val)
{
OwnShipTrack->Course.setValue(val);
}
void Entity::setPitch( double val)
{
OwnShipTrack->Pitch.setValue(val);
}
void Entity::start() void Entity::start()
{ {
stopCommandWorker = false; stopMainLoop = false;
stopSensorWorker = false;
stopTrackWorker = false;
stopPhysicsWorker = false; threads.emplace_back(std::thread(&Entity::MainLoop,this));
threads.emplace_back(std::thread(&Entity::CommandWorker,this));
threads.emplace_back(std::thread(&Entity::SensorWorker,this));
threads.emplace_back(std::thread(&Entity::TrackWorker,this));
threads.emplace_back(std::thread(&Entity::physicsWorker,this));
} }
void Entity::stop() void Entity::stop()
{ {
stopCommandWorker = true; stopMainLoop = true;
stopSensorWorker = true;
stopTrackWorker = true;
stopPhysicsWorker = true;
// for (auto &th :threads)
// {
// if (th.joinable()) {
// th.join();
// }
// }
for (std::vector<std::thread>::iterator it = threads.begin(); it != threads.end();) for (std::vector<std::thread>::iterator it = threads.begin(); it != threads.end();)
@@ -90,110 +100,49 @@ namespace Entities
} }
void Entity::physicsWorker() void Entity::startMovementWorker()
{ {
physicsIsRunning = true;
while (!stopPhysicsWorker) { LOG_S(INFO)<< "TODO: starting the movement app from here";
auto start = std::chrono::steady_clock::now(); if (MovemtServer_->countClients() > 0 )
std::this_thread::sleep_for(std::chrono::milliseconds(calculationPeriode));
auto end = std::chrono::steady_clock::now();
std::chrono::milliseconds::rep duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if(Movement_ != nullptr)
{ {
// Movement_->updatePosition(duration); auto msg = MovemtServer_->getLatestMessage();
Movement_->updatePositionSimple(duration); if (*msg.get() == "Hello Server")
}
// LOG_S(INFO)<<"Entity class working";
specificPhysicsCalculations(duration);
}
physicsIsRunning = false;
}
void Entity::CommandWorker()
{ {
auto CommandUDPListener = std::make_shared<WHISPER::InternalUDPListener>(CommandPort_) ; MovemtServer_->sendMessage(OwnShipTrack->buildMessage().serialize());
auto receiverQueue = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
CommandUDPListener->connect(receiverQueue);
auto CommandUDPSender = std::make_shared<WHISPER::InternalUDPSender>(CommandIPAddress_,CommandPort_);
while (!stopCommandWorker)
{
if (receiverQueue->size() > 0) {
auto msg = WHISPER::Message();
receiverQueue->get(msg);
switch (msg.msgType_) {
case WHISPER::MsgType::STRINGDATA :{
WHISPER::StringData stringMsg = WHISPER::StringData(msg.serialize());
std::string str = stringMsg.data_;
break;
}
case WHISPER::MsgType::COMMAND: {
WHISPER::StringData string = WHISPER::StringData(msg.serialize());
break;
} }
} }
} }
// LOG_S(INFO)<<"hello from command worker";
std::this_thread::sleep_for(std::chrono::milliseconds(900));
} void Entity::MainLoop()
}
void Entity::SensorWorker()
{ {
while (!stopSensorWorker) while (!stopMainLoop)
{ {
// LOG_S(INFO)<<"hello from sensor worker"; if (!MovementWorkerStarted)
std::this_thread::sleep_for(std::chrono::milliseconds(900));
}
}
void Entity::TrackWorker()
{ {
while (!stopTrackWorker) startMovementWorker();
{
// LOG_S(INFO)<<"hello from track worker";
std::this_thread::sleep_for(std::chrono::milliseconds(900));
}
}
void Entity::startSensor()
{
LOG_S(ERROR)<< "starting new pods is not implemented yet";
} }
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
} }