You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VEINS中获取指定时间车辆坐标并计算间距?Veins4.6环境求助

Hey there, let's tackle this problem step by step. I've worked with Veins 4.6, SUMO 0.25, and OMNeT++ 5.2 before, so I have some solid ideas on what might be going wrong and how to fix it.

First, Let's Diagnose the Coordinate Issue

The "abnormally small coordinates" for veh1 usually stems from one of these common pitfalls:

  • You're using the wrong method to fetch coordinates (bypassing Veins' built-in coordinate conversion)
  • veh1 hasn't started moving in SUMO yet (e.g., its depart time is later than your query time)
  • You're querying a non-existent vehicle ID (double-check SUMO's route file for exact IDs)
  • The handlePositionUpdate() function isn't triggering correctly for veh1

Proper Way to Get Positions at a Specific Time

Instead of relying solely on handlePositionUpdate() (which fires on every position tick), use an OMNeT++ timer to query positions exactly when you need them. Here's a complete implementation:

Step 1: Update the Header File (TraCIDemo11p.h)

Add a timer member variable to trigger your position query:

class TraCIDemo11p : public BaseWaveApplLayer {
    // ... existing code ...
    private:
        cMessage* positionQueryTimer; // Timer to trigger position check
};

Step 2: Initialize the Timer in initialize()

Set up the timer to fire at your target simulation time (e.g., 10.0 seconds):

void TraCIDemo11p::initialize(int stage) {
    BaseWaveApplLayer::initialize(stage);
    if (stage == 0) {
        // ... existing initialization code ...
        // Schedule position query at 10.0s (adjust to your target time)
        positionQueryTimer = new cMessage("positionQuery");
        scheduleAt(simTime() + 10.0, positionQueryTimer);
    }
}

Step 3: Handle the Timer and Query Positions

In handleMessage(), add logic to fetch both vehicles' coordinates and calculate distance:

void TraCIDemo11p::handleMessage(cMessage* msg) {
    if (msg == positionQueryTimer) {
        // Get current vehicle's mobility module and position
        TraCIMobility* mobility = TraCIMobilityAccess().get(getParentModule());
        Coord myPos = mobility->getCurrentPosition();
        std::string myId = getParentModule()->getFullName();
        EV_INFO << "[" << myId << "] Position at " << simTime() << ": " << myPos << endl;

        // Fetch the other vehicle's position via TraCI
        TraCICommandInterface* traci = mobility->getCommandInterface();
        const std::string targetVehId = "veh1"; // Match SUMO's vehicle ID

        if (traci->vehicleExists(targetVehId)) {
            TraCICommandInterface::Vehicle* targetVeh = traci->vehicle(targetVehId);
            Coord targetPos = targetVeh->getPosition();
            EV_INFO << "[" << targetVehId << "] Position at " << simTime() << ": " << targetPos << endl;

            // Calculate distance between the two vehicles
            double distance = myPos.distance(targetPos);
            EV_INFO << "Distance between " << myId << " and " << targetVehId << ": " << distance << " meters" << endl;
        } else {
            EV_WARN << "Vehicle " << targetVehId << " does not exist at time " << simTime() << endl;
        }

        // Optional: Reschedule the timer if you need repeated checks
        // scheduleAt(simTime() + 5.0, positionQueryTimer);
    } else {
        // Pass other messages to the base class
        BaseWaveApplLayer::handleMessage(msg);
    }
}

Step 4: Fix handlePositionUpdate() (If You Still Want to Use It)

If you prefer using handlePositionUpdate(), make sure you're fetching coordinates correctly (avoid direct member variable access):

void TraCIDemo11p::handlePositionUpdate(cObject* obj) {
    BaseWaveApplLayer::handlePositionUpdate(obj);

    TraCIMobility* mobility = TraCIMobilityAccess().get(getParentModule());
    Coord currentPos = mobility->getCurrentPosition();
    std::string myId = getParentModule()->getFullName();
    EV_INFO << "[" << myId << "] Position updated at " << simTime() << ": " << currentPos << endl;

    // Fetch veh1's position here too, same as above
    TraCICommandInterface* traci = mobility->getCommandInterface();
    if (traci->vehicleExists("veh1")) {
        TraCICommandInterface::Vehicle* veh1 = traci->vehicle("veh1");
        Coord veh1Pos = veh1->getPosition();
        double dist = currentPos.distance(veh1Pos);
        EV_INFO << "Distance to veh1: " << dist << endl;
    }
}

Key Checks to Fix the Small Coordinate Issue

  • Verify SUMO's Vehicle Configuration: Check your .rou.xml file to ensure veh1 has a valid depart time (earlier than your query time) and a proper route.
  • Use Correct Coordinate Methods: Always use getCurrentPosition() (from TraCIMobility) or getPosition() (from TraCICommandInterface::Vehicle)—these handle SUMO-to-OMNeT++ coordinate conversion automatically.
  • Validate Vehicle IDs: Ensure the ID you're using in code exactly matches the ID in SUMO (case-sensitive!).

内容的提问来源于stack exchange,提问作者Toothless

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:19:15