37#define DEBUG_COND (true)
51 oc.
addDescription(
"device.glosa.range",
"GLOSA Device",
TL(
"The communication range to the traffic light"));
54 oc.
addDescription(
"device.glosa.max-speedfactor",
"GLOSA Device",
TL(
"The maximum speed factor when approaching a green light"));
57 oc.
addDescription(
"device.glosa.min-speed",
"GLOSA Device",
TL(
"Minimum speed when coasting towards a red light"));
60 oc.
addDescription(
"device.glosa.add-switchtime",
"GLOSA Device",
TL(
"Additional time the vehicle shall need to reach the intersection after the signal turns green"));
63 oc.
addDescription(
"device.glosa.use-queue",
"GLOSA Device",
TL(
"Use queue in front of the tls for GLOSA calculation"));
66 oc.
addDescription(
"device.glosa.override-safety",
"GLOSA Device",
TL(
"Override safety features - ignore the current light state, always follow GLOSA's predicted state"));
69 oc.
addDescription(
"device.glosa.ignore-cfmodel",
"GLOSA Device",
TL(
"Vehicles follow a perfect speed calculation - ignore speed calculations from the CF model if not safety critical"));
85 into.push_back(device);
98 double addSwitchTime,
bool useQueue,
bool overrideSafety,
bool ignoreCFModel) :
101 myNextTLSLink(nullptr),
103 myMinSpeed(minSpeed),
105 myMaxSpeedFactor(maxSpeedFactor),
106 myAddSwitchTime(addSwitchTime),
107 myOverrideSafety(overrideSafety),
108 myIgnoreCFModel(ignoreCFModel),
109 mySpeedAdviceActive(false),
123 double newPos,
double ) {
131 bool currentPhaseGreen =
false;
132 bool currentPhaseStop =
false;
134 double nextSwitch = 0;
135 nextSwitch = timeToSwitch;
136 double QueueLength = 0;
137 double greenTime = 0;
138 double additionalJunctionTime = 0;
141 double factor = 0.21;
149 int switchOffset = 0;
154#if defined DEBUG_GLOSA || defined DEBUG_QUEUE
156 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" d=" <<
myDistance <<
" ttJ=" << timeToJunction <<
" ttS=" << timeToSwitch <<
"\n";
164 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" Queuelength=" << QueueLength <<
"\n";
167 if (currentPhaseGreen) {
170 additionalJunctionTime = (QueueLength * factor + addition) - greenTime;
171 if ((additionalJunctionTime > 0) && (additionalJunctionTime < nextSwitch)) {
173 timeToJunction += additionalJunctionTime;
176 additionalJunctionTime = 0;
180 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" Additonaljunctiontime=" << additionalJunctionTime <<
" Greentime=" << greenTime <<
" TimetoJunction(GreenQueue)=" << timeToJunction <<
"\n";
185 if (currentPhaseStop) {
186 nextSwitch += QueueLength * factor + addition;
189 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" Nextswitch(RedQueue)=" << nextSwitch <<
"\n";
196 for (
int countwhile = 1; countwhile < 11; countwhile++) {
197 if (currentPhaseGreen) {
200 nextSwitch -= QueueLength * factor + addition;
208 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" traffic light will be/is green" <<
"\n";
210 <<
" timeToJunction=" << timeToJunction
211 <<
" nextSwitch=" << nextSwitch
216 if (timeToJunction > nextSwitch) {
226 <<
" vMax2=" << vMax2
227 <<
" timetoJunction2=" << timetoJunction2
228 <<
" yellowSlack=" << yellowSlack <<
"\n";
232 if (timetoJunction2 <= (nextSwitch + yellowSlack)) {
250 }
else if (currentPhaseStop) {
254 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" traffic light will be/is red" <<
"\n";
256 <<
" timeToJunction=" << timeToJunction
257 <<
" nextSwitch=" << nextSwitch
263 nextSwitch += QueueLength * factor + addition;
266 std::cout <<
SIMTIME <<
" veh=" <<
myVeh.
getID() <<
" nextSwitch(redadditon): " << nextSwitch <<
"\n";
273 nextSwitch -= QueueLength * factor + addition;
274 timeToJunction -= additionalJunctionTime;
283 if (nextSwitch > 80.) {
284 nextSwitch += (double)switchOffset;
286 nextSwitch -= (double)switchOffset;
287 }
else if (nextSwitch > 60.) {
288 nextSwitch += (double)switchOffset;
290 nextSwitch -= (double)switchOffset;
291 }
else if (nextSwitch > 40.) {
292 nextSwitch += (double)switchOffset;
294 nextSwitch -= (double)switchOffset;
295 }
else if (nextSwitch > 20.) {
296 nextSwitch += (double)switchOffset;
298 nextSwitch -= (double)switchOffset;
321 if ((*linkIt)->isTLSControlled()) {
327 lane = (*linkIt)->getViaLaneOrLane();
340 double tlsRange = 1e10;
345 WRITE_WARNINGF(
TL(
"Invalid value '%' for parameter 'device.glosa.range' of traffic light '%'"),
362 assert(tlsLink !=
nullptr);
364 assert(tl !=
nullptr);
366 const int n = (int)phases.size();
370 for (
int i = 1; i < n; i++) {
371 const auto& phase = phases[(cur + i) % n];
372 const char ls = phase->getState()[tlsLink->
getTLIndex()];
374 || (tlsLink->
haveGreen() && ls !=
'g' && ls !=
'G')) {
378 result += phase->duration;
387 assert(tlsLink !=
nullptr);
389 assert(tl !=
nullptr);
391 const int n = (int)phases.size();
392 const int cur = countOld;
395 for (
int i = 0; i < n; i++) {
396 const auto& phase = phases[(cur + i) % n];
397 const char ls = phase->getState()[tlsLink->
getTLIndex()];
398 if (currentPhaseGreen && (ls ==
'g' || ls ==
'G')) {
399 countOld = (cur + i)%n;
402 if (currentPhaseStop && (ls !=
'g' && ls !=
'G')) {
403 countOld = (cur + i)%n;
406 result += phase->duration;
408 currentPhaseGreen = !currentPhaseGreen;
409 currentPhaseStop = !currentPhaseStop;
415 assert(tlsLink !=
nullptr);
417 assert(tl !=
nullptr);
419 const int n = (int)phases.size();
425 for (
int i = 1; i < n; i++) {
426 const auto& phase = phases[(cur - i) % n];
427 const char ls = phase->getState()[tlsLink->
getTLIndex()];
428 if ( ls !=
'g' && ls !=
'G') {
431 result += phase->duration;
445 const double remaining_time = remaining_dist / vMax;
446 return accel_time + remaining_time;
478 const double v = speed;
479 const double t = time;
482 return v * t + a * t * t / 2;
491 const double p_half = v / a;
492 const double t = -p_half + sqrt(p_half * p_half + 2 * d / a);
510 const double t = timeToSwitch;
513 const double w = vMax;
514 const double s = distance;
525 const double rootConst = a * a * t * t - 2.0 * a * w * t + 2 * a * s;
527 if (rootConst >= 0) {
528 vConst = sqrt(rootConst) - a * t + w;
536 const double sign0 = -1;
537 const double root_argument = a * d * ((2.0 * d * (s - (w * t))) - ((v - w) * (v - w)) + (a * ((d * (t * t)) + (2.0 * (s - (t * v))))));
538 if (root_argument < 0) {
540 WRITE_WARNINGF(
"GLOSA error 1 root_argument=% s=% t=% v=%", root_argument, s, t, v);
547 const double x = (((a * (v - (d * t))) + (d * w) - sign0 * sqrt(root_argument)) / (d + a));
548 double y = (v - x) / d;
552 if (s < (w * w - x * x) / 2.0 / a) {
555 std::vector<std::pair<SUMOTime, double> > speedTimeLine;
565 if (!(x >= u && x <= w && y < t)) {
567 WRITE_WARNINGF(
"GLOSA error 2 x=% y=% s=% t=% v=%", x, y, s, t, v);
572 std::cout <<
"veh=" <<
myVeh.
getID() <<
" cant go slow enough" <<
"\n";
583 std::cout <<
"veh=" <<
myVeh.
getID() <<
" cant go fast enough" <<
"\n";
589 const double targetSpeed = x;
590 const double duration =
MAX2(y,
TS);
594 std::cout <<
" targetSpeed=" << targetSpeed <<
" duration=" << duration <<
"\n";
598 std::vector<std::pair<SUMOTime, double> > speedTimeLine;
621 if (key ==
"minSpeed") {
636 if (key ==
"minSpeed") {
#define WRITE_WARNINGF(...)
@ SUMO_ATTR_JM_DRIVE_AFTER_YELLOW_TIME
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
void setChosenSpeedFactor(const double factor)
Returns the precomputed factor by which the driver wants to be faster than the speed limit.
double getChosenSpeedFactor() const
Returns the precomputed factor by which the driver wants to be faster than the speed limit.
SUMOTime getDeparture() const
Returns this vehicle's real departure time.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
A device which collects info on the vehicle trip (mainly on departure and arrival)
void generateOutput(OutputDevice *tripinfoOut) const
Called on writing tripinfo output.
const MSLink * myNextTLSLink
the upcoming traffic light
MSVehicle & myVeh
myHolder cast to needed type
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static double timeGreen(const MSLink *tlsLink)
bool mySpeedAdviceActive
If speedFactor is currently beeing changed by the GLOSA device.
bool myUseQueue
if true the queue in front of the TLS is used for calculation
double myAddSwitchTime
Additional time the vehicle shall need to reach the intersection after the signal turns green.
double myMaxSpeedFactor
maximum speed factor when trying to reach green light
double myRange
maximum communication range
void adaptSpeed(double distance, double timeToJunction, double timeToSwitch, bool &solved)
adapt speed to reach junction at green
double myMinSpeed
minimum approach speed towards red light
~MSDevice_GLOSA()
Destructor.
double time_to_junction_at_continuous_accel(double d, double v)
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_GLOSA-options.
static double getTimeToSwitch(const MSLink *tlsLink, int &countOld)
compute time to next (relevant) switch
double myDistance
the distance to the upcoming traffic light
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
updates next tls link
double distance_at_continuous_accel(double speed, double time)
double earliest_arrival(double speed, double distance)
return minimum number of seconds to reach the junction
double myOriginalSpeedFactor
original speed factor
static void cleanup()
resets counters
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
updates distance and computes speed advice
static double getTimeToNextSwitch(const MSLink *tlsLink, bool ¤tPhaseGreen, bool ¤tPhaseStop, int &countOld)
compute time to next (relevant) switch the vehicle can reach
const std::string deviceName() const
return the name for this type of device
MSDevice_GLOSA(SUMOVehicle &holder, const std::string &id, double minSpeed, double range, double maxSpeedFactor, double addSwitchTime, bool useQueue, bool overrideSafety, bool ignoreCFModel)
Constructor.
bool myIgnoreCFModel
if true ignore non-critical speed calculations from the CF model, follow GLOSA's perfect speed calcul...
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.
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.
bool isInternal() const
return whether this edge is an internal edge
Representation of a lane in the micro simulation.
static std::vector< MSLink * >::const_iterator succLinkSec(const SUMOVehicle &veh, int nRouteSuccs, const MSLane &succLinkSource, const std::vector< MSLane * > &conts)
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
double getLength() const
Returns the lane's length.
bool isLinkEnd(std::vector< MSLink * >::const_iterator &i) const
double getVehicleMaxSpeed(const SUMOTrafficObject *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
MSEdge & getEdge() const
Returns the lane's edge.
int getTLIndex() const
Returns the TLS index.
const MSLane * getLaneBefore() const
return the internalLaneBefore if it exists and the laneBefore otherwise
const MSTrafficLightLogic * getTLLogic() const
Returns the TLS index.
bool haveRed() const
Returns whether this link is blocked by a red (or redyellow) traffic light.
Notification
Definition of a vehicle state.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
The parent class for traffic light logics.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
virtual double getTLQueueLength(const std::string) const
return the estimated queue length at the upcoming traffic light
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
SUMOTime getSpentDuration(SUMOTime simStep=-1) const
Returns the duration spent in the current phase.
void setSpeedTimeLine(const std::vector< std::pair< SUMOTime, double > > &speedTimeLine)
Sets a new velocity timeline.
Abstract in-vehicle device.
Representation of a vehicle in the micro simulation.
void updateBestLanes(bool forceRebuild=false, const MSLane *startLane=0)
computes the best lanes to use in order to continue the route
const std::vector< MSLane * > & getBestLanesContinuation() const
Returns the best sequence of lanes to continue the route starting at myLane.
const MSLane * getLane() const
Returns the lane the vehicle is on.
Influencer & getInfluencer()
double getSpeed() const
Returns the vehicle's current speed.
const MSCFModel & getCarFollowModel() const
Returns the vehicle's car following model definition.
double getPositionOnLane() const
Get the vehicle's position along the lane.
const SUMOVTypeParameter & getParameter() const
const std::string & getID() const
Returns the id.
A storage for options typed value containers)
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
Representation of a vehicle, person, or container.
bool getBoolParam(const std::string ¶mName, const bool required=false, const bool deflt=false) const
Retrieve a boolean parameter for the traffic object.
double getFloatParam(const std::string ¶mName, const bool required=false, const double deflt=INVALID_DOUBLE) const
Retrieve a floating point parameter for the traffic object.
double getJMParam(const SumoXMLAttr attr, const double defaultValue) const
Returns the named value from the map, or the default if it is not contained there.
Representation of a vehicle.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter