车联网仿真之OMNeT++中如何获取车辆位置或速度
1. 获取节点位置
以OMNeT++示例代码为例,在veins->src->veins->modules->application->traci->TraCIDemo11p.cc
或MyVeinsApp.cc
中可以通过如下指令获取当前车辆位置。例如在TraCIDemo11p.cc
中通过函数void TraCIDemo11p::handlePositionUpdate(cObject* obj)
控制车辆在每次位置更新时输出位置,代码如下:
std::cout << "Car[" << this->myId << "] curPosition" << this->curPosition << std::endl;
std::cout << "Car[" << this->myId << "] curPosition.x" << this->curPosition.x << std::endl;
std::cout << "Car[" << this->myId << "] curPosition.y" << this->curPosition.y << std::endl;
std::cout << "Car[" << this->myId << "] curPosition.z" << this->curPosition.z << std::endl;
2. 获取节点速度
首先需在omnetpp.ini
文件中对 Mobility 部分进行如下修改,否则车辆节点获取不到车辆速度,导致输出速度为(0,0,0):
获取速度代码与获取位置类似,代码如下:
std::cout << "Car[" << this->myId << "] curSpeed " << this->curSpeed << std::endl;
std::cout << "Car[" << this->myId << "] curSpeed.x " << this->curSpeed.x << std::endl;
std::cout << "Car[" << this->myId << "] curSpeed.y " << this->curSpeed.y << std::endl;
std::cout << "Car[" << this->myId << "] curSpeed.z " << this->curSpeed.z << std::endl;
运行仿真,效果如下:
有了车辆位置和速度,就可以继续进行场景设计啦!
Veins平台教程推荐
根据之前学习的内容,在GitHub整理了一个简易教程供大家参考。【Veins_SUMO_OMNeTpp】