Eclipse SUMO - Simulation of Urban MObility
MSFCDExport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // Realises dumping Floating Car Data (FCD) Data
22 /****************************************************************************/
23 #include <config.h>
24 
28 #include <utils/geom/GeomHelper.h>
29 #include <libsumo/Helper.h>
32 #include <microsim/MSEdgeControl.h>
33 #include <microsim/MSEdge.h>
34 #include <microsim/MSLane.h>
35 #include <microsim/MSGlobals.h>
36 #include <microsim/MSNet.h>
37 #include <microsim/MSVehicle.h>
42 #include "MSFCDExport.h"
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 void
49 MSFCDExport::write(OutputDevice& of, SUMOTime timestep, bool elevation) {
50  const bool useGeo = OptionsCont::getOptions().getBool("fcd-output.geo");
51  const bool signals = OptionsCont::getOptions().getBool("fcd-output.signals");
52  const bool writeAccel = OptionsCont::getOptions().getBool("fcd-output.acceleration");
53  const bool writeDistance = OptionsCont::getOptions().getBool("fcd-output.distance");
54  const double maxLeaderDistance = OptionsCont::getOptions().getFloat("fcd-output.max-leader-distance");
55  std::vector<std::string> params = OptionsCont::getOptions().getStringVector("fcd-output.params");
56  const SUMOTime period = string2time(OptionsCont::getOptions().getString("device.fcd.period"));
57  const SUMOTime begin = string2time(OptionsCont::getOptions().getString("begin"));
58  if (period > 0 && (timestep - begin) % period != 0) {
59  return;
60  }
62  const double radius = OptionsCont::getOptions().getFloat("device.fcd.radius");
63  const bool filter = MSDevice_FCD::getEdgeFilter().size() > 0;
64  std::set<const Named*> inRadius;
65  if (radius > 0) {
66  // collect all vehicles in radius around equipped vehicles
67  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
68  const SUMOVehicle* veh = it->second;
69  MSDevice_FCD* fcdDevice = (MSDevice_FCD*)veh->getDevice(typeid(MSDevice_FCD));
70  if (fcdDevice != nullptr
71  && (veh->isOnRoad() || veh->isParking() || veh->isRemoteControlled())
72  && (!filter || MSDevice_FCD::getEdgeFilter().count(veh->getEdge()) > 0)) {
73  PositionVector shape;
74  shape.push_back(veh->getPosition());
77  }
78  }
79  }
80 
81  of.openTag("timestep").writeAttr(SUMO_ATTR_TIME, time2string(timestep));
82  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
83  const SUMOVehicle* veh = it->second;
84  const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
85  const MSBaseVehicle* baseVeh = dynamic_cast<const MSBaseVehicle*>(veh);
86  if ((veh->isOnRoad() || veh->isParking() || veh->isRemoteControlled())
87  // only filter on normal edges
88  && (!filter || MSDevice_FCD::getEdgeFilter().count(veh->getEdge()) > 0)
89  && (veh->getDevice(typeid(MSDevice_FCD)) != nullptr || (radius > 0 && inRadius.count(veh) > 0))) {
90  Position pos = veh->getPosition();
91  if (useGeo) {
94  }
96  of.writeAttr(SUMO_ATTR_ID, veh->getID());
97  of.writeAttr(SUMO_ATTR_X, pos.x());
98  of.writeAttr(SUMO_ATTR_Y, pos.y());
99  if (elevation) {
100  of.writeAttr(SUMO_ATTR_Z, pos.z());
101  }
104  of.writeAttr(SUMO_ATTR_SPEED, veh->getSpeed());
106  if (microVeh != nullptr) {
107  of.writeAttr(SUMO_ATTR_LANE, microVeh->getLane()->getID());
108  } else {
109  of.writeAttr(SUMO_ATTR_EDGE, veh->getEdge()->getID());
110  }
111  of.writeAttr(SUMO_ATTR_SLOPE, veh->getSlope());
112  if (microVeh != nullptr) {
113  if (signals) {
115  }
116  if (writeAccel) {
118  if (MSGlobals::gSublane) {
120  }
121  }
122  }
123  if (writeDistance) {
124  double distance = veh->getEdge()->getDistance();
125  if (microVeh != nullptr) {
126  if (microVeh->getLane()->isInternal()) {
127  distance += microVeh->getRoute().getDistanceBetween(0, microVeh->getPositionOnLane(),
128  microVeh->getEdge(), &microVeh->getLane()->getEdge(), true, microVeh->getRoutePosition());
129  } else {
130  distance += microVeh->getPositionOnLane();
131  }
132  } else {
133  distance += veh->getPositionOnLane();
134  }
135  // if the kilometrage runs counter to the edge direction edge->getDistance() is negative
136  of.writeAttr(SUMO_ATTR_DISTANCE, fabs(distance));
137  }
138  if (maxLeaderDistance >= 0 && microVeh != nullptr) {
139  std::pair<const MSVehicle* const, double> leader = microVeh->getLeader(maxLeaderDistance);
140  if (leader.first != nullptr) {
141  of.writeAttr(SUMO_ATTR_LEADER_ID, toString(leader.first->getID()));
142  of.writeAttr(SUMO_ATTR_LEADER_SPEED, toString(leader.first->getSpeed()));
143  of.writeAttr(SUMO_ATTR_LEADER_GAP, toString(leader.second + microVeh->getVehicleType().getMinGap()));
144  } else {
148  }
149  }
150  for (const std::string& key : params) {
151  std::string error;
152  const std::string value = baseVeh->getPrefixedParameter(key, error);
153  if (value != "") {
155  }
156  }
157  of.closeTag();
158  // write persons and containers
159  const MSEdge* edge = microVeh == nullptr ? veh->getEdge() : &veh->getLane()->getEdge();
160 
161  const std::vector<MSTransportable*>& persons = veh->getPersons();
162  for (MSTransportable* person : persons) {
163  writeTransportable(of, edge, person, true, SUMO_TAG_PERSON, useGeo, elevation);
164  }
165  const std::vector<MSTransportable*>& containers = veh->getContainers();
166  for (MSTransportable* container : containers) {
167  writeTransportable(of, edge, container, true, SUMO_TAG_CONTAINER, useGeo, elevation);
168  }
169  }
170  }
171  if (MSNet::getInstance()->getPersonControl().hasTransportables()) {
172  // write persons
174  const MSEdgeVector& edges = ec.getEdges();
175  for (MSEdgeVector::const_iterator e = edges.begin(); e != edges.end(); ++e) {
176  if (filter && MSDevice_FCD::getEdgeFilter().count(*e) == 0) {
177  continue;
178  }
179  const std::vector<MSTransportable*>& persons = (*e)->getSortedPersons(timestep);
180  for (MSTransportable* person : persons) {
181  writeTransportable(of, *e, person, inRadius.count(person) > 0, SUMO_TAG_PERSON, useGeo, elevation);
182  }
183  }
184  }
185  if (MSNet::getInstance()->getContainerControl().hasTransportables()) {
186  // write containers
188  const std::vector<MSEdge*>& edges = ec.getEdges();
189  for (std::vector<MSEdge*>::const_iterator e = edges.begin(); e != edges.end(); ++e) {
190  if (filter && MSDevice_FCD::getEdgeFilter().count(*e) == 0) {
191  continue;
192  }
193  const std::vector<MSTransportable*>& containers = (*e)->getSortedContainers(timestep);
194  for (MSTransportable* container : containers) {
195  writeTransportable(of, *e, container, inRadius.count(container) > 0, SUMO_TAG_CONTAINER, useGeo, elevation);
196  }
197  }
198  }
199  of.closeTag();
200 }
201 
202 
203 void
204 MSFCDExport::writeTransportable(OutputDevice& of, const MSEdge* e, MSTransportable* p, bool inRadius, SumoXMLTag tag, bool useGeo, bool elevation) {
205  if (p->getDevice(typeid(MSTransportableDevice_FCD)) == nullptr && !inRadius) {
206  return;
207  }
208  Position pos = p->getPosition();
209  if (useGeo) {
212  }
213  of.openTag(tag);
214  of.writeAttr(SUMO_ATTR_ID, p->getID());
215  of.writeAttr(SUMO_ATTR_X, pos.x());
216  of.writeAttr(SUMO_ATTR_Y, pos.y());
217  if (elevation) {
218  of.writeAttr(SUMO_ATTR_Z, pos.z());
219  }
223  of.writeAttr(SUMO_ATTR_EDGE, e->getID());
224  of.writeAttr(SUMO_ATTR_SLOPE, e->getLanes()[0]->getShape().slopeDegreeAtOffset(p->getEdgePos()));
225  of.closeTag();
226 }
227 
228 
229 /****************************************************************************/
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:31
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_VEHICLE
description of a vehicle
@ SUMO_TAG_CONTAINER
@ SUMO_TAG_PERSON
@ SUMO_ATTR_LANE
@ SUMO_ATTR_SPEED
@ SUMO_ATTR_Y
@ SUMO_ATTR_Z
@ SUMO_ATTR_EDGE
@ SUMO_ATTR_X
@ SUMO_ATTR_SIGNALS
@ SUMO_ATTR_LEADER_GAP
@ SUMO_ATTR_ACCELERATION_LAT
@ SUMO_ATTR_SLOPE
@ SUMO_ATTR_ANGLE
@ SUMO_ATTR_ACCELERATION
@ SUMO_ATTR_LEADER_SPEED
@ SUMO_ATTR_DISTANCE
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_ID
@ SUMO_ATTR_LEADER_ID
@ SUMO_ATTR_POSITION
@ SUMO_ATTR_TIME
trigger: the time of the step
int gPrecisionGeo
Definition: StdDefs.cpp:26
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
static double naviDegree(const double angle)
Definition: GeomHelper.cpp:192
double getAccelerationLat() const
return the lateral speed of the current lane change maneuver
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:51
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
std::string getPrefixedParameter(const std::string &key, std::string &error) const
retrieve parameters of devices, models and the vehicle itself
int getRoutePosition() const
return index of edge within route
const MSRoute & getRoute() const
Returns the current route.
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_FCD.h:46
static const std::set< const MSEdge * > & getEdgeFilter()
Definition: MSDevice_FCD.h:82
Stores edges and lanes, performs moving of vehicle.
Definition: MSEdgeControl.h:81
const MSEdgeVector & getEdges() const
Returns loaded edges.
A road/street connecting two junctions.
Definition: MSEdge.h:77
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:166
double getDistance() const
Returns the kilometrage/mileage at the start of the edge.
Definition: MSEdge.h:319
static void writeTransportable(OutputDevice &of, const MSEdge *e, MSTransportable *p, bool inRadius, SumoXMLTag tag, bool useGeo, bool elevation)
write transportable
static void write(OutputDevice &of, SUMOTime timestep, bool elevation)
Writes the position and the angle of each vehicle into the given device.
Definition: MSFCDExport.cpp:49
static bool gSublane
whether sublane simulation is enabled (sublane model or continuous lanechanging)
Definition: MSGlobals.h:130
bool isInternal() const
Definition: MSLane.cpp:2036
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:673
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:371
MSEdgeControl & getEdgeControl()
Returns the edge control.
Definition: MSNet.h:414
double getDistanceBetween(double fromPos, double toPos, const MSEdge *fromEdge, const MSEdge *toEdge, bool includeInternal=true, int routePosition=0) const
Compute the distance between 2 given edges on this route, including the length of internal lanes....
Definition: MSRoute.cpp:299
A device which collects info on the vehicle trip (mainly on departure and arrival)
virtual double getEdgePos() const
Return the position on the edge.
virtual double getAngle() const
return the current angle of the transportable
virtual double getSpeed() const
the current speed of the transportable
MSTransportableDevice * getDevice(const std::type_info &type) const
Returns a device of the given type if it exists or 0.
Position getPosition(const double) const
Return current position (x/y, cartesian)
The class responsible for building and deletion of vehicles.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:4699
double getAcceleration() const
Returns the vehicle's acceleration in m/s (this is computed as the last step's mean acceleration in c...
Definition: MSVehicle.h:483
int getSignals() const
Returns the signals.
Definition: MSVehicle.h:1150
std::pair< const MSVehicle *const, double > getLeader(double dist=0) const
Returns the leader of the vehicle looking for a fixed distance.
Definition: MSVehicle.cpp:5311
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:374
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:550
double getMinGap() const
Get the free space in front of vehicles of this class.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:90
const std::string & getID() const
Returns the id.
Definition: Named.h:73
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:239
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
double x() const
Returns the x-position.
Definition: Position.h:54
double z() const
Returns the z-position.
Definition: Position.h:64
double y() const
Returns the y-position.
Definition: Position.h:59
A list of positions.
virtual double getSlope() const =0
Returns the slope of the road at object's position in degrees.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
virtual const MSLane * getLane() const =0
Returns the lane the object is currently at.
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
Representation of a vehicle.
Definition: SUMOVehicle.h:58
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
virtual const std::vector< MSTransportable * > & getContainers() const =0
retrieve riding containers
virtual const std::vector< MSTransportable * > & getPersons() const =0
retrieve riding persons
virtual bool isOnRoad() const =0
Returns the information whether the vehicle is on a road (is simulated)
virtual bool isParking() const =0
Returns the information whether the vehicle is parked.
virtual bool isRemoteControlled() const =0
Returns the information whether the vehicle is fully controlled via TraCI.
virtual double getAngle() const =0
Get the vehicle's angle.
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static void collectObjectsInRange(int domain, const PositionVector &shape, double range, std::set< const Named * > &into)
Definition: Helper.cpp:603
TRACI_CONST int CMD_GET_VEHICLE_VARIABLE
TRACI_CONST int CMD_GET_PERSON_VARIABLE