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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A device for emergency vehicle. The behaviour of other traffic participants will be triggered with this device.
21 // For example building a rescue lane.
22 /****************************************************************************/
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
37 #include <microsim/MSNet.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSEdge.h>
40 #include <microsim/MSVehicle.h>
41 #include "MSDevice_Tripinfo.h"
42 #include "MSDevice_Bluelight.h"
44 #include <microsim/MSVehicleType.h>
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 // ---------------------------------------------------------------------------
50 // static initialisation methods
51 // ---------------------------------------------------------------------------
52 void
54  oc.addOptionSubTopic("Bluelight Device");
55  insertDefaultAssignmentOptions("bluelight", "Bluelight Device", oc);
56 
57  oc.doRegister("device.bluelight.parameter", new Option_Float(0.0));
58  oc.addDescription("device.bluelight.parameter", "Bluelight Device", "An exemplary parameter which can be used by all instances of the example device");
59 
60 }
61 
62 
63 void
64 MSDevice_Bluelight::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
66  if (equippedByDefaultAssignmentOptions(oc, "bluelight", v)) {
67  // build the device
68  // get custom vehicle parameter
69  double customParameter2 = -1;
70  if (v.getParameter().knowsParameter("bluelight")) {
71  try {
72  customParameter2 = TplConvert::_2double(v.getParameter().getParameter("bluelight", "-1").c_str());
73  } catch (...) {
74  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("bluelight", "-1") + "'for vehicle parameter 'example'");
75  }
76 
77  } else {
78  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'bluelight'. Using default of " << customParameter2 << "\n";
79  }
80  // get custom vType parameter
81  double customParameter3 = -1;
82  if (v.getVehicleType().getParameter().knowsParameter("bluelight")) {
83  try {
84  customParameter3 = TplConvert::_2double(v.getVehicleType().getParameter().getParameter("bluelight", "-1").c_str());
85  } catch (...) {
86  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("bluelight", "-1") + "'for vType parameter 'bluelight'");
87  }
88 
89  } else {
90  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'bluelight'. Using default of " << customParameter3 << "\n";
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  MSDevice(holder, id),
107  myCustomValue1(customValue1),
108  myCustomValue2(customValue2),
109  myCustomValue3(customValue3) {
110  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
111 }
112 
113 
115 }
116 
117 
118 bool
119 MSDevice_Bluelight::notifyMove(SUMOVehicle& veh, double /* oldPos */,
120  double /* newPos */, double /* newSpeed */) {
121  //std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
122  // check whether another device is present on the vehicle:
123  /*MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
124  if (otherDevice != 0) {
125  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
126  }*/
127  //todo violate red lights
128 
129  // build a rescue lane for all vehicles on the route of the emergency vehicle within the range of the siren
133  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
134  SUMOVehicle* veh2 = it->second;
135  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
136  // todo which distance is a normal reaction time
137  // todo only vehicles in front of the emergency vehicle should react
138  if (distanceDelta <= 100 && veh.getID() != veh2->getID()) {
139  //std::cout << "In Range Vehicle '" << veh2->getID() << "\n";
140  MSVehicleType& t = static_cast<MSVehicle*>(veh2)->getSingularType();
141  if (veh2->getLane()->getIndex() == 0) {
143  //std::cout << "New alignment to right for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
144  } else {
146  //std::cout << "New alignment to left for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
147  }
148 
149  }
150  }
151  return true; // keep the device
152 }
153 
154 
155 bool
157  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
158  return true; // keep the device
159 }
160 
161 
162 bool
163 MSDevice_Bluelight::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
164  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
165  return true; // keep the device
166 }
167 
168 
169 void
171  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
172  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
173  os.openTag("example_device");
174  os.writeAttr("customValue1", toString(myCustomValue1));
175  os.writeAttr("customValue2", toString(myCustomValue2));
176  os.closeTag();
177  }
178 }
179 
180 std::string
181 MSDevice_Bluelight::getParameter(const std::string& key) const {
182  if (key == "customValue1") {
183  return toString(myCustomValue1);
184  } else if (key == "customValue2") {
185  return toString(myCustomValue2);
186  } else if (key == "meaningOfLife") {
187  return "42";
188  }
189  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
190 }
191 
192 
193 void
194 MSDevice_Bluelight::setParameter(const std::string& key, const std::string& value) {
195  double doubleValue;
196  try {
197  doubleValue = TplConvert::_2double(value.c_str());
198  } catch (NumberFormatException) {
199  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
200  }
201  if (key == "customValue1") {
202  myCustomValue1 = doubleValue;
203  } else {
204  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
205  }
206 }
207 
208 
209 /****************************************************************************/
210 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:81
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
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:83
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.
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
double myCustomValue1
a value which is initialised based on a commandline/configuration option
const std::string & getID() const
Returns the id.
Definition: Named.h:65
drive on the right side
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
The car-following model and parameter.
Definition: MSVehicleType.h:72
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
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
int getIndex() const
Returns the lane&#39;s index.
Definition: MSLane.h:520
~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:55
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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:306
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:101
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
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:70
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:115
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:98
const std::string & getID() const
Returns the name of the vehicle type.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
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.
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:70
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:239
bool closeTag()
Closes the most recently opened tag.
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:77
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