]> git.tdb.fi Git - r2c2.git/commitdiff
Add turnout failure simulation to the dummy driver
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Feb 2015 12:54:49 +0000 (14:54 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Feb 2015 12:54:49 +0000 (14:54 +0200)
source/libr2c2/dummy.cpp
source/libr2c2/dummy.h

index a8efac3a1d71204f3b0d861843b482db41911f98..0c3c351d968d01b23a79597aa4e725791dea5d62 100644 (file)
@@ -1,3 +1,4 @@
+#include <cstdlib>
 #include <msp/strings/utils.h>
 #include <msp/time/utils.h>
 #include "dummy.h"
@@ -9,7 +10,8 @@ namespace R2C2 {
 
 Dummy::Dummy(const Options &opts):
        power(true),
-       turnout_delay(opts.get("turnout_delay", 0.0f)*Time::sec)
+       turnout_delay(opts.get("turnout_delay", 0.0f)*Time::sec),
+       turnout_fail_rate(opts.get("turnout_fail_rate", 0.0f)*RAND_MAX)
 { }
 
 void Dummy::set_power(bool p)
@@ -62,7 +64,8 @@ void Dummy::set_turnout(unsigned addr, unsigned state)
                turnout.timeout = Time::now()+turnout_delay;
        else
        {
-               turnout.state = state;
+               if(!turnout_fail_rate || rand()>=turnout_fail_rate)
+                       turnout.state = state;
                signal_turnout.emit(addr, state);
        }
 }
@@ -121,6 +124,11 @@ void Dummy::tick()
        {
                if(i->second.timeout && t>=i->second.timeout)
                {
+                       if(turnout_fail_rate && rand()<turnout_fail_rate)
+                       {
+                               signal_turnout_failed.emit(i->first);
+                               i->second.pending = i->second.state;
+                       }
                        i->second.state = i->second.pending;
                        i->second.timeout = Time::TimeStamp();
                        signal_turnout.emit(i->first, i->second.state);
index f7b48cf2709ac6ede5ae0b0847997272caddad80..547efc3269622850b764feeadea4adb73f08df17 100644 (file)
@@ -30,6 +30,7 @@ private:
        std::map<unsigned, LocoState> locos;
        std::map<unsigned, bool> sensors;
        Msp::Time::TimeDelta turnout_delay;
+       int turnout_fail_rate;
 
 public:
        Dummy(const Options &);