/* ------------------------------------------------------------------------- * GUITLLogicPhasesTrackerWindow-methods * ----------------------------------------------------------------------- */ GUITLLogicPhasesTrackerWindow::GUITLLogicPhasesTrackerWindow( GUIMainWindow& app, MSTrafficLightLogic& logic, GUITrafficLightLogicWrapper& wrapper, ValueSource<std::pair<SUMOTime, MSPhaseDefinition> >* src) : FXMainWindow(app.getApp(), "TLS-Tracker", NULL, NULL, DECOR_ALL, 20, 20, 300, 200), myApplication(&app), myTLLogic(&logic), myAmInTrackingMode(true) { // build the toolbar myToolBarDrag = new FXToolBarShell(this, FRAME_NORMAL); myToolBar = new FXToolBar(this, myToolBarDrag, LAYOUT_SIDE_TOP | LAYOUT_FILL_X | FRAME_RAISED); new FXToolBarGrip(myToolBar, myToolBar, FXToolBar::ID_TOOLBARGRIP, TOOLBARGRIP_DOUBLE); // interval manipulation myBeginOffset = new FXRealSpinDial(myToolBar, 10, this, MID_SIMSTEP, LAYOUT_TOP | FRAME_SUNKEN | FRAME_THICK); myBeginOffset->setFormatString("%.0f"); myBeginOffset->setIncrements(1, 10, 100); myBeginOffset->setRange(60, 3600); myBeginOffset->setValue(240); new FXLabel(myToolBar, "(s)", 0, LAYOUT_CENTER_Y); // myConnector = new GLObjectValuePassConnector<std::pair<SUMOTime, MSPhaseDefinition> >(wrapper, src, this); FXint height = (FXint)(myTLLogic->getLinks().size() * 20 + 30 + 8 + 30); app.addChild(this, true); for (size_t i = 0; i < myTLLogic->getLinks().size(); ++i) { myLinkNames.push_back(toString<size_t>(i)); } FXVerticalFrame* glcanvasFrame = new FXVerticalFrame(this, FRAME_SUNKEN | LAYOUT_SIDE_TOP | LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0); myPanel = new GUITLLogicPhasesTrackerPanel(glcanvasFrame, *myApplication, *this); setTitle((logic.getID() + " - " + logic.getProgramID() + " - tracker").c_str()); setIcon(GUIIconSubSys::getIcon(ICON_APP_TLSTRACKER)); setHeight(height); }
GUITLLogicPhasesTrackerWindow::GUITLLogicPhasesTrackerWindow( GUIMainWindow& app, MSTrafficLightLogic& logic, GUITrafficLightLogicWrapper& /*wrapper*/, const MSSimpleTrafficLightLogic::Phases& /*phases*/) : FXMainWindow(app.getApp(), "TLS-Tracker", NULL, NULL, DECOR_ALL, 20, 20, 300, 200), myApplication(&app), myTLLogic(&logic), myAmInTrackingMode(false), myToolBarDrag(0), myBeginOffset(0) { myConnector = 0; FXint height = (FXint)(myTLLogic->getLinks().size() * 20 + 30 + 8); setTitle("TLS-Tracker"); app.addChild(this, true); for (size_t i = 0; i < myTLLogic->getLinks().size(); ++i) { myLinkNames.push_back(toString<size_t>(i)); } FXVerticalFrame* glcanvasFrame = new FXVerticalFrame(this, FRAME_SUNKEN | LAYOUT_SIDE_TOP | LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0); myPanel = new GUITLLogicPhasesTrackerPanel(glcanvasFrame, *myApplication, *this); setTitle((logic.getID() + " - " + logic.getProgramID() + " - tracker").c_str()); setIcon(GUIIconSubSys::getIcon(ICON_APP_TLSTRACKER)); setHeight(height); }
SUMOTime MSTLLogicControl::WAUTSwitchProcedure::getDiffToStartOfPhase(MSTrafficLightLogic& logic, SUMOTime toTime) { unsigned int stepOfMyPos = logic.getIndexFromOffset(toTime); SUMOTime startOfPhase = logic.getOffsetFromIndex(stepOfMyPos); assert(toTime >= startOfPhase); return toTime - startOfPhase; }
void GUINet::initTLMap() { // get the list of loaded tl-logics const std::vector<MSTrafficLightLogic*> &logics = getTLSControl().getAllLogics(); // allocate storage for the wrappers myTLLogicWrappers.reserve(logics.size()); // go through the logics for (std::vector<MSTrafficLightLogic*>::const_iterator i=logics.begin(); i!=logics.end(); ++i) { // get the logic MSTrafficLightLogic *tll = (*i); // get the links const MSTrafficLightLogic::LinkVectorVector &links = tll->getLinks(); if (links.size()==0) { continue; } // build the wrapper GUITrafficLightLogicWrapper *tllw = new GUITrafficLightLogicWrapper(GUIGlObjectStorage::gIDStorage, *myLogics, *tll); // build the association link->wrapper MSTrafficLightLogic::LinkVectorVector::const_iterator j; for (j=links.begin(); j!=links.end(); j++) { MSTrafficLightLogic::LinkVector::const_iterator j2; for (j2=(*j).begin(); j2!=(*j).end(); j2++) { myLinks2Logic[*j2] = tll->getID(); } } myLogics2Wrapper[tll] = tllw; } }
bool MSTLLogicControl::WAUTSwitchProcedure::isPosAtGSP(SUMOTime currentTime, const MSTrafficLightLogic& logic) { SUMOTime gspTime = TIME2STEPS(getGSPValue(logic)) % logic.getDefaultCycleTime(); SUMOTime programTime = logic.getOffsetFromIndex(logic.getCurrentPhaseIndex()) + (logic.getCurrentPhaseDef().duration - (logic.getNextSwitchTime() - currentTime)); return gspTime == programTime; }
std::pair<SUMOTime, MSPhaseDefinition> MSTLLogicControl::getPhaseDef(const std::string &tlid) const { MSTrafficLightLogic *tl = getActive(tlid); return std::make_pair( MSNet::getInstance()->getCurrentTimeStep(), tl->getCurrentPhaseDef()); }
GUIGLObjectPopupMenu* GUITrafficLightLogicWrapper::getPopUpMenu(GUIMainWindow& app, GUISUMOAbstractView& parent) { myApp = &app; GUIGLObjectPopupMenu* ret = new GUITrafficLightLogicWrapperPopupMenu(app, parent, *this); buildPopupHeader(ret, app); buildCenterPopupEntry(ret); // const MSTLLogicControl::TLSLogicVariants& vars = myTLLogicControl.get(myTLLogic.getID()); std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics(); if (logics.size() > 1) { std::vector<MSTrafficLightLogic*>::const_iterator i; size_t index = 0; for (i = logics.begin(); i != logics.end(); ++i, ++index) { if (!vars.isActive(*i)) { new FXMenuCommand(ret, ("Switch to '" + (*i)->getProgramID() + "'").c_str(), GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, (FXSelector)(MID_SWITCH + index)); } } new FXMenuSeparator(ret); } new FXMenuCommand(ret, "Switch off", GUIIconSubSys::getIcon(ICON_FLAG_MINUS), ret, MID_SWITCH_OFF); new FXMenuCommand(ret, "Track Phases", 0, ret, MID_TRACKPHASES); new FXMenuCommand(ret, "Show Phases", 0, ret, MID_SHOWPHASES); new FXMenuSeparator(ret); MSTrafficLightLogic* tll = myTLLogicControl.getActive(myTLLogic.getID()); buildNameCopyPopupEntry(ret); buildSelectionPopupEntry(ret); new FXMenuCommand(ret, ("phase: " + toString(tll->getCurrentPhaseIndex())).c_str(), 0, 0, 0); new FXMenuSeparator(ret); buildPositionCopyEntry(ret, false); return ret; }
void MSTLLogicControl::WAUTSwitchProcedure::switchToPos(SUMOTime simStep, MSTrafficLightLogic& logic, SUMOTime toTime) { unsigned int stepTo = logic.getIndexFromOffset(toTime); SUMOTime diff = getDiffToStartOfPhase(logic, toTime); const MSPhaseDefinition& phase = logic.getPhase(stepTo); SUMOTime leftDuration = phase.duration - diff; logic.changeStepAndDuration(myControl, simStep, stepTo, leftDuration); }
/* ------------------------------------------------------------------------- * method definitions for WAUTSwitchProcedure * ----------------------------------------------------------------------- */ unsigned int MSTLLogicControl::WAUTSwitchProcedure::getGSPValue(const MSTrafficLightLogic& logic) const { std::string val = logic.getParameterValue("GSP"); if (val.length() == 0) { return 0; } return TplConvert::_2int(val.c_str()); }
/* ------------------------------------------------------------------------- * method definitions for WAUTSwitchProcedure * ----------------------------------------------------------------------- */ int MSTLLogicControl::WAUTSwitchProcedure::getGSPValue(const MSTrafficLightLogic& logic) const { std::string val = logic.getParameter("GSP", ""); if (val.length() == 0) { return 0; } return StringUtils::toInt(val); }
void GUIViewTraffic::onGamingClick(Position pos) { MSTLLogicControl& tlsControl = MSNet::getInstance()->getTLSControl(); const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics(); MSTrafficLightLogic* minTll = 0; SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity(); for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) { // get the logic MSTrafficLightLogic* tll = (*i); if (tlsControl.isActive(tll)) { // get the links const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0); if (lanes.size() > 0) { const Position& endPos = lanes[0]->getShape().back(); if (endPos.distanceTo(pos) < minDist) { minDist = endPos.distanceTo(pos); minTll = tll; } } } } if (minTll != 0) { const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID()); const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics(); if (logics.size() > 1) { MSSimpleTrafficLightLogic* l = (MSSimpleTrafficLightLogic*) logics[0]; for (unsigned int i = 0; i < logics.size() - 1; ++i) { if (minTll->getProgramID() == logics[i]->getProgramID()) { l = (MSSimpleTrafficLightLogic*) logics[i + 1]; tlsControl.switchTo(minTll->getID(), l->getProgramID()); } } if (l == logics[0]) { tlsControl.switchTo(minTll->getID(), l->getProgramID()); } l->changeStepAndDuration(tlsControl, MSNet::getInstance()->getCurrentTimeStep(), 0, l->getPhase(0).duration); update(); } } }
SUMOTime Command_SaveTLSSwitches::execute(SUMOTime currentTime) { MSTrafficLightLogic* light = myLogics.getActive(); const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks(); const std::string& state = light->getCurrentPhaseDef().getState(); for (unsigned int i = 0; i < (unsigned int) links.size(); i++) { if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) { if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) { // was not saved before myPreviousLinkStates[i] = currentTime; continue; } } else { if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) { // was not yet green continue; } const MSTrafficLightLogic::LinkVector& currLinks = links[i]; const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i); SUMOTime lastOn = myPreviousLinkStates[i]; for (int j = 0; j < (int) currLinks.size(); j++) { MSLink* link = currLinks[j]; myOutputDevice << " <tlsSwitch id=\"" << light->getID() << "\" programID=\"" << light->getProgramID() << "\" fromLane=\"" << currLanes[j]->getID() << "\" toLane=\"" << link->getLane()->getID() << "\" begin=\"" << time2string(lastOn) << "\" end=\"" << time2string(currentTime) << "\" duration=\"" << time2string(currentTime - lastOn) << "\"/>\n"; } myPreviousLinkStates.erase(myPreviousLinkStates.find(i)); } } return DELTA_T; }
void NLHandler::addConnection(const SUMOSAXAttributes& attrs) { bool ok = true; std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok); if (!MSGlobals::gUsingInternalLanes && fromID[0] == ':') { return; } try { bool ok = true; const std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok); const int fromLaneIdx = attrs.get<int>(SUMO_ATTR_FROM_LANE, 0, ok); const int toLaneIdx = attrs.get<int>(SUMO_ATTR_TO_LANE, 0, ok); LinkDirection dir = parseLinkDir(attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok)); LinkState state = parseLinkState(attrs.get<std::string>(SUMO_ATTR_STATE, 0, ok)); std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, ""); #ifdef HAVE_INTERNAL_LANES std::string viaID = attrs.getOpt<std::string>(SUMO_ATTR_VIA, 0, ok, ""); #endif MSEdge* from = MSEdge::dictionary(fromID); if (from == 0) { WRITE_ERROR("Unknown from-edge '" + fromID + "' in connection"); return; } MSEdge* to = MSEdge::dictionary(toID); if (to == 0) { WRITE_ERROR("Unknown to-edge '" + toID + "' in connection"); return; } if (fromLaneIdx < 0 || static_cast<unsigned int>(fromLaneIdx) >= from->getLanes().size() || toLaneIdx < 0 || static_cast<unsigned int>(toLaneIdx) >= to->getLanes().size()) { WRITE_ERROR("Invalid lane index in connection from '" + from->getID() + "' to '" + to->getID() + "'."); return; } MSLane* fromLane = from->getLanes()[fromLaneIdx]; MSLane* toLane = to->getLanes()[toLaneIdx]; assert(fromLane); assert(toLane); int tlLinkIdx = -1; if (tlID != "") { tlLinkIdx = attrs.get<int>(SUMO_ATTR_TLLINKINDEX, 0, ok); // make sure that the index is in range MSTrafficLightLogic* logic = myJunctionControlBuilder.getTLLogic(tlID).getActive(); if (tlLinkIdx < 0 || tlLinkIdx >= (int)logic->getCurrentPhaseDef().getState().size()) { WRITE_ERROR("Invalid " + toString(SUMO_ATTR_TLLINKINDEX) + " '" + toString(tlLinkIdx) + "' in connection controlled by '" + tlID + "'"); return; } if (!ok) { return; } } SUMOReal length = fromLane->getShape()[-1].distanceTo(toLane->getShape()[0]); MSLink* link = 0; // build the link #ifdef HAVE_INTERNAL_LANES MSLane* via = 0; if (viaID != "" && MSGlobals::gUsingInternalLanes) { via = MSLane::dictionary(viaID); if (via == 0) { WRITE_ERROR("An unknown lane ('" + viaID + "') should be set as a via-lane for lane '" + toLane->getID() + "'."); return; } length = via->getLength(); } link = new MSLink(toLane, via, dir, state, length); if (via != 0) { via->addIncomingLane(fromLane, link); } else { toLane->addIncomingLane(fromLane, link); } #else link = new MSLink(toLane, dir, state, length); toLane->addIncomingLane(fromLane, link); #endif toLane->addApproachingLane(fromLane); // if a traffic light is responsible for it, inform the traffic light // check whether this link is controlled by a traffic light if (tlID != "") { myJunctionControlBuilder.getTLLogic(tlID).addLink(link, fromLane, tlLinkIdx); } // add the link fromLane->addLink(link); } catch (InvalidArgument& e) { WRITE_ERROR(e.what()); } }
// =========================================================================== // method definitions // =========================================================================== bool TraCIServerAPI_TLS::processGet(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) { // variable & id int variable = inputStorage.readUnsignedByte(); std::string id = inputStorage.readString(); // check variable if (variable != ID_LIST && variable != TL_RED_YELLOW_GREEN_STATE && variable != TL_COMPLETE_DEFINITION_RYG && variable != TL_CONTROLLED_LANES && variable != TL_CONTROLLED_LINKS && variable != TL_CURRENT_PHASE && variable != TL_CURRENT_PROGRAM && variable != TL_NEXT_SWITCH && variable != TL_PHASE_DURATION && variable != ID_COUNT) { server.writeStatusCmd(CMD_GET_TL_VARIABLE, RTYPE_ERR, "Get TLS Variable: unsupported variable specified", outputStorage); return false; } // begin response building tcpip::Storage tempMsg; // response-code, variableID, objectID tempMsg.writeUnsignedByte(RESPONSE_GET_TL_VARIABLE); tempMsg.writeUnsignedByte(variable); tempMsg.writeString(id); if (variable == ID_LIST) { std::vector<std::string> ids = MSNet::getInstance()->getTLSControl().getAllTLIds(); tempMsg.writeUnsignedByte(TYPE_STRINGLIST); tempMsg.writeStringList(ids); } else if (variable == ID_COUNT) { std::vector<std::string> ids = MSNet::getInstance()->getTLSControl().getAllTLIds(); tempMsg.writeUnsignedByte(TYPE_INTEGER); tempMsg.writeInt((int) ids.size()); } else { if (!MSNet::getInstance()->getTLSControl().knows(id)) { server.writeStatusCmd(CMD_GET_TL_VARIABLE, RTYPE_ERR, "Traffic light '" + id + "' is not known", outputStorage); return false; } MSTLLogicControl::TLSLogicVariants& vars = MSNet::getInstance()->getTLSControl().get(id); switch (variable) { case ID_LIST: break; case TL_RED_YELLOW_GREEN_STATE: { tempMsg.writeUnsignedByte(TYPE_STRING); std::string state = vars.getActive()->getCurrentPhaseDef().getState(); tempMsg.writeString(state); } break; case TL_COMPLETE_DEFINITION_RYG: { std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics(); tempMsg.writeUnsignedByte(TYPE_COMPOUND); tcpip::Storage tempContent; unsigned int cnt = 0; tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt((int) logics.size()); ++cnt; for (unsigned int i = 0; i < logics.size(); ++i) { MSTrafficLightLogic* logic = logics[i]; tempContent.writeUnsignedByte(TYPE_STRING); tempContent.writeString(logic->getProgramID()); ++cnt; // type (always 0 by now) tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt(0); ++cnt; // subparameter (always 0 by now) tempContent.writeUnsignedByte(TYPE_COMPOUND); tempContent.writeInt(0); ++cnt; // (current) phase index tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt((int) logic->getCurrentPhaseIndex()); ++cnt; // phase number unsigned int phaseNo = logic->getPhaseNumber(); tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt((int) phaseNo); ++cnt; for (unsigned int j = 0; j < phaseNo; ++j) { MSPhaseDefinition phase = logic->getPhase(j); tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt(phase.duration); ++cnt; tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt(phase.minDuration); ++cnt; // not implemented tempContent.writeUnsignedByte(TYPE_INTEGER); tempContent.writeInt(phase.maxDuration); ++cnt; // not implemented const std::string& state = phase.getState(); //unsigned int linkNo = (unsigned int)(vars.getActive()->getLinks().size()); tempContent.writeUnsignedByte(TYPE_STRING); tempContent.writeString(state); ++cnt; } } tempMsg.writeInt((int) cnt); tempMsg.writeStorage(tempContent); } break; case TL_CONTROLLED_LANES: { const MSTrafficLightLogic::LaneVectorVector& lanes = vars.getActive()->getLanes(); tempMsg.writeUnsignedByte(TYPE_STRINGLIST); std::vector<std::string> laneIDs; for (MSTrafficLightLogic::LaneVectorVector::const_iterator i = lanes.begin(); i != lanes.end(); ++i) { const MSTrafficLightLogic::LaneVector& llanes = (*i); for (MSTrafficLightLogic::LaneVector::const_iterator j = llanes.begin(); j != llanes.end(); ++j) { laneIDs.push_back((*j)->getID()); } } tempMsg.writeStringList(laneIDs); } break; case TL_CONTROLLED_LINKS: { const MSTrafficLightLogic::LaneVectorVector& lanes = vars.getActive()->getLanes(); const MSTrafficLightLogic::LinkVectorVector& links = vars.getActive()->getLinks(); // tempMsg.writeUnsignedByte(TYPE_COMPOUND); tcpip::Storage tempContent; unsigned int cnt = 0; tempContent.writeUnsignedByte(TYPE_INTEGER); unsigned int no = (unsigned int) lanes.size(); tempContent.writeInt((int) no); for (unsigned int i = 0; i < no; ++i) { const MSTrafficLightLogic::LaneVector& llanes = lanes[i]; const MSTrafficLightLogic::LinkVector& llinks = links[i]; // number of links controlled by this signal (signal i) tempContent.writeUnsignedByte(TYPE_INTEGER); unsigned int no2 = (unsigned int) llanes.size(); tempContent.writeInt((int) no2); ++cnt; for (unsigned int j = 0; j < no2; ++j) { MSLink* link = llinks[j]; std::vector<std::string> def; // incoming lane def.push_back(llanes[j]->getID()); // approached non-internal lane (if any) def.push_back(link->getLane() != 0 ? link->getLane()->getID() : ""); // approached "via", internal lane (if any) #ifdef HAVE_INTERNAL_LANES def.push_back(link->getViaLane() != 0 ? link->getViaLane()->getID() : ""); #else def.push_back(""); #endif tempContent.writeUnsignedByte(TYPE_STRINGLIST); tempContent.writeStringList(def); ++cnt; } } tempMsg.writeInt((int) cnt); tempMsg.writeStorage(tempContent); } break; case TL_CURRENT_PHASE: tempMsg.writeUnsignedByte(TYPE_INTEGER); tempMsg.writeInt((int) vars.getActive()->getCurrentPhaseIndex()); break; case TL_CURRENT_PROGRAM: tempMsg.writeUnsignedByte(TYPE_STRING); tempMsg.writeString(vars.getActive()->getProgramID()); break; case TL_PHASE_DURATION: tempMsg.writeUnsignedByte(TYPE_INTEGER); tempMsg.writeInt((int) vars.getActive()->getCurrentPhaseDef().duration); break; case TL_NEXT_SWITCH: tempMsg.writeUnsignedByte(TYPE_INTEGER); tempMsg.writeInt((int) vars.getActive()->getNextSwitchTime()); break; case TL_CONTROLLED_JUNCTIONS: { } break; default: break; } } server.writeStatusCmd(CMD_GET_TL_VARIABLE, RTYPE_OK, "", outputStorage); server.writeResponseWithLength(outputStorage, tempMsg); return true; }
/* ------------------------------------------------------------------------- * GUITrafficLightLogicWrapper - methods * ----------------------------------------------------------------------- */ GUITrafficLightLogicWrapper::GUITrafficLightLogicWrapper( MSTLLogicControl& control, MSTrafficLightLogic& tll) : GUIGlObject(GLO_TLLOGIC, tll.getID()), myTLLogicControl(control), myTLLogic(tll) {}