From: Mikko Rasa Date: Mon, 12 Apr 2010 09:56:02 +0000 (+0000) Subject: Handle vehicle positioning while going backwards X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e94014530a6e28a42bc5678e579ee07e0ee5329b;p=r2c2.git Handle vehicle positioning while going backwards More sensible initial vehicle placement Activate occupied sensors while loading trains --- diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index a877174..f057992 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -119,8 +119,6 @@ void Train::set_reverse(bool rev) release_blocks(rsv_blocks); reverse_blocks(cur_blocks); - - // XXX Do something about the vehicles } void Train::set_function(unsigned func, bool state) @@ -213,7 +211,19 @@ void Train::place(Block &block, unsigned entry) } cur_blocks.push_back(BlockRef(&block, entry)); - set_position(block.get_endpoints()[entry]); + if(reverse) + { + unsigned exit = block.traverse(entry); + const Block::Endpoint &bep = block.get_endpoints()[exit]; + Track *track = bep.track->get_link(bep.track_ep); + unsigned ep = track->get_endpoint_by_link(*bep.track); + vehicles.front()->place(track, ep, 0, Vehicle::FRONT_BUFFER); + } + else + { + const Block::Endpoint &bep = block.get_endpoints()[entry]; + vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER); + } set_status("Stopped"); } @@ -267,7 +277,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) ok = i->block->get_tracks().count(track); if(ok) - vehicles[0]->advance(get_real_speed(current_speed)*(dt/Time::sec)); + { + float d = get_real_speed(current_speed)*(dt/Time::sec); + vehicles[0]->advance(reverse ? -d : d); + } } } @@ -352,7 +365,17 @@ void Train::sensor_event(unsigned addr, bool state) travel_dist += block_len; if(j->block->get_sensor_id()==addr) - set_position(j->block->get_endpoints()[j->entry]); + { + const Block::Endpoint &bep = j->block->get_endpoints()[j->entry]; + if(reverse) + { + Track *track = bep.track->get_link(bep.track_ep); + unsigned ep = track->get_endpoint_by_link(*bep.track); + vehicles.back()->place(track, ep, 0, Vehicle::BACK_AXLE); + } + else + vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE); + } } last_entry_time = Time::now(); pure_speed = true; @@ -662,11 +685,6 @@ void Train::set_status(const string &s) signal_status_changed.emit(s); } -void Train::set_position(const Block::Endpoint &bep) -{ - vehicles[0]->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE); -} - void Train::release_blocks(list &blocks) { release_blocks(blocks, blocks.begin(), blocks.end()); @@ -744,7 +762,11 @@ void Train::Loader::block(unsigned id) blk.reserve(&obj); obj.cur_blocks.push_back(BlockRef(&blk, entry)); obj.set_status("Stopped"); - obj.set_position(blk.get_endpoints()[entry]); + const Block::Endpoint &bep = blk.get_endpoints()[entry]; + obj.vehicles.back()->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); prev_block = &blk; } diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index f9dba5d..5a20bd0 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -133,7 +133,6 @@ private: float get_real_speed(unsigned) const; unsigned find_speed(float) const; void set_status(const std::string &); - void set_position(const Block::Endpoint &); void release_blocks(std::list &); void release_blocks(std::list &, std::list::iterator, std::list::iterator); void reverse_blocks(std::list &) const; diff --git a/source/libmarklin/vehicle.cpp b/source/libmarklin/vehicle.cpp index 7c299c7..6011537 100644 --- a/source/libmarklin/vehicle.cpp +++ b/source/libmarklin/vehicle.cpp @@ -35,6 +35,7 @@ Vehicle::~Vehicle() void Vehicle::place(Track *t, unsigned e, float o, PlaceMode m) { track_pos = TrackPosition(t, e, o); + if(m==FRONT_AXLE) { float front = type.get_length()/2; @@ -47,6 +48,23 @@ void Vehicle::place(Track *t, unsigned e, float o, PlaceMode m) } track_pos.advance(-front); } + else if(m==FRONT_BUFFER) + track_pos.advance(-type.get_length()/2); + else if(m==BACK_AXLE) + { + float back = type.get_length()/2; + if(!type.get_axles().empty()) + back = type.get_axles().back().position; + if(!type.get_bogies().empty()) + { + const VehicleType::Bogie &bogie = type.get_bogies().back(); + back = min(back, bogie.position+bogie.axles.back().position); + } + track_pos.advance(-back); + } + else if(m==BACK_BUFFER) + track_pos.advance(type.get_length()/2); + update_position(); } diff --git a/source/libmarklin/vehicle.h b/source/libmarklin/vehicle.h index dcf7737..d24bebe 100644 --- a/source/libmarklin/vehicle.h +++ b/source/libmarklin/vehicle.h @@ -22,7 +22,10 @@ public: enum PlaceMode { CENTER, - FRONT_AXLE + FRONT_AXLE, + FRONT_BUFFER, + BACK_AXLE, + BACK_BUFFER }; private: