From 2571c111ec85b0d6a56ae369c83b5763b1975f93 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 27 Sep 2010 20:27:45 +0000 Subject: [PATCH] Rename ControlModel to Controller Rename SimplePhysics to SimpleController --- source/libmarklin/aicontrol.cpp | 2 +- source/libmarklin/aicontrol.h | 8 +++--- .../{controlmodel.h => controller.h} | 12 ++++----- ...simplephysics.cpp => simplecontroller.cpp} | 12 ++++----- .../{simplephysics.h => simplecontroller.h} | 10 +++---- source/libmarklin/timetable.cpp | 23 +++++++++++++--- source/libmarklin/timetable.h | 2 ++ source/libmarklin/train.cpp | 26 +++++++++---------- source/libmarklin/train.h | 6 ++--- 9 files changed, 59 insertions(+), 42 deletions(-) rename source/libmarklin/{controlmodel.h => controller.h} (81%) rename source/libmarklin/{simplephysics.cpp => simplecontroller.cpp} (78%) rename source/libmarklin/{simplephysics.h => simplecontroller.h} (80%) diff --git a/source/libmarklin/aicontrol.cpp b/source/libmarklin/aicontrol.cpp index eefc7f8..8876724 100644 --- a/source/libmarklin/aicontrol.cpp +++ b/source/libmarklin/aicontrol.cpp @@ -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)), diff --git a/source/libmarklin/aicontrol.h b/source/libmarklin/aicontrol.h index bcd6cbb..90906ca 100644 --- a/source/libmarklin/aicontrol.h +++ b/source/libmarklin/aicontrol.h @@ -9,24 +9,24 @@ Distributed under the GPL #define LIBMARKLIN_AICONTROL_H_ #include -#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/controlmodel.h b/source/libmarklin/controller.h similarity index 81% rename from source/libmarklin/controlmodel.h rename to source/libmarklin/controller.h index 3032f66..b807548 100644 --- a/source/libmarklin/controlmodel.h +++ b/source/libmarklin/controller.h @@ -5,8 +5,8 @@ Copyright © 2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ -#ifndef LIBMARKLIN_CONTROLMODEL_H_ -#define LIBMARKLIN_CONTROLMODEL_H_ +#ifndef LIBMARKLIN_CONTROLLER_H_ +#define LIBMARKLIN_CONTROLLER_H_ #include #include @@ -17,18 +17,18 @@ namespace Marklin { struct TrainControl; /** -Interface class for train control models. Takes input through a uniform named +Interface class for train controllers. Takes input through a uniform named control interface. Provides information about train movement on output. */ -class ControlModel +class Controller { public: sigc::signal signal_control_changed; protected: - ControlModel() { } + Controller() { } public: - virtual ~ControlModel() { } + virtual ~Controller() { } virtual void set_control(const std::string &, float) = 0; virtual const TrainControl &get_control(const std::string &) const = 0; diff --git a/source/libmarklin/simplephysics.cpp b/source/libmarklin/simplecontroller.cpp similarity index 78% rename from source/libmarklin/simplephysics.cpp rename to source/libmarklin/simplecontroller.cpp index 3bcbd57..01d0411 100644 --- a/source/libmarklin/simplephysics.cpp +++ b/source/libmarklin/simplecontroller.cpp @@ -7,14 +7,14 @@ Distributed under the GPL #include #include -#include "simplephysics.h" +#include "simplecontroller.h" using namespace std; using namespace Msp; namespace Marklin { -SimplePhysics::SimplePhysics(): +SimpleController::SimpleController(): target_speed(TrainControl::continuous("speed", 0, 1000)), reverse(TrainControl::binary("reverse")), accel(0.07), @@ -23,7 +23,7 @@ SimplePhysics::SimplePhysics(): target_speed.set(0); } -void SimplePhysics::set_control(const string &name, float v) +void SimpleController::set_control(const string &name, float v) { if(name=="speed") { @@ -39,7 +39,7 @@ void SimplePhysics::set_control(const string &name, float v) } } -const TrainControl &SimplePhysics::get_control(const string &name) const +const TrainControl &SimpleController::get_control(const string &name) const { if(name=="speed") return target_speed; @@ -49,12 +49,12 @@ const TrainControl &SimplePhysics::get_control(const string &name) const throw KeyError("Unknown control", name); } -float SimplePhysics::get_braking_distance() const +float SimpleController::get_braking_distance() const { return speed*speed/(2*accel); } -void SimplePhysics::tick(const Time::TimeDelta &dt) +void SimpleController::tick(const Time::TimeDelta &dt) { float secs = dt/Time::sec; if(speed -#include "controlmodel.h" +#include "controller.h" #include "traincontrol.h" namespace Marklin { -class SimplePhysics: public ControlModel +class SimpleController: public Controller { private: TrainControl target_speed; @@ -23,7 +23,7 @@ private: float speed; public: - SimplePhysics(); + SimpleController(); virtual void set_control(const std::string &, float); virtual const TrainControl &get_control(const std::string &) const; diff --git a/source/libmarklin/timetable.cpp b/source/libmarklin/timetable.cpp index 1d65d09..bd9f5f0 100644 --- a/source/libmarklin/timetable.cpp +++ b/source/libmarklin/timetable.cpp @@ -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 &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(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.substr(10, nondigit-10))); + return Row(SPEED, lexical_cast(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(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)); diff --git a/source/libmarklin/timetable.h b/source/libmarklin/timetable.h index ec4212e..3abbf83 100644 --- a/source/libmarklin/timetable.h +++ b/source/libmarklin/timetable.h @@ -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 }; diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 9347099..920ae4a 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -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::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); diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index f9ea6f1..e4458be 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -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 cur_blocks; std::list 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); -- 2.45.2