]> git.tdb.fi Git - r2c2.git/commitdiff
Allow intercepting and denying turnout route and locomotive speed changes
authorMikko Rasa <tdb@tdb.fi>
Fri, 22 May 2009 21:49:41 +0000 (21:49 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 22 May 2009 21:49:41 +0000 (21:49 +0000)
Reroute train when a reserved turnout changes its route
Various minor fixes

source/engineer/engineer.cpp
source/libmarklin/block.cpp
source/libmarklin/block.h
source/libmarklin/control.h
source/libmarklin/except.h [new file with mode: 0644]
source/libmarklin/locomotive.cpp
source/libmarklin/locomotive.h
source/libmarklin/train.cpp
source/libmarklin/train.h
source/libmarklin/turnout.cpp
source/libmarklin/turnout.h

index 581a301d8f194df79574671c15e95d165e4714c0..be4c050b9ccf1fee465b2d222aab7115354097da 100644 (file)
@@ -20,6 +20,7 @@ Distributed under the GPL
 #include <msp/strings/formatter.h>
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/regex.h>
+#include "libmarklin/except.h"
 #include "libmarklin/tracktype.h"
 #include "engineer.h"
 #include "mainpanel.h"
@@ -99,7 +100,8 @@ Engineer::Engineer(int argc, char **argv):
 
 Engineer::~Engineer()
 {
-       trfc_mgr->save("engineer.state");
+       if(!simulate)
+               trfc_mgr->save("engineer.state");
        delete trfc_mgr;
 }
 
@@ -352,7 +354,15 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
                        if(unsigned tid=track->get_track().get_turnout_id())
                        {
                                Turnout &turnout=control.get_turnout(tid);
-                               turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
+                               try
+                               {
+                                       turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
+                                       main_panel->set_status_text(format("Turnout %d switched", turnout.get_address()));
+                               }
+                               catch(const TurnoutBusy &e)
+                               {
+                                       main_panel->set_status_text(e.what());
+                               }
                        }
                        else if(simulate)
                        {
index 94be126c77d088c1b44ba63303a99253a0338957..ee0a48cb950498474e0fe7d2b8bc602a5ac77987 100644 (file)
@@ -21,6 +21,7 @@ Block::Block(TrafficManager &tm, Track &start):
        trfc_mgr(tm),
        id(next_id++),
        sensor_id(start.get_sensor_id()),
+       turnout_id(start.get_turnout_id()),
        train(0)
 {
        tracks.insert(&start);
@@ -37,7 +38,7 @@ Block::Block(TrafficManager &tm, Track &start):
                for(unsigned i=0; i<links.size(); ++i)
                        if(links[i] && !tracks.count(links[i]))
                        {
-                               if(links[i]->get_sensor_id()==sensor_id && !links[i]->get_turnout_id() && !track->get_turnout_id())
+                               if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
                                {
                                        queue.push_back(links[i]);
                                        tracks.insert(links[i]);
index bdbcc3f3eb0ed2687a6d09e6e5d743782cef5ce9..68c57b88e39c102f8d38e5f8b75f00c93fd51afd 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -34,6 +34,7 @@ private:
        TrafficManager &trfc_mgr;
        unsigned       id;
        unsigned       sensor_id;
+       unsigned       turnout_id;
        std::set<Track *>     tracks;
        std::vector<Endpoint> endpoints;
        const Train    *train;
@@ -42,6 +43,7 @@ public:
        Block(TrafficManager &, Track &);
 
        unsigned get_sensor_id() const { return sensor_id; }
+       unsigned get_turnout_id() const { return turnout_id; }
        const std::set<Track *> &get_tracks() const { return tracks; }
        const std::vector<Endpoint> &get_endpoints() const { return endpoints; }
        int get_endpoint_by_link(const Block &) const;
index 0ec3d5ba53022a6f3d2bde7614547d1c24703ee0..1d19263fb93da651807d5b0abaaa3d0978166eb5 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -47,6 +47,7 @@ public:
        void       set_power(bool);
        bool       get_power() const { return power; }
        void       set_debug(bool);
+       const std::map<unsigned, Turnout *> &get_turnouts() const { return turnouts; }
        const std::map<unsigned, Sensor *> &get_sensors() const { return sensors; }
        unsigned   get_queue_length() const { return queue.size(); }
        void       open(const std::string &);
diff --git a/source/libmarklin/except.h b/source/libmarklin/except.h
new file mode 100644 (file)
index 0000000..85fe431
--- /dev/null
@@ -0,0 +1,31 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2009  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef LIBMARKLIN_EXCEPT_H_
+#define LIBMARKLIN_EXCEPT_H_
+
+#include <msp/core/except.h>
+
+namespace Marklin {
+
+class Train;
+
+class TurnoutBusy: public Msp::Exception
+{
+private:
+       Train *train;
+
+public:
+       TurnoutBusy(Train *t): Exception("Turnout is busy"), train(t) { }
+       virtual ~TurnoutBusy() throw() { }
+
+       Train *get_train() const throw() { return train; }
+};
+
+}
+
+#endif
index a42d8d7820f0c4b3ce18e3c0bf7bfc19a7a6e8f4..32f2ecc1a185f2fa1ac387cb4659ad61e1926f01 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -33,8 +33,10 @@ Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a):
 
 void Locomotive::set_speed(unsigned spd)
 {
-       speed=min(spd, 14U);
+       spd=min(spd, 14U);
+       signal_speed_changing.emit(spd);
 
+       speed=spd;
        send_command(false);
 
        signal_speed_changed.emit(speed);
index e3cbf7de238770e18dad536fb74dcc7cfc7d7d95..89b275ecb0db2020cc635dc695c8853648bd1d02 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -30,6 +30,7 @@ private:
        unsigned funcs;
 
 public:
+       sigc::signal<void, unsigned> signal_speed_changing;
        sigc::signal<void, unsigned> signal_speed_changed;
        sigc::signal<void, unsigned, bool> signal_function_changed;
 
index f8ef1715dbc5aa6d7a6a33bd476bb9db510d0939..7d6afbae1f1c074ad281712ae43cde0be3888460 100644 (file)
@@ -10,6 +10,7 @@ Distributed under the GPL
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
 #include "control.h"
+#include "except.h"
 #include "tracktype.h"
 #include "trafficmanager.h"
 #include "train.h"
@@ -38,6 +39,13 @@ Train::Train(TrafficManager &tm, Locomotive &l):
        const map<unsigned, Sensor *> &sensors=trfc_mgr.get_control().get_sensors();
        for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
                i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
+
+       const map<unsigned, Turnout *> &turnouts=trfc_mgr.get_control().get_turnouts();
+       for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
+       {
+               i->second->signal_route_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changing), i->second));
+               i->second->signal_route_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changed), i->second));
+       }
 }
 
 void Train::set_name(const string &n)
@@ -60,6 +68,8 @@ void Train::set_speed(unsigned speed)
                        i->block->reserve(0);
                rsv_blocks.clear();
        }
+       else
+               reserve_more();
 
        update_speed();
        pure_speed=false;
@@ -93,16 +103,24 @@ void Train::place(Block *block, unsigned entry)
 
 bool Train::free_block(Block *block)
 {
+       unsigned nsens=0;
        for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+       {
                if(i->block==block)
                {
+                       if(nsens<1)
+                               return false;
                        while(i!=rsv_blocks.end())
                        {
                                i->block->reserve(0);
                                i=rsv_blocks.erase(i);
                        }
+                       update_speed();
                        return true;
                }
+               else if(i->block->get_sensor_id())
+                       ++nsens;
+       }
 
        return false;
 }
@@ -110,7 +128,10 @@ bool Train::free_block(Block *block)
 void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 {
        if(try_reserve && t>try_reserve)
+       {
+               reserve_more();
                update_speed();
+       }
 
        if(cur_track)
        {
@@ -193,7 +214,10 @@ void Train::sensor_event(bool state, Sensor *sensor)
                                set_position(i->block->get_endpoints()[i->entry]);
 
                if(target_speed)
+               {
+                       reserve_more();
                        update_speed();
+               }
        }
        else
        {
@@ -212,6 +236,44 @@ void Train::sensor_event(bool state, Sensor *sensor)
        }
 }
 
+void Train::turnout_route_changing(unsigned, Turnout *turnout)
+{
+       unsigned tid=turnout->get_address();
+       for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+               if(i->block->get_turnout_id()==tid)
+                       throw TurnoutBusy(this);
+       
+       unsigned nsens=0;
+       for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+       {
+               if(i->block->get_turnout_id()==tid)
+               {
+                       if(nsens<1)
+                               throw TurnoutBusy(this);
+                       break;
+               }
+               else if(i->block->get_sensor_id())
+                       ++nsens;
+       }
+}
+
+void Train::turnout_route_changed(unsigned, Turnout *turnout)
+{
+       unsigned tid=turnout->get_address();
+       for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+               if(i->block->get_turnout_id()==tid)
+               {
+                       while(i!=rsv_blocks.end())
+                       {
+                               i->block->reserve(0);
+                               i=rsv_blocks.erase(i);
+                       }
+                       reserve_more();
+                       update_speed();
+                       return;
+               }
+}
+
 unsigned Train::reserve_more()
 {
        BlockRef *last=0;
@@ -274,15 +336,19 @@ void Train::update_speed()
        }
        else
        {
-               unsigned n=reserve_more();
-               if(n==0)
+               unsigned nsens=0;
+               for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+                       if(i->block->get_sensor_id())
+                               ++nsens;
+
+               if(nsens==0)
                {
                        loco.set_speed(0);
                        pure_speed=false;
                        try_reserve=Time::now()+2*Time::sec;
                        set_status("Blocked");
                }
-               else if(n==1)
+               else if(nsens==1 && target_speed>3)
                {
                        loco.set_speed(3);
                        pure_speed=false;
index b84844aa050a999ddeeb5b93cb8ce97c5666ab46..d95d4c43ea50b3e152d85bdc545fcccab70572e7 100644 (file)
@@ -76,6 +76,8 @@ public:
        void save(std::list<Msp::DataFile::Statement> &) const;
 private:
        void sensor_event(bool, Sensor *);
+       void turnout_route_changing(unsigned, Turnout *);
+       void turnout_route_changed(unsigned, Turnout *);
        unsigned reserve_more();
        void update_speed();
        void set_status(const std::string &);
index 60bab121042bf1f8b8c46aaebe36b6245cab7edf..3e6e10dcc69f0d37c3148b3abf7506c3fadb6f35 100644 (file)
@@ -41,8 +41,9 @@ Turnout::Turnout(Control &c, unsigned a, bool d):
 
 void Turnout::set_route(unsigned r)
 {
-       route=r;
+       signal_route_changing.emit(r);
 
+       route=r;
        command(true);
        control.set_timer(200*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout));
 
index ae6865406523007285bf34ba561ae55105b1375a..616a7fedb3bcabddef78cd9dc52c56ea0a6185b1 100644 (file)
@@ -28,6 +28,7 @@ private:
        bool     dual;
 
 public:
+       sigc::signal<void, unsigned> signal_route_changing;
        sigc::signal<void, unsigned> signal_route_changed;
 
        Turnout(Control &, unsigned, bool =false);