--- /dev/null
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include "speedquantizer.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace R2C2 {
+
+SpeedQuantizer::SpeedQuantizer(unsigned n):
+ steps(n+1)
+{
+ if(n<1)
+ throw InvalidParameterValue("Must have at leats one speed step");
+}
+
+void SpeedQuantizer::learn(unsigned i, float s, float w)
+{
+ if(i>=steps.size())
+ throw InvalidParameterValue("Speed step index out of range");
+ steps[i].add(s, w);
+}
+
+float SpeedQuantizer::get_speed(unsigned i) const
+{
+ if(i==0)
+ return 0;
+ if(steps[i].weight)
+ return steps[i].speed;
+
+ unsigned low;
+ unsigned high;
+ for(low=i; low>0; --low)
+ if(steps[low].weight)
+ break;
+ for(high=i; high+1<steps.size(); ++high)
+ if(steps[high].weight)
+ break;
+
+ if(steps[high].weight)
+ {
+ if(steps[low].weight)
+ return (steps[low].speed*(high-i)+steps[high].speed*(i-low))/(high-low);
+ else
+ return steps[high].speed*i/high;
+ }
+ else if(steps[low].weight)
+ return steps[low].speed*i/low;
+ else
+ return 0;
+}
+
+float SpeedQuantizer::quantize_speed(float speed) const
+{
+ return get_speed(find_speed_step(speed));
+}
+
+unsigned SpeedQuantizer::find_speed_step(float speed) const
+{
+ if(speed<=steps[1].speed*0.5)
+ return 0;
+
+ unsigned low = 0;
+ unsigned high = 0;
+ unsigned last = 0;
+ for(unsigned i=0; (!high && i<steps.size()); ++i)
+ if(steps[i].weight)
+ {
+ last = i;
+ if(steps[i].speed>=speed)
+ high = i;
+ else if(steps[i].speed>steps[low].speed)
+ low = i;
+ }
+
+ if(!high)
+ {
+ unsigned limit = steps.size()/5;
+ if(!low)
+ {
+ if(speed)
+ return limit;
+ else
+ return 0;
+ }
+ return min(min(static_cast<unsigned>(low*speed/steps[low].speed), steps.size()-1), last+limit);
+ }
+
+ float f = (speed-steps[low].speed)/(steps[high].speed-steps[low].speed);
+ return static_cast<unsigned>(low*(1-f)+high*f+0.5);
+}
+
+void SpeedQuantizer::save(list<DataFile::Statement> &st) const
+{
+ for(unsigned i=0; i<steps.size(); ++i)
+ if(steps[i].weight)
+ st.push_back((DataFile::Statement("step"), i, steps[i].speed, steps[i].weight));
+}
+
+
+SpeedQuantizer::SpeedStep::SpeedStep():
+ speed(0),
+ weight(0)
+{ }
+
+void SpeedQuantizer::SpeedStep::add(float s, float w)
+{
+ speed = (speed*weight+s*w)/(weight+w);
+ weight = min(weight+w, 300.0f);
+}
+
+
+SpeedQuantizer::Loader::Loader(SpeedQuantizer &q):
+ DataFile::ObjectLoader<SpeedQuantizer>(q)
+{
+ add("step", &Loader::step);
+}
+
+void SpeedQuantizer::Loader::step(unsigned i, float s, float w)
+{
+ if(i>=obj.steps.size())
+ return;
+
+ obj.steps[i].speed = s;
+ obj.steps[i].weight = w;
+}
+
+} // namespace R2C2
/* $Id$
This file is part of R²C²
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
#include "layout.h"
#include "route.h"
#include "simplecontroller.h"
+#include "speedquantizer.h"
#include "timetable.h"
#include "trackiter.h"
#include "tracktype.h"
status("Unplaced"),
travel_dist(0),
pure_speed(false),
- real_speed(layout.get_driver().get_protocol_speed_steps(protocol)+1),
+ speed_quantizer(0),
accurate_position(false),
overshoot_dist(false)
{
if(!loco_type.is_locomotive())
throw InvalidParameterValue("Initial vehicle must be a locomotive");
+ unsigned speed_steps = layout.get_driver().get_protocol_speed_steps(protocol);
+ if(speed_steps)
+ speed_quantizer = new SpeedQuantizer(speed_steps);
+
vehicles.push_back(new Vehicle(layout, loco_type));
layout.add_train(*this);
timetable->tick(t);
controller->tick(dt);
float speed = controller->get_speed();
- unsigned speed_step = find_speed_step(speed);
if(controller->get_reverse()!=reverse)
{
reserve_more();
}
- if(speed_step!=current_speed_step && !speed_changing && !driver.is_halted() && driver.get_power())
+
+ if(speed_quantizer)
{
- speed_changing = true;
- driver.set_loco_speed(address, speed_step);
+ unsigned speed_step = speed_quantizer->find_speed_step(speed);
+ if(speed_step!=current_speed_step && !speed_changing && !driver.is_halted() && driver.get_power())
+ {
+ speed_changing = true;
+ driver.set_loco_speed(address, speed_step);
- pure_speed = false;
+ pure_speed = false;
- if(speed_step)
- set_status(format("Traveling %d kmh", get_travel_speed()));
- else
- set_status("Waiting");
+ if(speed_step)
+ set_status(format("Traveling %d kmh", get_travel_speed()));
+ else
+ set_status("Waiting");
+ }
+
+ speed = speed_quantizer->get_speed(current_speed_step);
}
if(speed)
for(BlockList::const_iterator i=blocks.begin(); (!ok && i!=cur_blocks_end); ++i)
ok = (*i)->has_track(*track);
- float d;
- if(real_speed.size()>1)
- d = get_real_speed(current_speed_step)*(dt/Time::sec);
- else
- d = speed*(dt/Time::sec);
+ float d = speed*(dt/Time::sec);
if(ok)
{
SetFlag setf(advancing);
if(i!=vehicles.begin())
st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number()));
- for(unsigned i=0; i<real_speed.size(); ++i)
- if(real_speed[i].weight)
- st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
+ if(speed_quantizer)
+ {
+ DataFile::Statement ss("quantized_speed");
+ speed_quantizer->save(ss.sub);
+ st.push_back(ss);
+ }
if(!blocks.empty() && cur_blocks_end!=blocks.begin())
{
if(pure_speed)
{
- if(current_speed_step>0)
- {
- RealSpeed &rs = real_speed[current_speed_step];
- rs.add(travel_dist/travel_time_secs, travel_time_secs);
- }
+ if(speed_quantizer && current_speed_step>0)
+ speed_quantizer->learn(current_speed_step, travel_dist/travel_time_secs, travel_time_secs);
set_status(format("Traveling %d kmh", get_travel_speed()));
}
return result;
}
-float Train::get_real_speed(unsigned i) const
-{
- if(i==0)
- return 0;
- if(real_speed[i].weight)
- return real_speed[i].speed;
-
- unsigned low;
- unsigned high;
- for(low=i; low>0; --low)
- if(real_speed[low].weight)
- break;
- for(high=i; high+1<real_speed.size(); ++high)
- if(real_speed[high].weight)
- break;
-
- if(real_speed[high].weight)
- {
- if(real_speed[low].weight)
- {
- float f = float(i-low)/(high-low);
- return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
- }
- else
- return real_speed[high].speed*float(i)/high;
- }
- else if(real_speed[low].weight)
- return real_speed[low].speed*float(i)/low;
- else
- return 0;
-}
-
-unsigned Train::find_speed_step(float real) const
-{
- if(real_speed.size()<=1)
- return 0;
- if(real<=real_speed[1].speed*0.5)
- return 0;
-
- unsigned low = 0;
- unsigned high = 0;
- unsigned last = 0;
- for(unsigned i=0; (!high && i<real_speed.size()); ++i)
- if(real_speed[i].weight)
- {
- last = i;
- if(real_speed[i].speed>=real)
- high = i;
- else if(real_speed[i].speed>real_speed[low].speed)
- low = i;
- }
- if(!high)
- {
- unsigned limit = real_speed.size()/5;
- if(!low)
- {
- if(real)
- return limit;
- else
- return 0;
- }
- return min(min(static_cast<unsigned>(low*real/real_speed[low].speed), real_speed.size()-1), last+limit);
- }
-
- float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
- return static_cast<unsigned>(low*(1-f)+high*f+0.5);
-}
-
float Train::get_travel_speed() const
{
- float speed = get_real_speed(current_speed_step);
+ float speed = 0;
+ if(speed_quantizer)
+ speed = speed_quantizer->get_speed(current_speed_step);
+ else
+ speed = controller->get_speed();
float scale = layout.get_catalogue().get_scale();
return static_cast<int>(round(speed/scale*3.6/5))*5;
}
{ }
-Train::RealSpeed::RealSpeed():
- speed(0),
- weight(0)
-{ }
-
-void Train::RealSpeed::add(float s, float w)
-{
- speed = (speed*weight+s*w)/(weight+w);
- weight = min(weight+w, 300.0f);
-}
-
-
Train::Loader::Loader(Train &t):
DataFile::BasicLoader<Train>(t),
prev_block(0),
add("block_hint", &Loader::block_hint);
add("name", &Loader::name);
add("priority", &Train::priority);
- add("real_speed", &Loader::real_speed);
+ add("quantized_speed", &Loader::quantized_speed);
add("route", &Loader::route);
add("timetable", &Loader::timetable);
add("vehicle", &Loader::vehicle);
obj.set_name(n);
}
-void Train::Loader::real_speed(unsigned i, float speed, float weight)
+void Train::Loader::quantized_speed()
{
- if(i>=obj.real_speed.size())
- return;
- obj.real_speed[i].speed = speed;
- obj.real_speed[i].weight = weight;
+ if(obj.speed_quantizer)
+ load_sub(*obj.speed_quantizer);
}
void Train::Loader::route(const string &n)