LCOV - code coverage report
Current view: top level - src/microsim/devices - MSDevice_BTreceiver.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 78.6 % 42 33
Test Date: 2024-11-22 15:46:21 Functions: 71.4 % 14 10

            Line data    Source code
       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              : /****************************************************************************/
      14              : /// @file    MSDevice_BTreceiver.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @date    14.08.2013
      18              : ///
      19              : // A BT receiver
      20              : /****************************************************************************/
      21              : #pragma once
      22              : #include <config.h>
      23              : 
      24              : #include <random>
      25              : #include "MSVehicleDevice.h"
      26              : #include "MSDevice_BTsender.h"
      27              : #include <utils/common/SUMOTime.h>
      28              : #include <utils/common/Command.h>
      29              : #include <utils/common/RandHelper.h>
      30              : 
      31              : 
      32              : // ===========================================================================
      33              : // class declarations
      34              : // ===========================================================================
      35              : class SUMOVehicle;
      36              : 
      37              : 
      38              : // ===========================================================================
      39              : // class definitions
      40              : // ===========================================================================
      41              : /**
      42              :  * @class MSDevice_BTreceiver
      43              :  * @brief A BT receiver
      44              :  *
      45              :  * @see MSDevice
      46              :  */
      47              : class MSDevice_BTreceiver {
      48              : public:
      49              : 
      50              :     /** @brief Returns the configured range
      51              :      * @return the device range
      52              :      */
      53              :     static double getRange() {
      54            0 :         return myRange;
      55              :     }
      56              : 
      57              : 
      58              :     static SumoRNG* getRNG() {
      59              :         return &sRecognitionRNG;
      60              :     }
      61              : 
      62              : public:
      63              : 
      64              :     /// @brief Destructor.
      65              :     ~MSDevice_BTreceiver();
      66              : 
      67              : 
      68              : 
      69              :     /// @name Methods inherited from MSMoveReminder.
      70              :     /// @{
      71              : 
      72              :     /** @brief Adds the vehicle to running vehicles if it (re-) enters the network
      73              :      *
      74              :      * @param[in] veh The entering vehicle.
      75              :      * @param[in] reason how the vehicle enters the lane
      76              :      * @return Always true
      77              :      * @see MSMoveReminder::notifyEnter
      78              :      * @see MSMoveReminder::Notification
      79              :      */
      80              :     bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
      81              : 
      82              : 
      83              :     /** @brief Checks whether the reminder still has to be notified about the vehicle moves
      84              :      *
      85              :      * Indicator if the reminders is still active for the passed
      86              :      * vehicle/parameters. If false, the vehicle will erase this reminder
      87              :      * from its reminder-container.
      88              :      *
      89              :      * @param[in] veh Vehicle that asks this reminder.
      90              :      * @param[in] oldPos Position before move.
      91              :      * @param[in] newPos Position after move with newSpeed.
      92              :      * @param[in] newSpeed Moving speed.
      93              :      *
      94              :      * @return True if vehicle hasn't passed the reminder completely.
      95              :      */
      96              :     bool notifyMove(SUMOTrafficObject& veh, double oldPos, double newPos, double newSpeed);
      97              : 
      98              : 
      99              :     /** @brief Moves (the known) vehicle from running to arrived vehicles' list
     100              :      *
     101              :      * @param[in] veh The leaving vehicle.
     102              :      * @param[in] lastPos Position on the lane when leaving.
     103              :      * @param[in] isArrival whether the vehicle arrived at its destination
     104              :      * @param[in] isLaneChange whether the vehicle changed from the lane
     105              :      * @see MSMoveReminder
     106              :      * @see MSMoveReminder::notifyLeave
     107              :      */
     108              :     bool notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
     109              :     ///@}
     110              : 
     111              :     /** @class MeetingPoint
     112              :      * @brief Holds the information about exact positions/speeds/time of the begin/end of a meeting
     113              :      */
     114              :     class MeetingPoint {
     115              :     public:
     116              :         /** @brief Constructor
     117              :          * @param[in] _t The time of the meeting
     118              :          * @param[in] _observerState The position, speed, lane etc. the observer had at the time
     119              :          * @param[in] _seenState The position, speed, lane etc. the seen vehicle had at the time
     120              :          */
     121        57388 :         MeetingPoint(double _t, const MSDevice_BTsender::VehicleState& _observerState,
     122              :                      const MSDevice_BTsender::VehicleState& _seenState)
     123        57388 :             : t(_t), observerState(_observerState), seenState(_seenState) {}
     124              : 
     125              :         /// @brief Destructor
     126          724 :         ~MeetingPoint() {}
     127              : 
     128              :     public:
     129              :         /// @brief The time of the meeting
     130              :         const double t;
     131              :         /// @brief The state the observer had at the time
     132              :         const MSDevice_BTsender::VehicleState observerState;
     133              :         /// @brief The state the seen vehicle had at the time
     134              :         const MSDevice_BTsender::VehicleState seenState;
     135              : 
     136              :     private:
     137              :         /// @brief Invalidated assignment operator.
     138              :         MeetingPoint& operator=(const MeetingPoint&) = delete;
     139              : 
     140              :     };
     141              : 
     142              : 
     143              : 
     144              :     /** @class SeenDevice
     145              :      * @brief Class representing a single seen device
     146              :      */
     147              :     class SeenDevice {
     148              :     public:
     149              :         /** @brief Constructor
     150              :          * @param[in] meetingBegin_ Description of the meeting's begin
     151              :          */
     152              :         SeenDevice(const MeetingPoint& meetingBegin_)
     153          724 :             : meetingBegin(meetingBegin_), meetingEnd(0), lastView(meetingBegin_.t), nextView(-1.) {}
     154              : 
     155              :         /// @brief Destructor
     156          724 :         ~SeenDevice() {
     157         1392 :             delete meetingEnd;
     158        56720 :             for (std::vector<MeetingPoint*>::iterator i = recognitionPoints.begin(); i != recognitionPoints.end(); ++i) {
     159       111992 :                 delete *i;
     160              :             }
     161              :             recognitionPoints.clear();
     162          724 :         }
     163              : 
     164              : 
     165              :     public:
     166              :         /// @brief Description of the meeting's begin
     167              :         const MeetingPoint meetingBegin;
     168              :         /// @brief Description of the meeting's end
     169              :         MeetingPoint* meetingEnd;
     170              :         /// @brief Last recognition point
     171              :         double lastView;
     172              :         /// @brief Next possible recognition point
     173              :         double nextView;
     174              :         /// @brief List of recognition points
     175              :         std::vector<MeetingPoint*> recognitionPoints;
     176              :         /// @brief string of travelled receiver edges
     177              :         std::string receiverRoute;
     178              :         /// @brief string of travelled sender edges
     179              :         std::string senderRoute;
     180              : 
     181              :     private:
     182              :         /// @brief Invalidated assignment operator.
     183              :         SeenDevice& operator=(const SeenDevice&);
     184              : 
     185              :     };
     186              : 
     187              : 
     188              : 
     189              :     /** @brief Clears the given containers deleting the stored items
     190              :      * @param[in] c The currently seen container to clear
     191              :      * @param[in] s The seen container to clear
     192              :      */
     193              :     static void cleanUp(std::map<std::string, SeenDevice*>& c, std::map<std::string, std::vector<SeenDevice*> >& s);
     194              : 
     195              :     static SumoRNG* getRecognitionRNG() {
     196              :         return &sRecognitionRNG;
     197              :     }
     198              : 
     199              :     static void hasSendingPersons() {
     200          132 :         myHasPersons = true;
     201          132 :     }
     202              : 
     203              : protected:
     204              :     /** @brief Constructor
     205              :      *
     206              :      * @param[in] holder The vehicle that holds this device
     207              :      * @param[in] id The ID of the device
     208              :      */
     209              :     MSDevice_BTreceiver() {}
     210              : 
     211              : 
     212              :     /// @brief Whether the bt-system was already initialised
     213              :     static bool myWasInitialised;
     214              : 
     215              :     /// @brief Whether the bt-system includes persons
     216              :     static bool myHasPersons;
     217              : 
     218              :     /// @brief The range of the device
     219              :     static double myRange;
     220              : 
     221              :     /// @brief The offtime of the device
     222              :     static double myOffTime;
     223              : 
     224              : 
     225              :     /** @class VehicleInformation
     226              :      * @brief Stores the information of a vehicle
     227              :      */
     228              :     class VehicleInformation : public MSDevice_BTsender::VehicleInformation {
     229              :     public:
     230              :         /** @brief Constructor
     231              :          * @param[in] id The id of the vehicle
     232              :          * @param[in] range Recognition range of the vehicle
     233              :          */
     234          588 :         VehicleInformation(const std::string& id, const double _range) : MSDevice_BTsender::VehicleInformation(id), range(_range) {}
     235              : 
     236              :         /// @brief Destructor
     237         1176 :         ~VehicleInformation() {
     238              :             std::map<std::string, SeenDevice*>::iterator i;
     239          644 :             for (i = currentlySeen.begin(); i != currentlySeen.end(); i++) {
     240           56 :                 delete i->second;
     241              :             }
     242              :             std::map<std::string, std::vector<SeenDevice*> >::iterator j;
     243              :             std::vector<SeenDevice*>::iterator k;
     244          896 :             for (j = seen.begin(); j != seen.end(); j++) {
     245          976 :                 for (k = j->second.begin(); k != j->second.end(); k++) {
     246          668 :                     delete *k;
     247              :                 }
     248              :             }
     249         1176 :         }
     250              : 
     251              :         /// @brief Recognition range of the vehicle
     252              :         const double range;
     253              : 
     254              :         /// @brief The map of devices seen by the vehicle at removal time
     255              :         std::map<std::string, SeenDevice*> currentlySeen;
     256              : 
     257              :         /// @brief The past episodes of removed vehicle
     258              :         std::map<std::string, std::vector<SeenDevice*> > seen;
     259              : 
     260              :     private:
     261              :         /// @brief Invalidated copy constructor.
     262              :         VehicleInformation(const VehicleInformation&);
     263              : 
     264              :         /// @brief Invalidated assignment operator.
     265              :         VehicleInformation& operator=(const VehicleInformation&);
     266              : 
     267              :     };
     268              : 
     269              : 
     270              : 
     271              :     /** @class BTreceiverUpdate
     272              :      * @brief A global update performer
     273              :      */
     274              :     class BTreceiverUpdate : public Command {
     275              :     public:
     276              :         /// @brief Constructor
     277              :         BTreceiverUpdate();
     278              : 
     279              :         /// @brief Destructor
     280              :         ~BTreceiverUpdate();
     281              : 
     282              :         /** @brief Performs the update
     283              :          * @param[in] currentTime The current simulation time
     284              :          * @return Always DELTA_T - the time to being called back
     285              :          */
     286              :         SUMOTime execute(SUMOTime currentTime);
     287              : 
     288              : 
     289              :         /** @brief Rechecks the visibility for a given receiver/sender pair
     290              :          * @param[in] receiver Definition of the receiver vehicle
     291              :          * @param[in] sender Definition of the sender vehicle
     292              :          */
     293              :         void updateVisibility(VehicleInformation& receiver, MSDevice_BTsender::VehicleInformation& sender);
     294              : 
     295              : 
     296              :         /** @brief Informs the receiver about a sender entering it's radius
     297              :          * @param[in] atOffset The time offset to the current time step
     298              :          * @param[in] receiverState The position, speed, lane etc. the observer had at the time
     299              :          * @param[in] senderID The ID of the entering sender
     300              :          * @param[in] senderState The position, speed, lane etc. the seen vehicle had at the time
     301              :          * @param[in] currentlySeen The container storing episodes
     302              :          */
     303              :         void enterRange(double atOffset, const MSDevice_BTsender::VehicleState& receiverState,
     304              :                         const std::string& senderID, const MSDevice_BTsender::VehicleState& senderState,
     305              :                         std::map<std::string, SeenDevice*>& currentlySeen);
     306              : 
     307              : 
     308              :         /** @brief Removes the sender from the currently seen devices to past episodes
     309              :          * @param[in] receiverInfo The static information of the observer (id, route, etc.)
     310              :          * @param[in] receiverState The position, speed, lane etc. the observer had at the time
     311              :          * @param[in] senderInfo The static information of the seen vehicle (id, route, etc.)
     312              :          * @param[in] senderState The position, speed, lane etc. the seen vehicle had at the time
     313              :          * @param[in] tOffset The time offset to the current time step
     314              :          */
     315              :         void leaveRange(VehicleInformation& receiverInfo, const MSDevice_BTsender::VehicleState& receiverState,
     316              :                         MSDevice_BTsender::VehicleInformation& senderInfo, const MSDevice_BTsender::VehicleState& senderState,
     317              :                         double tOffset);
     318              : 
     319              : 
     320              : 
     321              : 
     322              :         /** @brief Adds a point of recognition
     323              :          * @param[in] tEnd The time of the recognition
     324              :          * @param[in] receiverState The position, speed, lane etc. the observer had at the time
     325              :          * @param[in] senderState The position, speed, lane etc. the seen vehicle had at the time
     326              :          * @param[in] senderDevice The device of the entering sender
     327              :          */
     328              :         void addRecognitionPoint(const double tEnd, const MSDevice_BTsender::VehicleState& receiverState,
     329              :                                  const MSDevice_BTsender::VehicleState& senderState,
     330              :                                  SeenDevice* senderDevice) const;
     331              : 
     332              : 
     333              :         /** @brief Writes the output
     334              :          * @param[in] id The id of the receiver
     335              :          * @param[in] seen The information about seen senders
     336              :          * @param[in] allRecognitions Whether all recognitions shall be written
     337              :          */
     338              :         void writeOutput(const std::string& id, const std::map<std::string, std::vector<SeenDevice*> >& seen,
     339              :                          bool allRecognitions);
     340              : 
     341              : 
     342              : 
     343              : 
     344              :     };
     345              : 
     346              : 
     347              :     static double inquiryDelaySlots(const int backoffLimit);
     348              : 
     349              :     /// @brief A random number generator used to determine whether the opposite was recognized
     350              :     static SumoRNG sRecognitionRNG;
     351              : 
     352              :     /// @brief The list of arrived receivers
     353              :     static std::map<std::string, VehicleInformation*> sVehicles;
     354              : 
     355              : 
     356              : 
     357              : private:
     358              :     /// @brief Invalidated copy constructor.
     359              :     MSDevice_BTreceiver(const MSDevice_BTreceiver&);
     360              : 
     361              :     /// @brief Invalidated assignment operator.
     362              :     MSDevice_BTreceiver& operator=(const MSDevice_BTreceiver&);
     363              : 
     364              : };
     365              : 
     366              : 
     367              : class MSVehicleDevice_BTreceiver : public MSDevice_BTreceiver, public MSVehicleDevice {
     368              : public:
     369              : 
     370              :     /** @brief Inserts MSDevice_BTreceiver-options
     371              :      * @param[filled] oc The options container to add the options to
     372              :      */
     373              :     static void insertOptions(OptionsCont& oc);
     374              : 
     375              : 
     376              :     /** @brief Build devices for the given vehicle, if needed
     377              :      *
     378              :      * The options are read and evaluated whether a bt-sender-device shall be built
     379              :      *  for the given vehicle.
     380              :      *
     381              :      * The built device is stored in the given vector.
     382              :      *
     383              :      * @param[in] v The vehicle for which a device may be built
     384              :      * @param[filled] into The vector to store the built device in
     385              :      */
     386              :     static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
     387              : 
     388              :     /// @brief return the name for this type of device
     389            0 :     const std::string deviceName() const {
     390            0 :         return "btreceiver";
     391              :     }
     392              : 
     393         5460 :     bool notifyEnter(SUMOTrafficObject& veh, Notification reason, const MSLane* enteredLane = 0) {
     394         5460 :         return MSDevice_BTreceiver::notifyEnter(veh, reason, enteredLane);
     395              :     }
     396              : 
     397        60156 :     bool notifyMove(SUMOTrafficObject& veh, double oldPos, double newPos, double newSpeed) {
     398        60156 :         return MSDevice_BTreceiver::notifyMove(veh, oldPos, newPos, newSpeed);
     399              :     }
     400              : 
     401         5412 :     bool notifyLeave(SUMOTrafficObject& veh, double lastPos, Notification reason, const MSLane* enteredLane = 0) {
     402         5412 :         return MSDevice_BTreceiver::notifyLeave(veh, lastPos, reason, enteredLane);
     403              :     }
     404              : 
     405              : 
     406              :     /** @brief Constructor
     407              :      *
     408              :      * @param[in] holder The vehicle that holds this device
     409              :      * @param[in] id The ID of the device
     410              :      */
     411          456 :     MSVehicleDevice_BTreceiver(SUMOVehicle& holder, const std::string& id) :
     412          456 :         MSVehicleDevice(holder, id) {
     413          456 :     }
     414              : 
     415              : };
     416              : 
     417              : class MSTransportableDevice_BTreceiver : public MSDevice_BTreceiver, public MSTransportableDevice {
     418              : public:
     419              : 
     420              :     /** @brief Inserts MSDevice_BTreceiver-options
     421              :      * @param[filled] oc The options container to add the options to
     422              :      */
     423              :     static void insertOptions(OptionsCont& oc);
     424              : 
     425              : 
     426              :     /** @brief Build devices for the given vehicle, if needed
     427              :      *
     428              :      * The options are read and evaluated whether a bt-sender-device shall be built
     429              :      *  for the given vehicle.
     430              :      *
     431              :      * The built device is stored in the given vector.
     432              :      *
     433              :      * @param[in] v The vehicle for which a device may be built
     434              :      * @param[filled] into The vector to store the built device in
     435              :      */
     436              :     static void buildDevices(MSTransportable& t, std::vector<MSTransportableDevice*>& into);
     437              : 
     438              :     /// @brief return the name for this type of device
     439            0 :     const std::string deviceName() const {
     440            0 :         return "btreceiver";
     441              :     }
     442              : 
     443          132 :     bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0) {
     444          132 :         return MSDevice_BTreceiver::notifyEnter(veh, reason, enteredLane);
     445              :     }
     446              : 
     447            0 :     bool notifyMove(SUMOTrafficObject& veh, double oldPos, double newPos, double newSpeed) {
     448            0 :         return MSDevice_BTreceiver::notifyMove(veh, oldPos, newPos, newSpeed);
     449              :     }
     450              : 
     451            0 :     bool notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0) {
     452            0 :         return MSDevice_BTreceiver::notifyLeave(veh, lastPos, reason, enteredLane);
     453              :     }
     454              : 
     455              : 
     456              : private:
     457              :     /** @brief Constructor
     458              :      *
     459              :      * @param[in] holder The transportable that holds this device
     460              :      * @param[in] id The ID of the device
     461              :      */
     462          132 :     MSTransportableDevice_BTreceiver(MSTransportable& holder, const std::string& id) :
     463          132 :         MSTransportableDevice(holder, id) {
     464          132 :     }
     465              : 
     466              : };
        

Generated by: LCOV version 2.0-1