X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=d87d3768a7e0a482db808d934cd5f5211f389197;hb=59e4c75da550e96ae74162a5ed70e8b1999721d8;hp=c53f30f66e6de0f05cca61da9b24f20486579e13;hpb=39d3db437ae419bf8195d33422d5ee38962175aa;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index c53f30f..d87d376 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(signal_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; @@ -163,6 +163,16 @@ void Train::set_function(unsigned func, bool state) layout.get_driver().set_loco_function(address+1, func-4, state); } +float Train::get_control(const string &ctrl) const +{ + return controller->get_control(ctrl).value; +} + +float Train::get_speed() const +{ + return controller->get_speed(); +} + bool Train::get_function(unsigned func) const { return (functions>>func)&1; @@ -235,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); @@ -356,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(); - unsigned speed_notch = find_speed(abs(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); @@ -408,8 +418,11 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) } } } - else if(end_of_route) + else if(end_of_route && rsv_blocks.empty()) + { + signal_arrived.emit(); set_route(0); + } if(!cur_blocks.empty() && !cur_blocks.front().block->get_sensor_id()) { @@ -501,6 +514,11 @@ void Train::save(list &st) const } } +void Train::control_changed(const Controller::Control &ctrl) +{ + signal_control_changed.emit(ctrl.name, ctrl.value); +} + void Train::loco_speed_event(unsigned addr, unsigned speed, bool) { if(addr==address) @@ -609,11 +627,7 @@ void Train::sensor_event(unsigned addr, bool state) // Try to get more blocks if we're moving if(active) - { - unsigned nsens = reserve_more(); - if(!nsens && end_of_route) - signal_arrived.emit(); - } + reserve_more(); } else if(result==3) layout.emergency("Sensor for "+name+" triggered out of order");