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 MSTransportableDevice_FCDReplay.cpp
15 : /// @author Michael Behrisch
16 : /// @date 04.03.2024
17 : ///
18 : // A device which replays recorded floating car data
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <utils/options/OptionsCont.h>
23 : #include <libsumo/Person.h>
24 : #include <microsim/MSEdge.h>
25 : #include <microsim/MSEventControl.h>
26 : #include <microsim/MSLane.h>
27 : #include <microsim/MSNet.h>
28 : #include <microsim/devices/MSDevice_Transportable.h>
29 : #include <microsim/transportables/MSPerson.h>
30 : #include <microsim/transportables/MSStageWalking.h>
31 : #include <microsim/transportables/MSTransportable.h>
32 : #include <microsim/transportables/MSTransportableControl.h>
33 : #include "MSTransportableDevice_FCDReplay.h"
34 :
35 :
36 : // ===========================================================================
37 : // static member initializations
38 : // ===========================================================================
39 : bool MSTransportableDevice_FCDReplay::myAmActive = false;
40 :
41 :
42 : // ===========================================================================
43 : // method definitions
44 : // ===========================================================================
45 : // ---------------------------------------------------------------------------
46 : // static initialisation methods
47 : // ---------------------------------------------------------------------------
48 : void
49 0 : MSTransportableDevice_FCDReplay::insertOptions(OptionsCont& oc) {
50 0 : insertDefaultAssignmentOptions("fcd-replay", "FCD Replay Device", oc, true);
51 0 : }
52 :
53 :
54 : void
55 501966 : MSTransportableDevice_FCDReplay::buildDevices(MSTransportable& t, std::vector<MSTransportableDevice*>& into) {
56 501966 : OptionsCont& oc = OptionsCont::getOptions();
57 1003932 : if (equippedByDefaultAssignmentOptions(oc, "fcd-replay", t, oc.isSet("device.fcd-replay.file"), true)) {
58 40 : MSTransportableDevice_FCDReplay* device = new MSTransportableDevice_FCDReplay(t, "fcdReplay_" + t.getID());
59 40 : into.push_back(device);
60 40 : if (!myAmActive) {
61 4 : MSNet::getInstance()->getBeginOfTimestepEvents()->addEvent(new MovePedestrians(), SIMSTEP + DELTA_T);
62 4 : myAmActive = true;
63 : }
64 : }
65 501966 : }
66 :
67 :
68 : // ---------------------------------------------------------------------------
69 : // MSTransportableDevice_FCDReplay-methods
70 : // ---------------------------------------------------------------------------
71 40 : MSTransportableDevice_FCDReplay::MSTransportableDevice_FCDReplay(MSTransportable& holder, const std::string& id) :
72 40 : MSTransportableDevice(holder, id) {
73 40 : }
74 :
75 :
76 80 : MSTransportableDevice_FCDReplay::~MSTransportableDevice_FCDReplay() {
77 80 : }
78 :
79 :
80 : bool
81 15200 : MSTransportableDevice_FCDReplay::move(SUMOTime currentTime) {
82 15200 : if (myTrajectory == nullptr || myTrajectoryIndex == (int)myTrajectory->size()) {
83 : // removing person
84 : return true;
85 : }
86 15160 : MSPerson* person = dynamic_cast<MSPerson*>(&myHolder);
87 15160 : const auto& te = myTrajectory->at(myTrajectoryIndex);
88 15160 : if (person == nullptr || !person->hasDeparted() || te.time > currentTime) {
89 1800 : return false;
90 : }
91 13360 : if (person->getCurrentStageType() == MSStageType::DRIVING) {
92 4428 : if (person->getVehicle() == nullptr) { // entering the vehicle
93 1528 : const MSEdge* const edge = person->getEdge();
94 1820 : for (const SUMOVehicle* v : edge->getVehicles()) {
95 292 : if (v->getSpeed() == 0. && fabs(v->getPositionOnLane() - te.lanePos) < POSITION_EPS) {
96 40 : v->getLane()->getVehiclesSecure(); // lock the lane
97 40 : SUMOTime dummy = -1; // boarding- and loading-time are not considered
98 40 : MSNet::getInstance()->getPersonControl().loadAnyWaiting(edge, const_cast<SUMOVehicle*>(v), dummy, dummy, person);
99 40 : v->getLane()->releaseVehicles(); // unlock the lane
100 : }
101 1528 : }
102 : } else {
103 : SUMOVehicle* v = person->getVehicle();
104 2900 : if (te.speed == 0. && fabs(v->getPositionOnLane() - te.lanePos) >= POSITION_EPS) { // leaving the vehicle
105 40 : v->getLane()->getVehiclesSecure(); // lock the lane
106 40 : MSDevice_Transportable* transDev = static_cast<MSDevice_Transportable*>(v->getDevice(typeid(MSDevice_Transportable)));
107 40 : transDev->removeTransportable(person);
108 40 : person->proceed(MSNet::getInstance(), currentTime);
109 40 : v->getLane()->releaseVehicles(); // unlock the lane
110 : }
111 : }
112 : }
113 13360 : if (person->getCurrentStageType() == MSStageType::WALKING) {
114 7852 : libsumo::Person::moveToXY(person->getID(), te.edgeOrLane, te.pos.x(), te.pos.y(), te.angle, 7);
115 : MSStageWalking* walk = static_cast<MSStageWalking*>(person->getCurrentStage());
116 7852 : if (myTrajectoryIndex > 0 && myTrajectory->at(myTrajectoryIndex - 1).edgeOrLane != te.edgeOrLane) {
117 160 : walk->moveToNextEdge(person, currentTime, 1, nullptr, true);
118 : }
119 : }
120 : // person->setPreviousSpeed(std::get<3>(p), std::numeric_limits<double>::min());
121 13360 : myTrajectoryIndex++;
122 13360 : return false;
123 : }
124 :
125 :
126 4 : MSTransportableDevice_FCDReplay::MovePedestrians::MovePedestrians() {
127 : // higher than default command priority of 0
128 4 : priority = 1;
129 4 : }
130 :
131 :
132 : SUMOTime
133 1892 : MSTransportableDevice_FCDReplay::MovePedestrians::execute(SUMOTime currentTime) {
134 1892 : MSTransportableControl& c = MSNet::getInstance()->getPersonControl();
135 : std::vector<MSTransportableDevice_FCDReplay*> devices;
136 17092 : for (MSTransportableControl::constVehIt i = c.loadedBegin(); i != c.loadedEnd(); ++i) {
137 15200 : MSTransportableDevice_FCDReplay* device = static_cast<MSTransportableDevice_FCDReplay*>(i->second->getDevice(typeid(MSTransportableDevice_FCDReplay)));
138 15200 : if (device != nullptr) {
139 15200 : devices.push_back(device);
140 : }
141 : }
142 17092 : for (MSTransportableDevice_FCDReplay* d : devices) {
143 15200 : if (d->move(currentTime)) {
144 40 : d->getHolder().removeStage(0, false);
145 : }
146 : }
147 1892 : return DELTA_T;
148 1892 : }
149 :
150 :
151 : /****************************************************************************/
|