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>
16 #include "tracktype.h"
17 #include "trafficmanager.h"
25 Train::Train(TrafficManager &tm, Locomotive &l):
38 trfc_mgr.add_train(this);
40 loco.signal_reverse_changed.connect(sigc::mem_fun(this, &Train::locomotive_reverse_changed));
42 const map<unsigned, Sensor *> &sensors = trfc_mgr.get_control().get_sensors();
43 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
44 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
46 const map<unsigned, Turnout *> &turnouts = trfc_mgr.get_control().get_turnouts();
47 for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
49 i->second->signal_path_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changing), i->second));
50 i->second->signal_path_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changed), i->second));
54 void Train::set_name(const string &n)
58 signal_name_changed.emit(name);
61 void Train::set_speed(unsigned speed)
63 if(speed==target_speed)
65 travel_speed = static_cast<int>(round(get_real_speed(speed)*87*3.6/5))*5;
70 trfc_mgr.get_control().set_timer(3*Time::sec).signal_timeout.connect(
71 sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Train::release_blocks), sigc::ref(rsv_blocks)), false));
76 signal_target_speed_changed.emit(target_speed);
82 void Train::set_reverse(bool rev)
84 loco.set_reverse(rev);
87 void Train::set_route(const Route *r)
90 signal_route_changed.emit(route);
93 void Train::place(Block &block, unsigned entry)
98 release_blocks(rsv_blocks);
99 release_blocks(cur_blocks);
101 if(!block.reserve(this))
103 set_status("Unplaced");
107 cur_blocks.push_back(BlockRef(&block, entry));
108 set_position(block.get_endpoints()[entry]);
110 set_status("Stopped");
113 bool Train::free_block(Block &block)
116 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
122 for(list<BlockRef>::iterator j=i; j!=rsv_blocks.end(); ++j)
123 j->block->reserve(0);
124 rsv_blocks.erase(i, rsv_blocks.end());
128 else if(i->block->get_sensor_id())
135 int Train::get_entry_to_block(Block &block) const
137 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
140 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
146 void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
148 if(try_reserve && t>try_reserve)
154 if(cur_track->get_turnout_id())
155 path = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_path();
157 offset += get_real_speed(loco.get_speed())*(dt/Time::sec);
158 float path_len = cur_track->get_type().get_path_length(path);
161 unsigned out = cur_track->traverse(cur_track_ep, path);
162 Track *next = cur_track->get_link(out);
165 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
166 ok = i->block->get_tracks().count(next);
171 cur_track_ep = next->get_endpoint_by_link(*cur_track);
176 offset = path_len-0.001;
180 pos = cur_track->get_point(cur_track_ep, path, offset);
184 void Train::save(list<DataFile::Statement> &st) const
186 st.push_back((DataFile::Statement("name"), name));
187 for(unsigned i=0; i<=14; ++i)
188 if(real_speed[i].weight)
189 st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
191 st.push_back((DataFile::Statement("route"), route->get_name()));
193 if(!cur_blocks.empty())
195 list<BlockRef> blocks = cur_blocks;
196 if(loco.get_reverse())
197 reverse_blocks(blocks);
199 Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
200 st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
202 for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
203 st.push_back((DataFile::Statement("block"), i->block->get_id()));
207 void Train::locomotive_reverse_changed(bool)
209 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
210 i->block->reserve(0);
212 reverse_blocks(cur_blocks);
218 if(unsigned turnout = cur_track->get_turnout_id())
219 path = trfc_mgr.get_control().get_turnout(turnout).get_path();
220 cur_track_ep = cur_track->traverse(cur_track_ep, path);
221 offset = cur_track->get_type().get_path_length(path)-offset;
225 void Train::sensor_event(bool state, Sensor *sensor)
227 unsigned addr = sensor->get_address();
231 // Find the first sensor block from our reserved blocks that isn't this sensor
232 list<BlockRef>::iterator i;
233 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
234 if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
237 if(i!=rsv_blocks.begin())
239 // Compute speed and update related state
240 float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
241 travel_speed = static_cast<int>(round(travel_dist/travel_time_secs*87*3.6/5))*5;
245 RealSpeed &rs = real_speed[loco.get_speed()];
246 rs.add(travel_dist/travel_time_secs, travel_time_secs);
251 for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
253 j->block->traverse(j->entry, &block_len);
254 travel_dist += block_len;
256 if(j->block->get_sensor_id()==addr)
257 set_position(j->block->get_endpoints()[j->entry]);
259 last_entry_time = Time::now();
262 // Move blocks up to the next sensor to our current blocks
263 cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
265 // Try to get more blocks if we're moving
266 if(target_speed && reserve_more()<2)
272 // Find the first sensor in our current blocks that's still active
273 list<BlockRef>::iterator end = cur_blocks.begin();
274 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
275 if(i->block->get_sensor_id())
277 if(trfc_mgr.get_control().get_sensor(i->block->get_sensor_id()).get_state())
283 if(end!=cur_blocks.begin())
285 // Free blocks up to the last inactive sensor
287 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=end; ++i)
288 i->block->reserve(0);
289 cur_blocks.erase(cur_blocks.begin(), end);
292 // XXX Should watch for trfc_mgr.signal_block_reserved rather than sensors
293 if(target_speed && pending_block && addr==pending_block->get_sensor_id())
298 void Train::turnout_path_changing(unsigned, Turnout *turnout)
300 unsigned tid = turnout->get_address();
301 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
302 if(i->block->get_turnout_id()==tid)
303 throw TurnoutBusy(this);
306 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
308 if(i->block->get_turnout_id()==tid)
311 throw TurnoutBusy(this);
314 else if(i->block->get_sensor_id())
319 void Train::turnout_path_changed(unsigned, Turnout *turnout)
321 unsigned tid = turnout->get_address();
322 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
323 if(i->block->get_turnout_id()==tid)
325 while(i!=rsv_blocks.end())
327 i->block->reserve(0);
328 i = rsv_blocks.erase(i);
334 if(pending_block && tid==pending_block->get_turnout_id())
338 unsigned Train::reserve_more()
341 if(!rsv_blocks.empty())
342 last = &rsv_blocks.back();
343 else if(!cur_blocks.empty())
344 last = &cur_blocks.back();
350 // See how many blocks we already have
352 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
353 if(i->block->get_sensor_id())
356 bool got_more = false;
357 BlockRef *good = last;
358 unsigned good_sens = nsens;
361 // Traverse to the next block
362 unsigned exit = last->block->traverse(last->entry);
363 Block *link = last->block->get_link(exit);
367 int entry = link->get_endpoint_by_link(*last->block);
368 if(!link->reserve(this))
370 // If we found another train going in the same direction as us, we can keep the blocks we got
371 int other_entry = link->get_train()->get_entry_to_block(*link);
372 if(other_entry==entry || link->traverse(entry)==link->traverse(other_entry))
377 pending_block = link;
381 if(link->get_turnout_id())
383 const Block::Endpoint &ep = link->get_endpoints()[entry];
384 const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep];
386 if(track_ep.paths&(track_ep.paths-1))
388 // We're facing the points - keep the blocks reserved so far
393 Turnout &turnout = trfc_mgr.get_control().get_turnout(link->get_turnout_id());
395 // Figure out what path we'd like to take on the turnout
398 path = route->get_turnout(link->get_turnout_id());
400 path = turnout.get_path();
401 if(!((track_ep.paths>>path)&1))
403 for(unsigned i=0; track_ep.paths>>i; ++i)
404 if((track_ep.paths>>i)&1)
408 if(path!=turnout.get_path())
410 // The turnout is set to wrong path - switch and wait for it
412 pending_block = link;
413 turnout.set_path(path);
418 rsv_blocks.push_back(BlockRef(link, entry));
419 last = &rsv_blocks.back();
420 if(last->block->get_sensor_id())
427 // Unreserve blocks that were not good
428 while(!rsv_blocks.empty() && last!=good)
430 last->block->reserve(0);
431 rsv_blocks.erase(--rsv_blocks.end());
432 if(!rsv_blocks.empty())
433 last = &rsv_blocks.back();
442 void Train::update_speed()
447 try_reserve = Time::TimeStamp();
448 set_status("Stopped");
453 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
454 if(i->block->get_sensor_id())
457 unsigned slow_speed = find_speed(0.1); // 31.3 km/h
462 try_reserve = Time::now()+2*Time::sec;
463 set_status("Blocked");
465 else if(nsens==1 && target_speed>slow_speed)
467 loco.set_speed(slow_speed);
469 try_reserve = Time::now()+2*Time::sec;
474 loco.set_speed(target_speed);
475 try_reserve = Time::TimeStamp();
476 set_status(format("Traveling %d kmh", travel_speed));
481 float Train::get_real_speed(unsigned i) const
483 if(real_speed[i].weight)
484 return real_speed[i].speed;
488 for(low=i; low>0; --low)
489 if(real_speed[low].weight)
491 for(high=i; high<14; ++high)
492 if(real_speed[high].weight)
495 if(real_speed[high].weight)
497 if(real_speed[low].weight)
499 float f = float(i-low)/(high-low);
500 return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
503 return real_speed[high].speed*float(i)/high;
505 else if(real_speed[low].weight)
506 return real_speed[low].speed*float(i)/low;
511 unsigned Train::find_speed(float real) const
513 if(real<=real_speed[0].speed)
518 for(unsigned i=0; (!high && i<=14); ++i)
519 if(real_speed[i].weight)
521 if(real_speed[i].speed<real)
530 return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
533 float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
534 return static_cast<unsigned>(low*(1-f)+high*f+0.5);
537 void Train::set_status(const string &s)
540 signal_status_changed.emit(s);
543 void Train::set_position(const Block::Endpoint &bep)
545 cur_track = bep.track;
546 cur_track_ep = bep.track_ep;
548 pos = cur_track->get_endpoint_position(cur_track_ep);
551 void Train::release_blocks(list<BlockRef> &blocks)
553 for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
554 i->block->reserve(0);
558 void Train::reverse_blocks(list<BlockRef> &blocks) const
561 for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
562 i->entry = i->block->traverse(i->entry);
566 Train::RealSpeed::RealSpeed():
571 void Train::RealSpeed::add(float s, float w)
573 speed = (speed*weight+s*w)/(weight+w);
574 weight = min(weight+w, 300.0f);
578 Train::Loader::Loader(Train &t):
579 DataFile::BasicLoader<Train>(t),
582 add("block", &Loader::block);
583 add("block_hint", &Loader::block_hint);
584 add("name", &Train::name);
585 add("real_speed", &Loader::real_speed);
586 add("route", &Loader::route);
589 void Train::Loader::block(unsigned id)
591 Block &blk = obj.trfc_mgr.get_block(id);
594 entry = blk.get_endpoint_by_link(*prev_block);
599 obj.cur_blocks.push_back(BlockRef(&blk, entry));
600 obj.status = "Stopped";
601 obj.set_position(blk.get_endpoints()[entry]);
606 void Train::Loader::block_hint(unsigned id)
608 prev_block = &obj.trfc_mgr.get_block(id);
611 void Train::Loader::real_speed(unsigned i, float speed, float weight)
613 obj.real_speed[i].speed = speed;
614 obj.real_speed[i].weight = weight;
617 void Train::Loader::route(const string &n)
619 obj.route = &obj.trfc_mgr.get_layout().get_route(n);
622 } // namespace Marklin