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 
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<MSDevice*>& 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 = TplConvert::_2double(v.getParameter().getParameter("bluelight", "-1").c_str());
68  } catch (...) {
69  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("bluelight", "-1") + "'for vehicle parameter 'example'");
70  }
71 
72  } else {
73  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'bluelight'. Using default of " << customParameter2 << "\n";
74  }
75  // get custom vType parameter
76  double customParameter3 = -1;
77  if (v.getVehicleType().getParameter().knowsParameter("bluelight")) {
78  try {
79  customParameter3 = TplConvert::_2double(v.getVehicleType().getParameter().getParameter("bluelight", "-1").c_str());
80  } catch (...) {
81  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("bluelight", "-1") + "'for vType parameter 'bluelight'");
82  }
83 
84  } else {
85  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'bluelight'. Using default of " << customParameter3 << "\n";
86  }
87  MSDevice_Bluelight* device = new MSDevice_Bluelight(v, "bluelight_" + v.getID(),
88  oc.getFloat("device.bluelight.parameter"),
89  customParameter2,
90  customParameter3);
91  into.push_back(device);
92  }
93 }
94 
95 
96 // ---------------------------------------------------------------------------
97 // MSDevice_Bluelight-methods
98 // ---------------------------------------------------------------------------
99 MSDevice_Bluelight::MSDevice_Bluelight(SUMOVehicle& holder, const std::string& id,
100  double customValue1, double customValue2, double customValue3) :
101  MSDevice(holder, id),
102  myCustomValue1(customValue1),
103  myCustomValue2(customValue2),
104  myCustomValue3(customValue3) {
105  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
106 }
107 
108 
110 }
111 
112 
113 bool
114 MSDevice_Bluelight::notifyMove(SUMOVehicle& veh, double /* oldPos */,
115  double /* newPos */, double newSpeed) {
116  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
117  // check whether another device is present on the vehicle:
118  /*MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
119  if (otherDevice != 0) {
120  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
121  }*/
122  //violate red lights this only need to be done once so shift it todo
123  MSVehicle::Influencer& redLight = static_cast<MSVehicle&>(veh).getInfluencer();
124  redLight.setSpeedMode(7);
125  // build a rescue lane for all vehicles on the route of the emergency vehicle within the range of the siren
129  std::string currentEdgeID = veh.getEdge()->getID();
130  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
131  SUMOVehicle* veh2 = it->second;
132  //Vehicle only from edge should react
133  if (currentEdgeID == veh2->getEdge()->getID()) {
134  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
135  // the perception of the sound of the siren should be around 25 meters
136  // todo only vehicles in front of the emergency vehicle should react
137  if (distanceDelta <= 25 && veh.getID() != veh2->getID() && influencedVehicles.count(veh2->getID()) == 0) {
138  influencedVehicles.insert(static_cast<std::string>(veh2->getID()));
139  influencedTypes.insert(std::make_pair(static_cast<std::string>(veh2->getID()), veh2->getVehicleType().getID()));
140  //Vehicle gets a new Vehicletype to change the alignment and the lanechange options
141  MSVehicleType& t = static_cast<MSVehicle*>(veh2)->getSingularType();
142  MSVehicle::Influencer& lanechange = static_cast<MSVehicle*>(veh2)->getInfluencer();
143 
144  //other vehicle should not use the rescue lane so they should not make any lane changes
145  lanechange.setLaneChangeMode(1605);
146  const int numLanes = (int)veh2->getEdge()->getLanes().size();
147  //Setting the lateral alignment to build a rescue lane
148  if (veh2->getLane()->getIndex() == numLanes - 1) {
150  // the alignement is changet to left for the vehicle std::cout << "New alignment to left for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
151  } else {
153  // the alignement is changet to right for the vehicle std::cout << "New alignment to right for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
154  }
155 
156  }
157 
158  } else { //if vehicle is passed all vehicles which had to react should get their state back after they leave the communication range
159  if (influencedVehicles.count(veh2->getID()) > 0) {
160  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
161  if (distanceDelta > 25 && veh.getID() != veh2->getID()) {
162  influencedVehicles.erase(veh2->getID());
163  std::map<std::string, std::string>::iterator it = influencedTypes.find(veh2->getID());
164  if (it != influencedTypes.end()) {
165  // The vehicle gets back its old VehicleType after the emergency vehicle have passed them
166  MSVehicleType* targetType = MSNet::getInstance()->getVehicleControl().getVType(it->second);
167  static_cast<MSVehicle*>(veh2)->replaceVehicleType(targetType);
168  }
169  }
170  }
171  }
172  }
173  return true; // keep the device
174 }
175 
176 
177 bool
179  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
180  return true; // keep the device
181 }
182 
183 
184 bool
185 MSDevice_Bluelight::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
186  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
187  return true; // keep the device
188 }
189 
190 
191 void
193  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
194  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
195  os.openTag("example_device");
196  os.writeAttr("customValue1", toString(myCustomValue1));
197  os.writeAttr("customValue2", toString(myCustomValue2));
198  os.closeTag();
199  }
200 }
201 
202 std::string
203 MSDevice_Bluelight::getParameter(const std::string& key) const {
204  if (key == "customValue1") {
205  return toString(myCustomValue1);
206  } else if (key == "customValue2") {
207  return toString(myCustomValue2);
208  } else if (key == "meaningOfLife") {
209  return "42";
210  }
211  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
212 }
213 
214 
215 void
216 MSDevice_Bluelight::setParameter(const std::string& key, const std::string& value) {
217  double doubleValue;
218  try {
219  doubleValue = TplConvert::_2double(value.c_str());
220  } catch (NumberFormatException&) {
221  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
222  }
223  if (key == "customValue1") {
224  myCustomValue1 = doubleValue;
225  } else {
226  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
227  }
228 }
229 
230 
231 /****************************************************************************/
232 
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
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
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 ...
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:1351
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:160
double myCustomValue1
a value which is initialised based on a commandline/configuration option
const std::string & getID() const
Returns the id.
Definition: Named.h:69
drive on the right side
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:242
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:452
int getIndex() const
Returns the lane&#39;s index.
Definition: MSLane.h:530
~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.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:309
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.
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:112
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
std::map< std::string, std::string > influencedTypes
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.
Abstract in-vehicle device.
Definition: MSDevice.h:65
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
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:305
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
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:462
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v, bool outputOptionSet)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:126
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:233
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:71
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