Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 : // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
4 : /****************************************************************************/
5 : //
6 : // This program and the accompanying materials
7 : // are made available under the terms of the Eclipse Public License v2.0
8 : // which accompanies this distribution, and is available at
9 : // http://www.eclipse.org/legal/epl-v20.html
10 : //
11 : /****************************************************************************/
12 : /// @file TraCI_Simulation.cpp
13 : /// @author Laura Bieker-Walz
14 : /// @author Robert Hilbrich
15 : /// @date 15.09.2017
16 : /// @version $Id$
17 : ///
18 : // C++ TraCI client API implementation
19 : /****************************************************************************/
20 :
21 : // ===========================================================================
22 : // included modules
23 : // ===========================================================================
24 : #ifdef _MSC_VER
25 : #include <windows_config.h>
26 : #else
27 : #include <config.h>
28 : #endif
29 :
30 : #ifndef NO_TRACI
31 :
32 : #include <utils/common/StdDefs.h>
33 : #include <utils/common/StringTokenizer.h>
34 : #include <utils/common/StringUtils.h>
35 : #include <utils/geom/GeoConvHelper.h>
36 : #include <microsim/MSNet.h>
37 : #include <microsim/MSEdgeControl.h>
38 : #include <microsim/MSInsertionControl.h>
39 : #include <microsim/MSEdge.h>
40 : #include <microsim/MSLane.h>
41 : #include <microsim/MSVehicle.h>
42 : #include <microsim/MSVehicleControl.h>
43 : #include <microsim/MSStateHandler.h>
44 : #include <microsim/MSStoppingPlace.h>
45 : #include <traci-server/lib/TraCI.h>
46 : #include "TraCI_Simulation.h"
47 : #include <traci-server/TraCIDefs.h>
48 :
49 : // ===========================================================================
50 : // member definitions
51 : // ===========================================================================
52 : SUMOTime
53 7743 : TraCI_Simulation::getCurrentTime(){
54 7743 : return MSNet::getInstance()->getCurrentTimeStep();
55 : }
56 :
57 :
58 : SUMOTime
59 3 : TraCI_Simulation::getDeltaT(){
60 3 : return DELTA_T;
61 : }
62 :
63 :
64 : TraCIBoundary
65 3 : TraCI_Simulation::getNetBoundary() {
66 3 : Boundary b = GeoConvHelper::getFinal().getConvBoundary();
67 : TraCIBoundary tb;
68 3 : tb.xMin = b.xmin();
69 3 : tb.xMax = b.xmax();
70 3 : tb.yMin = b.ymin();
71 3 : tb.yMax = b.ymax();
72 3 : tb.zMin = b.zmin();
73 3 : tb.zMax = b.zmax();
74 3 : return tb;
75 : }
76 :
77 : int
78 23782 : TraCI_Simulation::getMinExpectedNumber() {
79 23782 : return MSNet::getInstance()->getVehicleControl().getActiveVehicleCount() + MSNet::getInstance()->getInsertionControl().getPendingFlowCount();
80 : }
81 :
82 :
83 : TraCIStage
84 6 : TraCI_Simulation::findRoute(const std::string& from, const std::string& to, const std::string& typeID, const SUMOTime depart, const int routingMode) {
85 6 : TraCIStage result(MSTransportable::DRIVING);
86 6 : const MSEdge* const fromEdge = MSEdge::dictionary(from);
87 6 : if (fromEdge == 0) {
88 3 : throw TraCIException("Unknown from edge '" + from + "'.");
89 : }
90 3 : const MSEdge* const toEdge = MSEdge::dictionary(to);
91 3 : if (toEdge == 0) {
92 0 : throw TraCIException("Unknown to edge '" + from + "'.");
93 : }
94 3 : SUMOVehicle* vehicle = 0;
95 3 : if (typeID != "") {
96 0 : SUMOVehicleParameter* pars = new SUMOVehicleParameter();
97 0 : MSVehicleType* type = MSNet::getInstance()->getVehicleControl().getVType(typeID);
98 0 : if (type == 0) {
99 0 : throw TraCIException("The vehicle type '" + typeID + "' is not known.");
100 : }
101 0 : const MSRoute* const routeDummy = new MSRoute("", ConstMSEdgeVector({ fromEdge }), false, 0, std::vector<SUMOVehicleParameter::Stop>());
102 0 : vehicle = MSNet::getInstance()->getVehicleControl().buildVehicle(pars, routeDummy, type, false);
103 : }
104 6 : ConstMSEdgeVector edges;
105 3 : const SUMOTime dep = depart < 0 ? MSNet::getInstance()->getCurrentTimeStep() : depart;
106 3 : MSNet::getInstance()->getRouterTT().compute(fromEdge, toEdge, vehicle, dep, edges);
107 9 : for (const MSEdge* e : edges) {
108 6 : result.edges.push_back(e->getID());
109 : }
110 3 : result.travelTime = result.cost = MSNet::getInstance()->getRouterTT().recomputeCosts(edges, vehicle, dep);
111 3 : if (vehicle != 0) {
112 0 : MSNet::getInstance()->getVehicleControl().deleteVehicle(vehicle, true);
113 : }
114 6 : return result;
115 : }
116 :
117 :
118 : std::vector<TraCIStage>
119 6 : TraCI_Simulation::findIntermodalRoute(const std::string& from, const std::string& to,
120 : const std::string modes, const SUMOTime depart, const int routingMode, const double speed, const double walkFactor,
121 : const double departPos, const double arrivalPos, const double departPosLat,
122 : const std::string& pType, const std::string& vehType) {
123 6 : std::vector<TraCIStage> result;
124 6 : const MSEdge* const fromEdge = MSEdge::dictionary(from);
125 6 : if (fromEdge == 0) {
126 3 : throw TraCIException("Unknown from edge '" + from + "'.");
127 : }
128 3 : const MSEdge* const toEdge = MSEdge::dictionary(to);
129 3 : if (toEdge == 0) {
130 0 : throw TraCIException("Unknown to edge '" + from + "'.");
131 : }
132 3 : MSVehicleControl& vehControl = MSNet::getInstance()->getVehicleControl();
133 3 : SVCPermissions modeSet = 0;
134 6 : for (StringTokenizer st(modes); st.hasNext();) {
135 0 : const std::string mode = st.next();
136 0 : if (mode == "car") {
137 0 : modeSet |= SVC_PASSENGER;
138 0 : } else if (mode == "bicycle") {
139 0 : modeSet |= SVC_BICYCLE;
140 0 : } else if (mode == "public") {
141 0 : modeSet |= SVC_BUS;
142 : } else {
143 0 : throw TraCIException("Unknown person mode '" + mode + "'.");
144 : }
145 3 : }
146 3 : const MSVehicleType* pedType = vehControl.hasVType(pType) ? vehControl.getVType(pType) : vehControl.getVType(DEFAULT_PEDTYPE_ID);
147 3 : const SUMOTime dep = depart < 0 ? MSNet::getInstance()->getCurrentTimeStep() : depart;
148 3 : if (modeSet == 0) {
149 3 : ConstMSEdgeVector edges;
150 3 : const double cost = MSNet::getInstance()->getPedestrianRouter().compute(fromEdge, toEdge, departPos, arrivalPos, speed > 0 ? speed : pedType->getMaxSpeed(), dep, 0, edges);
151 3 : if (cost < 0) {
152 0 : return result;
153 : }
154 3 : result.emplace_back(TraCIStage(MSTransportable::MOVING_WITHOUT_VEHICLE));
155 9 : for (const MSEdge* e : edges) {
156 6 : result.back().edges.push_back(e->getID());
157 : }
158 3 : result.back().travelTime = result.back().cost = cost;
159 : } else {
160 0 : SUMOVehicleParameter* pars = new SUMOVehicleParameter();
161 0 : SUMOVehicle* vehicle = 0;
162 0 : if (vehType != "") {
163 0 : MSVehicleType* type = MSNet::getInstance()->getVehicleControl().getVType(vehType);
164 0 : if (type->getVehicleClass() != SVC_IGNORING && (fromEdge->getPermissions() & type->getVehicleClass()) == 0) {
165 0 : throw TraCIException("Invalid vehicle type '" + type->getID() + "', it is not allowed on the start edge.");
166 : }
167 0 : const MSRoute* const routeDummy = new MSRoute("", ConstMSEdgeVector({ fromEdge }), false, 0, std::vector<SUMOVehicleParameter::Stop>());
168 0 : vehicle = vehControl.buildVehicle(pars, routeDummy, type, !MSGlobals::gCheckRoutes);
169 : }
170 0 : std::vector<MSNet::MSIntermodalRouter::TripItem> items;
171 0 : if (MSNet::getInstance()->getIntermodalRouter().compute(fromEdge, toEdge, departPos, arrivalPos,
172 0 : pedType->getMaxSpeed() * walkFactor, vehicle, modeSet, dep, items)) {
173 0 : for (std::vector<MSNet::MSIntermodalRouter::TripItem>::iterator it = items.begin(); it != items.end(); ++it) {
174 0 : if (!it->edges.empty()) {
175 0 : if (it->line == "") {
176 0 : result.emplace_back(TraCIStage(MSTransportable::MOVING_WITHOUT_VEHICLE));
177 0 : } else if (vehicle != 0 && it->line == vehicle->getID()) {
178 0 : result.emplace_back(TraCIStage(MSTransportable::DRIVING));
179 : }
180 0 : result.back().destStop = it->destStop;
181 0 : result.back().line = it->line;
182 0 : for (const MSEdge* e : it->edges) {
183 0 : result.back().edges.push_back(e->getID());
184 : }
185 0 : result.back().travelTime = result.back().cost = it->cost;
186 : }
187 : }
188 : }
189 0 : if (vehicle != 0) {
190 0 : vehControl.deleteVehicle(vehicle, true);
191 0 : }
192 : }
193 3 : return result;
194 : }
195 :
196 :
197 : std::string
198 12 : TraCI_Simulation::getParameter(const std::string& objectID, const std::string& key) {
199 12 : if (StringUtils::startsWith(key, "chargingStation.")) {
200 9 : const std::string attrName = key.substr(16);
201 9 : MSChargingStation* cs = MSNet::getInstance()->getChargingStation(objectID);
202 9 : if (cs == 0) {
203 3 : throw TraCIException("Invalid chargingStation '" + objectID + "'");
204 : }
205 6 : if (attrName == toString(SUMO_ATTR_TOTALENERGYCHARGED)) {
206 6 : return toString(cs->getTotalCharged());
207 : } else {
208 3 : throw TraCIException("Invalid chargingStation parameter '" + attrName + "'");
209 9 : }
210 : } else {
211 3 : throw TraCIException("Parameter '" + key + "' is not supported.");
212 : }
213 43554 : }
214 :
215 :
216 : #endif
217 :
218 :
219 : /****************************************************************************/
220 :
|