Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [sumo-user] getSubscriptionResults in libtraci / C++

Hi Bart,

Here are some snippets. For getSubscriptionResults():

    auto sub_results = Vehicle::getSubscriptionResults(name.c_str());
    for (auto r : sub_results) {
      pt_LogDbg("- Ego subscr result type 0x%x: '%s'", r.first, r.second->getString().c_str());
    }

I'm using a context subscription really (only report within a specific radius):

  libsumo::SubscriptionResults subscriptionResults;
  if (prefLogSumoCalls)
    pt_Log("SUMO call: Vehicle::getContextSubscriptionResults(%s)", name.c_str());

  subscriptionResults = Vehicle::getContextSubscriptionResults(name.c_str());

  // Go through results (parse)
  std::string entity_id;
  for (auto e : subscriptionResults) {
    entity_id = e.first;
    //pt_LogDbg("- Ego ctx subscr result type '%s'", entity_id.c_str());

    auto res = e.second;
    for (auto r : res) {
      //pt_LogDbg("  - subscr result var 0x%x value '%s''", r.first, r.second->getString().c_str());
      switch (r.first) {
        case libsumo::VAR_ANGLE:
          vehicle->angle = ParseDouble(r.second->getString());
          break;
        case libsumo::VAR_POSITION3D:
           // Convert string to Vector3-like struct
          ParsePosition(r.second->getString(), vehicle->position);
          break;
        case libsumo::VAR_TYPE:
          //pt_LogDbg("VAR_TYPE='%s'", r.second->getString().c_str());
          vehicle->type_sumo = r.second->getString();
          break;
        //////////////// etc
        default:
          pt_LogWarn("Var 0x%x is not parsed; fix the source", r.first);
          break;
      }
    }
  }

Hope that helps,
Ruud



On Mon, Dec 4, 2023 at 11:35 PM T Eenachtacht via sumo-user <sumo-user@xxxxxxxxxxx> wrote:

Dear All,


Does anyone have experience, or can send a link to documentation, on how to process results from a subscription in C++ using libtraci? I need to convert the following from Python into C++. The subscription is for multiple types of variables. (not only VAR_ARRIVED_VEHICLES_IDS).

In Python the implementation is rather straightforward with the documentation (e.g. in https://sumo.dlr.de/docs/TraCI/Interfacing_TraCI_from_Python.html).


results = traci.simulation.getSubscriptionResults()

for vehicle_id in results[tc.VAR_ARRIVED_VEHICLES_IDS]:

# process vehicle_id

This can be converted to C++ like this:

libsumo::TraCIResults results = libtraci::Simulation::getSubscriptionResults();

//std::map<int, std::shared_ptr<libsumo::TraCIResult> > libsumo::TraCIResults

for (auto entry : results) {

int i = entry.first;

shared_ptr<libsumo::TraCIResult> result = entry.second;

How can the TraCIResults be filtered for ' libsumo::VAR_ARRIVED_VEHICLES_ID' values, and

how to get the id values from the result?

Thanks for the help,

Bart Netten

_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user

Back to the top