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