SUMO - Simulation of Urban MObility
MSDevice_Example.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 /****************************************************************************/
19 // A device which stands as an implementation example and which outputs movereminder calls
20 /****************************************************************************/
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
35 #include <microsim/MSNet.h>
36 #include <microsim/MSLane.h>
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSVehicle.h>
39 #include "MSDevice_Tripinfo.h"
40 #include "MSDevice_Example.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 // ---------------------------------------------------------------------------
47 // static initialisation methods
48 // ---------------------------------------------------------------------------
49 void
51  oc.addOptionSubTopic("Example Device");
52  insertDefaultAssignmentOptions("example", "Example Device", oc);
53 
54  oc.doRegister("device.example.parameter", new Option_Float(0.0));
55  oc.addDescription("device.example.parameter", "Example Device", "An exemplary parameter which can be used by all instances of the example device");
56 }
57 
58 
59 void
60 MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
62  if (equippedByDefaultAssignmentOptions(oc, "example", v)) {
63  // build the device
64  // get custom vehicle parameter
65  double customParameter2 = -1;
66  if (v.getParameter().knowsParameter("example")) {
67  try {
68  customParameter2 = TplConvert::_2double(v.getParameter().getParameter("example", "-1").c_str());
69  } catch (...) {
70  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("example", "-1") + "'for vehicle parameter 'example'");
71  }
72 
73  } else {
74  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
75  }
76  // get custom vType parameter
77  double customParameter3 = -1;
78  if (v.getVehicleType().getParameter().knowsParameter("example")) {
79  try {
80  customParameter3 = TplConvert::_2double(v.getVehicleType().getParameter().getParameter("example", "-1").c_str());
81  } catch (...) {
82  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("example", "-1") + "'for vType parameter 'example'");
83  }
84 
85  } else {
86  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
87  }
88  MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
89  oc.getFloat("device.example.parameter"),
90  customParameter2,
91  customParameter3);
92  into.push_back(device);
93  }
94 }
95 
96 
97 // ---------------------------------------------------------------------------
98 // MSDevice_Example-methods
99 // ---------------------------------------------------------------------------
100 MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
101  double customValue1, double customValue2, double customValue3) :
102  MSDevice(holder, id),
103  myCustomValue1(customValue1),
104  myCustomValue2(customValue2),
105  myCustomValue3(customValue3) {
106  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
107 }
108 
109 
111 }
112 
113 
114 bool
115 MSDevice_Example::notifyMove(SUMOVehicle& veh, double /* oldPos */,
116  double /* newPos */, double newSpeed) {
117  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
118  // check whether another device is present on the vehicle:
119  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
120  if (otherDevice != 0) {
121  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
122  }
123  return true; // keep the device
124 }
125 
126 
127 bool
129  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
130  return true; // keep the device
131 }
132 
133 
134 bool
135 MSDevice_Example::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
136  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
137  return true; // keep the device
138 }
139 
140 
141 void
143  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
144  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
145  os.openTag("example_device");
146  os.writeAttr("customValue1", toString(myCustomValue1));
147  os.writeAttr("customValue2", toString(myCustomValue2));
148  os.closeTag();
149  }
150 }
151 
152 std::string
153 MSDevice_Example::getParameter(const std::string& key) const {
154  if (key == "customValue1") {
155  return toString(myCustomValue1);
156  } else if (key == "customValue2") {
157  return toString(myCustomValue2);
158  } else if (key == "meaningOfLife") {
159  return "42";
160  }
161  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
162 }
163 
164 
165 void
166 MSDevice_Example::setParameter(const std::string& key, const std::string& value) {
167  double doubleValue;
168  try {
169  doubleValue = TplConvert::_2double(value.c_str());
170  } catch (NumberFormatException) {
171  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
172  }
173  if (key == "customValue1") {
174  myCustomValue1 = doubleValue;
175  } else {
176  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
177  }
178 }
179 
180 
181 /****************************************************************************/
182 
~MSDevice_Example()
Destructor.
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
double myCustomValue1
a value which is initialised based on a commandline/configuration option
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
Notification
Definition of a vehicle state.
MSDevice_Example(SUMOVehicle &holder, const std::string &id, double customValue1, double customValue2, double customValue3)
Constructor.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
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
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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 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.
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 buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
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
void generateOutput() const
Called on writing tripinfo output.
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)
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
A storage for options typed value containers)
Definition: OptionsCont.h:98
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.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
virtual MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
double myCustomValue2
a value which is initialised based on a vehicle parameter
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const std::string deviceName() const
return the name for this type of device
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
double myCustomValue3
a value which is initialised based on a vType parameter