Skip to content
Snippets Groups Projects
Commit 30945204 authored by Franz Bethke's avatar Franz Bethke
Browse files

Implements PLC-logic

parent 2830a41a
Branches
No related merge requests found
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "EgoMotion.h" #include "EgoMotion.h"
#include "PlatoonState.h" #include "PlatoonState.h"
#include <atomic> #include <atomic>
#include <chrono>
namespace car namespace car
{ {
...@@ -30,21 +31,29 @@ public: ...@@ -30,21 +31,29 @@ public:
Callback cruiseControllerNotify; Callback cruiseControllerNotify;
// TODO check these values !
// TODO make values ATOMIC ! // TODO make values ATOMIC !
// these values need to be stored and atomic, since they will be pulled from other modules // these value need to be stored and atomic, since they will be pulled from other modules
PlatoonState curState = PlatoonState::CACC_FV; PlatoonState curState = PlatoonState::ACC;
float desSpeed = 0.0;
platoonProtocol::PlatoonConfig platoonConfig; platoonProtocol::PlatoonConfig platoonConfig; // TODO needs to be removed, once C2C returns TimedValues
private: private:
platoonProtocol::VehicleFacade& c2c; platoonProtocol::VehicleFacade& c2c;
pc2car::CommandReceiver::Ptr pc;
EgoMotion& egoMotion;
bool c2cAlive = false; bool c2cAlive = false;
// void updateC2cConfig(); // once C2C returns TimedValues
pc2car::CommandReceiver::Ptr pc; void updatePcConfig();
pc2car::TimedValue<platoonProtocol::PlatoonSpeed> PS{0.0f};
pc2car::TimedValue<platoonProtocol::InnerPlatoonDistance> IPD{0.0f};
EgoMotion& egoMotion; void updateDesSpeed();
pc2car::TimedValue<float> desSpeed{0.0f};
void setupC2C();
// METHODS // METHODS
void run_ACC(); void run_ACC();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "../../include/mainNode/PlatoonController.h" #include "../../include/mainNode/PlatoonController.h"
#include "PlatoonProtocolLib/VehicleFacade.h" #include "PlatoonProtocolLib/VehicleFacade.h"
#include "PC2CarLib/TimedValue.h"
#include <iostream> #include <iostream>
namespace car namespace car
...@@ -12,58 +13,134 @@ namespace car ...@@ -12,58 +13,134 @@ namespace car
PlatoonController::PlatoonController(platoonProtocol::VehicleFacade& c2c, PlatoonController::PlatoonController(platoonProtocol::VehicleFacade& c2c,
pc2car::CommandReceiver::Ptr pc, EgoMotion& egoMotion) pc2car::CommandReceiver::Ptr pc, EgoMotion& egoMotion)
: cruiseControllerNotify() : cruiseControllerNotify()
, platoonConfig(c2c.getPlatoonConfig())
, c2c(c2c) , c2c(c2c)
, pc(pc) , pc(pc)
, egoMotion(egoMotion) , egoMotion(egoMotion)
, platoonConfig(c2c.getPlatoonConfig())
{} {}
void PlatoonController::updatePcConfig() {
pc2car::TimedValue<platoonProtocol::PlatoonSpeed> otherPS = pc->getPlatoonSpeed();
pc2car::TimedValue<platoonProtocol::InnerPlatoonDistance> otherIPD = pc->getInnerPlatoonDistance();
if ( otherPS.getTimestamp() > PS.getTimestamp() ) { PS = otherPS; }
if ( otherIPD.getTimestamp() > IPD.getTimestamp() ) { IPD = otherIPD; }
void PlatoonController::run() cruiseControllerNotify();
{ }
std::cout << "PlatoonController was run." << std::endl;
void PlatoonController::updateDesSpeed() {
switch (curState) { pc2car::TimedValue<float> otherDesSpeed = pc->getInnerPlatoonDistance();
case PlatoonState::ACC: { if ( otherDesSpeed.getTimestamp() > desSpeed.getTimestamp() ) { desSpeed = otherDesSpeed; }
run_ACC();
break; cruiseControllerNotify();
} }
case PlatoonState::CACC_FV: { void PlatoonController::setupC2C() {
run_CACC_FV(); bool wantsPlatoon = pc->isPlatoonEnabled().get();
break;
} if ( c2cAlive ) { return; } // if C2C is already alive ... do nothing
if (!wantsPlatoon ) { return; } // if we dont want platoon ...
case PlatoonState::CACC_LV: {
run_CACC_LV(); // get role according to distance
break; if ( egoMotion.getDistance() == std::numeric_limits<float>::infinity() ) {
c2c.reset(platoonProtocol::Vehicle::Role::LEADER);
c2c.setInnerPlatoonDistance(IPD.get());
c2c.setPlatoonSpeed(PS.get());
} else {
c2c.reset(platoonProtocol::Vehicle::Role::FOLLOWER);
} }
} c2c.createPlatoon();
c2cAlive = true;
} }
void PlatoonController::run() {
std::cout << "PlatoonController was run." << std::endl;
switch (curState) {
case PlatoonState::ACC: { run_ACC(); break; }
case PlatoonState::CACC_FV: { run_CACC_FV(); break; }
case PlatoonState::CACC_LV: { run_CACC_LV(); break; }
}
}
void PlatoonController::run_ACC() { void PlatoonController::run_ACC() {
bool inPlatoon = c2c.isPlatoonRunning();
bool wantsPlatoon = pc->isPlatoonEnabled().get(); bool wantsPlatoon = pc->isPlatoonEnabled().get();
// platoonProtocol::PlatoonSpeed PS = pc.getPlatoonSpeed();
if (!wantsPlatoon) {
if (c2cAlive) { c2c.leavePlatoon(); c2cAlive = false;}
updateDesSpeed();
return;
}
// wantsPlatoon
if (!c2cAlive) {
updateDesSpeed();
setupC2C();
return;
}
// c2cAlive + wantsPlatoon
if (c2c.isPlatoonRunning()) {
if (c2c.getRole() == platoonProtocol::Vehicle::Role::LEADER) {
updatePcConfig();
curState = PlatoonState::CACC_LV;
}
else {curState = PlatoonState::CACC_FV;}
return;
}
// !inPlatoon + c2cAlive + wantsPlatoon
bool isLeader = c2c.getRole() == platoonProtocol::Vehicle::Role::LEADER;
bool hasFiniteDistance = egoMotion.getDistance() < std::numeric_limits<float>::infinity();
if ( isLeader != hasFiniteDistance ) { // role does not fit
c2c.leavePlatoon();
c2cAlive = false;
updateDesSpeed();
setupC2C();
return;
}
// role fits + !inPlatoon + c2cAlive + wantsPlatoon
if (c2c.getRole() == platoonProtocol::Vehicle::Role::LEADER) {updatePcConfig();}
updateDesSpeed();
return;
} }
void PlatoonController::run_CACC_FV() { void PlatoonController::run_CACC_FV() {
bool inPlatoon = c2c.isPlatoonRunning(); bool inPlatoon = c2c.isPlatoonRunning();
bool wantsPlatoon = pc->isPlatoonEnabled().get(); bool wantsPlatoon = pc->isPlatoonEnabled().get();
// Although this value will not be used in this method it still // Although this value will not be used in this method it still
// needs to be updated, so the new value can be pulled be CC // needs to be updated, so the new value can be pulled be CC
platoonConfig = c2c.getPlatoonConfig(); platoonConfig = c2c.getPlatoonConfig(); // TODO <- updateC2CConfig();
std::cout << "Running PlatoonController::run_CACC_FV: inPlatoon = " << inPlatoon std::cout << "Running PlatoonController::run_CACC_FV: inPlatoon = " << inPlatoon
<< ", wantsPlatoon = " << wantsPlatoon << std::endl; << ", wantsPlatoon = " << wantsPlatoon << std::endl;
if (inPlatoon && wantsPlatoon) { cruiseControllerNotify(); return; }
if (inPlatoon && !wantsPlatoon) { c2c.leavePlatoon(); }
c2cAlive = false;
curState = PlatoonState::ACC;
cruiseControllerNotify();
run_ACC();
return;
}
void PlatoonController::run_CACC_LV() {
bool inPlatoon = c2c.isPlatoonRunning();
bool wantsPlatoon = pc->isPlatoonEnabled().get();
std::cout << "Running PlatoonController::run_CACC_LV: inPlatoon = " << inPlatoon
<< ", wantsPlatoon = " << wantsPlatoon << std::endl;
if (inPlatoon && wantsPlatoon) { if (inPlatoon && wantsPlatoon) {
cruiseControllerNotify(); updatePcConfig();
return; c2c.setInnerPlatoonDistance(IPD.get());
return;
} }
if (inPlatoon && !wantsPlatoon) { if (inPlatoon && !wantsPlatoon) {
...@@ -74,11 +151,7 @@ void PlatoonController::run_CACC_FV() { ...@@ -74,11 +151,7 @@ void PlatoonController::run_CACC_FV() {
curState = PlatoonState::ACC; curState = PlatoonState::ACC;
cruiseControllerNotify(); cruiseControllerNotify();
run_ACC(); run_ACC();
return; return;
}
void PlatoonController::run_CACC_LV() {
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment