From 3133b97af17c8a11fb3364453b53d970790a1426 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 23 Apr 2010 18:21:12 +0000 Subject: [PATCH] Handle reversing in a way that allows backing out of a dead end --- source/libmarklin/aicontrol.cpp | 8 ++++++++ source/libmarklin/aicontrol.h | 1 + source/libmarklin/controlmodel.h | 1 + source/libmarklin/simplephysics.h | 1 + source/libmarklin/train.cpp | 6 +++--- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/libmarklin/aicontrol.cpp b/source/libmarklin/aicontrol.cpp index b97b037..d5a29c0 100644 --- a/source/libmarklin/aicontrol.cpp +++ b/source/libmarklin/aicontrol.cpp @@ -61,6 +61,14 @@ float AIControl::get_speed() const return next_model->get_speed(); } +bool AIControl::get_reverse() const +{ + if(float ns = next_model->get_speed()) + return ns<0; + else + return target_speed.value<0; +} + float AIControl::get_braking_distance() const { return next_model->get_braking_distance(); diff --git a/source/libmarklin/aicontrol.h b/source/libmarklin/aicontrol.h index d02011f..465d2ab 100644 --- a/source/libmarklin/aicontrol.h +++ b/source/libmarklin/aicontrol.h @@ -32,6 +32,7 @@ public: virtual const TrainControl &get_control(const std::string &) const; virtual float get_speed() const; + virtual bool get_reverse() const; virtual float get_braking_distance() const; virtual void tick(const Msp::Time::TimeDelta &); diff --git a/source/libmarklin/controlmodel.h b/source/libmarklin/controlmodel.h index a45a30e..17bb72b 100644 --- a/source/libmarklin/controlmodel.h +++ b/source/libmarklin/controlmodel.h @@ -30,6 +30,7 @@ public: virtual const TrainControl &get_control(const std::string &) const = 0; virtual float get_speed() const = 0; + virtual bool get_reverse() const = 0; virtual float get_braking_distance() const = 0; virtual void tick(const Msp::Time::TimeDelta &) = 0; diff --git a/source/libmarklin/simplephysics.h b/source/libmarklin/simplephysics.h index 6e33e4e..64d961b 100644 --- a/source/libmarklin/simplephysics.h +++ b/source/libmarklin/simplephysics.h @@ -28,6 +28,7 @@ public: virtual const TrainControl &get_control(const std::string &) const; virtual float get_speed() const { return speed; } + virtual bool get_reverse() const { return speed<0; } virtual float get_braking_distance() const; virtual void tick(const Msp::Time::TimeDelta &); diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 44f9298..1abf535 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -322,10 +322,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) float speed = control->get_speed(); unsigned speed_notch = find_speed(abs(speed)); - if(speed && (speed<0)!=reverse) + if(control->get_reverse()!=reverse) { - driver.set_loco_reverse(address, speed<0); - reverse = speed<0; + reverse = control->get_reverse(); + driver.set_loco_reverse(address, reverse); release_blocks(rsv_blocks); reverse_blocks(cur_blocks); -- 2.45.2