SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSDevice_Example.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A device which stands as an implementation example and which outputs movereminder calls
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2013-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
36 #include <microsim/MSNet.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSEdge.h>
39 #include <microsim/MSVehicle.h>
40 #include "MSDevice_Tripinfo.h"
41 #include "MSDevice_Example.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 // ---------------------------------------------------------------------------
52 // static initialisation methods
53 // ---------------------------------------------------------------------------
54 void
56  oc.addOptionSubTopic("Example Device");
57 
58  oc.doRegister("device.example.explicit", new Option_String());
59  oc.addDescription("device.example.explicit", "Example Device", "Assign a device to named vehicles");
60 
61  oc.doRegister("device.example.parameter", new Option_Float(0.0));
62  oc.addDescription("device.example.parameter", "Example Device", "An exemplary parameter which can be used by all instances of the example device");
63 }
64 
65 
66 void
67 MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
69  if (equippedByDefaultAssignmentOptions(oc, "example", v)) {
70  // build the device
71  // get custom vehicle parameter
72  SUMOReal customParameter2 = -1;
73  if (v.getParameter().knowsParameter("example")) {
74  try {
75  customParameter2 = TplConvert::_2SUMOReal(v.getParameter().getParameter("example", "-1").c_str());
76  } catch (...) {
77  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("example", "-1") + "'for vehicle parameter 'example'");
78  }
79 
80  } else {
81  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
82  }
83  // get custom vType parameter
84  SUMOReal customParameter3 = -1;
85  if (v.getVehicleType().getParameter().knowsParameter("example")) {
86  try {
87  customParameter3 = TplConvert::_2SUMOReal(v.getVehicleType().getParameter().getParameter("example", "-1").c_str());
88  } catch (...) {
89  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("example", "-1") + "'for vType parameter 'example'");
90  }
91 
92  } else {
93  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
94  }
95  MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
96  oc.getFloat("device.example.parameter"),
97  customParameter2,
98  customParameter3);
99  into.push_back(device);
100  }
101 }
102 
103 
104 // ---------------------------------------------------------------------------
105 // MSDevice_Example-methods
106 // ---------------------------------------------------------------------------
107 MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
108  SUMOReal customValue1, SUMOReal customValue2, SUMOReal customValue3) :
109  MSDevice(holder, id),
110  myCustomValue1(customValue1),
111  myCustomValue2(customValue2),
112  myCustomValue3(customValue3) {
113  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
114 }
115 
116 
118 }
119 
120 
121 bool
123  SUMOReal /* newPos */, SUMOReal newSpeed) {
124  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
125  // check whether another device is present on the vehicle:
126  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
127  if (otherDevice != 0) {
128  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
129  }
130  return true; // keep the device
131 }
132 
133 
134 bool
136  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
137  return true; // keep the device
138 }
139 
140 
141 bool
144  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
145  return true; // keep the device
146 }
147 
148 
149 void
151  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
152  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
153  os.openTag("example_device");
154  os.writeAttr("customValue1", toString(myCustomValue1));
155  os.writeAttr("customValue2", toString(myCustomValue2));
156  os.closeTag();
157  }
158 }
159 
160 
161 
162 /****************************************************************************/
163