1
\$\begingroup\$

I have a program that works by periodic phases, and I need to understand, depending on the current time, which phase should be executed. The following is not for busy wait, it's just a check.

I'm trying to improve the reliability of the calculations, not to heavily refactor the whole thing.

Disclaimer: I'm on C++03. If I were using Java, a Timer would probably look like a good choice for a situation like this. However, I'd rather avoid using it in this specific case, because of the way the bigger programs works.

Here is a file with the constants defining the first time a phase is started (...TIME_0) and the time that passes before its next execution (TIME_TO_NEXT_...):

/* Times for phase changes */
const double CONF_0_TIME = 1.0;
const double GEN_MODEL_TIME = 1.5;
const double CONF_1_TIME = 2.0;
const double ATK_TIME = 2.5;
const double ATK_CK_TIME = 3.0;
const double UPD_MODEL_TIME_0 = 3.5;

const double TIME_FOR_1_CK = 1.0;
const double TIME_TO_NEXT_CK = TIME_FOR_1_CK * 4.0;
const double TIME_TO_NEXT_UPD = TIME_FOR_1_CK;

const double INTER_CK_B_TIME_0 = 4.0;
const double INTRA_CK_A_TIME_0 = INTER_CK_B_TIME_0 + TIME_FOR_1_CK;
const double INTER_CK_A_TIME_0 = INTRA_CK_A_TIME_0 + TIME_FOR_1_CK;
const double INTRA_CK_B_TIME_0 = INTER_CK_A_TIME_0 + TIME_FOR_1_CK;

Here is the file checking the current time and trying to deduce the phase:

char msg [200];

double cur_time = op_sim_time();
double adj_time = fmod (cur_time - (UPD_MODEL_TIME_0 - ATK_CK_TIME), TIME_TO_NEXT_CK);

if (cur_time == UPD_MODEL_TIME_0)
    {
    snprintf (msg, 200, "time %f) supervisor updating model after initial attack checks", cur_time);
    }
else if (adj_time == fmod (INTER_CK_B_TIME_0, TIME_TO_NEXT_CK))
    {
    snprintf (msg, 200, "time %f) supervisor updating model after inter B checks", cur_time);
    }
else if (adj_time == fmod (INTRA_CK_A_TIME_0, TIME_TO_NEXT_CK))
    {
    snprintf (msg, 200, "time %f) supervisor updating model after intra A checks", cur_time);
    }
else if (adj_time == fmod (INTER_CK_A_TIME_0, TIME_TO_NEXT_CK))
    {
    snprintf (msg, 200, "time %f) supervisor updating model after inter A checks", cur_time);
    }
else if (adj_time == fmod (INTRA_CK_B_TIME_0, TIME_TO_NEXT_CK))
    {
    snprintf (msg, 200, "time %f) supervisor updating model after intra B checks", cur_time);
    }
else
    {
    snprintf (msg, 200, "time %f) supervisor updating model, adj_time = %f", cur_time, adj_time);
    }

op_sim_message (msg, "");
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.