3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/strings/formatter.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
15 #include "tracktype.h"
16 #include "trafficmanager.h"
24 Train::Train(TrafficManager &tm, Locomotive &l):
37 trfc_mgr.add_train(this);
39 loco.signal_reverse_changed.connect(sigc::mem_fun(this, &Train::locomotive_reverse_changed));
41 const map<unsigned, Sensor *> &sensors = trfc_mgr.get_control().get_sensors();
42 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
43 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
45 const map<unsigned, Turnout *> &turnouts = trfc_mgr.get_control().get_turnouts();
46 for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
48 i->second->signal_path_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changing), i->second));
49 i->second->signal_path_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changed), i->second));
53 void Train::set_name(const string &n)
57 signal_name_changed.emit(name);
60 void Train::set_speed(unsigned speed)
62 if(speed==target_speed)
64 travel_speed = static_cast<int>(round(get_real_speed(speed)*87*3.6/5))*5;
69 trfc_mgr.get_control().set_timer(3*Time::sec).signal_timeout.connect(
70 sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Train::release_blocks), sigc::ref(rsv_blocks)), false));
75 signal_target_speed_changed.emit(target_speed);
81 void Train::set_reverse(bool rev)
83 loco.set_reverse(rev);
86 void Train::set_route(const Route *r)
89 signal_route_changed.emit(route);
92 void Train::place(Block &block, unsigned entry)
97 release_blocks(rsv_blocks);
98 release_blocks(cur_blocks);
100 if(!block.reserve(this))
102 set_status("Unplaced");
106 cur_blocks.push_back(BlockRef(&block, entry));
107 set_position(block.get_endpoints()[entry]);
109 set_status("Stopped");
112 bool Train::free_block(Block &block)
115 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
121 for(list<BlockRef>::iterator j=i; j!=rsv_blocks.end(); ++j)
122 j->block->reserve(0);
123 rsv_blocks.erase(i, rsv_blocks.end());
127 else if(i->block->get_sensor_id())
134 int Train::get_entry_to_block(Block &block) const
136 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
139 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
145 void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
147 if(try_reserve && t>try_reserve)
153 if(cur_track->get_turnout_id())
154 path = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_path();
156 offset += get_real_speed(loco.get_speed())*(dt/Time::sec);
157 if(offset>cur_track->get_type().get_path_length(path))
159 int out = cur_track->traverse(cur_track_ep, path);
162 Track *next = cur_track->get_link(out);
164 cur_track_ep = next->get_endpoint_by_link(*cur_track);
173 pos = cur_track->get_point(cur_track_ep, path, offset);
177 void Train::save(list<DataFile::Statement> &st) const
179 st.push_back((DataFile::Statement("name"), name));
180 for(unsigned i=0; i<=14; ++i)
181 if(real_speed[i].weight)
182 st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
185 void Train::locomotive_reverse_changed(bool)
187 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
188 i->block->reserve(0);
190 cur_blocks.reverse();
191 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
192 i->entry = i->block->traverse(i->entry);
198 if(unsigned turnout = cur_track->get_turnout_id())
199 path = trfc_mgr.get_control().get_turnout(turnout).get_path();
200 cur_track_ep = cur_track->traverse(cur_track_ep, path);
201 offset = cur_track->get_type().get_path_length(path)-offset;
205 void Train::sensor_event(bool state, Sensor *sensor)
207 unsigned addr = sensor->get_address();
211 list<BlockRef>::iterator i;
212 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
213 if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
216 if(i!=rsv_blocks.begin())
218 float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
219 travel_speed = static_cast<int>(round(travel_dist/travel_time_secs*87*3.6/5))*5;
223 RealSpeed &rs = real_speed[loco.get_speed()];
224 rs.add(travel_dist/travel_time_secs, travel_time_secs);
229 for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
231 j->block->traverse(j->entry, &block_len);
232 travel_dist += block_len;
234 last_entry_time = Time::now();
237 cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
240 for(i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
241 if(i->block->get_sensor_id()==addr)
242 set_position(i->block->get_endpoints()[i->entry]);
244 if(target_speed && reserve_more()<2)
249 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
250 if(unsigned b_addr = i->block->get_sensor_id())
255 for(list<BlockRef>::iterator j=cur_blocks.begin(); j!=i; ++j)
256 j->block->reserve(0);
257 cur_blocks.erase(cur_blocks.begin(), i);
262 if(target_speed && pending_block && addr==pending_block->get_sensor_id())
267 void Train::turnout_path_changing(unsigned, Turnout *turnout)
269 unsigned tid = turnout->get_address();
270 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
271 if(i->block->get_turnout_id()==tid)
272 throw TurnoutBusy(this);
275 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
277 if(i->block->get_turnout_id()==tid)
280 throw TurnoutBusy(this);
283 else if(i->block->get_sensor_id())
288 void Train::turnout_path_changed(unsigned, Turnout *turnout)
290 unsigned tid = turnout->get_address();
291 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
292 if(i->block->get_turnout_id()==tid)
294 while(i!=rsv_blocks.end())
296 i->block->reserve(0);
297 i = rsv_blocks.erase(i);
303 if(pending_block && tid==pending_block->get_turnout_id())
307 unsigned Train::reserve_more()
310 if(!rsv_blocks.empty())
311 last = &rsv_blocks.back();
312 else if(!cur_blocks.empty())
313 last = &cur_blocks.back();
319 // See how many blocks we already have
321 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
322 if(i->block->get_sensor_id())
325 bool got_more = false;
326 BlockRef *good = last;
327 unsigned good_sens = nsens;
330 // Traverse to the next block
331 unsigned exit = last->block->traverse(last->entry);
332 Block *link = last->block->get_link(exit);
336 int entry = link->get_endpoint_by_link(*last->block);
337 if(!link->reserve(this))
339 // If we found another train going in the same direction as us, we can keep the blocks we got
340 int other_entry = link->get_train()->get_entry_to_block(*link);
341 if(other_entry==entry || link->traverse(entry)==link->traverse(other_entry))
346 pending_block = link;
350 if(link->get_turnout_id())
352 const Block::Endpoint &ep = link->get_endpoints()[entry];
353 const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep];
355 if(track_ep.paths&(track_ep.paths-1))
357 // We're facing the points - keep the blocks reserved so far
362 Turnout &turnout = trfc_mgr.get_control().get_turnout(link->get_turnout_id());
364 // Figure out what path we'd like to take on the turnout
367 path = route->get_turnout(link->get_turnout_id());
369 path = turnout.get_path();
370 if(!((track_ep.paths>>path)&1))
372 for(unsigned i=0; track_ep.paths>>i; ++i)
373 if((track_ep.paths>>i)&1)
377 if(path!=turnout.get_path())
379 // The turnout is set to wrong path - switch and wait for it
381 pending_block = link;
382 turnout.set_path(path);
387 rsv_blocks.push_back(BlockRef(link, entry));
388 last = &rsv_blocks.back();
389 if(last->block->get_sensor_id())
396 // Unreserve blocks that were not good
397 while(!rsv_blocks.empty() && last!=good)
399 last->block->reserve(0);
400 rsv_blocks.erase(--rsv_blocks.end());
401 if(!rsv_blocks.empty())
402 last = &rsv_blocks.back();
411 void Train::update_speed()
416 try_reserve = Time::TimeStamp();
417 set_status("Stopped");
422 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
423 if(i->block->get_sensor_id())
426 unsigned slow_speed = find_speed(0.1); // 31.3 km/h
431 try_reserve = Time::now()+2*Time::sec;
432 set_status("Blocked");
434 else if(nsens==1 && target_speed>slow_speed)
436 loco.set_speed(slow_speed);
438 try_reserve = Time::now()+2*Time::sec;
443 loco.set_speed(target_speed);
444 try_reserve = Time::TimeStamp();
445 set_status(format("Traveling %d kmh", travel_speed));
450 float Train::get_real_speed(unsigned i) const
452 if(real_speed[i].weight)
453 return real_speed[i].speed;
457 for(low=i; low>0; --low)
458 if(real_speed[low].weight)
460 for(high=i; high<14; ++high)
461 if(real_speed[high].weight)
464 if(real_speed[high].weight)
466 if(real_speed[low].weight)
468 float f = float(i-low)/(high-low);
469 return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
472 return real_speed[high].speed*float(i)/high;
474 else if(real_speed[low].weight)
475 return real_speed[low].speed*float(i)/low;
480 unsigned Train::find_speed(float real) const
482 if(real<=real_speed[0].speed)
487 for(unsigned i=0; (!high && i<=14); ++i)
488 if(real_speed[i].weight)
490 if(real_speed[i].speed<real)
499 return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
502 float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
503 return static_cast<unsigned>(low*(1-f)+high*f+0.5);
506 void Train::set_status(const string &s)
509 signal_status_changed.emit(s);
512 void Train::set_position(const Block::Endpoint &bep)
514 cur_track = bep.track;
515 cur_track_ep = bep.track_ep;
517 pos = cur_track->get_endpoint_position(cur_track_ep);
520 void Train::release_blocks(list<BlockRef> &blocks)
522 for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
523 i->block->reserve(0);
528 Train::RealSpeed::RealSpeed():
533 void Train::RealSpeed::add(float s, float w)
535 speed = (speed*weight+s*w)/(weight+w);
536 weight = min(weight+w, 300.0f);
540 Train::Loader::Loader(Train &t):
541 DataFile::BasicLoader<Train>(t)
543 add("name", &Train::name);
544 add("real_speed", &Loader::real_speed);
547 void Train::Loader::real_speed(unsigned i, float speed, float weight)
549 obj.real_speed[i].speed = speed;
550 obj.real_speed[i].weight = weight;
553 } // namespace Marklin