SUMO - Simulation of Urban MObility
MSDevice_Bluelight.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A device for emergency vehicle. The behaviour of other traffic participants will be triggered with this device.
11 // For example building a rescue lane.
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2013-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
38 #include <microsim/MSNet.h>
39 #include <microsim/MSLane.h>
40 #include <microsim/MSEdge.h>
41 #include <microsim/MSVehicle.h>
42 #include "MSDevice_Tripinfo.h"
43 #include "MSDevice_Bluelight.h"
45 #include <microsim/MSVehicleType.h>
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 // ---------------------------------------------------------------------------
51 // static initialisation methods
52 // ---------------------------------------------------------------------------
53 void
55  oc.addOptionSubTopic("Bluelight Device");
56  insertDefaultAssignmentOptions("bluelight", "Bluelight Device", oc);
57 
58  oc.doRegister("device.bluelight.parameter", new Option_Float(0.0));
59  oc.addDescription("device.bluelight.parameter", "Bluelight Device", "An exemplary parameter which can be used by all instances of the example device");
60 
61 }
62 
63 
64 void
65 MSDevice_Bluelight::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
67  if (equippedByDefaultAssignmentOptions(oc, "bluelight", v)) {
68  // build the device
69  // get custom vehicle parameter
70  double customParameter2 = -1;
71  if (v.getParameter().knowsParameter("bluelight")) {
72  try {
73  customParameter2 = TplConvert::_2double(v.getParameter().getParameter("bluelight", "-1").c_str());
74  } catch (...) {
75  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("bluelight", "-1") + "'for vehicle parameter 'example'");
76  }
77 
78  } else {
79  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'bluelight'. Using default of " << customParameter2 << "\n";
80  }
81  // get custom vType parameter
82  double customParameter3 = -1;
83  if (v.getVehicleType().getParameter().knowsParameter("bluelight")) {
84  try {
85  customParameter3 = TplConvert::_2double(v.getVehicleType().getParameter().getParameter("bluelight", "-1").c_str());
86  } catch (...) {
87  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("bluelight", "-1") + "'for vType parameter 'bluelight'");
88  }
89 
90  } else {
91  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'bluelight'. Using default of " << customParameter3 << "\n";
92  }
93  MSDevice_Bluelight* device = new MSDevice_Bluelight(v, "bluelight_" + v.getID(),
94  oc.getFloat("device.bluelight.parameter"),
95  customParameter2,
96  customParameter3);
97  into.push_back(device);
98  }
99 }
100 
101 
102 // ---------------------------------------------------------------------------
103 // MSDevice_Bluelight-methods
104 // ---------------------------------------------------------------------------
106  double customValue1, double customValue2, double customValue3) :
107  MSDevice(holder, id),
108  myCustomValue1(customValue1),
109  myCustomValue2(customValue2),
110  myCustomValue3(customValue3) {
111  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
112 }
113 
114 
116 }
117 
118 
119 bool
120 MSDevice_Bluelight::notifyMove(SUMOVehicle& veh, double /* oldPos */,
121  double /* newPos */, double /* newSpeed */) {
122  //std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
123  // check whether another device is present on the vehicle:
124  /*MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
125  if (otherDevice != 0) {
126  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
127  }*/
128  //todo violate red lights
129 
130  // build a rescue lane for all vehicles on the route of the emergency vehicle within the range of the siren
134  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
135  SUMOVehicle* veh2 = it->second;
136  double distanceDelta = veh.getPosition().distanceTo(veh2->getPosition());
137  // todo which distance is a normal reaction time
138  // todo only vehicles in front of the emergency vehicle should react
139  if (distanceDelta <= 100 && veh.getID() != veh2->getID()) {
140  //std::cout << "In Range Vehicle '" << veh2->getID() << "\n";
141  MSVehicleType& t = static_cast<MSVehicle*>(veh2)->getSingularType();
142  if (veh2->getLane()->getIndex() == 0) {
144  //std::cout << "New alignment to right for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
145  } else {
147  //std::cout << "New alignment to left for vehicle: " << veh2->getID() << " " << veh2->getVehicleType().getPreferredLateralAlignment() << "\n";
148  }
149 
150  }
151  }
152  return true; // keep the device
153 }
154 
155 
156 bool
158  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
159  return true; // keep the device
160 }
161 
162 
163 bool
164 MSDevice_Bluelight::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
165  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
166  return true; // keep the device
167 }
168 
169 
170 void
172  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
173  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
174  os.openTag("example_device");
175  os.writeAttr("customValue1", toString(myCustomValue1));
176  os.writeAttr("customValue2", toString(myCustomValue2));
177  os.closeTag();
178  }
179 }
180 
181 std::string
182 MSDevice_Bluelight::getParameter(const std::string& key) const {
183  if (key == "customValue1") {
184  return toString(myCustomValue1);
185  } else if (key == "customValue2") {
186  return toString(myCustomValue2);
187  } else if (key == "meaningOfLife") {
188  return "42";
189  }
190  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
191 }
192 
193 
194 void
195 MSDevice_Bluelight::setParameter(const std::string& key, const std::string& value) {
196  double doubleValue;
197  try {
198  doubleValue = TplConvert::_2double(value.c_str());
199  } catch (NumberFormatException) {
200  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
201  }
202  if (key == "customValue1") {
203  myCustomValue1 = doubleValue;
204  } else {
205  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
206  }
207 }
208 
209 
210 /****************************************************************************/
211 
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:261
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key ...
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
double myCustomValue3
a value which is initialised based on a vType parameter
Notification
Definition of a vehicle state.
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
double myCustomValue1
a value which is initialised based on a commandline/configuration option
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
drive on the right side
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The car-following model and parameter.
Definition: MSVehicleType.h:73
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
drive on the left side
const std::string & getID() const
Returns the id.
Definition: Named.h:66
~MSDevice_Bluelight()
Destructor.
const SUMOVTypeParameter & getParameter() const
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
Representation of a vehicle.
Definition: SUMOVehicle.h:67
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:305
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
const std::string deviceName() const
return the name for this type of device
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:97
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.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
maintain the current alignment
int getIndex() const
Returns the lane&#39;s index.
Definition: MSLane.h:507
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:240
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:71
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:111
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
A storage for options typed value containers)
Definition: OptionsCont.h:99
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:312
const std::string & getID() const
Returns the name of the vehicle type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
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:71
bool closeTag()
Closes the most recently opened tag.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
The class responsible for building and deletion of vehicles.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
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.
void generateOutput() const
Called on writing tripinfo output.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
double myCustomValue2
a value which is initialised based on a vehicle parameter