Have you considered building a finite state machine to handle your pump? Something that would vaguely look like this:
class pump {
enum pump_state {
on, // running
wait_endcourse, // waiting to reach your "83%" course
wait_off, // waiting to reach the point where pump shall be turned off
off // stopped
};
pump_state state;
pump::manage (MySplendidEventType event)
{
switch (state)
{
case on:
if (event == command_shutdown)
{
whatever_I_need_to_do_to_start_shutdown();
Timer.start (time_to_reach_83_pc_of_the_course);
state = wait_endcourse;
}
break;
case wait_endcourse:
if (event == timeout)
{
whatever_I_need_to_do_at_83_pc_course();
Timer.start(time_to_go_the_remaining_17_pc_of_the_course);
state = wait_off;
}
break;
case wait_off:
if (event == timeout)
{
whatever_I_need_to_do_to_finish_shutdown();
state = off;
}
}
}
};
class pump {
enum pump_state {
on, // running
wait_endcourse, // waiting to reach your "83%" course
wait_off, // waiting to reach the point where pump shall be turned off
off // stopped
};
pump_state state;
pump::manage (MySplendidEventType event)
{
switch (state)
{
case on:
if (event == command_shutdown)
{
whatever_I_need_to_do_to_start_shutdown();
Timer.start (time_to_reach_83_pc_of_the_course);
state = wait_endcourse;
}
break;
case wait_endcourse:
if (event == timeout)
{
whatever_I_need_to_do_at_83_pc_course();
Timer.start(time_to_go_the_remaining_17_pc_of_the_course);
state = wait_off;
}
break;
case wait_off:
if (event == timeout)
{
whatever_I_need_to_do_to_finish_shutdown();
state = off;
}
}
}
};
To copy to clipboard, switch view to plain text mode
Note that if you intend to command a real pump you are in dire need of feedback. I hope you don't assume sending the "on" command will prevent the pump to jam if, let's say, the janitor's broomstick has been sucked into the intake?
This finite state machine technique allows to handle these feedbacks, for instance:
case wait_endcourse:
if (some_sensor_tells_me_that_the_pump_is_jammed)
{
fire_the_bloody_janitor();
state = broomstick_stuck_in_intake;
}
else if (event == timeout)
{
whatever_I_need_to_do_at_83_pc_course();
Timer.start(time_to_go_the_remaining_17_pc_of_the_course);
state = wait_off;
}
break;
case wait_endcourse:
if (some_sensor_tells_me_that_the_pump_is_jammed)
{
fire_the_bloody_janitor();
state = broomstick_stuck_in_intake;
}
else if (event == timeout)
{
whatever_I_need_to_do_at_83_pc_course();
Timer.start(time_to_go_the_remaining_17_pc_of_the_course);
state = wait_off;
}
break;
To copy to clipboard, switch view to plain text mode
Bookmarks