]> git.tdb.fi Git - r2c2.git/commitdiff
Use current monitoring to detect failed turnouts
authorMikko Rasa <tdb@tdb.fi>
Tue, 1 Apr 2014 18:35:31 +0000 (21:35 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 1 Apr 2014 18:35:31 +0000 (21:35 +0300)
The turnout solenoid takes quite a bit of current, which is easy to
detect.  If there's no current spike after activating a solenoid, it
may have a broken microswitch that prevents operation.

source/libr2c2/arducontrol.cpp
source/libr2c2/arducontrol.h

index dbe3013ddd9e5f057d1733e65f2340bc6ecb35c4..fa95a6936be746bd6e2d4ee4ff3244986a5d4574 100644 (file)
@@ -405,6 +405,8 @@ void ArduControl::tick()
                        acc.state.set(acc.state^lowest_bit);
                        PendingCommand cmd(acc, Accessory::ACTIVATE, i);
                        command_queue.push(cmd);
+
+                       monitor.reset_peak();
                }
                else
                        accessory_queue.pop_front();
@@ -415,8 +417,23 @@ void ArduControl::tick()
                Time::TimeStamp t = Time::now();
                if(t>off_timeout)
                {
+                       Accessory &acc = *active_accessory;
+
+                       if(acc.kind==Accessory::TURNOUT && monitor.get_peak()<0.5f)
+                       {
+                               unsigned bit = 1<<active_index;
+                               if(acc.uncertain&bit)
+                                       acc.uncertain &= ~bit;
+                               else
+                               {
+                                       signal_turnout_failed.emit(acc.address);
+                                       acc.state.rollback();
+                                       acc.target ^= bit;
+                               }
+                       }
+
                        off_timeout = Time::TimeStamp();
-                       PendingCommand cmd(*active_accessory, Accessory::DEACTIVATE, active_index);
+                       PendingCommand cmd(acc, Accessory::DEACTIVATE, active_index);
                        command_queue.push(cmd);
                }
        }
@@ -526,6 +543,8 @@ ArduControl::Accessory::Accessory(Kind k, unsigned a, unsigned b):
        address(a),
        bits(b),
        state(0),
+       uncertain((1<<bits)-1),
+       target(0),
        active_time(500*Time::msec)
 { }
 
index 555280985767a7e37ea512607a0337359ac11618..c3ca7a4106f570a418a6a7166a19a9048c1bcf67 100644 (file)
@@ -117,6 +117,7 @@ private:
 
                bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
                bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
+               void rollback() { pending = current; ++serial; }
 
                operator T() const { return current; }
        };
@@ -173,6 +174,7 @@ private:
                unsigned address;
                unsigned bits;
                ControlledVariable<unsigned> state;
+               unsigned uncertain;
                unsigned target;
                Msp::Time::TimeDelta active_time;