Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDevice_Example.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2013-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// A device which stands as an implementation example and which outputs movereminder calls
21/****************************************************************************/
22#include <config.h>
23
28#include <microsim/MSNet.h>
29#include <microsim/MSLane.h>
30#include <microsim/MSEdge.h>
31#include <microsim/MSVehicle.h>
32#include "MSDevice_Tripinfo.h"
33#include "MSDevice_Example.h"
34
35
36// ===========================================================================
37// method definitions
38// ===========================================================================
39// ---------------------------------------------------------------------------
40// static initialisation methods
41// ---------------------------------------------------------------------------
42void
44 oc.addOptionSubTopic("Example Device");
45 insertDefaultAssignmentOptions("example", "Example Device", oc);
46
47 oc.doRegister("device.example.parameter", new Option_Float(0.0));
48 oc.addDescription("device.example.parameter", "Example Device", TL("An exemplary parameter which can be used by all instances of the example device"));
49}
50
51
52void
53MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
55 if (equippedByDefaultAssignmentOptions(oc, "example", v, false)) {
56 // build the device
57 // get custom vehicle parameter
58 double customParameter2 = -1;
59 if (v.getParameter().hasParameter("example")) {
60 try {
61 customParameter2 = StringUtils::toDouble(v.getParameter().getParameter("example", "-1"));
62 } catch (...) {
63 WRITE_WARNINGF(TL("Invalid value '%' for vehicle parameter 'example'"), v.getParameter().getParameter("example", "-1"));
64 }
65
66 } else {
67 std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
68 }
69 // get custom vType parameter
70 double customParameter3 = -1;
71 if (v.getVehicleType().getParameter().hasParameter("example")) {
72 try {
73 customParameter3 = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("example", "-1"));
74 } catch (...) {
75 WRITE_WARNINGF(TL("Invalid value '%' for vType parameter 'example'"), v.getVehicleType().getParameter().getParameter("example", "-1"));
76 }
77
78 } else {
79 std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
80 }
81 MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
82 oc.getFloat("device.example.parameter"),
83 customParameter2,
84 customParameter3);
85 into.push_back(device);
86 }
87}
88
89void
91 // cleaning up global state (if any)
92}
93
94// ---------------------------------------------------------------------------
95// MSDevice_Example-methods
96// ---------------------------------------------------------------------------
97MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
98 double customValue1, double customValue2, double customValue3) :
99 MSVehicleDevice(holder, id),
100 myCustomValue1(customValue1),
101 myCustomValue2(customValue2),
102 myCustomValue3(customValue3) {
103 std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
104}
105
106
109
110
111bool
113 double /* newPos */, double newSpeed) {
114 std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
115 if (tObject.isVehicle()) {
116 SUMOVehicle& veh = static_cast<SUMOVehicle&>(tObject);
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 != nullptr) {
120 std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
121 }
122 }
123 return true; // keep the device
124}
125
126
127bool
129 std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
130 return true; // keep the device
131}
132
133
134bool
135MSDevice_Example::notifyLeave(SUMOTrafficObject& 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
141void
143 if (tripinfoOut != nullptr) {
144 tripinfoOut->openTag("example_device");
145 tripinfoOut->writeAttr("customValue1", toString(myCustomValue1));
146 tripinfoOut->writeAttr("customValue2", toString(myCustomValue2));
147 tripinfoOut->closeTag();
148 }
149}
150
151std::string
152MSDevice_Example::getParameter(const std::string& key) const {
153 if (key == "customValue1") {
154 return toString(myCustomValue1);
155 } else if (key == "customValue2") {
156 return toString(myCustomValue2);
157 } else if (key == "meaningOfLife") {
158 return "42";
159 }
160 throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
161}
162
163
164void
165MSDevice_Example::setParameter(const std::string& key, const std::string& value) {
166 double doubleValue;
167 try {
168 doubleValue = StringUtils::toDouble(value);
169 } catch (NumberFormatException&) {
170 throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
171 }
172 if (key == "customValue1") {
173 myCustomValue1 = doubleValue;
174 } else {
175 throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
176 }
177}
178
179
180/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define TL(string)
Definition MsgHandler.h:315
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
A device which collects info on the vehicle trip (mainly on departure and arrival)
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key
~MSDevice_Example()
Destructor.
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
const std::string deviceName() const
return the name for this type of device
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
double myCustomValue1
a value which is initialised based on a commandline/configuration option
static void cleanup()
resets counters
double myCustomValue3
a value which is initialised based on a vType parameter
MSDevice_Example(SUMOVehicle &holder, const std::string &id, double customValue1, double customValue2, double customValue3)
Constructor.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
void generateOutput(OutputDevice *tripinfoOut) const
Called on writing tripinfo output.
double myCustomValue2
a value which is initialised based on a vehicle parameter
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
A device which collects info on the vehicle trip (mainly on departure and arrival)
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:155
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:195
Representation of a lane in the micro simulation.
Definition MSLane.h:84
Notification
Definition of a vehicle state.
Abstract in-vehicle device.
const SUMOVTypeParameter & getParameter() const
const std::string & getID() const
Returns the id.
Definition Named.h:74
A storage for options typed value containers)
Definition OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
bool hasParameter(const std::string &key) const
Returns whether the parameter is set.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
Representation of a vehicle, person, or container.
virtual bool isVehicle() const
Whether it is a vehicle.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or nullptr if not.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
Representation of a vehicle.
Definition SUMOVehicle.h:62
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter