Skip to content

Commit

Permalink
reworked traci error handling, magic parmeter is now 'NEMA_offset' refs
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed Nov 11, 2021
1 parent 63f4c2f commit 5b8b703
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 51 deletions.
88 changes: 40 additions & 48 deletions src/libsumo/TrafficLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ void
TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
if (paramName=="NEMA_timing"){
TrafficLight::setNEMATiming(tlsID,value);
} else if (paramName=="offset") {
} else if (paramName=="NEMA_offset") {
TrafficLight::setNEMAOffset(tlsID,value);
} else{
return Helper::getTLS(tlsID).getActive()->setParameter(paramName, value);
Expand All @@ -702,57 +702,49 @@ TrafficLight::setParameter(const std::string& tlsID, const std::string& paramNam
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(TrafficLight, TL)

//timing="2.0 3.0 4.0 5.0 2.0 3.0 4.0 5.0"
bool TrafficLight::setNEMATiming(const std::string& tlsID,const std::string& timing){
try{
double newTiming[8];
std::string _timing = timing;
// convert string s to vector<string>
std::vector<std::string> split;
std::string delimiter = " ";
size_t pos = 0;
std::string token;
while ((pos = _timing.find(delimiter)) != std::string::npos) {
token = _timing.substr(0, pos);
split.push_back(token);
_timing.erase(0, pos + delimiter.length());
}
split.push_back(_timing);
//convert vector<string> to double[]
int i = 0;
for (auto s : split) {
double temp = std::stod(s);
newTiming[i] = temp;
i++;
}
// send the new timing to the controller
std::string programID = "NEMA";
MSTLLogicControl::TLSLogicVariants& test_logic = MSNet::getInstance()->getTLSControl().get(tlsID);
NEMALogic* target_logic = dynamic_cast<NEMALogic*>(test_logic.getLogic(programID));
if (target_logic != nullptr) {
target_logic->setNewTiming(newTiming);
}
return true;
}
catch(ProcessError& e){
return false;
void
TrafficLight::setNEMATiming(const std::string& tlsID,const std::string& timing){
double newTiming[8];
std::string _timing = timing;
// convert string s to vector<string>
std::vector<std::string> split;
std::string delimiter = " ";
size_t pos = 0;
std::string token;
while ((pos = _timing.find(delimiter)) != std::string::npos) {
token = _timing.substr(0, pos);
split.push_back(token);
_timing.erase(0, pos + delimiter.length());
}
split.push_back(_timing);
//convert vector<string> to double[]
int i = 0;
for (auto s : split) {
double temp = std::stod(s);
newTiming[i] = temp;
i++;
}
// send the new timing to the controller
MSTLLogicControl::TLSLogicVariants& test_logic = MSNet::getInstance()->getTLSControl().get(tlsID);
NEMALogic* target_logic = dynamic_cast<NEMALogic*>(test_logic.getActive());
if (target_logic != nullptr) {
target_logic->setNewTiming(newTiming);
} else {
throw TraCIException("'" + tlsID + "' is not a NEMA controller");
}
}


bool TrafficLight::setNEMAOffset(const std::string& tlsID,const std::string& offset) {
try {
double d_offset = std::stod(offset);
// send the new offset to the controller
std::string programID = "NEMA";
MSTLLogicControl::TLSLogicVariants& test_logic = MSNet::getInstance()->getTLSControl().get(tlsID);
NEMALogic* target_logic = dynamic_cast<NEMALogic*>(test_logic.getLogic(programID));
if (target_logic != nullptr) {
target_logic->setNewOffset(d_offset);
}
return true;
}
catch (ProcessError& e) {
return false;
void
TrafficLight::setNEMAOffset(const std::string& tlsID,const std::string& offset) {
double d_offset = std::stod(offset);
// send the new offset to the controller
MSTLLogicControl::TLSLogicVariants& test_logic = MSNet::getInstance()->getTLSControl().get(tlsID);
NEMALogic* target_logic = dynamic_cast<NEMALogic*>(test_logic.getActive());
if (target_logic != nullptr) {
target_logic->setNewOffset(d_offset);
} else {
throw TraCIException("'" + tlsID + "' is not a NEMA controller");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsumo/TrafficLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class TrafficLight {
static std::vector<libsumo::TraCISignalConstraint> findConstraintsDeadLocks(const std::string& foeId, const std::string& tripId, const std::string& foeSignal, const std::string& tlsID);
static SUMOVehicle* getVehicleByTripId(const std::string tripOrVehID);

static bool setNEMATiming(const std::string& tlsID,const std::string& timing);
static bool setNEMAOffset(const std::string& tlsID,const std::string& offset);
static void setNEMATiming(const std::string& tlsID,const std::string& timing);
static void setNEMAOffset(const std::string& tlsID,const std::string& offset);


private:
Expand Down
2 changes: 1 addition & 1 deletion tools/traci/_trafficlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def setNemaSplits(self, tlsID, timing):
self.setParameter(tlsID,"NEMA_timing",timing)

def setNemaOffset(self, tlsID, offset):
self.setParameter(tlsID,"offset",offset)
self.setParameter(tlsID,"NEMA_offset",offset)

def setPhaseDuration(self, tlsID, phaseDuration):
"""setPhaseDuration(string, double) -> None
Expand Down

0 comments on commit 5b8b703

Please sign in to comment.