3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/core/except.h>
9 #include <msp/time/units.h>
10 #include "simplecontroller.h"
17 SimpleController::SimpleController():
18 target_speed(Control::continuous("speed", 0, 1000)),
19 reverse(Control::binary("reverse")),
26 void SimpleController::set_control(const string &name, float v)
31 signal_control_changed.emit(target_speed);
33 else if(name=="reverse")
35 if(target_speed.value || speed)
36 throw InvalidState("Must be stopped to change reverse");
38 signal_control_changed.emit(reverse);
42 const Controller::Control &SimpleController::get_control(const string &name) const
46 else if(name=="reverse")
49 throw KeyError("Unknown control", name);
52 float SimpleController::get_braking_distance() const
54 return speed*speed/(2*accel);
57 void SimpleController::tick(const Time::TimeDelta &dt)
59 float secs = dt/Time::sec;
60 if(speed<target_speed.value)
63 if(speed>target_speed.value)
64 speed = target_speed.value;
66 else if(speed>target_speed.value)
69 if(speed<target_speed.value)
70 speed = target_speed.value;
74 } // namespace Marklin