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-2018 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
121 MSDevice_Bluelight::notifyMove(SUMOVehicle& veh, double /* oldPos */,
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  //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  // the perception of the sound of the siren should be around 25 meters
151  // todo only vehicles in front of the emergency vehicle should react
152  if (distanceDelta <= 25 && veh.getID() != veh2->getID() && influencedVehicles.count(veh2->getID()) == 0) {
153  influencedVehicles.insert(static_cast<std::string>(veh2->getID()));
154  influencedTypes.insert(std::make_pair(static_cast<std::string>(veh2->getID()), veh2->getVehicleType().getID()));
155  //Vehicle gets a new Vehicletype to change the alignment and the lanechange options
156  MSVehicleType& t = static_cast<MSVehicle*>(veh2)->getSingularType();
157  MSVehicle::Influencer& lanechange = static_cast<MSVehicle*>(veh2)->getInfluencer();
158 
159  //other vehicle should not use the rescue lane so they should not make any lane changes
160  lanechange.setLaneChangeMode(1605);
161  const int numLanes = (int)veh2->getEdge()->getLanes().size();
162  //Setting the lateral alignment to build a rescue lane
163  if (veh2->getLane()->getIndex() == numLanes - 1) {
165  // the alignement is changet to left for the vehicle std::cout << "New alignment to left for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
166  } else {
168  // the alignement is changet to right for the vehicle std::cout << "New alignment to right for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
169  }
170 
171  }
172 
173  } else { //if vehicle is passed all vehicles which had to react should get their state back after they leave the communication range
174  if (influencedVehicles.count(veh2->getID()) > 0) {
175  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
176  if (distanceDelta > 25 && veh.getID() != veh2->getID()) {
177  influencedVehicles.erase(veh2->getID());
178  std::map<std::string, std::string>::iterator it = influencedTypes.find(veh2->getID());
179  if (it != influencedTypes.end()) {
180  // The vehicle gets back its old VehicleType after the emergency vehicle have passed them
181  MSVehicleType* targetType = MSNet::getInstance()->getVehicleControl().getVType(it->second);
182  //targetType is nullptr if the vehicle type has already changed to its old vehicleType
183  if (targetType != nullptr) {
184  static_cast<MSVehicle*>(veh2)->replaceVehicleType(targetType);
185  }
186  }
187  }
188  }
189  }
190  }
191  return true; // keep the device
192 }
193 
194 
195 bool
197 #ifdef DEBUG_BLUELIGHT
198  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
199 #else
200  UNUSED_PARAMETER(veh);
201  UNUSED_PARAMETER(reason);
202 #endif
203  return true; // keep the device
204 }
205 
206 
207 bool
208 MSDevice_Bluelight::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
209 #ifdef DEBUG_BLUELIGHT
210  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
211 #else
212  UNUSED_PARAMETER(veh);
213  UNUSED_PARAMETER(reason);
214 #endif
215  return true; // keep the device
216 }
217 
218 
219 void
221  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
222  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
223  os.openTag("example_device");
224  os.writeAttr("customValue1", toString(myCustomValue1));
225  os.writeAttr("customValue2", toString(myCustomValue2));
226  os.closeTag();
227  }
228 }
229 
230 std::string
231 MSDevice_Bluelight::getParameter(const std::string& key) const {
232  if (key == "customValue1") {
233  return toString(myCustomValue1);
234  } else if (key == "customValue2") {
235  return toString(myCustomValue2);
236  } else if (key == "meaningOfLife") {
237  return "42";
238  }
239  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
240 }
241 
242 
243 void
244 MSDevice_Bluelight::setParameter(const std::string& key, const std::string& value) {
245  double doubleValue;
246  try {
247  doubleValue = StringUtils::toDouble(value);
248  } catch (NumberFormatException&) {
249  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
250  }
251  if (key == "customValue1") {
252  myCustomValue1 = doubleValue;
253  } else {
254  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
255  }
256 }
257 
258 
259 /****************************************************************************/
260 
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:79
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
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
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:1355
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:162
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
double myCustomValue1
a value which is initialised based on a commandline/configuration option
const std::string & getID() const
Returns the id.
Definition: Named.h:78
drive on the right side
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:33
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
The car-following model and parameter.
Definition: MSVehicleType.h:66
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
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 setSpeedMode(int speedMode)
Sets speed-constraining behaviors.
Definition: MSVehicle.cpp:643
int getIndex() const
Returns the lane&#39;s index.
Definition: MSLane.h:537
~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:49
Representation of a vehicle.
Definition: SUMOVehicle.h:60
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
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:316
std::set< std::string > influencedVehicles
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
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:121
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)
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=0)
Returns the named vehicle type or a sample from the named distribution.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Bluelight-options.
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:208
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:92
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.
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
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:653
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
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.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
double myCustomValue2
a value which is initialised based on a vehicle parameter