Eclipse SUMO - Simulation of Urban MObility
MSCModel_NonInteracting.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2014-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 // The container following model for tranship (prototype)
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 //
23 #include <cmath>
24 #include <algorithm>
26 #include <utils/geom/GeomHelper.h>
28 #include <microsim/MSNet.h>
29 #include <microsim/MSEdge.h>
30 #include <microsim/MSLane.h>
31 #include <microsim/MSJunction.h>
34 
35 // ===========================================================================
36 // static members
37 // ===========================================================================
39 
40 
41 // named constants
42 const int CState::FORWARD(1);
43 const int CState::BACKWARD(-1);
44 const int CState::UNDEFINED_DIRECTION(0);
45 const double CState::LATERAL_OFFSET(0);
46 
47 // ===========================================================================
48 // MSCModel_NonInteracting method definitions
49 // ===========================================================================
50 
52  myNet(net) {
53  assert(myNet != 0);
54 }
55 
56 
58 }
59 
62  if (myModel == nullptr) {
63  MSNet* net = MSNet::getInstance();
65  }
66  return myModel;
67 }
68 
69 CState*
71  CState* state = new CState();
72  const SUMOTime firstEdgeDuration = state->computeTranshipTime(nullptr, *stage, now);
73  myNet->getBeginOfTimestepEvents()->addEvent(new MoveToNextEdge(container, *stage), now + firstEdgeDuration);
74  return state;
75 }
76 
77 
78 void
80  if (myModel != nullptr) {
81  delete myModel;
82  myModel = nullptr;
83  }
84 }
85 
86 
90  const MSEdge* old = myParent.getEdge();
91  const bool arrived = myParent.moveToNextEdge(myContainer, currentTime);
92  if (arrived) {
93  // tranship finished. clean up state
94  delete state;
95  return 0;
96  } else {
97  return state->computeTranshipTime(old, myParent, currentTime);
98  }
99 }
100 
101 
102 double
104  return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime);
105 }
106 
107 
108 Position
110  const double dist = myCurrentBeginPosition.distanceTo2D(myCurrentEndPosition); //distance between begin and end position of this tranship stage
111  double pos = MIN2(STEPS2TIME(now - myLastEntryTime) * stage.getMaxSpeed(), dist); //the containerd shall not go beyond its end position
112  return PositionVector::positionAtOffset2D(myCurrentBeginPosition, myCurrentEndPosition, pos, 0);
113 }
114 
115 
116 double
118  double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 1.5 * M_PI : 0.5 * M_PI);
119  if (angle > M_PI) {
120  angle -= 2 * M_PI;
121  }
122  return angle;
123 }
124 
125 
126 double
128  return stage.getMaxSpeed();
129 }
130 
131 
132 SUMOTime
134  myLastEntryTime = currentTime;
135 
136  myCurrentBeginPos = stage.getDepartPos();
137  myCurrentEndPos = stage.getArrivalPos();
138 
139  const MSLane* fromLane = stage.getFromEdge()->getLanes().front(); //the lane the container starts from during its tranship stage
140  myCurrentBeginPosition = stage.getLanePosition(fromLane, myCurrentBeginPos, LATERAL_OFFSET);
141  const MSLane* toLane = stage.getToEdge()->getLanes().front(); //the lane the container ends during its tranship stage
142  myCurrentEndPosition = stage.getLanePosition(toLane, myCurrentEndPos, LATERAL_OFFSET);
143 
144  myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPosition.distanceTo(myCurrentBeginPosition)) / stage.getMaxSpeed()));
145  return myCurrentDuration;
146 }
147 
148 
149 
150 /****************************************************************************/
CState::getSpeed
double getSpeed(const MSContainer::MSContainerStage_Tranship &stage) const
return the current speed of the container
Definition: MSCModel_NonInteracting.cpp:127
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
MSCModel_NonInteracting::cleanup
static void cleanup()
remove state at simulation end
Definition: MSCModel_NonInteracting.cpp:79
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
CState::UNDEFINED_DIRECTION
static const int UNDEFINED_DIRECTION
Definition: MSCModel_NonInteracting.h:100
MSCModel_NonInteracting::MoveToNextEdge
Definition: MSCModel_NonInteracting.h:70
MSTransportable::Stage::getLanePosition
Position getLanePosition(const MSLane *lane, double at, double offset) const
get position on lane at length at with orthogonal offset
Definition: MSTransportable.cpp:128
OptionsCont.h
MSContainer::MSContainerStage_Tranship::getMaxSpeed
double getMaxSpeed() const
accessors to be used by MSCModel_NonInteracting
Definition: MSContainer.h:187
MSCModel_NonInteracting.h
MSCModel_NonInteracting::MoveToNextEdge::execute
SUMOTime execute(SUMOTime currentTime)
Executes the command.
Definition: MSCModel_NonInteracting.cpp:88
MSNet::getBeginOfTimestepEvents
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:429
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:91
MSContainer::MSContainerStage_Tranship::getDepartPos
double getDepartPos() const
Definition: MSContainer.h:191
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
CState::FORWARD
static const int FORWARD
Definition: MSCModel_NonInteracting.h:95
MSCModel_NonInteracting::MoveToNextEdge::myContainer
MSTransportable * myContainer
Definition: MSCModel_NonInteracting.h:78
MSContainer::MSContainerStage_Tranship::getArrivalPos
double getArrivalPos() const
Definition: MSContainer.h:195
MSEdge.h
MSTransportable
Definition: MSTransportable.h:58
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:79
CState
Definition: MSCModel_NonInteracting.h:91
MSCModel_NonInteracting::~MSCModel_NonInteracting
~MSCModel_NonInteracting()
Definition: MSCModel_NonInteracting.cpp:57
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
MSJunction.h
MSContainer::MSContainerStage_Tranship::getFromEdge
const MSEdge * getFromEdge() const
Returns first edge of the containers route.
Definition: MSContainer.cpp:180
MSCModel_NonInteracting::add
CState * add(MSTransportable *container, MSContainer::MSContainerStage_Tranship *stage, SUMOTime now)
register the given container as a transhiped container
Definition: MSCModel_NonInteracting.cpp:70
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:56
MSCModel_NonInteracting
The container following model for tranship.
Definition: MSCModel_NonInteracting.h:50
MSContainer::MSContainerStage_Tranship::moveToNextEdge
bool moveToNextEdge(MSTransportable *container, SUMOTime currentTime, MSEdge *nextInternal=0)
move forward and return whether the container arrived
Definition: MSContainer.cpp:269
MSCModel_NonInteracting::myNet
MSNet * myNet
the net to which to issue moveToNextEdge commands
Definition: MSCModel_NonInteracting.h:87
CState::getAngle
double getAngle(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the direction in which the container heading to
Definition: MSCModel_NonInteracting.cpp:117
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
MSCModel_NonInteracting::MSCModel_NonInteracting
MSCModel_NonInteracting(MSNet *net)
Constructor (it should not be necessary to construct more than one instance)
Definition: MSCModel_NonInteracting.cpp:51
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
MSCModel_NonInteracting::myModel
static MSCModel_NonInteracting * myModel
Definition: MSCModel_NonInteracting.h:67
Position::distanceTo2D
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition: Position.h:243
CState::BACKWARD
static const int BACKWARD
Definition: MSCModel_NonInteracting.h:99
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
M_PI
#define M_PI
Definition: odrSpiral.cpp:40
PositionVector::positionAtOffset2D
Position positionAtOffset2D(double pos, double lateralOffset=0) const
Returns the position at the given length.
Definition: PositionVector.cpp:273
MSTransportable::Stage::getEdgeAngle
double getEdgeAngle(const MSEdge *e, double at) const
get angle of the edge at a certain position
Definition: MSTransportable.cpp:133
CState::computeTranshipTime
SUMOTime computeTranshipTime(const MSEdge *prev, const MSContainer::MSContainerStage_Tranship &stage, SUMOTime currentTime)
compute tranship time on edge and update state members
Definition: MSCModel_NonInteracting.cpp:133
MSEdge::getLanes
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:167
CState::getEdgePos
double getEdgePos(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the offset from the start of the current edge measured in its natural direction
Definition: MSCModel_NonInteracting.cpp:103
CState::LATERAL_OFFSET
static const double LATERAL_OFFSET
the offset for computing container positions when being transhiped
Definition: MSCModel_NonInteracting.h:103
config.h
GeomHelper.h
RandHelper.h
MSContainer::MSContainerStage_Tranship::getContainerState
CState * getContainerState() const
Definition: MSContainer.h:203
MSContainer::MSContainerStage_Tranship
Definition: MSContainer.h:107
CState::getPosition
Position getPosition(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the network coordinate of the container
Definition: MSCModel_NonInteracting.cpp:109
MSEventControl.h
MSLane.h
MSContainer::MSContainerStage_Tranship::getToEdge
const MSEdge * getToEdge() const
Returns last edge of the containers route.
Definition: MSContainer.cpp:185
MSCModel_NonInteracting::MoveToNextEdge::myParent
MSContainer::MSContainerStage_Tranship & myParent
Definition: MSCModel_NonInteracting.h:77
MSCModel_NonInteracting::getModel
static MSCModel_NonInteracting * getModel()
Definition: MSCModel_NonInteracting.cpp:61
MSContainer::MSContainerStage_Tranship::getEdge
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSContainer.cpp:171