From: Mikko Rasa Date: Mon, 27 Sep 2010 20:27:45 +0000 (+0000) Subject: Rename ControlModel to Controller X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2571c111ec85b0d6a56ae369c83b5763b1975f93;p=r2c2.git Rename ControlModel to Controller Rename SimplePhysics to SimpleController --- 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/controller.h b/source/libmarklin/controller.h new file mode 100644 index 0000000..b807548 --- /dev/null +++ b/source/libmarklin/controller.h @@ -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 +#include +#include + +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 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 index 3032f66..0000000 --- a/source/libmarklin/controlmodel.h +++ /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 -#include -#include - -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 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 index 0000000..01d0411 --- /dev/null +++ b/source/libmarklin/simplecontroller.cpp @@ -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 +#include +#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(speedtarget_speed.value) + speed = target_speed.value; + } + else if(speed>target_speed.value) + { + speed -= secs*accel; + if(speed +#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 index 3bcbd57..0000000 --- a/source/libmarklin/simplephysics.cpp +++ /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 -#include -#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(speedtarget_speed.value) - speed = target_speed.value; - } - else if(speed>target_speed.value) - { - speed -= secs*accel; - if(speed -#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 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);