MSActuatedTrafficLightLogic.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <utility>
00031 #include <vector>
00032 #include <bitset>
00033 #include <microsim/MSEventControl.h>
00034 #include <microsim/output/MSInductLoop.h>
00035 #include <microsim/MSNet.h>
00036 #include "MSTrafficLightLogic.h"
00037 #include "MSActuatedTrafficLightLogic.h"
00038 #include <microsim/MSLane.h>
00039 #include <netload/NLDetectorBuilder.h>
00040 #include <utils/common/TplConvert.h>
00041
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045
00046
00047
00048
00049
00050 MSActuatedTrafficLightLogic::MSActuatedTrafficLightLogic(MSTLLogicControl &tlcontrol,
00051 const std::string &id, const std::string &subid,
00052 const Phases &phases,
00053 unsigned int step, SUMOTime delay, SUMOReal maxGap, SUMOReal passingTime,
00054 SUMOReal detectorGap) throw()
00055 : MSSimpleTrafficLightLogic(tlcontrol, id, subid, phases, step, delay),
00056 myContinue(false),
00057 myMaxGap(maxGap), myPassingTime(passingTime), myDetectorGap(detectorGap) {}
00058
00059
00060 void
00061 MSActuatedTrafficLightLogic::init(NLDetectorBuilder &nb) throw(ProcessError) {
00062 SUMOReal det_offset = TplConvert<char>::_2SUMOReal(myParameter.find("detector_offset")->second.c_str());
00063
00064 SUMOTime inductLoopInterval = 1;
00065 LaneVectorVector::const_iterator i2;
00066 LaneVector::const_iterator i;
00067
00068 for (i2=myLanes.begin(); i2!=myLanes.end(); ++i2) {
00069 const LaneVector &lanes = *i2;
00070 for (i=lanes.begin(); i!=lanes.end(); i++) {
00071 MSLane *lane = (*i);
00072 SUMOReal length = lane->getLength();
00073 SUMOReal speed = lane->getMaxSpeed();
00074 SUMOReal inductLoopPosition = myDetectorGap * speed;
00075
00076 SUMOReal ilpos = length - inductLoopPosition;
00077 if (ilpos<0) {
00078 ilpos = 0;
00079 }
00080
00081 std::string id = "TLS" + myID + "_" + mySubID + "_InductLoopOn_" + lane->getID();
00082 if (myInductLoops.find(lane)==myInductLoops.end()) {
00083 myInductLoops[lane] =
00084 nb.createInductLoop(id, lane, ilpos);
00085 }
00086 }
00087
00088 for (i=lanes.begin(); i!=lanes.end(); i++) {
00089 MSLane *lane = (*i);
00090 SUMOReal length = lane->getLength();
00091
00092 SUMOReal lslen = det_offset;
00093 if (lslen>length) {
00094 lslen = length;
00095 }
00096 }
00097 }
00098 }
00099
00100
00101 MSActuatedTrafficLightLogic::~MSActuatedTrafficLightLogic() throw() {
00102 for (InductLoopMap::iterator i=myInductLoops.begin(); i!=myInductLoops.end(); ++i) {
00103 delete(*i).second;
00104 }
00105 }
00106
00107
00108
00109 SUMOTime
00110 MSActuatedTrafficLightLogic::trySwitch(bool) throw() {
00111
00112 gapControl();
00113 if (myContinue) {
00114 return duration();
00115 }
00116
00117 myStep++;
00118 assert(myStep<=myPhases.size());
00119 if (myStep==myPhases.size()) {
00120 myStep = 0;
00121 }
00122
00123 myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
00124
00125 return duration();
00126 }
00127
00128
00129
00130 SUMOTime
00131 MSActuatedTrafficLightLogic::duration() const throw() {
00132 if (myContinue) {
00133 return 1;
00134 }
00135 assert(myPhases.size()>myStep);
00136 if (!getCurrentPhaseDef().isGreenPhase()) {
00137 return getCurrentPhaseDef().duration;
00138 }
00139
00140 int newduration = (int) getCurrentPhaseDef().minDuration;
00141 const std::string &state = getCurrentPhaseDef().getState();
00142 for (unsigned int i=0; i<(unsigned int) state.size(); i++) {
00143 if (state[i]==MSLink::LINKSTATE_TL_GREEN_MAJOR||state[i]==MSLink::LINKSTATE_TL_GREEN_MINOR) {
00144 const std::vector<MSLane*> &lanes = getLanesAt(i);
00145 if (lanes.empty()) {
00146 break;
00147 }
00148 for (LaneVector::const_iterator j=lanes.begin(); j!=lanes.end(); j++) {
00149 InductLoopMap::const_iterator k = myInductLoops.find(*j);
00150 SUMOReal waiting = (SUMOReal)(*k).second->getNVehContributed();
00151 SUMOReal tmpdur = myPassingTime * waiting;
00152 if (tmpdur > newduration) {
00153
00154 newduration = (int) tmpdur;
00155 }
00156 if (newduration > (int) getCurrentPhaseDef().maxDuration) {
00157 return getCurrentPhaseDef().maxDuration;
00158 }
00159 }
00160 }
00161 }
00162 return newduration;
00163 }
00164
00165
00166 void
00167 MSActuatedTrafficLightLogic::gapControl() throw() {
00168
00169 assert(myPhases.size()>myStep);
00170 if (!getCurrentPhaseDef().isGreenPhase()) {
00171 myContinue = false;
00172 return;
00173 }
00174
00175
00176 SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
00177 if (actDuration >= getCurrentPhaseDef().maxDuration) {
00178 myContinue = false;
00179 return;
00180 }
00181
00182
00183 const std::string &state = getCurrentPhaseDef().getState();
00184 for (unsigned int i=0; i<(unsigned int) state.size(); i++) {
00185 if (state[i]==MSLink::LINKSTATE_TL_GREEN_MAJOR||state[i]==MSLink::LINKSTATE_TL_GREEN_MINOR) {
00186 const std::vector<MSLane*> &lanes = getLanesAt(i);
00187 if (lanes.empty()) {
00188 break;
00189 }
00190 for (LaneVector::const_iterator j=lanes.begin(); j!=lanes.end(); j++) {
00191 if (myInductLoops.find(*j)==myInductLoops.end()) {
00192 continue;
00193 }
00194 SUMOReal actualGap =
00195 myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
00196 if (actualGap < myMaxGap) {
00197 myContinue = true;
00198 return;
00199 }
00200 }
00201 }
00202 }
00203 myContinue = false;
00204 }
00205
00206
00207
00208
00209