X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=44f9298d142daf72682594148476699a0ce89554;hb=3b3099eb9e6d14e2a0495ea78144a734a102112f;hp=fad68605e7c70952faf21ac6931616a7114a7f70;hpb=6968273080fa2a1cbcfc506610d5f249299611e9;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index fad6860..44f9298 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -44,7 +44,9 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): status("Unplaced"), travel_dist(0), pure_speed(false), - real_speed(15) + real_speed(15), + accurate_position(false), + overshoot_dist(false) { vehicles.push_back(new Vehicle(layout, loco_type)); @@ -58,11 +60,15 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Train::sensor_event)); layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Train::turnout_event)); + layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event)); + control->signal_control_changed.connect(signal_control_changed); } Train::~Train() { + delete control; + delete timetable; for(vector::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) delete *i; layout.remove_train(*this); @@ -198,6 +204,7 @@ void Train::place(Block &block, unsigned entry) release_blocks(cur_blocks); set_active(false); + accurate_position = false; if(!block.reserve(this)) { @@ -257,6 +264,8 @@ float Train::get_reserved_distance() const const VehicleType &vtype = veh.get_type(); Track *track = veh.get_track(); + if(!track) + return 0; unsigned entry = veh.get_entry(); float result = -vtype.get_length()/2; @@ -276,6 +285,9 @@ float Train::get_reserved_distance() const result += track->get_type().get_path_length(track->get_active_path()); first = false; + if(track->get_type().get_endpoints().size()<2) + return result; + unsigned exit = track->traverse(entry); Track *next = track->get_link(exit); @@ -302,6 +314,8 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) stop_timeout = Time::TimeStamp(); } + Driver &driver = layout.get_driver(); + if(timetable) timetable->tick(t); control->tick(dt); @@ -310,7 +324,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) if(speed && (speed<0)!=reverse) { - layout.get_driver().set_loco_reverse(address, speed<0); + driver.set_loco_reverse(address, speed<0); reverse = speed<0; release_blocks(rsv_blocks); @@ -318,10 +332,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) reserve_more(); } - if(speed_notch!=current_speed && !speed_changing) + if(speed_notch!=current_speed && !speed_changing && !driver.is_halted() && driver.get_power()) { speed_changing = true; - layout.get_driver().set_loco_speed(address, speed_notch); + driver.set_loco_speed(address, speed_notch); pure_speed = false; @@ -343,19 +357,77 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) for(list::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i) ok = i->block->get_tracks().count(track); + float d = get_real_speed(current_speed)*(dt/Time::sec); if(ok) - { - float d = get_real_speed(current_speed)*(dt/Time::sec); vehicle.advance(reverse ? -d : d); + else if(accurate_position) + { + overshoot_dist += d; + if(overshoot_dist>40*layout.get_catalogue().get_scale()) + { + layout.emergency(name+" has not arrived at sensor"); + accurate_position = false; + } } } else if(end_of_route) set_route(0); + + if(!cur_blocks.empty() && !cur_blocks.front().block->get_sensor_id()) + { + Vehicle &veh = *(reverse ? vehicles.front() : vehicles.back()); + + list::iterator i = cur_blocks.begin(); + const Block::Endpoint &bep = i->block->get_endpoints()[i->entry]; + + Track *track = bep.track; + unsigned entry = bep.track_ep; + + bool found = false; + float dist = veh.get_offset()-veh.get_type().get_length()/2; + while(1) + { + if(track==veh.get_track()) + { + found = true; + break; + } + + if(i!=cur_blocks.begin()) + { + float path_len = track->get_type().get_path_length(track->get_active_path()); + dist += path_len; + } + + unsigned exit = track->traverse(entry); + Track *next = track->get_link(exit); + entry = next->get_endpoint_by_link(*track); + track = next; + + if(!i->block->get_tracks().count(track)) + { + ++i; + if(i==cur_blocks.end()) + break; + } + } + + if(found && i!=cur_blocks.begin() && dist>10*layout.get_catalogue().get_scale()) + { + cur_blocks.front().block->reserve(0); + cur_blocks.erase(cur_blocks.begin()); + } + } } void Train::save(list &st) const { st.push_back((DataFile::Statement("name"), name)); + + for(vector::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i) + if(i!=vehicles.begin()) + st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number())); + for(unsigned i=0; i<=14; ++i) if(real_speed[i].weight) st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight)); @@ -374,7 +446,12 @@ void Train::save(list &st) const } if(route) - st.push_back((DataFile::Statement("route"), route->get_name())); + { + if(!route->is_temporary()) + st.push_back((DataFile::Statement("route"), route->get_name())); + else if(next_route && !next_route->is_temporary()) + st.push_back((DataFile::Statement("route"), next_route->get_name())); + } if(timetable) { @@ -390,6 +467,7 @@ void Train::loco_speed_event(unsigned addr, unsigned speed, bool) { current_speed = speed; speed_changing = false; + pure_speed = false; } } @@ -414,11 +492,24 @@ void Train::sensor_event(unsigned addr, bool state) { // Find the first sensor block from our reserved blocks that isn't this sensor list::iterator i; + unsigned result = 0; for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i) - if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr) - break; + if(i->block->get_sensor_id()) + { + if(i->block->get_sensor_id()!=addr) + { + if(result==0) + result = 2; + else if(result==1) + break; + } + else if(result==0) + result = 1; + else if(result==2) + result = 3; + } - if(i!=rsv_blocks.begin()) + if(result==1 && i!=rsv_blocks.begin()) { // Compute speed and update related state float travel_time_secs = (Time::now()-last_entry_time)/Time::sec; @@ -452,6 +543,8 @@ void Train::sensor_event(unsigned addr, bool state) } last_entry_time = Time::now(); pure_speed = true; + accurate_position = true; + overshoot_dist = 0; // Check if we've reached the next route if(next_route) @@ -479,6 +572,8 @@ void Train::sensor_event(unsigned addr, bool state) signal_arrived.emit(); } } + else if(result==3) + layout.emergency("Sensor for "+name+" triggered out of order"); } else { @@ -490,15 +585,15 @@ void Train::sensor_event(unsigned addr, bool state) if(layout.get_driver().get_sensor(i->block->get_sensor_id())) break; else + { end = i; + ++end; + } } if(end!=cur_blocks.begin()) - { // Free blocks up to the last inactive sensor - ++end; release_blocks(cur_blocks, cur_blocks.begin(), end); - } } } @@ -513,6 +608,12 @@ void Train::turnout_event(unsigned addr, bool) } } +void Train::halt_event(bool h) +{ + if(h) + accurate_position = false; +} + void Train::block_reserved(const Block &block, const Train *train) { if(&block==pending_block && !train) @@ -558,6 +659,13 @@ unsigned Train::reserve_more() unsigned good_sens = nsens; while(good_sens<3) { + if(last->block->get_endpoints().size()<2) + { + good = last; + good_sens = nsens; + break; + } + // Traverse to the next block unsigned exit = last->block->traverse(last->entry); Block *link = last->block->get_link(exit); @@ -650,6 +758,12 @@ unsigned Train::reserve_more() last = &rsv_blocks.back(); } + // Make any sensorless blocks at the beginning immediately current + list::iterator i; + for(i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !i->block->get_sensor_id()); ++i) ; + if(i!=rsv_blocks.begin()) + cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i); + return good_sens; } @@ -802,7 +916,7 @@ void Train::Loader::block(unsigned id) obj.cur_blocks.push_back(BlockRef(&blk, entry)); obj.set_status("Stopped"); const Block::Endpoint &bep = blk.get_endpoints()[entry]; - obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER); + obj.vehicles.front()->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);