Eclipse SUMO - Simulation of Urban MObility
MSDevice_BTsender.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-2022 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 /****************************************************************************/
19 // A BT sender
20 /****************************************************************************/
21 #include <config.h>
22 
27 #include <microsim/MSNet.h>
28 #include <microsim/MSLane.h>
29 #include <microsim/MSEdge.h>
30 #include <microsim/MSVehicle.h>
31 #include "MSDevice_Tripinfo.h"
32 #include "MSDevice_BTsender.h"
33 #include "MSDevice_BTreceiver.h"
34 
35 
36 // ===========================================================================
37 // static members
38 // ===========================================================================
39 std::map<std::string, MSDevice_BTsender::VehicleInformation*> MSDevice_BTsender::sVehicles;
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 // ---------------------------------------------------------------------------
46 // static initialisation methods
47 // ---------------------------------------------------------------------------
48 void
50  insertDefaultAssignmentOptions("btsender", "Communication", oc);
51 }
52 
53 
54 void
55 MSDevice_BTsender::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
56  if (equippedByDefaultAssignmentOptions(OptionsCont::getOptions(), "btsender", v, false)) {
57  MSDevice_BTsender* device = new MSDevice_BTsender(v, "btsender_" + v.getID());
58  into.push_back(device);
59  }
60 }
61 
62 void
64  std::map<std::string, MSDevice_BTsender::VehicleInformation*>::iterator i;
65  for (i = sVehicles.begin(); i != sVehicles.end(); i++) {
66  delete i->second;
67  }
68 }
69 
70 
71 // ---------------------------------------------------------------------------
72 // MSDevice_BTsender-methods
73 // ---------------------------------------------------------------------------
74 MSDevice_BTsender::MSDevice_BTsender(SUMOVehicle& holder, const std::string& id)
75  : MSVehicleDevice(holder, id) {
76 }
77 
78 
80 }
81 
82 
83 bool
84 MSDevice_BTsender::notifyEnter(SUMOTrafficObject& veh, Notification reason, const MSLane* /* enteredLane */) {
85  if (reason == MSMoveReminder::NOTIFICATION_DEPARTED && sVehicles.find(veh.getID()) == sVehicles.end()) {
86  sVehicles[veh.getID()] = new VehicleInformation(veh.getID());
87  sVehicles[veh.getID()]->route.push_back(veh.getEdge());
88  }
89  if (reason == MSMoveReminder::NOTIFICATION_TELEPORT && sVehicles.find(veh.getID()) != sVehicles.end()) {
90  sVehicles[veh.getID()]->amOnNet = true;
91  }
93  sVehicles[veh.getID()]->route.push_back(veh.getEdge());
94  }
95  const std::string location = MSGlobals::gUseMesoSim ? veh.getEdge()->getID() : static_cast<MSVehicle&>(veh).getLane()->getID();
96  const MSBaseVehicle& v = static_cast<MSBaseVehicle&>(veh);
97  sVehicles[veh.getID()]->updates.push_back(VehicleState(veh.getSpeed(), veh.getPosition(), location, veh.getPositionOnLane(), v.getRoutePosition()));
98  return true;
99 }
100 
101 
102 bool
103 MSDevice_BTsender::notifyMove(SUMOTrafficObject& veh, double /* oldPos */, double newPos, double newSpeed) {
104  if (sVehicles.find(veh.getID()) == sVehicles.end()) {
105  WRITE_WARNING("btsender: Can not update position of vehicle '" + veh.getID() + "' which is not on the road.");
106  return true;
107  }
108  const std::string location = MSGlobals::gUseMesoSim ? veh.getEdge()->getID() : static_cast<MSVehicle&>(veh).getLane()->getID();
109  const MSBaseVehicle& v = static_cast<MSBaseVehicle&>(veh);
110  sVehicles[veh.getID()]->updates.push_back(VehicleState(newSpeed, veh.getPosition(), location, newPos, v.getRoutePosition()));
111  return true;
112 }
113 
114 
115 bool
116 MSDevice_BTsender::notifyLeave(SUMOTrafficObject& veh, double /* lastPos */, Notification reason, const MSLane* /* enteredLane */) {
118  return true;
119  }
120  if (sVehicles.find(veh.getID()) == sVehicles.end()) {
121  WRITE_WARNING("btsender: Can not update position of vehicle '" + veh.getID() + "' which is not on the road.");
122  return true;
123  }
124  const std::string location = MSGlobals::gUseMesoSim ? veh.getEdge()->getID() : static_cast<MSVehicle&>(veh).getLane()->getID();
125  const MSBaseVehicle& v = static_cast<MSBaseVehicle&>(veh);
126  sVehicles[veh.getID()]->updates.push_back(VehicleState(veh.getSpeed(), veh.getPosition(), location, veh.getPositionOnLane(), v.getRoutePosition()));
128  sVehicles[veh.getID()]->amOnNet = false;
129  }
130  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
131  sVehicles[veh.getID()]->amOnNet = false;
132  sVehicles[veh.getID()]->haveArrived = true;
133  }
134  return true;
135 }
136 
137 
138 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:51
int getRoutePosition() const
return index of edge within route
Stores the information of a vehicle.
A single movement state of the vehicle.
MSDevice_BTsender(SUMOVehicle &holder, const std::string &id)
Constructor.
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, Notification reason, const MSLane *enteredLane=0)
Moves (the known) vehicle from running to arrived vehicles' list.
bool notifyEnter(SUMOTrafficObject &veh, Notification reason, const MSLane *enteredLane=0)
Adds the vehicle to running vehicles if it (re-) enters the network.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
~MSDevice_BTsender()
Destructor.
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the reminder still has to be notified about the vehicle moves.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void cleanup()
removes remaining vehicleInformation in sVehicles
static std::map< std::string, VehicleInformation * > sVehicles
The list of arrived senders.
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:137
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:205
static bool gUseMesoSim
Definition: MSGlobals.h:94
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
const MSLane * getLane() const
Returns the lane the reminder works on.
Notification
Definition of a vehicle state.
@ NOTIFICATION_ARRIVED
The vehicle arrived at its destination (is deleted)
@ NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
@ NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
@ NOTIFICATION_TELEPORT
The vehicle is being teleported.
Abstract in-vehicle device.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:75
const std::string & getID() const
Returns the id.
Definition: Named.h:74
A storage for options typed value containers)
Definition: OptionsCont.h:89
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Representation of a vehicle, person, or container.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
Representation of a vehicle.
Definition: SUMOVehicle.h:60