]> git.tdb.fi Git - r2c2.git/commitdiff
Rename ControlModel to Controller
authorMikko Rasa <tdb@tdb.fi>
Mon, 27 Sep 2010 20:27:45 +0000 (20:27 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 27 Sep 2010 20:27:45 +0000 (20:27 +0000)
Rename SimplePhysics to SimpleController

12 files changed:
source/libmarklin/aicontrol.cpp
source/libmarklin/aicontrol.h
source/libmarklin/controller.h [new file with mode: 0644]
source/libmarklin/controlmodel.h [deleted file]
source/libmarklin/simplecontroller.cpp [new file with mode: 0644]
source/libmarklin/simplecontroller.h [new file with mode: 0644]
source/libmarklin/simplephysics.cpp [deleted file]
source/libmarklin/simplephysics.h [deleted file]
source/libmarklin/timetable.cpp
source/libmarklin/timetable.h
source/libmarklin/train.cpp
source/libmarklin/train.h

index eefc7f87f230d586c2a083dbc0741ae0d4b1763d..8876724f05edee50d3c36dab110c10658d9a71ef 100644 (file)
@@ -15,7 +15,7 @@ using namespace Msp;
 
 namespace Marklin {
 
-AIControl::AIControl(Train &t, ControlModel *n):
+AIControl::AIControl(Train &t, Controller *n):
        train(t),
        next_model(n),
        target_speed(TrainControl::continuous("speed", 0, 1000)),
index bcd6cbba6405ea6fef17d2ae078c268f4c10cc71..90906caaea98bd091515317a92e877c771bae9f6 100644 (file)
@@ -9,24 +9,24 @@ Distributed under the GPL
 #define LIBMARKLIN_AICONTROL_H_
 
 #include <sigc++/trackable.h>
-#include "controlmodel.h"
+#include "controller.h"
 #include "traincontrol.h"
 
 namespace Marklin {
 
 class Train;
 
-class AIControl: public ControlModel, public sigc::trackable
+class AIControl: public Controller, public sigc::trackable
 {
 private:
        Train &train;
-       ControlModel *next_model;
+       Controller *next_model;
        TrainControl target_speed;
        bool blocked;
        bool approach;
 
 public:
-       AIControl(Train &, ControlModel *);
+       AIControl(Train &, Controller *);
        virtual ~AIControl();
 
        virtual void set_control(const std::string &, float);
diff --git a/source/libmarklin/controller.h b/source/libmarklin/controller.h
new file mode 100644 (file)
index 0000000..b807548
--- /dev/null
@@ -0,0 +1,50 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef LIBMARKLIN_CONTROLLER_H_
+#define LIBMARKLIN_CONTROLLER_H_
+
+#include <string>
+#include <sigc++/signal.h>
+#include <msp/time/timedelta.h>
+
+namespace Marklin {
+
+struct TrainControl;
+
+/**
+Interface class for train controllers.  Takes input through a uniform named
+control interface.  Provides information about train movement on output.
+*/
+class Controller
+{
+public:
+       sigc::signal<void, const TrainControl &> signal_control_changed;
+
+protected:
+       Controller() { }
+public:
+       virtual ~Controller() { }
+
+       virtual void set_control(const std::string &, float) = 0;
+       virtual const TrainControl &get_control(const std::string &) const = 0;
+
+       /** Returns the current speed.  Always non-negative. */
+       virtual float get_speed() const = 0;
+
+       /** Returns true if traveling in reverse. */
+       virtual bool get_reverse() const = 0;
+
+       /** Determines the distance required to come to a full stop. */
+       virtual float get_braking_distance() const = 0;
+
+       virtual void tick(const Msp::Time::TimeDelta &) = 0;
+};
+
+} // namespace Marklin
+
+#endif
diff --git a/source/libmarklin/controlmodel.h b/source/libmarklin/controlmodel.h
deleted file mode 100644 (file)
index 3032f66..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
-#ifndef LIBMARKLIN_CONTROLMODEL_H_
-#define LIBMARKLIN_CONTROLMODEL_H_
-
-#include <string>
-#include <sigc++/signal.h>
-#include <msp/time/timedelta.h>
-
-namespace Marklin {
-
-struct TrainControl;
-
-/**
-Interface class for train control models.  Takes input through a uniform named
-control interface.  Provides information about train movement on output.
-*/
-class ControlModel
-{
-public:
-       sigc::signal<void, const TrainControl &> signal_control_changed;
-
-protected:
-       ControlModel() { }
-public:
-       virtual ~ControlModel() { }
-
-       virtual void set_control(const std::string &, float) = 0;
-       virtual const TrainControl &get_control(const std::string &) const = 0;
-
-       /** Returns the current speed.  Always non-negative. */
-       virtual float get_speed() const = 0;
-
-       /** Returns true if traveling in reverse. */
-       virtual bool get_reverse() const = 0;
-
-       /** Determines the distance required to come to a full stop. */
-       virtual float get_braking_distance() const = 0;
-
-       virtual void tick(const Msp::Time::TimeDelta &) = 0;
-};
-
-} // namespace Marklin
-
-#endif
diff --git a/source/libmarklin/simplecontroller.cpp b/source/libmarklin/simplecontroller.cpp
new file mode 100644 (file)
index 0000000..01d0411
--- /dev/null
@@ -0,0 +1,74 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/core/except.h>
+#include <msp/time/units.h>
+#include "simplecontroller.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace Marklin {
+
+SimpleController::SimpleController():
+       target_speed(TrainControl::continuous("speed", 0, 1000)),
+       reverse(TrainControl::binary("reverse")),
+       accel(0.07),
+       speed(0)
+{
+       target_speed.set(0);
+}
+
+void SimpleController::set_control(const string &name, float v)
+{
+       if(name=="speed")
+       {
+               target_speed.set(v);
+               signal_control_changed.emit(target_speed);
+       }
+       else if(name=="reverse")
+       {
+               if(target_speed.value || speed)
+                       throw InvalidState("Must be stopped to change reverse");
+               reverse.set(v);
+               signal_control_changed.emit(reverse);
+       }
+}
+
+const TrainControl &SimpleController::get_control(const string &name) const
+{
+       if(name=="speed")
+               return target_speed;
+       else if(name=="reverse")
+               return reverse;
+       else
+               throw KeyError("Unknown control", name);
+}
+
+float SimpleController::get_braking_distance() const
+{
+       return speed*speed/(2*accel);
+}
+
+void SimpleController::tick(const Time::TimeDelta &dt)
+{
+       float secs = dt/Time::sec;
+       if(speed<target_speed.value)
+       {
+               speed += secs*accel;
+               if(speed>target_speed.value)
+                       speed = target_speed.value;
+       }
+       else if(speed>target_speed.value)
+       {
+               speed -= secs*accel;
+               if(speed<target_speed.value)
+                       speed = target_speed.value;
+       }
+}
+
+} // namespace Marklin
diff --git a/source/libmarklin/simplecontroller.h b/source/libmarklin/simplecontroller.h
new file mode 100644 (file)
index 0000000..c86d537
--- /dev/null
@@ -0,0 +1,40 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef LIBMARKLIN_SIMPLECONTROLLER_H_
+#define LIBMARKLIN_SIMPLECONTROLLER_H_
+
+#include <string>
+#include "controller.h"
+#include "traincontrol.h"
+
+namespace Marklin {
+
+class SimpleController: public Controller
+{
+private:
+       TrainControl target_speed;
+       TrainControl reverse;
+       float accel;
+       float speed;
+
+public:
+       SimpleController();
+
+       virtual void set_control(const std::string &, float);
+       virtual const TrainControl &get_control(const std::string &) const;
+
+       virtual float get_speed() const { return speed; }
+       virtual bool get_reverse() const { return reverse.value; }
+       virtual float get_braking_distance() const;
+
+       virtual void tick(const Msp::Time::TimeDelta &);
+};
+
+} // namespace Marklin
+
+#endif
diff --git a/source/libmarklin/simplephysics.cpp b/source/libmarklin/simplephysics.cpp
deleted file mode 100644 (file)
index 3bcbd57..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
-#include <msp/core/except.h>
-#include <msp/time/units.h>
-#include "simplephysics.h"
-
-using namespace std;
-using namespace Msp;
-
-namespace Marklin {
-
-SimplePhysics::SimplePhysics():
-       target_speed(TrainControl::continuous("speed", 0, 1000)),
-       reverse(TrainControl::binary("reverse")),
-       accel(0.07),
-       speed(0)
-{
-       target_speed.set(0);
-}
-
-void SimplePhysics::set_control(const string &name, float v)
-{
-       if(name=="speed")
-       {
-               target_speed.set(v);
-               signal_control_changed.emit(target_speed);
-       }
-       else if(name=="reverse")
-       {
-               if(target_speed.value || speed)
-                       throw InvalidState("Must be stopped to change reverse");
-               reverse.set(v);
-               signal_control_changed.emit(reverse);
-       }
-}
-
-const TrainControl &SimplePhysics::get_control(const string &name) const
-{
-       if(name=="speed")
-               return target_speed;
-       else if(name=="reverse")
-               return reverse;
-       else
-               throw KeyError("Unknown control", name);
-}
-
-float SimplePhysics::get_braking_distance() const
-{
-       return speed*speed/(2*accel);
-}
-
-void SimplePhysics::tick(const Time::TimeDelta &dt)
-{
-       float secs = dt/Time::sec;
-       if(speed<target_speed.value)
-       {
-               speed += secs*accel;
-               if(speed>target_speed.value)
-                       speed = target_speed.value;
-       }
-       else if(speed>target_speed.value)
-       {
-               speed -= secs*accel;
-               if(speed<target_speed.value)
-                       speed = target_speed.value;
-       }
-}
-
-} // namespace Marklin
diff --git a/source/libmarklin/simplephysics.h b/source/libmarklin/simplephysics.h
deleted file mode 100644 (file)
index 5da6bb8..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
-#ifndef LIBMARKLIN_SIMPLEPHYSICS_H_
-#define LIBMARKLIN_SIMPLEPHYSICS_H_
-
-#include <string>
-#include "controlmodel.h"
-#include "traincontrol.h"
-
-namespace Marklin {
-
-class SimplePhysics: public ControlModel
-{
-private:
-       TrainControl target_speed;
-       TrainControl reverse;
-       float accel;
-       float speed;
-
-public:
-       SimplePhysics();
-
-       virtual void set_control(const std::string &, float);
-       virtual const TrainControl &get_control(const std::string &) const;
-
-       virtual float get_speed() const { return speed; }
-       virtual bool get_reverse() const { return reverse.value; }
-       virtual float get_braking_distance() const;
-
-       virtual void tick(const Msp::Time::TimeDelta &);
-};
-
-} // namespace Marklin
-
-#endif
index 1d65d0969791461abadd4f02f380496cd466aab1..bd9f5f0f046d5c24cd5c99eccf05ddd6b8233247 100644 (file)
@@ -90,7 +90,6 @@ void Timetable::tick(const Time::TimeStamp &t)
                {
                case GOTO:
                        train.go_to(**parse_location(row.strparam).get_tracks().begin());
-                       executing = false;
                        break;
                case TRAVEL:
                        pending_block = &parse_location(row.strparam);
@@ -100,6 +99,9 @@ void Timetable::tick(const Time::TimeStamp &t)
                        wait_timeout = t+row.intparam*Time::sec;
                        executing = false;
                        break;
+               case ARRIVE:
+                       executing = false;
+                       break;
                case SPEED:
                        train.set_control("speed", row.intparam/3.6*train.get_layout().get_catalogue().get_scale());
                        break;
@@ -128,6 +130,9 @@ void Timetable::save(list<DataFile::Statement> &st) const
                case WAIT:
                        st.push_back((DataFile::Statement("wait"), i->intparam));
                        break;
+               case ARRIVE:
+                       st.push_back(DataFile::Statement("arrive"));
+                       break;
                case SPEED:
                        st.push_back((DataFile::Statement("speed"), i->intparam));
                        break;
@@ -158,7 +163,7 @@ void Timetable::sensor_event(unsigned addr, bool state)
 void Timetable::train_arrived()
 {
        Row &row = rows[current_row];
-       if(row.type==GOTO)
+       if(row.type==ARRIVE)
        {
                current_row = (current_row+1)%rows.size();
                executing = true;
@@ -187,6 +192,8 @@ string Timetable::Row::str() const
                return "travel to "+strparam;
        case WAIT:
                return format("wait for %d seconds", intparam);
+       case ARRIVE:
+               return "wait for arrival";
        case SPEED:
                return format("set speed %d km/h", intparam);
        case ROUTE:
@@ -209,12 +216,14 @@ Timetable::Row Timetable::Row::parse(const string &s)
                        ++nondigit;
                return Row(WAIT, lexical_cast<unsigned>(s.substr(9, nondigit-9)));
        }
+       else if(s=="wait for arrival")
+               return Row(ARRIVE, 0);
        else if(!s.compare(0, 10, "set speed "))
        {
                unsigned nondigit = 11;
-               while(nondigit<s.size() && isdigit(s[nondigit]))
+               while(nondigit<s.size() && (isdigit(s[nondigit]) || s[nondigit]=='-'))
                        ++nondigit;
-               return Row(SPEED, lexical_cast<unsigned>(s.substr(10, nondigit-10)));
+               return Row(SPEED, lexical_cast<int>(s.substr(10, nondigit-10)));
        }
        else if(!s.compare(0, 10, "set route "))
                return Row(ROUTE, s.substr(10));
@@ -226,6 +235,7 @@ Timetable::Row Timetable::Row::parse(const string &s)
 Timetable::Loader::Loader(Timetable &tt):
        DataFile::ObjectLoader<Timetable>(tt)
 {
+       add("arrive", &Loader::arrive);
        add("goto",   &Loader::go_to);
        add("route",  &Loader::route);
        add("speed",  &Loader::speed);
@@ -233,6 +243,11 @@ Timetable::Loader::Loader(Timetable &tt):
        add("wait",   &Loader::wait);
 }
 
+void Timetable::Loader::arrive()
+{
+       obj.rows.push_back(Row(ARRIVE, 0));
+}
+
 void Timetable::Loader::go_to(const string &t)
 {
        obj.rows.push_back(Row(GOTO, t));
index ec4212e64c459da07895ed4c3958a9a6e67abcd0..3abbf83149e87e256bb9852a1c146a49c833c629 100644 (file)
@@ -27,6 +27,7 @@ public:
        public:
                Loader(Timetable &);
        private:
+               void arrive();
                void go_to(const std::string &);
                void route(const std::string &);
                void speed(int);
@@ -39,6 +40,7 @@ public:
                GOTO,
                TRAVEL,
                WAIT,
+               ARRIVE,
                SPEED,
                ROUTE
        };
index 93470997133a6e534736f6f6d71b256e0533bc13..920ae4ab37e678a64ae5c54a5b8953b545a3e61c 100644 (file)
@@ -14,7 +14,7 @@ Distributed under the GPL
 #include "driver.h"
 #include "layout.h"
 #include "route.h"
-#include "simplephysics.h"
+#include "simplecontroller.h"
 #include "timetable.h"
 #include "tracktype.h"
 #include "train.h"
@@ -32,7 +32,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a):
        address(a),
        priority(0),
        pending_block(0),
-       control(new AIControl(*this, new SimplePhysics)),
+       controller(new AIControl(*this, new SimpleController)),
        timetable(0),
        active(false),
        current_speed(0),
@@ -66,12 +66,12 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a):
 
        layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
 
-       control->signal_control_changed.connect(sigc::mem_fun(this, &Train::control_changed));
+       controller->signal_control_changed.connect(sigc::mem_fun(this, &Train::control_changed));
 }
 
 Train::~Train()
 {
-       delete control;
+       delete controller;
        delete timetable;
        for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
                delete *i;
@@ -130,14 +130,14 @@ const Vehicle &Train::get_vehicle(unsigned i) const
 
 void Train::set_control(const string &n, float v)
 {
-       control->set_control(n, v);
+       controller->set_control(n, v);
 }
 
 void Train::set_active(bool a)
 {
        if(a==active)
                return;
-       if(!a && control->get_speed())
+       if(!a && controller->get_speed())
                throw InvalidState("Can't deactivate while moving");
 
        active = a;
@@ -165,12 +165,12 @@ void Train::set_function(unsigned func, bool state)
 
 float Train::get_control(const string &ctrl) const
 {
-       return control->get_control(ctrl).value;
+       return controller->get_control(ctrl).value;
 }
 
 float Train::get_speed() const
 {
-       return control->get_speed();
+       return controller->get_speed();
 }
 
 bool Train::get_function(unsigned func) const
@@ -245,7 +245,7 @@ void Train::go_to(const Track &to)
 
 void Train::place(Block &block, unsigned entry)
 {
-       if(control->get_speed())
+       if(controller->get_speed())
                throw InvalidState("Must be stopped before placing");
 
        release_blocks(rsv_blocks);
@@ -366,13 +366,13 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
 
        if(timetable)
                timetable->tick(t);
-       control->tick(dt);
-       float speed = control->get_speed();
+       controller->tick(dt);
+       float speed = controller->get_speed();
        unsigned speed_notch = find_speed(speed);
 
-       if(control->get_reverse()!=reverse)
+       if(controller->get_reverse()!=reverse)
        {
-               reverse = control->get_reverse();
+               reverse = controller->get_reverse();
                driver.set_loco_reverse(address, reverse);
 
                release_blocks(rsv_blocks);
index f9ea6f17ae0001b39432e8cd83b57ac8fb4cf9ce..e4458be82d7bfb47bf7a853b57e1ef6f8cb73298 100644 (file)
@@ -15,7 +15,7 @@ Distributed under the GPL
 
 namespace Marklin {
 
-class ControlModel;
+class Controller;
 class Route;
 class Timetable;
 class Vehicle;
@@ -79,7 +79,7 @@ private:
        std::list<BlockRef> cur_blocks;
        std::list<BlockRef> rsv_blocks;
        Block *pending_block;
-       ControlModel *control;
+       Controller *controller;
        Timetable *timetable;
        bool active;
        unsigned current_speed;
@@ -110,7 +110,7 @@ public:
        const std::string &get_name() const { return name; }
        void set_priority(int);
        int get_priority() const { return priority; }
-       ControlModel &get_control_model() const { return *control; }
+       Controller &get_controller() const { return *controller; }
 
        void add_vehicle(const VehicleType &);
        void remove_vehicle(unsigned);