1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file MSSOTLE2Sensors.cpp
11 /// @author Gianfilippo Slager
12 /// @author Alessio Bonfietti
13 /// @author Federico Caselli
14 /// @date 2010-02-25
15 /// @version $Id$
16 ///
17 // The class for SOTL sensors of "E2" type
18 /****************************************************************************/
19
20 #include <utils/common/StringUtils.h>
21 #include <microsim/output/MSDetectorControl.h>
22 #include <microsim/MSEdge.h>
23 #include <microsim/MSVehicleType.h>
24 #include "MSSOTLE2Sensors.h"
25
26 #define INVALID_POSITION std::numeric_limits<double>::max()
27
MSSOTLE2Sensors(std::string tlLogicID,const MSTrafficLightLogic::Phases * phases)28 MSSOTLE2Sensors::MSSOTLE2Sensors(std::string tlLogicID,
29 const MSTrafficLightLogic::Phases* phases) :
30 MSSOTLSensors(tlLogicID, phases) {
31 }
32
~MSSOTLE2Sensors(void)33 MSSOTLE2Sensors::~MSSOTLE2Sensors(void) {
34 //Deleting sensors takes place in the detector control
35 }
36
buildSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes,NLDetectorBuilder & nb)37 void MSSOTLE2Sensors::buildSensors(
38 MSTrafficLightLogic::LaneVectorVector controlledLanes,
39 NLDetectorBuilder& nb) {
40 buildSensors(controlledLanes, nb, INPUT_SENSOR_LENGTH);
41 }
42
buildSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes,NLDetectorBuilder & nb,double sensorLength)43 void MSSOTLE2Sensors::buildSensors(
44 MSTrafficLightLogic::LaneVectorVector controlledLanes,
45 NLDetectorBuilder& nb, double sensorLength) {
46 //for each lane build an appropriate sensor on it
47 MSLane* currentLane = nullptr;
48
49 //input and ouput lanes
50 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
51 controlledLanes.begin(); laneVector != controlledLanes.end();
52 laneVector++) {
53 for (MSTrafficLightLogic::LaneVector::const_iterator lane =
54 laneVector->begin(); lane != laneVector->end(); lane++) {
55 currentLane = (*lane);
56 buildSensorForLane(currentLane, nb, sensorLength);
57 }
58 }
59 }
60 /****************************************************************************/
61 /*
62 * Count Sensors. Should be refactor to make a new class.
63 */
buildCountSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes,NLDetectorBuilder & nb)64 void MSSOTLE2Sensors::buildCountSensors(
65 MSTrafficLightLogic::LaneVectorVector controlledLanes,
66 NLDetectorBuilder& nb) {
67 //for each lane build an appropriate sensor on it
68 MSLane* currentLane = nullptr;
69 //input and ouput lanes
70 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
71 controlledLanes.begin(); laneVector != controlledLanes.end();
72 laneVector++) {
73 for (MSTrafficLightLogic::LaneVector::const_iterator lane =
74 laneVector->begin(); lane != laneVector->end(); lane++) {
75 currentLane = (*lane);
76 buildCountSensorForLane(currentLane, nb);
77 }
78 }
79 }
80
buildCountOutSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes,NLDetectorBuilder & nb)81 void MSSOTLE2Sensors::buildCountOutSensors(
82 MSTrafficLightLogic::LaneVectorVector controlledLanes,
83 NLDetectorBuilder& nb) {
84 //for each lane build an appropriate sensor on it
85 MSLane* currentLane = nullptr;
86
87 //input and ouput lanes
88 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
89 controlledLanes.begin(); laneVector != controlledLanes.end();
90 laneVector++) {
91 for (MSTrafficLightLogic::LaneVector::const_iterator lane =
92 laneVector->begin(); lane != laneVector->end(); lane++) {
93 currentLane = (*lane);
94 buildCountSensorForOutLane(currentLane, nb);
95 }
96 }
97 }
98
buildOutSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes,NLDetectorBuilder & nb)99 void MSSOTLE2Sensors::buildOutSensors(
100 MSTrafficLightLogic::LaneVectorVector controlledLanes,
101 NLDetectorBuilder& nb) {
102 buildOutSensors(controlledLanes, nb, OUTPUT_SENSOR_LENGTH);
103 }
buildOutSensors(MSTrafficLightLogic::LaneVectorVector controlledLanes,NLDetectorBuilder & nb,double sensorLength)104 void MSSOTLE2Sensors::buildOutSensors(
105 MSTrafficLightLogic::LaneVectorVector controlledLanes,
106 NLDetectorBuilder& nb, double sensorLength) {
107 //for each lane build an appropriate sensor on it
108 MSLane* currentLane = nullptr;
109
110 //input and ouput lanes
111 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
112 controlledLanes.begin(); laneVector != controlledLanes.end();
113 laneVector++) {
114 for (MSTrafficLightLogic::LaneVector::const_iterator lane =
115 laneVector->begin(); lane != laneVector->end(); lane++) {
116 currentLane = (*lane);
117 buildSensorForOutLane(currentLane, nb, sensorLength);
118 }
119 }
120 }
121
buildCountSensorForLane(MSLane * lane,NLDetectorBuilder & nb)122 void MSSOTLE2Sensors::buildCountSensorForLane(MSLane* lane, NLDetectorBuilder& nb) {
123 double sensorPos;
124 double lensorLength;
125 MSE2Collector* newSensor = nullptr;
126 //Check not to have more than a sensor for lane
127 if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
128
129 //Check and set zero if the lane is not long enough for the specified sensor start
130 sensorPos = COUNT_SENSOR_START <= lane->getLength() ? COUNT_SENSOR_START : 0;
131
132 //Original:
133 double sensorLength = INPUT_COUNT_SENSOR_LENGTH;
134
135 //Check and trim if the lane is not long enough for the specified sensor length
136 lensorLength = sensorLength <= (lane->getLength() - sensorPos) ? sensorLength : (lane->getLength() - sensorPos);
137
138 //TODO check this lengths
139 // DBG(
140 // std::ostringstream phero_str;
141 // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
142 // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
143 // << lane->getLength() << " ,lensorLength= " << lensorLength
144 // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
145 // WRITE_MESSAGE(
146 // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
147 // )
148
149 //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
150 newSensor = nb.createE2Detector(
151 "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
152 DU_TL_CONTROL, lane,
153 (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
154 HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "");
155
156 MSNet::getInstance()->getDetectorControl().add(
157 SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
158
159 m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
160 m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
161 }
162 }
163
buildCountSensorForOutLane(MSLane * lane,NLDetectorBuilder & nb)164 void MSSOTLE2Sensors::buildCountSensorForOutLane(MSLane* lane, NLDetectorBuilder& nb) {
165 double sensorPos;
166 double lensorLength;
167 MSE2Collector* newSensor = nullptr;
168 //Check not to have more than a sensor for lane
169 if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
170
171 //Original:
172 double sensorLength = OUTPUT_COUNT_SENSOR_LENGTH;
173 //Check and set zero if the lane is not long enough for the specified sensor start
174 sensorPos = (lane->getLength() - sensorLength)
175 - (SENSOR_START <= lane->getLength() ? SENSOR_START : 0);
176
177 //Check and trim if the lane is not long enough for the specified sensor lenght
178 lensorLength =
179 sensorLength <= (lane->getLength() - sensorPos) ?
180 sensorLength : (lane->getLength() - sensorPos);
181
182 //TODO check this lengths
183 // DBG(
184 // std::ostringstream phero_str;
185 // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
186 // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
187 // << lane->getLength() << " ,lensorLength= " << lensorLength
188 // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
189 // WRITE_MESSAGE(
190 // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
191 // )
192
193 //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
194 newSensor = nb.createE2Detector(
195 "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
196 DU_TL_CONTROL, lane,
197 (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
198 HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "");
199
200 MSNet::getInstance()->getDetectorControl().add(
201 SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
202
203 m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
204 m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
205 }
206 }
207
getPassedVeh(std::string laneId,bool)208 int MSSOTLE2Sensors::getPassedVeh(std::string laneId, bool /* out */) {
209 MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator;
210 sensorsIterator = m_sensorMap.find(laneId);
211 if (sensorsIterator == m_sensorMap.end()) {
212 assert(0);
213 return 0;
214 } else {
215 int additional = 0;
216 if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end())
217 for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
218 int tmp = 0;
219 if (getVelueFromSensor(*it, &MSE2Collector::getPassedVeh, tmp)) {
220 additional += tmp;
221 }
222 }
223 return sensorsIterator->second->getPassedVeh() + additional;
224 }
225 }
226
227 void
subtractPassedVeh(std::string laneId,int passed)228 MSSOTLE2Sensors::subtractPassedVeh(std::string laneId, int passed) {
229 //TODO Ivan: ciclare tutti i sensori di count e resettare passedVeh
230 MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator;
231
232 sensorsIterator = m_sensorMap.find(laneId);
233 if (sensorsIterator != m_sensorMap.end()) {
234 sensorsIterator->second->subtractPassedVeh(passed);
235 }
236 }
237
238 /*******************************************************************************/
239
buildSensorForLane(MSLane * lane,NLDetectorBuilder & nb)240 void MSSOTLE2Sensors::buildSensorForLane(MSLane* lane, NLDetectorBuilder& nb) {
241 buildSensorForLane(lane, nb, INPUT_SENSOR_LENGTH);
242 }
243
244
245
buildSensorForLane(MSLane * lane,NLDetectorBuilder & nb,double sensorLength)246 void MSSOTLE2Sensors::buildSensorForLane(MSLane* lane, NLDetectorBuilder& nb, double sensorLength) {
247 const MSEdge* edge = &lane->getEdge();
248 if (edge->isInternal() || edge->isWalkingArea() || edge->isCrossing()) {
249 return;
250 }
251 double sensorPos;
252 double lensorLength;
253 MSE2Collector* newSensor = nullptr;
254 //Check not to have more than a sensor for lane
255 if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
256
257 //Check and set zero if the lane is not long enough for the specified sensor start
258 sensorPos = SENSOR_START <= lane->getLength() ? SENSOR_START : 0;
259 //Check and trim if the lane is not long enough for the specified sensor lenght
260 lensorLength = sensorLength <= (lane->getLength() - sensorPos) ? sensorLength : (lane->getLength() - sensorPos);
261 //TODO check this lengths
262 // DBG(
263 // std::ostringstream phero_str;
264 // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
265 // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
266 // << lane->getLength() << " ,lensorLength= " << lensorLength
267 // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
268 // WRITE_MESSAGE(
269 // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
270 // )
271
272 //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
273 newSensor = nb.createE2Detector(
274 "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
275 DU_TL_CONTROL, lane,
276 (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
277 HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "");
278
279 MSNet::getInstance()->getDetectorControl().add(SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
280
281 m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
282 m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
283 //Avoid creating really small sensors. Only add a further sensors if the one created is less than 90% the requested length
284 if (lensorLength < sensorLength * 0.9) {
285 std::ostringstream oss;
286 oss << "Sensor on lane " << lane->getID() << " is long " << lensorLength << ", while it should be " << sensorLength << ". Continuing it on the other lanes if possible";
287 WRITE_MESSAGE(oss.str())
288 for (std::vector<MSLane::IncomingLaneInfo>::const_iterator it = lane->getIncomingLanes().begin(); it != lane->getIncomingLanes().end(); ++it) {
289 const MSEdge* edge = &it->lane->getEdge();
290 if (!edge->isInternal() && !edge->isWalkingArea() && !edge->isCrossing()) {
291 buildContinueSensior(lane, nb, sensorLength, it->lane, lensorLength);
292 }
293 }
294 }
295 }
296 }
297
buildContinueSensior(MSLane * lane,NLDetectorBuilder & nb,double sensorLength,MSLane * continueOnLane,double usedLength)298 void MSSOTLE2Sensors::buildContinueSensior(MSLane* lane, NLDetectorBuilder& nb, double sensorLength, MSLane* continueOnLane, double usedLength) {
299 double availableLength = sensorLength - usedLength;
300 if (m_sensorMap.find(continueOnLane->getID()) == m_sensorMap.end()) {
301 double length = availableLength <= continueOnLane->getLength() ? availableLength : continueOnLane->getLength();
302 MSE2Collector* newSensor = nb.createE2Detector(
303 "SOTL_E2_lane:" + continueOnLane->getID() + "_tl:" + tlLogicID,
304 DU_TL_CONTROL, continueOnLane,
305 (continueOnLane->getLength() - length), INVALID_POSITION, length,
306 HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "");
307 MSNet::getInstance()->getDetectorControl().add(SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
308 m_sensorMap.insert(MSLaneID_MSE2Collector(continueOnLane->getID(), newSensor));
309 m_continueSensorOnLanes[lane->getID()].push_back(continueOnLane->getID());
310 std::ostringstream oss;
311 oss << "Continue sensor on lane " << continueOnLane->getID() << ". Current length " << (length + usedLength);
312 WRITE_MESSAGE(oss.str())
313 //Continue other line if needed.
314 if (length + usedLength < sensorLength * 0.9) {
315 for (std::vector<MSLane::IncomingLaneInfo>::const_iterator it = continueOnLane->getIncomingLanes().begin(); it != continueOnLane->getIncomingLanes().end(); ++it) {
316 const MSEdge* edge = &it->lane->getEdge();
317 if (!edge->isInternal() && !edge->isWalkingArea() && !edge->isCrossing()) {
318 buildContinueSensior(lane, nb, sensorLength, it->lane, length + usedLength);
319 }
320 }
321 }
322 }
323 }
324
buildSensorForOutLane(MSLane * lane,NLDetectorBuilder & nb)325 void MSSOTLE2Sensors::buildSensorForOutLane(MSLane* lane,
326 NLDetectorBuilder& nb) {
327 buildSensorForOutLane(lane, nb, OUTPUT_SENSOR_LENGTH);
328 }
329
buildSensorForOutLane(MSLane * lane,NLDetectorBuilder & nb,double sensorLength)330 void MSSOTLE2Sensors::buildSensorForOutLane(MSLane* lane,
331 NLDetectorBuilder& nb, double sensorLength) {
332 double sensorPos;
333 double lensorLength;
334 MSE2Collector* newSensor = nullptr;
335 //Check not to have more than a sensor for lane
336 if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
337
338 //Original:
339 //double sensorLength = OUTPUT_SENSOR_LENGTH;
340 //Check and set zero if the lane is not long enough for the specified sensor start
341 sensorPos = (lane->getLength() - sensorLength)
342 - (SENSOR_START <= lane->getLength() ? SENSOR_START : 0);
343
344 //Check and trim if the lane is not long enough for the specified sensor lenght
345 lensorLength =
346 sensorLength <= (lane->getLength() - sensorPos) ?
347 sensorLength : (lane->getLength() - sensorPos);
348
349 //TODO check this lengths
350 // DBG(
351 // std::ostringstream phero_str;
352 // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
353 // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
354 // << lane->getLength() << " ,lensorLength= " << lensorLength
355 // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
356 // WRITE_MESSAGE(
357 // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
358 // )
359
360 //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
361 newSensor = nb.createE2Detector(
362 "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
363 DU_TL_CONTROL, lane,
364 (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
365 HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "");
366
367 MSNet::getInstance()->getDetectorControl().add(
368 SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
369
370 m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
371 m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
372 }
373 }
374
countVehicles(MSLane * lane)375 int MSSOTLE2Sensors::countVehicles(MSLane* lane) {
376 return countVehicles(lane->getID());
377 }
378
379 /*
380 * Estimate queue lenght according to the distance of the last vehicles
381 */
382
getEstimateQueueLength(std::string laneId)383 double MSSOTLE2Sensors::getEstimateQueueLength(std::string laneId) {
384 MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator = m_sensorMap.find(laneId);
385 if (sensorsIterator == m_sensorMap.end()) {
386 assert(0);
387 return 0;
388 } else {
389 double estQL = sensorsIterator->second->getEstimateQueueLength();
390 if (estQL == -1) {
391 return 0;
392 } else
393 // DBG(
394 // std::ostringstream str;
395 // str << "MSSOTLE2Sensors::getEstimateQueueLength lane " << sensorsIterator->second->getLane()->getID()
396 // << " laneLength " << sensorsIterator->second->getLane()->getLength() << " estimateQueueLength " << estQL;
397 // WRITE_MESSAGE(str.str());
398 // )
399 {
400 return estQL;
401 }
402 }
403 }
404
405 /*
406 * Estimate queue lenght according to the distance of the last vehicles that exceed a threshold
407 */
408
estimateVehicles(std::string laneId)409 int MSSOTLE2Sensors::estimateVehicles(std::string laneId) {
410 MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator = m_sensorMap.find(laneId);
411 if (sensorsIterator == m_sensorMap.end()) {
412 assert(0);
413 return 0;
414 }
415 int additional = 0;
416 if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end())
417 for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
418 if (m_sensorMap.find(*it) != m_sensorMap.end()) {
419 additional += m_sensorMap[*it]->getEstimatedCurrentVehicleNumber(speedThresholdParam);
420 }
421 }
422 return sensorsIterator->second->getEstimatedCurrentVehicleNumber(speedThresholdParam) + additional;
423 }
424
countVehicles(std::string laneId)425 int MSSOTLE2Sensors::countVehicles(std::string laneId) {
426 MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator = m_sensorMap.find(laneId);
427 if (sensorsIterator == m_sensorMap.end()) {
428 assert(0);
429 return 0;
430 }
431 int additional = 0;
432 if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end()) {
433 for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
434 if (m_sensorMap.find(*it) != m_sensorMap.end()) {
435 additional += count(m_sensorMap[*it]);
436 }
437 }
438 }
439 return count(sensorsIterator->second) + additional;
440 }
441
getMaxSpeed(std::string laneId)442 double MSSOTLE2Sensors::getMaxSpeed(std::string laneId) {
443 MSLaneID_MaxSpeedMap::const_iterator sensorsIteratorIn = m_maxSpeedMap.find(laneId);
444 if (sensorsIteratorIn == m_maxSpeedMap.end()) {
445 assert(0);
446 WRITE_ERROR("MSSOTLE2Sensors::meanVehiclesSpeed:: No lane found " + laneId);
447 return 0;
448 }
449 return sensorsIteratorIn->second;
450 }
451
meanVehiclesSpeed(MSLane * lane)452 double MSSOTLE2Sensors::meanVehiclesSpeed(MSLane* lane) {
453 return meanVehiclesSpeed(lane->getID());
454 }
455
meanVehiclesSpeed(std::string laneId)456 double MSSOTLE2Sensors::meanVehiclesSpeed(std::string laneId) {
457 MSLaneID_MSE2CollectorMap::const_iterator sensorsIteratorOut = m_sensorMap.find(laneId);
458 if (sensorsIteratorOut == m_sensorMap.end()) {
459 assert(0);
460 WRITE_ERROR("MSSOTLE2Sensors::meanVehiclesSpeed:: No lane found " + laneId);
461 return 0;
462 }
463 double meanSpeedAcc = 0;
464 int totalCarNumer = 0;
465 if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end())
466 for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
467 int number = 0;
468 double mean = -1;
469 if (!getVelueFromSensor(*it, &MSE2Collector::getCurrentVehicleNumber, number)) {
470 continue;
471 }
472 totalCarNumer += number;
473 getVelueFromSensor(*it, &MSE2Collector::getCurrentMeanSpeed, mean);
474 meanSpeedAcc += mean * (double) number;
475 }
476 int number = sensorsIteratorOut->second->getCurrentVehicleNumber();
477 totalCarNumer += number;
478 double mean = sensorsIteratorOut->second->getCurrentMeanSpeed();
479 meanSpeedAcc += mean * (double) number;
480 return totalCarNumer == 0 ? -1 : meanSpeedAcc / (double) totalCarNumer;
481 }
482
trim(std::string & str)483 std::string trim(std::string& str) {
484 int first = (int)str.find_first_not_of(' ');
485 int last = (int)str.find_last_not_of(' ');
486 return str.substr(first, (last - first + 1));
487 }
488
split(const std::string & s,char delim,std::vector<std::string> & elems)489 std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems) {
490 std::stringstream ss(s);
491 std::string item;
492 while (std::getline(ss, item, delim)) {
493 if (!item.empty()) {
494 elems.push_back(item);
495 }
496 }
497 return elems;
498 }
499
setVehicleWeigths(const std::string & weightString)500 void MSSOTLE2Sensors::setVehicleWeigths(const std::string& weightString) {
501 std::vector<std::string> types;
502 split(weightString, ';', types);
503 std::ostringstream logstr;
504 logstr << "[MSSOTLE2Sensors::setVehicleWeigths] ";
505 for (std::vector<std::string>::iterator typesIt = types.begin(); typesIt != types.end(); ++typesIt) {
506 std::vector<std::string> typeWeight;
507 split(*typesIt, '=', typeWeight);
508 if (typeWeight.size() == 2) {
509 std::string type = trim(typeWeight[0]);
510 int value = StringUtils::toInt(typeWeight[1]);
511 logstr << type << "=" << value << " ";
512 m_typeWeightMap[type] = value;
513 }
514 }
515 WRITE_MESSAGE(logstr.str());
516 }
517
count(MSE2Collector * sensor)518 int MSSOTLE2Sensors::count(MSE2Collector* sensor) {
519 int totCars = sensor->getCurrentVehicleNumber();
520 if (m_typeWeightMap.size() == 0) {
521 return totCars;
522 }
523 int number = 0;
524 const std::vector<MSE2Collector::VehicleInfo*> vehicles = sensor->getCurrentVehicles();
525 std::ostringstream logstr;
526 logstr << "[MSSOTLE2Sensors::count]";
527 for (std::vector<MSE2Collector::VehicleInfo*>::const_iterator vit = vehicles.begin(); vit != vehicles.end(); ++vit) {
528 if ((*vit)->onDetector) {
529 const std::string vtype = (*vit)->type;
530 if (m_typeWeightMap.find(vtype) != m_typeWeightMap.end()) {
531 number += m_typeWeightMap[vtype];
532 // DBG(logstr << " Added " << m_typeWeightMap[vtype] << " for vtype " << vtype;)
533 } else {
534 ++number;
535 }
536 }
537 }
538 // DBG(if (totCars != number) {
539 // logstr << ". Real number " << totCars << "; weighted " << number;
540 // WRITE_MESSAGE(logstr.str());
541 // })
542 return number;
543 }
544