layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+ layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event));
if(FS::exists("engineer.state"))
DataFile::load(layout, "engineer.state");
Panel(r),
engineer(e)
{
- set_size(200, 100);
+ set_size(200, 133);
GLtk::Button *btn;
add(*(btn=new GLtk::Button(res, "Off")));
- btn->set_geometry(GLtk::Geometry(10, 53, 40, 25));
+ btn->set_geometry(GLtk::Geometry(10, 86, 40, 25));
btn->set_style("red");
btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::power_off));
add(*(ind_off=new GLtk::Indicator(res)));
- ind_off->set_geometry(GLtk::Geometry(10, 78, 40, 12));
+ ind_off->set_geometry(GLtk::Geometry(10, 111, 40, 12));
ind_off->set_style("red");
add(*(btn=new GLtk::Button(res, "On")));
- btn->set_geometry(GLtk::Geometry(50, 53, 40, 25));
+ btn->set_geometry(GLtk::Geometry(50, 86, 40, 25));
btn->set_style("green");
btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::power_on));
add(*(ind_on=new GLtk::Indicator(res)));
- ind_on->set_geometry(GLtk::Geometry(50, 78, 40, 12));
+ ind_on->set_geometry(GLtk::Geometry(50, 111, 40, 12));
ind_on->set_style("green");
add(*(btn=new GLtk::Button(res, "Quit")));
- btn->set_geometry(GLtk::Geometry(150, 53, 40, 25));
+ btn->set_geometry(GLtk::Geometry(150, 86, 40, 25));
btn->set_style("red");
btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::quit));
add(*(btn=new GLtk::Button(res, "+Loc")));
- btn->set_geometry(GLtk::Geometry(90, 53, 40, 25));
+ btn->set_geometry(GLtk::Geometry(90, 86, 40, 25));
btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::new_loc));
+ add(*(ind_halt=new GLtk::Indicator(res)));
+ ind_halt->set_geometry(GLtk::Geometry(10, 69, 40, 12));
+
+ add(*(btn=new GLtk::Button(res, "Halt")));
+ btn->set_geometry(GLtk::Geometry(10, 44, 40, 25));
+ btn->signal_clicked.connect(sigc::mem_fun(this, &MainPanel::toggle_halt));
+
add(*(lbl_status=new GLtk::Label(res)));
lbl_status->set_geometry(GLtk::Geometry(10, 10, 180, 24));
lbl_status->set_style("digital");
ind_off->set_active(true);
driver.signal_power.connect(sigc::mem_fun(this, &MainPanel::power_event));
+ driver.signal_halt.connect(sigc::mem_fun(this, &MainPanel::halt_event));
}
void MainPanel::set_status_text(const string &txt)
engineer.get_layout().get_driver().set_power(false);
}
+void MainPanel::toggle_halt()
+{
+ Marklin::Driver &driver = engineer.get_layout().get_driver();
+ driver.halt(!driver.is_halted());
+}
+
void MainPanel::new_loc()
{
TrainProperties *dialog = new TrainProperties(engineer, res, 0);
ind_on->set_active(p);
ind_off->set_active(!p);
}
+
+void MainPanel::halt_event(bool h)
+{
+ ind_halt->set_active(h);
+}
Msp::GLtk::Indicator *ind_on;
Msp::GLtk::Indicator *ind_off;
Msp::GLtk::Label *lbl_status;
+ Msp::GLtk::Indicator *ind_halt;
public:
MainPanel(Engineer &, Msp::GLtk::Resources &);
private:
void power_on();
void power_off();
+ void toggle_halt();
void new_loc();
void quit();
void power_event(bool);
+ void halt_event(bool);
};
#endif
{
public:
sigc::signal<void, bool> signal_power;
+ sigc::signal<void, bool> signal_halt;
sigc::signal<void, unsigned, unsigned, bool> signal_loco_speed;
sigc::signal<void, unsigned, unsigned, bool> signal_loco_function;
sigc::signal<void, unsigned, bool> signal_turnout;
virtual void set_power(bool) = 0;
virtual bool get_power() const = 0;
+ virtual void halt(bool) = 0;
+ virtual bool is_halted() const = 0;
virtual void add_loco(unsigned) = 0;
virtual void set_loco_speed(unsigned, unsigned) = 0;
virtual void set_power(bool);
virtual bool get_power() const { return power; }
+ virtual void halt(bool) { }
+ virtual bool is_halted() const { return false; }
virtual void add_loco(unsigned) { }
virtual void set_loco_speed(unsigned, unsigned);
Intellibox::Intellibox(const string &dev):
power(false),
+ halted(false),
update_sensors(false),
command_sent(false)
{
signal_power.emit(power);
}
+void Intellibox::halt(bool h)
+{
+ halted = h;
+ if(halted)
+ {
+ for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
+ if(i->second.speed)
+ set_loco_speed(i->first, 0);
+ }
+
+ signal_halt.emit(halted);
+}
+
void Intellibox::add_loco(unsigned addr)
{
if(!locos.count(addr))
Locomotive &loco = locos[addr];
if(speed==loco.speed)
return;
+ if(speed && halted)
+ return;
loco.speed = speed;
loco_command(addr, speed, loco.reverse, loco.funcs|0x100);
unsigned char err;
read_all(&err, 1);
+ unsigned addr = queue.front().addr;
+ Turnout &turnout = turnouts[addr];
+
if(err==ERR_NO_ERROR)
{
- unsigned addr = queue.front().addr;
- Turnout &turnout = turnouts[addr];
turnout.state = turnout.pending;
if(turnout.active)
{
}
else if(err==ERR_NO_I2C_SPACE)
queue.push_back(queue.front());
+ else
+ turnout.pending = turnout.state;
}
else if(cmd==CMD_TURNOUT_STATUS)
{
int serial_fd;
bool power;
+ bool halted;
std::map<unsigned, Locomotive> locos;
std::map<unsigned, Turnout> turnouts;
std::map<unsigned, Sensor> sensors;
virtual void set_power(bool);
virtual bool get_power() const { return power; }
+ virtual void halt(bool);
+ virtual bool is_halted() const { return halted; }
virtual void add_loco(unsigned);
virtual void set_loco_speed(unsigned, unsigned);
Layout::Layout(Catalogue &c, Driver *d):
catalogue(c),
driver(d)
-{ }
+{
+ if(driver)
+ driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
+}
Layout::~Layout()
{
i->second->tick(t, dt);
}
+void Layout::emergency(const string &msg)
+{
+ if(driver)
+ driver->halt(true);
+ signal_emergency.emit(msg);
+}
+
void Layout::save(const string &fn)
{
IO::BufferedFile out(fn, IO::M_WRITE);
}
}
+void Layout::sensor_event(unsigned addr, bool state)
+{
+ if(state)
+ {
+ for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ if((*i)->get_sensor_id()==addr)
+ {
+ if(!(*i)->get_train())
+ emergency("Unreserved sensor triggered");
+ break;
+ }
+ }
+}
+
Layout::Loader::Loader(Layout &l):
DataFile::BasicLoader<Layout>(l),
sigc::signal<void, Vehicle &> signal_vehicle_added;
sigc::signal<void, Vehicle &> signal_vehicle_removed;
sigc::signal<void, Block &, Train *> signal_block_reserved;
+ sigc::signal<void, const std::string &> signal_emergency;
private:
Catalogue &catalogue;
void remove_vehicle(Vehicle &);
void tick();
+ void emergency(const std::string &);
void save(const std::string &);
void save_trains(const std::string &);
private:
void check_links();
void check_routes();
+ void sensor_event(unsigned, bool);
};
} // namespace Marklin
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));
release_blocks(cur_blocks);
set_active(false);
+ accurate_position = false;
if(!block.reserve(this))
{
stop_timeout = Time::TimeStamp();
}
+ Driver &driver = layout.get_driver();
+
if(timetable)
timetable->tick(t);
control->tick(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);
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;
for(list<BlockRef>::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");
}
}
else if(end_of_route)
void Train::save(list<DataFile::Statement> &st) const
{
st.push_back((DataFile::Statement("name"), name));
+
+ for(vector<Vehicle *>::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));
{
current_speed = speed;
speed_changing = false;
+ pure_speed = false;
}
}
{
// Find the first sensor block from our reserved blocks that isn't this sensor
list<BlockRef>::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;
}
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)
signal_arrived.emit();
}
}
+ else if(result==3)
+ layout.emergency("Sensor for "+name+" triggered out of order");
}
else
{
last = &rsv_blocks.back();
}
+ // Make any sensorless blocks at the beginning immediately current
+ list<BlockRef>::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;
}
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);
float travel_dist;
bool pure_speed;
std::vector<RealSpeed> real_speed;
+ bool accurate_position;
+ float overshoot_dist;
public:
Train(Layout &, const LocoType &, unsigned);