veins中实现rsu与车辆通信

受博客 https://blog.csdn.net/PERSEUS_/article/details/105647598
的启发,学习之后,自己试着实现一个小例子:rsu广播自己的id等信息,车辆收到后将rsu的id记录下来。

一、创建msg消息

1 在路径veins/modules/application/traci/创建BeaconRSU.msg
cplusplus{{
#import "veins/base/utils/Coord.h"
#import "veins/modules/utility/Consts80211p.h"
#include "veins/modules/messages/BaseFrame1609_4_m.h"
#include "veins/base/utils/SimpleAddress.h"

}};
 
namespace veins;
 
class noncobject Coord;
class BaseFrame1609_4;

packet BeaconRSU extends BaseFrame1609_4{
	//id of the originator
	int RSUId = 0;
	Coord position[100];
    double beaconrate[100]; 
    string myDemoData;
    Coord slotpos;
    simtime_t timestamp = 0;
}
2 build project后生成对应的.h和.cc文件

二、创建RSU的应用层实现类

在我的设定中,rsu用来发送消息,将原有的MyVeinsApp复制一份,重命名为MyVeinsAppRSU,在头文件中增加

cMessage* sendBeacon;

MyVeinsAppRSU.cc文件中,初始化函数中发送一个自消息

void MyVeinsAppRSU::initialize(int stage)
{
    DemoBaseApplLayer::initialize(stage);
    if (stage == 0) {
        sendBeacon = new cMessage("send Beacon");
        EV << "Initializing " << par("appName").stringValue() << std::endl;
    }
    else if (stage == 1) {
        if (sendBeacon->isScheduled()) {
              cancelEvent(sendBeacon);
       }
       scheduleAt(simTime()+5,sendBeacon);
    }
}

再重写一下handleSelfMsg函数

void MyVeinsAppRSU::handleSelfMsg(cMessage* msg)
{
    if (msg == sendBeacon) {

           BeaconRSU* rsuBeacon = new BeaconRSU();
           rsuBeacon->setRSUId(this->getParentModule()->getIndex());
           rsuBeacon->setMyDemoData("RSU message!!");
//          新建WSM,这是应用层和MAC层通信的消息
           BaseFrame1609_4* WSM = new BaseFrame1609_4();
           //把rsuBeacon封装在WSM中
           WSM->encapsulate(rsuBeacon);
           //设置WSM的基本信息
           populateWSM(WSM);
           send(WSM,lowerLayerOut);
           EV << "rsu send success" <<endl;
           if (simTime() < 2000) {
               scheduleAt(simTime()+1,sendBeacon);
           }
           return;
       }
}

这样,rsu就可以实现发送消息的功能了

三、创建车辆的应用层实现类

车辆用来接收rsu的消息,所以只用重写initialize和handleLowerMsg函数,继续将原有的MyVeinsApp复制一份,重命名为MyVeinsAppCar,头文件如下

#pragma once

#include "veins/veins.h"
#include "veins/modules/application/ieee80211p/DemoBaseApplLayer.h"
#include "veins/modules/application/traci/RSUBeacon_m.h"

using namespace omnetpp;

namespace veins {

class VEINS_API MyVeinsAppCar : public DemoBaseApplLayer {
public:
    void initialize(int stage) override;
    void finish() override;

protected:
    void onBSM(DemoSafetyMessage* bsm) override;
    void onWSM(BaseFrame1609_4* wsm) override;
    void onWSA(DemoServiceAdvertisment* wsa) override;
    void handleLowerMsg(cMessage* msg) override;
    void handleSelfMsg(cMessage* msg) override;
    void handlePositionUpdate(cObject* obj) override;

    cOutVector RSUIndex;
    int a;
};

} // namespace veins

在MyVeinsAppCar.cc中,initialize函数如下,其中RSUIndex用来记录收到消息的rsu的id

void MyVeinsAppCar::initialize(int stage)
{
    DemoBaseApplLayer::initialize(stage);
    if (stage == 0) {
        EV << "Initializing " << par("appName").stringValue() << std::endl;
        int a = INT_MIN;
    }
    else if (stage == 1) {
        RSUIndex.setName("test");
        int a = INT_MIN;
    }
}

handleLowerMsg函数如下:

void MyVeinsAppCar::handleLowerMsg(cMessage* msg) {
    EV << "receive message  !!!" << endl;
    //消息传换成WSM
    BaseFrame1609_4* WSM = check_and_cast<BaseFrame1609_4*>(msg);
    //从WSM中解封数据包
    cPacket* enc = WSM->getEncapsulatedPacket();
    //数据包转换成BeaconRSU
    BeaconRSU* bc = dynamic_cast<BeaconRSU*>(enc);

    if(a!=bc->getRSUId()){
       RSUIndex.record(bc->getRSUId());
       a=bc->getRSUId();
  }

  EV << "my message = " <<bc->getMyDemoData()<<endl;
  EV <<"send message RSU id:" <<bc->getRSUId() << "  Receive successfully !!!!!!!!!!!" << endl;
}

四、在ini文件中修改配置信息

在ini文件中,进行如下修改

*.rsu[*].applType = "MyVeinsAppRSU"
*.node[*].applType = "MyVeinsAppCar"

五、结果

运行之后,就可以观察到车辆收到消息,如下图所示

  • 9
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值