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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // A BT sender
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
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 /****************************************************************************/
139 
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
SUMOTrafficObject::getPosition
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
MSDevice_BTsender::sVehicles
static std::map< std::string, VehicleInformation * > sVehicles
The list of arrived senders.
Definition: MSDevice_BTsender.h:220
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
OptionsCont.h
SUMOTrafficObject::getEdge
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:90
MSBaseVehicle::getRoutePosition
int getRoutePosition() const
return index of edge within route
Definition: MSBaseVehicle.cpp:391
MSEdge.h
MSDevice_BTsender::MSDevice_BTsender
MSDevice_BTsender(SUMOVehicle &holder, const std::string &id)
Constructor.
Definition: MSDevice_BTsender.cpp:74
MSVehicle.h
MSDevice_BTsender::notifyLeave
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, Notification reason, const MSLane *enteredLane=0)
Moves (the known) vehicle from running to arrived vehicles' list.
Definition: MSDevice_BTsender.cpp:116
MSDevice_BTsender::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, Notification reason, const MSLane *enteredLane=0)
Adds the vehicle to running vehicles if it (re-) enters the network.
Definition: MSDevice_BTsender.cpp:84
SUMOVehicle.h
MSDevice_BTsender::VehicleInformation
Stores the information of a vehicle.
Definition: MSDevice_BTsender.h:171
MSDevice::insertDefaultAssignmentOptions
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:126
MSDevice_BTsender::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
Definition: MSDevice_BTsender.cpp:49
OutputDevice.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSDevice_BTsender.h
MSMoveReminder::NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
Definition: MSMoveReminder.h:93
MSDevice_BTsender::cleanup
static void cleanup()
removes remaining vehicleInformation in sVehicles
Definition: MSDevice_BTsender.cpp:63
MSDevice_BTreceiver.h
MSDevice_BTsender
A BT sender.
Definition: MSDevice_BTsender.h:49
StringUtils.h
MSDevice::equippedByDefaultAssignmentOptions
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:203
MSMoveReminder::NOTIFICATION_ARRIVED
The vehicle arrived at its destination (is deleted)
Definition: MSMoveReminder.h:107
MSBaseVehicle
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:51
config.h
MSDevice_Tripinfo.h
MSLane.h
MSDevice_BTsender::~MSDevice_BTsender
~MSDevice_BTsender()
Destructor.
Definition: MSDevice_BTsender.cpp:79
SUMOTrafficObject::getPositionOnLane
virtual double getPositionOnLane() const =0
Get the vehicle's position along the lane.
MSDevice_BTsender::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_BTsender.cpp:55
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:91
MSMoveReminder::NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Definition: MSMoveReminder.h:95
MSDevice_BTsender::VehicleState
A single movement state of the vehicle.
Definition: MSDevice_BTsender.h:138
MSMoveReminder::NOTIFICATION_TELEPORT
The vehicle is being teleported.
Definition: MSMoveReminder.h:103
SUMOTrafficObject::getSpeed
virtual double getSpeed() const =0
Returns the vehicle's current speed.
MSVehicleDevice
Abstract in-vehicle device.
Definition: MSVehicleDevice.h:54
MSDevice_BTsender::notifyMove
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the reminder still has to be notified about the vehicle moves.
Definition: MSDevice_BTsender.cpp:103