Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Car Localization and Distance Estimation #12

Description

@Y-AMDIAZ

I did a small project where I detect the location of cars and measure the distance between a target car and other target cars vehicles. The goal is to identify the closest car to the one I’m interested in and calculate the distance between them.

(//
// Copyright (C) 2016 David Eckhoff david.eckhoff@fau.de
//
// Documentation for these modules is at http://veins.car2x.org/
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

#include "veins/modules/application/traci/MyVeinsApp.h"

using namespace veins;

Define_Module(veins::MyVeinsApp);

void MyVeinsApp::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);

  if (stage == 0) {
      // Initialisation de base
      EV << "Initializing " << par("appName").stringValue() << std::endl;
  }
  else if (stage == 1) {

      // 🔧 Initialisation du module de mobilité
      mobilityModule = veins::TraCIMobilityAccess().get(getParentModule());
      EV << "[DEBUG] mobilityModule initialisé ✅" << std::endl;

  }

}

void MyVeinsApp::finish()
{
DemoBaseApplLayer::finish();
// statistics recording goes here
}

void MyVeinsApp::onBSM(DemoSafetyMessage* bsm)
{
// Your application has received a beacon message from another car or RSU
// code for handling the message goes here
}

void MyVeinsApp::onWSM(BaseFrame1609_4* 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 MyVeinsApp::onWSA(DemoServiceAdvertisment* 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
}

void MyVeinsApp::handleSelfMsg(cMessage* msg)
{
DemoBaseApplLayer::handleSelfMsg(msg);
// this method is for self messages (mostly timers)
// it is important to call the DemoBaseApplLayer function for BSM and WSM transmission
}

void MyVeinsApp::handlePositionUpdate(cObject* obj)
{
DemoBaseApplLayer::handlePositionUpdate(obj);

Coord myPos = mobilityModule->getPositionAt(simTime());
std::string nodeName = getParentModule()->getFullName(); // ex: node[3]


if (nodeName == "node[3]" || nodeName == "node[4]" || nodeName == "node[5]") {
    EV << "\n[ PATIENT] " << nodeName << " à la position " << myPos.str() << "\n";


    std::vector<std::string> ambulanceNodes = {"node[1]", "node[6]", "node[10]"};

    for (const auto& ambuName : ambulanceNodes) {
        cModule* ambuMod = getSimulation()->getModuleByPath(("RSUExampleScenario." + ambuName).c_str());
        if (!ambuMod) {
            EV << "[⚠️ ERREUR] Ambulance " << ambuName << " introuvable\n";
            continue;
        }

        auto* ambuMobility = veins::TraCIMobilityAccess().get(ambuMod);
        Coord ambuPos = ambuMobility->getPositionAt(simTime());
        double dist = myPos.distance(ambuPos);

        EV << "Ambulance " << ambuName
           << " est à la position " << ambuPos.str()
           << " -> Distance au patient : " << dist << " m\n";
    }

    EV << "------------------------------------\n";
}

}

)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions