如何解决在 OMNeT++ 中改变交通灯的状态
我正在进行一项研究,其中包括一个交叉点。模拟应由 SUMO、OMNeT++ 和 Veins 完成。当紧急车辆接近十字路口时,该道路的红绿灯状态应更改为绿色,该道路在穿越该状态后应恢复到先前状态。我已经在 OMNeT++ 上生成了 XML 和 SUMO 文件以及一些代码,但是交通灯的状态无法更改。我认为即使是 onBSM 功能也不起作用!!!如果有人可以帮助我,我将不胜感激。
'''
#include <iostream>
#include "mynet.h"
#define debugEV_clear EV
#define debugEV EV << logName() << "::" << getClassName() << ": "
//here you can see mynet.cc file
Define_Module(Veins::mynet);
void Veins::mynet::initialize(int stage) {
BaseWaveApplLayer::initialize(stage);
if (stage == 0) {
}
else if (stage == 1) {
initMsg = new cmessage("traffic light init",77);
phaseMsg = new cmessage("phase msg",88); //88 & 77 are just
random
number for handling messag efunction
scheduleAt(simTime()+0.1,initMsg);
}
}
void Veins::mynet::onBSM(BasicSafetyMessage* bsm) {
//Your application has received a beacon message from another car or
RSU
//code for handling the message goes here
//emergency car number is 2,if its distance from intersection got
less 20 meters so set traffic light to program1,the light changes
to gree
if (bsm->getSenderAddress() == 2){
if ((curPosition-bsm->getSenderPos()).length() < 20) {
if (!phaseMsg->isScheduled()) {
//trafficLightId = "light1";
traci-
>trafficlight(trafficLightId).setProgram("program1");
//traci->trafficlight(trafficLightId).setPhaseIndex(0);
scheduleAt(simTime()+5,phaseMsg);
}
}
}
}
void Veins::mynet::onWSM(WaveShortMessage* wsm) {
//Your application has received a data message from another car or
RSU
//code for handling the message goes here,see TraciDemo11p.cc for
examples
}
void Veins::mynet::onWSA(WaveServiceAdvertisment* wsa) {
//Your application has received a service advertisement from another
car
or RSU
//code for handling the message goes here,see TraciDemo11p.cc for
examples
}
//handle self message function can be seen below
void Veins::mynet::handleSelfMsg(cmessage* msg) {
BaseWaveApplLayer::handleSelfMsg(msg);
//BasicSafetyMessage* bsm = dynamic_cast<BasicSafetyMessage *>(msg);
//scheduleAt(simTime()+0.1,initMsg);
//this method is for self messages (mostly timers)
//it is important to call the BaseWaveApplLayer function for BSM and
WSM
transmission
//after emergency car crosses the intersection the traffic light
return
to the previuos state
switch (msg->getKind())
{
case 77:
manager = TraCIScenarioManagerAccess().get();
traci = manager->getCommandInterface();
trafficLightId = "light1";
traci->trafficlight(trafficLightId).setProgram("program2");
//traci->trafficlight(trafficLightId).setPhaseIndex(2);
break;
case 88:
traci->trafficlight(trafficLightId).setProgram("program2");
//traci->trafficlight(trafficLightId).setPhaseIndex(2);
break;
default:
assert(0);
break;
}
}
'''
'''
//here you can see mynet.h file
#include"veins/modules/application/ieee80211p/BaseWaveApplLayer.h"
#include "veins/modules/mobility/traci/TraCIScenarioManager.h"
#include "veins/modules/mobility/traci/TraCICommandInterface.h"
#include "veins/modules/mobility/traci/TraCImobility.h"
namespace Veins{
class mynet : public BaseWaveApplLayer {
protected:
virtual void initialize( int stage);
virtual void onWSM(WaveShortMessage* wsm);
virtual void onWSA(WaveServiceAdvertisment* wsa);
virtual void onBSM(BasicSafetyMessage * bsm);
virtual void handleSelfMsg(cmessage* msg);
TraCIScenarioManager* manager;
std::string trafficLightId;
cmessage* initMsg;
cmessage* phaseMsg;
};
}
'''
'''
//here you can see omnet.ini file
[General]
cmdenv-express-mode = true
cmdenv-autoflush = true
cmdenv-status-frequency = 1s
**.cmdenv-log-level = info
ned-path = .
image-path = ../../images
network = mynet_rsu
##########################################################
# Simulation parameters #
##########################################################
debug-on-errors = true
print-undisposed = true
sim-time-limit = 200s
**.scalar-recording = true
**.vector-recording = true
**.debug = false
**.coreDebug = false
*.playgroundSizeX = 2500m
*.playgroundSizeY = 2500m
*.playgroundSizeZ = 50m
##########################################################
# Annotation parameters #
##########################################################
*.annotations.draw = true
##########################################################
# TraCIScenarioManager parameters #
##########################################################
*.manager.updateInterval = 0.1s
*.manager.host = "localhost"
*.manager.port = 9999
*.manager.autoShutdown = true
*.manager.launchConfig = xmldoc("mynet.launchd.xml")
##########################################################
# RSU SETTINGS #
# #
# #
##########################################################
*.rsu[0].mobility.x = 190
*.rsu[0].mobility.y = 190
*.rsu[0].mobility.z = 3
*.rsu[*].appltype = "mynet"
*.rsu[*].appl.headerLength = 80 bit
*.rsu[*].appl.sendbeacons = false
*.rsu[*].appl.dataOnSch = false
*.rsu[*].appl.beaconInterval = 0.1s
*.rsu[*].appl.beaconUserPriority = 7
*.rsu[*].appl.dataUserPriority = 5
##########################################################
# 11p specific parameters #
# #
# NIC-Settings #
##########################################################
*.connectionManager.sendDirect = true
*.connectionManager.maxInterfdist = 2600m
*.connectionManager.drawMaxIntfdist = false
*.**.nic.mac1609_4.useServiceChannel = false
*.**.nic.mac1609_4.txPower = 20mW
*.**.nic.mac1609_4.bitrate = 6Mbps
*.**.nic.phy80211p.sensitivity = -89dBm
*.**.nic.phy80211p.useThermalNoise = true
*.**.nic.phy80211p.thermalNoise = -110dBm
*.**.nic.phy80211p.decider = xmldoc("config.xml")
*.**.nic.phy80211p.analogueModels = xmldoc("config.xml")
*.**.nic.phy80211p.usePropagationDelay = true
*.**.nic.phy80211p.antenna = xmldoc("antenna.xml","/root/Antenna[@id='monopole']")
##########################################################
# WaveAppLayer #
##########################################################
*.node[*].appltype = "mynet"
*.node[*].appl.headerLength = 80 bit
*.node[*].appl.sendbeacons = false
*.node[*].appl.dataOnSch = false
*.node[*].appl.beaconInterval = 0.1s
##########################################################
# Traffic Light #
##########################################################
*.manager.trafficLightModuleType =
"org.car2x.veins.nodes.TrafficLight"
*.manager.trafficLightModuleName = "trafficLight"
*.manager.trafficLightModuledisplayString = default
*.manager.trafficLightFilter = "trafficLightId"
*.trafficLight[*].mobility.x = 190
*.trafficLight[*].mobility.y = 190
*.trafficLight[*].mobility.z = 3
*.trafficLight[*].appltype = "mynet"
*.trafficLight[*].logicType = "static"
##########################################################
# mobility #
##########################################################
*.node[*].veinsmobilityType.debug = true
*.node[*].veinsmobility.x = 0
*.node[*].veinsmobility.y = 0
*.node[*].veinsmobility.z = 1.895
[Config Default]
[Config WithBeaconing]
*.rsu[*].appl.sendbeacons = true
*.node[*].appl.sendbeacons = true
[Config WithChannelSwitching]
*.**.nic.mac1609_4.useServiceChannel = true
*.node[*].appl.dataOnSch = true
*.rsu[*].appl.dataOnSch = true
'''
'''
//here you can see my mynet.tls.xml for simulatig traffic light file
of
SUMO which also repeated this part in mynet.net.xml file
<tlLogics version="0.27" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
<tls>
<tlLogic id="light1" type="static" programID="program1" offset="0">
<phase duration="999" state="GrGr"/>
<phase duration="999" state="yryr"/>
<phase duration="999" state="rGrG"/>
<phase duration="999" state="ryry"/>
</tlLogic>
<tlLogic id="light1" type="static" programID="program2" offset="0">
<phase duration="999" state="rGrG"/>
<phase duration="999" state="ryry"/>
<phase duration="999" state="GrGr"/>
<phase duration="999" state="yryr"/>
</tlLogic>
</tls>
<connection from="1to2" to="2to3" fromLane="0" toLane="0" tl="light1"
linkIndex="3"/>
<connection from="3to2" to="2to1" fromLane="0" toLane="0" tl="light1"
linkIndex="1"/>
<connection from="4to2" to="2to5" fromLane="0" toLane="0" tl="light1"
linkIndex="0"/>
<connection from="5to2" to="2to4" fromLane="0" toLane="0" tl="light1"
linkIndex="2"/>
</tlLogics>
'''
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。