Eclipse SUMO - Simulation of Urban MObility
MSDevice_Bluelight.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-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 /****************************************************************************/
17 // A device for emergency vehicle. The behaviour of other traffic participants will be triggered with this device.
18 // For example building a rescue lane.
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
30 #include <microsim/MSNet.h>
31 #include <microsim/MSLane.h>
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSVehicle.h>
34 #include "MSDevice_Tripinfo.h"
35 #include "MSDevice_Bluelight.h"
37 #include <microsim/MSVehicleType.h>
38 
39 //#define DEBUG_BLUELIGHT
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 // ---------------------------------------------------------------------------
45 // static initialisation methods
46 // ---------------------------------------------------------------------------
47 void
49  oc.addOptionSubTopic("Bluelight Device");
50  insertDefaultAssignmentOptions("bluelight", "Bluelight Device", oc);
51 
52  oc.doRegister("device.bluelight.parameter", new Option_Float(0.0));
53  oc.addDescription("device.bluelight.parameter", "Bluelight Device", "An exemplary parameter which can be used by all instances of the example device");
54 
55 }
56 
57 
58 void
59 MSDevice_Bluelight::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
61  if (equippedByDefaultAssignmentOptions(oc, "bluelight", v, false)) {
62  // build the device
63  // get custom vehicle parameter
64  double customParameter2 = -1;
65  if (v.getParameter().knowsParameter("bluelight")) {
66  try {
67  customParameter2 = StringUtils::toDouble(v.getParameter().getParameter("bluelight", "-1"));
68  } catch (...) {
69  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("bluelight", "-1") + "'for vehicle parameter 'example'");
70  }
71 
72  } else {
73 #ifdef DEBUG_BLUELIGHT
74  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'bluelight'. Using default of " << customParameter2 << "\n";
75 #endif
76  }
77  // get custom vType parameter
78  double customParameter3 = -1;
79  if (v.getVehicleType().getParameter().knowsParameter("bluelight")) {
80  try {
81  customParameter3 = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("bluelight", "-1"));
82  } catch (...) {
83  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("bluelight", "-1") + "'for vType parameter 'bluelight'");
84  }
85 
86  } else {
87 #ifdef DEBUG_BLUELIGHT
88  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'bluelight'. Using default of " << customParameter3 << "\n";
89 #endif
90  }
91  MSDevice_Bluelight* device = new MSDevice_Bluelight(v, "bluelight_" + v.getID(),
92  oc.getFloat("device.bluelight.parameter"),
93  customParameter2,
94  customParameter3);
95  into.push_back(device);
96  }
97 }
98 
99 
100 // ---------------------------------------------------------------------------
101 // MSDevice_Bluelight-methods
102 // ---------------------------------------------------------------------------
104  double customValue1, double customValue2, double customValue3) :
105  MSVehicleDevice(holder, id),
106  myCustomValue1(customValue1),
107  myCustomValue2(customValue2),
108  myCustomValue3(customValue3) {
109 #ifdef DEBUG_BLUELIGHT
110  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
111 #endif
112 }
113 
114 
116 }
117 
118 
119 bool
121  double /* newPos */, double newSpeed) {
122 #ifdef DEBUG_BLUELIGHT
123  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
124 #else
125  UNUSED_PARAMETER(newSpeed);
126 #endif
127  // check whether another device is present on the vehicle:
128  /*MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
129  if (otherDevice != 0) {
130  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
131  }*/
132  //violate red lights this only need to be done once so shift it todo
133  MSVehicle::Influencer& redLight = static_cast<MSVehicle&>(veh).getInfluencer();
134  redLight.setSpeedMode(7);
135  // build a rescue lane for all vehicles on the route of the emergency vehicle within the range of the siren
139  std::string currentEdgeID = veh.getEdge()->getID();
140  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
141  SUMOVehicle* veh2 = it->second;
142  int maxdist = 25;
143  //Vehicle only from edge should react
144  if (currentEdgeID == veh2->getEdge()->getID()) {
145  if (veh2->getDevice(typeid(MSDevice_Bluelight)) != nullptr) {
146  // emergency vehicles should not react
147  continue;
148  }
149  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
150  //emergency vehicle has to slow down when entering the resuce lane
151  if (distanceDelta <= 10 && veh.getID() != veh2->getID() && influencedVehicles.count(veh2->getID()) > 0 && veh2->getSpeed() < 1) {
152  // set ev speed to 20 km/h 0 5.56 m/s
153  std::vector<std::pair<SUMOTime, double> > speedTimeLine;
154  speedTimeLine.push_back(std::make_pair(MSNet::getInstance()->getCurrentTimeStep(), veh.getSpeed()));
155  speedTimeLine.push_back(std::make_pair(MSNet::getInstance()->getCurrentTimeStep() + TIME2STEPS(2), 5.56));
156  redLight.setSpeedTimeLine(speedTimeLine);
157  }
158 
159  // the perception of the sound of the siren should be around 25 meters
160  // todo only vehicles in front of the emergency vehicle should react
161  if (distanceDelta <= maxdist && veh.getID() != veh2->getID() && influencedVehicles.count(veh2->getID()) == 0) {
162  //online a percentage of vehicles should react to the emergency vehicle to make the behaviour more realistic
163  double reaction = RandHelper::rand();
164  MSVehicle::Influencer& lanechange = static_cast<MSVehicle*>(veh2)->getInfluencer();
165 
166  //other vehicle should not use the rescue lane so they should not make any lane changes
167  lanechange.setLaneChangeMode(1605);//todo change lane back
168  const int numLanes = (int)veh2->getEdge()->getLanes().size();
169  // the vehicles should react according to the distance to the emergency vehicle taken from real world data
170  if (reaction < (distanceDelta * -1.6 + 100) / 100) {
171  influencedVehicles.insert(static_cast<std::string>(veh2->getID()));
172  influencedTypes.insert(std::make_pair(static_cast<std::string>(veh2->getID()), veh2->getVehicleType().getID()));
173 
174  //Vehicle gets a new Vehicletype to change the alignment and the lanechange options
175  MSVehicleType& t = static_cast<MSVehicle*>(veh2)->getSingularType();
176  //Setting the lateral alignment to build a rescue lane
177  if (veh2->getLane()->getIndex() == numLanes - 1) {
179  // the alignement is changet to left for the vehicle std::cout << "New alignment to left for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
180  } else {
182  // the alignement is changet to right for the vehicle std::cout << "New alignment to right for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
183  }
184  }
185  }
186 
187  } else { //if vehicle is passed all vehicles which had to react should get their state back after they leave the communication range
188  if (influencedVehicles.count(veh2->getID()) > 0) {
189  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
190  if (distanceDelta > maxdist && veh.getID() != veh2->getID()) {
191  influencedVehicles.erase(veh2->getID());
192  std::map<std::string, std::string>::iterator it = influencedTypes.find(veh2->getID());
193  if (it != influencedTypes.end()) {
194  // The vehicle gets back its old VehicleType after the emergency vehicle have passed them
195  MSVehicleType* targetType = MSNet::getInstance()->getVehicleControl().getVType(it->second);
196  //targetType is nullptr if the vehicle type has already changed to its old vehicleType
197  if (targetType != nullptr) {
198  static_cast<MSVehicle*>(veh2)->replaceVehicleType(targetType);
199  }
200  }
201  }
202  }
203  }
204  }
205  return true; // keep the device
206 }
207 
208 
209 bool
211 #ifdef DEBUG_BLUELIGHT
212  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
213 #else
214  UNUSED_PARAMETER(veh);
215  UNUSED_PARAMETER(reason);
216 #endif
217  return true; // keep the device
218 }
219 
220 
221 bool
222 MSDevice_Bluelight::notifyLeave(SUMOTrafficObject& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
223 #ifdef DEBUG_BLUELIGHT
224  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
225 #else
226  UNUSED_PARAMETER(veh);
227  UNUSED_PARAMETER(reason);
228 #endif
229  return true; // keep the device
230 }
231 
232 
233 void
235  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
236  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
237  os.openTag("example_device");
238  os.writeAttr("customValue1", toString(myCustomValue1));
239  os.writeAttr("customValue2", toString(myCustomValue2));
240  os.closeTag();
241  }
242 }
243 
244 std::string
245 MSDevice_Bluelight::getParameter(const std::string& key) const {
246  if (key == "customValue1") {
247  return toString(myCustomValue1);
248  } else if (key == "customValue2") {
249  return toString(myCustomValue2);
250  } else if (key == "meaningOfLife") {
251  return "42";
252  }
253  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
254 }
255 
256 
257 void
258 MSDevice_Bluelight::setParameter(const std::string& key, const std::string& value) {
259  double doubleValue;
260  try {
261  doubleValue = StringUtils::toDouble(value);
262  } catch (NumberFormatException&) {
263  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
264  }
265  if (key == "customValue1") {
266  myCustomValue1 = doubleValue;
267  } else {
268  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
269  }
270 }
271 
272 
273 /****************************************************************************/
274 
MSVehicleType
The car-following model and parameter.
Definition: MSVehicleType.h:65
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
MSDevice_Bluelight::notifyLeave
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
Definition: MSDevice_Bluelight.cpp:222
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
LATALIGN_LEFT
@ LATALIGN_LEFT
drive on the left side
Definition: SUMOXMLDefinitions.h:1340
SUMOTrafficObject::getPosition
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
MSVehicleType::getID
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:93
MSVehicleType::setPreferredLateralAlignment
void setPreferredLateralAlignment(LateralAlignment latAlignment)
Set vehicle's preferred lateral alignment.
Definition: MSVehicleType.cpp:161
MSVehicle::Influencer
Changes the wished vehicle speed / lanes.
Definition: MSVehicle.h:1455
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
MSVehicleControl::getVType
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=nullptr)
Returns the named vehicle type or a sample from the named distribution.
Definition: MSVehicleControl.cpp:353
SUMOVehicle::getParameter
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
LATALIGN_ARBITRARY
@ LATALIGN_ARBITRARY
maintain the current alignment
Definition: SUMOXMLDefinitions.h:1334
MSDevice_Bluelight::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Bluelight.cpp:59
OptionsCont.h
SUMOTrafficObject::getEdge
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSDevice_Bluelight::MSDevice_Bluelight
MSDevice_Bluelight(SUMOVehicle &holder, const std::string &id, double customValue1, double customValue2, double customValue3)
Constructor.
Definition: MSDevice_Bluelight.cpp:103
MSEdge.h
MSVehicleControl::constVehIt
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
Definition: MSVehicleControl.h:74
MSDevice_Bluelight::influencedTypes
std::map< std::string, std::string > influencedTypes
Definition: MSDevice_Bluelight.h:152
MSDevice_Bluelight::myCustomValue1
double myCustomValue1
a value which is initialised based on a commandline/configuration option
Definition: MSDevice_Bluelight.h:155
Parameterised::getParameter
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Definition: Parameterised.cpp:72
MSVehicle.h
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
MSDevice_Bluelight::getParameter
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
Definition: MSDevice_Bluelight.cpp:245
MSVehicleType.h
LATALIGN_RIGHT
@ LATALIGN_RIGHT
drive on the right side
Definition: SUMOXMLDefinitions.h:1330
MSDevice_Bluelight::myCustomValue2
double myCustomValue2
a value which is initialised based on a vehicle parameter
Definition: MSDevice_Bluelight.h:158
NumberFormatException
Definition: UtilExceptions.h:95
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
SUMOVehicle.h
RandHelper::rand
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:53
OptionsCont::addDescription
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Definition: OptionsCont.cpp:469
MSDevice_Bluelight::influencedVehicles
std::set< std::string > influencedVehicles
Definition: MSDevice_Bluelight.h:149
MSDevice_Bluelight::generateOutput
void generateOutput() const
Called on writing tripinfo output.
Definition: MSDevice_Bluelight.cpp:234
MSDevice::insertDefaultAssignmentOptions
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc, const bool isPerson=false)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:126
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
Position::distanceTo
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:233
MSDevice_Bluelight::setParameter
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key
Definition: MSDevice_Bluelight.cpp:258
MSVehicleControl::loadedVehEnd
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
Definition: MSVehicleControl.h:185
SUMOVehicle::getDevice
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
SUMOVehicle::getLane
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
MSVehicle::Influencer::setSpeedTimeLine
void setSpeedTimeLine(const std::vector< std::pair< SUMOTime, double > > &speedTimeLine)
Sets a new velocity timeline.
Definition: MSVehicle.cpp:386
OutputDevice.h
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:74
MSDevice_Bluelight::myCustomValue3
double myCustomValue3
a value which is initialised based on a vType parameter
Definition: MSDevice_Bluelight.h:161
MSDevice_Bluelight.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSDevice_Bluelight::deviceName
const std::string deviceName() const
return the name for this type of device
Definition: MSDevice_Bluelight.h:116
OptionsCont::addOptionSubTopic
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
Definition: OptionsCont.cpp:519
MSDevice_Bluelight::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
Definition: MSDevice_Bluelight.cpp:210
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSDevice::equippedByDefaultAssignmentOptions
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.h:203
InvalidArgument
Definition: UtilExceptions.h:56
Option_Float
Definition: Option.h:470
MSVehicle::Influencer::setLaneChangeMode
void setLaneChangeMode(int value)
Sets lane changing behavior.
Definition: MSVehicle.cpp:767
MSVehicleType::getParameter
const SUMOVTypeParameter & getParameter() const
Definition: MSVehicleType.h:560
MSEdge::getLanes
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:167
MSDevice_Bluelight::~MSDevice_Bluelight
~MSDevice_Bluelight()
Destructor.
Definition: MSDevice_Bluelight.cpp:115
config.h
MSVehicleControl
The class responsible for building and deletion of vehicles.
Definition: MSVehicleControl.h:71
MSDevice_Tripinfo.h
MSDevice_Bluelight::notifyMove
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
Definition: MSDevice_Bluelight.cpp:120
MSVehicleControl::loadedVehBegin
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
Definition: MSVehicleControl.h:177
MSLane.h
MSVehicle::Influencer::setSpeedMode
void setSpeedMode(int speedMode)
Sets speed-constraining behaviors.
Definition: MSVehicle.cpp:757
MSDevice_Bluelight::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Bluelight-options.
Definition: MSDevice_Bluelight.cpp:48
MSVehicleControl.h
MSDevice_Bluelight
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_Bluelight.h:47
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:91
OutputDevice::getDeviceByOption
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
Definition: OutputDevice.cpp:116
MSLane::getIndex
int getIndex() const
Returns the lane's index.
Definition: MSLane.h:563
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:336
Parameterised::knowsParameter
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Definition: Parameterised.cpp:66
SUMOTrafficObject::getSpeed
virtual double getSpeed() const =0
Returns the vehicle's current speed.
MSVehicle
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
MSVehicleDevice
Abstract in-vehicle device.
Definition: MSVehicleDevice.h:54