]> git.tdb.fi Git - r2c2.git/commitdiff
Halt all trains in various unexpected situations
authorMikko Rasa <tdb@tdb.fi>
Wed, 21 Apr 2010 19:20:46 +0000 (19:20 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 21 Apr 2010 19:20:46 +0000 (19:20 +0000)
Turnouts no longer get stuck if a switching attempt fails
Save the vehicles of trains in state file
It's better to put trains too far back rather than forward

source/engineer/engineer.cpp
source/engineer/mainpanel.cpp
source/engineer/mainpanel.h
source/libmarklin/driver.h
source/libmarklin/dummy.h
source/libmarklin/intellibox.cpp
source/libmarklin/intellibox.h
source/libmarklin/layout.cpp
source/libmarklin/layout.h
source/libmarklin/train.cpp
source/libmarklin/train.h

index 4c9360c54fee891de134c92f29f3373e1cb177de..897c7ca1ea8121f21d205a26d007b4ec35e26f8a 100644 (file)
@@ -77,6 +77,7 @@ Engineer::Engineer(int argc, char **argv):
 
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
        layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+       layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
        layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event));
        if(FS::exists("engineer.state"))
                DataFile::load(layout, "engineer.state");
index 197c1a4bc9cbbe970605fb988800b7159eb78cf8..49750bfc32af7abee5f5a83a78b366fccdfcde7f 100644 (file)
@@ -19,37 +19,44 @@ MainPanel::MainPanel(Engineer &e, GLtk::Resources &r):
        Panel(r),
        engineer(e)
 {
-       set_size(200, 100);
+       set_size(200, 133);
 
        GLtk::Button *btn;
 
        add(*(btn=new GLtk::Button(res, "Off")));
-       btn->set_geometry(GLtk::Geometry(10, 53, 40, 25));
+       btn->set_geometry(GLtk::Geometry(10, 86, 40, 25));
        btn->set_style("red");
        btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::power_off));
 
        add(*(ind_off=new GLtk::Indicator(res)));
-       ind_off->set_geometry(GLtk::Geometry(10, 78, 40, 12));
+       ind_off->set_geometry(GLtk::Geometry(10, 111, 40, 12));
        ind_off->set_style("red");
 
        add(*(btn=new GLtk::Button(res, "On")));
-       btn->set_geometry(GLtk::Geometry(50, 53, 40, 25));
+       btn->set_geometry(GLtk::Geometry(50, 86, 40, 25));
        btn->set_style("green");
        btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::power_on));
 
        add(*(ind_on=new GLtk::Indicator(res)));
-       ind_on->set_geometry(GLtk::Geometry(50, 78, 40, 12));
+       ind_on->set_geometry(GLtk::Geometry(50, 111, 40, 12));
        ind_on->set_style("green");
 
        add(*(btn=new GLtk::Button(res, "Quit")));
-       btn->set_geometry(GLtk::Geometry(150, 53, 40, 25));
+       btn->set_geometry(GLtk::Geometry(150, 86, 40, 25));
        btn->set_style("red");
        btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::quit));
 
        add(*(btn=new GLtk::Button(res, "+Loc")));
-       btn->set_geometry(GLtk::Geometry(90, 53, 40, 25));
+       btn->set_geometry(GLtk::Geometry(90, 86, 40, 25));
        btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::new_loc));
 
+       add(*(ind_halt=new GLtk::Indicator(res)));
+       ind_halt->set_geometry(GLtk::Geometry(10, 69, 40, 12));
+
+       add(*(btn=new GLtk::Button(res, "Halt")));
+       btn->set_geometry(GLtk::Geometry(10, 44, 40, 25));
+       btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::toggle_halt));
+
        add(*(lbl_status=new GLtk::Label(res)));
        lbl_status->set_geometry(GLtk::Geometry(10, 10, 180, 24));
        lbl_status->set_style("digital");
@@ -61,6 +68,7 @@ MainPanel::MainPanel(Engineer &e, GLtk::Resources &r):
                ind_off->set_active(true);
 
        driver.signal_power.connect(sigc::mem_fun(this, &MainPanel::power_event));
+       driver.signal_halt.connect(sigc::mem_fun(this, &MainPanel::halt_event));
 }
 
 void MainPanel::set_status_text(const string &txt)
@@ -78,6 +86,12 @@ void MainPanel::power_off()
        engineer.get_layout().get_driver().set_power(false);
 }
 
+void MainPanel::toggle_halt()
+{
+       Marklin::Driver &driver = engineer.get_layout().get_driver();
+       driver.halt(!driver.is_halted());
+}
+
 void MainPanel::new_loc()
 {
        TrainProperties *dialog = new TrainProperties(engineer, res, 0);
@@ -96,3 +110,8 @@ void MainPanel::power_event(bool p)
        ind_on->set_active(p);
        ind_off->set_active(!p);
 }
+
+void MainPanel::halt_event(bool h)
+{
+       ind_halt->set_active(h);
+}
index 017617dafff437f9fdec9281b7337a10a5d2b7c8..a24ef4976c59af9a0e6b9b7e7b71277ace13daa4 100644 (file)
@@ -22,6 +22,7 @@ private:
        Msp::GLtk::Indicator *ind_on;
        Msp::GLtk::Indicator *ind_off;
        Msp::GLtk::Label *lbl_status;
+       Msp::GLtk::Indicator *ind_halt;
 
 public:
        MainPanel(Engineer &, Msp::GLtk::Resources &);
@@ -30,9 +31,11 @@ public:
 private:
        void power_on();
        void power_off();
+       void toggle_halt();
        void new_loc();
        void quit();
        void power_event(bool);
+       void halt_event(bool);
 };
 
 #endif
index 9cab5cf9ebe4f97b48933b511d28d274fffb6aeb..cfee532639b52688cf74f2dbd014f97a652680f8 100644 (file)
@@ -17,6 +17,7 @@ class Driver
 {
 public:
        sigc::signal<void, bool> signal_power;
+       sigc::signal<void, bool> signal_halt;
        sigc::signal<void, unsigned, unsigned, bool> signal_loco_speed;
        sigc::signal<void, unsigned, unsigned, bool> signal_loco_function;
        sigc::signal<void, unsigned, bool> signal_turnout;
@@ -29,6 +30,8 @@ public:
 
        virtual void set_power(bool) = 0;
        virtual bool get_power() const = 0;
+       virtual void halt(bool) = 0;
+       virtual bool is_halted() const = 0;
 
        virtual void add_loco(unsigned) = 0;
        virtual void set_loco_speed(unsigned, unsigned) = 0;
index 3004c8bd1a8f768841bb42bb3df53db757726b3e..239573127fe22592c0efb57181eeb1a94bb68a64 100644 (file)
@@ -32,6 +32,8 @@ public:
 
        virtual void set_power(bool);
        virtual bool get_power() const { return power; }
+       virtual void halt(bool) { }
+       virtual bool is_halted() const { return false; }
 
        virtual void add_loco(unsigned) { }
        virtual void set_loco_speed(unsigned, unsigned);
index ace23e8f6166762926782fbd2a9dbcb01e290656..70372f4f10dae20b930f091affe7cc80416249a4 100644 (file)
@@ -20,6 +20,7 @@ namespace Marklin {
 
 Intellibox::Intellibox(const string &dev):
        power(false),
+       halted(false),
        update_sensors(false),
        command_sent(false)
 {
@@ -80,6 +81,19 @@ void Intellibox::set_power(bool p)
        signal_power.emit(power);
 }
 
+void Intellibox::halt(bool h)
+{
+       halted = h;
+       if(halted)
+       {
+               for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
+                       if(i->second.speed)
+                               set_loco_speed(i->first, 0);
+       }
+
+       signal_halt.emit(halted);
+}
+
 void Intellibox::add_loco(unsigned addr)
 {
        if(!locos.count(addr))
@@ -98,6 +112,8 @@ void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
        Locomotive &loco = locos[addr];
        if(speed==loco.speed)
                return;
+       if(speed && halted)
+               return;
 
        loco.speed = speed;
        loco_command(addr, speed, loco.reverse, loco.funcs|0x100);
@@ -411,10 +427,11 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                unsigned char err;
                read_all(&err, 1);
 
+               unsigned addr = queue.front().addr;
+               Turnout &turnout = turnouts[addr];
+
                if(err==ERR_NO_ERROR)
                {
-                       unsigned addr = queue.front().addr;
-                       Turnout &turnout = turnouts[addr];
                        turnout.state = turnout.pending;
                        if(turnout.active)
                        {
@@ -424,6 +441,8 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                }
                else if(err==ERR_NO_I2C_SPACE)
                        queue.push_back(queue.front());
+               else
+                       turnout.pending = turnout.state;
        }
        else if(cmd==CMD_TURNOUT_STATUS)
        {
index a6b7cda966a50141026af672311b3ab07936e1b3..483d0efb4acae8f7a7ac2acc4098289cf4b60ae5 100644 (file)
@@ -98,6 +98,7 @@ private:
 
        int serial_fd;
        bool power;
+       bool halted;
        std::map<unsigned, Locomotive> locos;
        std::map<unsigned, Turnout> turnouts;
        std::map<unsigned, Sensor> sensors;
@@ -111,6 +112,8 @@ public:
 
        virtual void set_power(bool);
        virtual bool get_power() const { return power; }
+       virtual void halt(bool);
+       virtual bool is_halted() const { return halted; }
 
        virtual void add_loco(unsigned);
        virtual void set_loco_speed(unsigned, unsigned);
index 7855dca7d7115333dc1b060ff4f3d57b68950bbd..af88d7d8fd2e35f3a44af86ff24640c85c003d1d 100644 (file)
@@ -28,7 +28,10 @@ namespace Marklin {
 Layout::Layout(Catalogue &c, Driver *d):
        catalogue(c),
        driver(d)
-{ }
+{
+       if(driver)
+               driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
+}
 
 Layout::~Layout()
 {
@@ -209,6 +212,13 @@ void Layout::tick()
                i->second->tick(t, dt);
 }
 
+void Layout::emergency(const string &msg)
+{
+       if(driver)
+               driver->halt(true);
+       signal_emergency.emit(msg);
+}
+
 void Layout::save(const string &fn)
 {
        IO::BufferedFile out(fn, IO::M_WRITE);
@@ -344,6 +354,20 @@ void Layout::check_routes()
        }
 }
 
+void Layout::sensor_event(unsigned addr, bool state)
+{
+       if(state)
+       {
+               for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+                       if((*i)->get_sensor_id()==addr)
+                       {
+                               if(!(*i)->get_train())
+                                       emergency("Unreserved sensor triggered");
+                               break;
+                       }
+       }
+}
+
 
 Layout::Loader::Loader(Layout &l):
        DataFile::BasicLoader<Layout>(l),
index 385a3cfab760e17d7beab074501e6d4cd3bf30ce..6683e2d7ddb8de5c91ac781781c9c9fdb1bc01d9 100644 (file)
@@ -50,6 +50,7 @@ public:
        sigc::signal<void, Vehicle &> signal_vehicle_added;
        sigc::signal<void, Vehicle &> signal_vehicle_removed;
        sigc::signal<void, Block &, Train *> signal_block_reserved;
+       sigc::signal<void, const std::string &> signal_emergency;
 
 private:
        Catalogue &catalogue;
@@ -97,12 +98,14 @@ public:
        void remove_vehicle(Vehicle &);
 
        void tick();
+       void emergency(const std::string &);
 
        void save(const std::string &);
        void save_trains(const std::string &);
 private:
        void check_links();
        void check_routes();
+       void sensor_event(unsigned, bool);
 };
 
 } // namespace Marklin
index 6d963396e3baa81bb5cdffa26b8b0546adfad139..0c741722d7b7dfecf749be196183bbb1df3a3e58 100644 (file)
@@ -44,7 +44,9 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
        status("Unplaced"),
        travel_dist(0),
        pure_speed(false),
-       real_speed(15)
+       real_speed(15),
+       accurate_position(false),
+       overshoot_dist(false)
 {
        vehicles.push_back(new Vehicle(layout, loco_type));
 
@@ -200,6 +202,7 @@ void Train::place(Block &block, unsigned entry)
        release_blocks(cur_blocks);
 
        set_active(false);
+       accurate_position = false;
 
        if(!block.reserve(this))
        {
@@ -304,6 +307,8 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                stop_timeout = Time::TimeStamp();
        }
 
+       Driver &driver = layout.get_driver();
+
        if(timetable)
                timetable->tick(t);
        control->tick(dt);
@@ -312,7 +317,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 
        if(speed && (speed<0)!=reverse)
        {
-               layout.get_driver().set_loco_reverse(address, speed<0);
+               driver.set_loco_reverse(address, speed<0);
                reverse = speed<0;
 
                release_blocks(rsv_blocks);
@@ -320,10 +325,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 
                reserve_more();
        }
-       if(speed_notch!=current_speed && !speed_changing)
+       if(speed_notch!=current_speed && !speed_changing && !driver.is_halted() && driver.get_power())
        {
                speed_changing = true;
-               layout.get_driver().set_loco_speed(address, speed_notch);
+               driver.set_loco_speed(address, speed_notch);
 
                pure_speed = false;
 
@@ -345,10 +350,14 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
                        ok = i->block->get_tracks().count(track);
 
+               float d = get_real_speed(current_speed)*(dt/Time::sec);
                if(ok)
-               {
-                       float d = get_real_speed(current_speed)*(dt/Time::sec);
                        vehicle.advance(reverse ? -d : d);
+               else if(accurate_position)
+               {
+                       overshoot_dist += d;
+                       if(overshoot_dist>40*layout.get_catalogue().get_scale())
+                               layout.emergency(name+" has not arrived at sensor");
                }
        }
        else if(end_of_route)
@@ -358,6 +367,11 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 void Train::save(list<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("name"), name));
+
+       for(vector<Vehicle *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
+               if(i!=vehicles.begin())
+                       st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number()));
+
        for(unsigned i=0; i<=14; ++i)
                if(real_speed[i].weight)
                        st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
@@ -392,6 +406,7 @@ void Train::loco_speed_event(unsigned addr, unsigned speed, bool)
        {
                current_speed = speed;
                speed_changing = false;
+               pure_speed = false;
        }
 }
 
@@ -416,11 +431,24 @@ void Train::sensor_event(unsigned addr, bool state)
        {
                // Find the first sensor block from our reserved blocks that isn't this sensor
                list<BlockRef>::iterator i;
+               unsigned result = 0;
                for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
-                       if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
-                               break;
+                       if(i->block->get_sensor_id())
+                       {
+                               if(i->block->get_sensor_id()!=addr)
+                               {
+                                       if(result==0)
+                                               result = 2;
+                                       else if(result==1)
+                                               break;
+                               }
+                               else if(result==0)
+                                       result = 1;
+                               else if(result==2)
+                                       result = 3;
+                       }
 
-               if(i!=rsv_blocks.begin())
+               if(result==1 && i!=rsv_blocks.begin())
                {
                        // Compute speed and update related state
                        float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
@@ -454,6 +482,8 @@ void Train::sensor_event(unsigned addr, bool state)
                        }
                        last_entry_time = Time::now();
                        pure_speed = true;
+                       accurate_position = true;
+                       overshoot_dist = 0;
 
                        // Check if we've reached the next route
                        if(next_route)
@@ -481,6 +511,8 @@ void Train::sensor_event(unsigned addr, bool state)
                                        signal_arrived.emit();
                        }
                }
+               else if(result==3)
+                       layout.emergency("Sensor for "+name+" triggered out of order");
        }
        else
        {
@@ -652,6 +684,12 @@ unsigned Train::reserve_more()
                        last = &rsv_blocks.back();
        }
 
+       // Make any sensorless blocks at the beginning immediately current
+       list<BlockRef>::iterator i;
+       for(i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !i->block->get_sensor_id()); ++i) ;
+       if(i!=rsv_blocks.begin())
+               cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
+
        return good_sens;
 }
 
@@ -804,7 +842,7 @@ void Train::Loader::block(unsigned id)
        obj.cur_blocks.push_back(BlockRef(&blk, entry));
        obj.set_status("Stopped");
        const Block::Endpoint &bep = blk.get_endpoints()[entry];
-       obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+       obj.vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
 
        if(blk.get_sensor_id())
                obj.layout.get_driver().set_sensor(blk.get_sensor_id(), true);
index 635a9466b9c1cab90625dc0eef8287ec42ebefdc..866454164ee1592b9cb794f8413cc9509c3571a5 100644 (file)
@@ -92,6 +92,8 @@ private:
        float travel_dist;
        bool pure_speed;
        std::vector<RealSpeed> real_speed;
+       bool accurate_position;
+       float overshoot_dist;
 
 public:
        Train(Layout &, const LocoType &, unsigned);