如何在SimuLTE与Veins中启用SUMO TraCI并获取实时交通数据?
Hey there! Let me break down exactly how to make this work for your SimuLTE + Veins setup—since you’re already familiar with pure Veins, this will feel familiar with just a few extra steps for LTE integration.
In the combined SimuLTE + Veins simulation, Veins handles traffic mobility and TraCI interactions, while SimuLTE manages LTE communication. The key is to fetch real-time traffic data via TraCI in your Veins node's application layer, then pass that data to SimuLTE's LTE stack to send as safety messages.
1. Pick the Right Code Location
Just like in pure Veins, you’ll work within the application layer module of your Veins node (usually attached to a node with the TraCIMobility module, like VeinsVehicle). This is where you’ll add TraCI logic and handle data handoff to SimuLTE.
2. Use TraCICommandInterface to Grab Real-Time Data
First, get a reference to the TraCIMobility module and its associated TraCI interface in your application's .cc file:
// Fetch the TraCIMobility module from the parent node TraCIMobility* mobility = dynamic_cast<TraCIMobility*>(getModuleByPath("^.mobility")); if (!mobility) { error("Failed to locate TraCIMobility module"); } // Access the TraCI command interface TraCICommandInterface* traci = mobility->getCommandInterface();
Example 1: Get Average Speed for the Current Road
If you need the average speed of vehicles on the road your car is currently on:
// Get the ID of the road your vehicle is traveling on std::string currentRoad = traci->vehicle(getParentModule()->getFullName())->getRoadId(); // Fetch the average speed (in m/s) for that road double roadAvgSpeed = traci->road(currentRoad)->getAverageSpeed();
Example 2: Calculate Overall Fleet Average Speed
If you need the average speed of all connected vehicles in the simulation:
double totalSpeed = 0.0; int vehicleCount = 0; // Iterate over all vehicles in the simulation for (const auto& vehId : traci->getVehicleIds()) { TraCICommandInterface::Vehicle* vehicle = traci->vehicle(vehId); totalSpeed += vehicle->getSpeed(); vehicleCount++; } double fleetAvgSpeed = vehicleCount > 0 ? totalSpeed / vehicleCount : 0.0;
3. Pass Data to SimuLTE for LTE Transmission
Once you have your traffic data, package it into a custom message (e.g., SafetyMessage) and send it through SimuLTE's LTE stack. Make sure your node's NED file connects the application layer to the LTE NIC's upper layer interface:
// Create your custom safety message SafetyMessage* safetyMsg = new SafetyMessage(); safetyMsg->setAvgSpeed(roadAvgSpeed); // Populate with your data safetyMsg->setSenderId(getParentModule()->getId()); // Fetch the SimuLTE LTE NIC module LteNic* lteNic = dynamic_cast<LteNic*>(getParentModule()->getSubmodule("nic")); if (lteNic) { // Send the message to the LTE stack for transmission send(safetyMsg, "upperLayerOut"); }
Note: Double-check your NED file to ensure the application's upperLayerOut gate links to the LTE NIC's upperLayerIn gate.
4. Test & Debug
- Start by validating TraCI data fetching in a pure Veins setup first—use OMNeT++'s
EVlogging to confirm data is correct:EV_INFO << "Current road average speed: " << roadAvgSpeed << " m/s\n"; - Then test the full SimuLTE + Veins setup to ensure messages are transmitted and received over LTE.
- For real-time updates, set up a timer (e.g., every 100ms) to periodically fetch data and send messages—avoid blocking the simulation loop with continuous TraCI calls.
- Make sure your Veins and SimuLTE versions are compatible, and your launch script (like
run.sh) correctly configures paths for both frameworks. - Always call TraCI methods within the simulation event loop (e.g., in
handleMessageor a scheduled timer event) to avoid disrupting simulation timing.
内容的提问来源于stack exchange,提问作者J.G

