From b3cc0d29a0fda2d48443d66881214c81a04b1729 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 11 Feb 2015 14:54:49 +0200 Subject: [PATCH] Add turnout failure simulation to the dummy driver --- source/libr2c2/dummy.cpp | 12 ++++++++++-- source/libr2c2/dummy.h | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/source/libr2c2/dummy.cpp b/source/libr2c2/dummy.cpp index a8efac3..0c3c351 100644 --- a/source/libr2c2/dummy.cpp +++ b/source/libr2c2/dummy.cpp @@ -1,3 +1,4 @@ +#include #include #include #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()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); diff --git a/source/libr2c2/dummy.h b/source/libr2c2/dummy.h index f7b48cf..547efc3 100644 --- a/source/libr2c2/dummy.h +++ b/source/libr2c2/dummy.h @@ -30,6 +30,7 @@ private: std::map locos; std::map sensors; Msp::Time::TimeDelta turnout_delay; + int turnout_fail_rate; public: Dummy(const Options &); -- 2.45.2