Skip to content

Commit

Permalink
Fix computing time for clock_nanosleep (Fix #357)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhard-thiele committed May 19, 2022
1 parent b2fa5d7 commit 53f21d0
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ DllExport void MDD_RTSyncDestructor(void* rtSyncObj) {
}
}

#include <stdint.h>

/** Slow down sampling period so that simulation time == real-time.
*
*
Expand All @@ -891,6 +893,7 @@ DllExport void MDD_RTSyncSynchronize(void * rtSyncObj, double simTime, double sc
struct timespec t_now;
struct timespec t_elapsed;
struct timespec t_abs; /* Absolute time until which execution will be delayed (to catch up with real-time) */
struct timespec t_subtrahend_might_be_modified_for_carry; /* TODO consider changing `timespec_subtract(...)` for less surprising code */
double samplingPeriod = 0, fractpart = 0, intpart = 0;
int ret = 0;

Expand Down Expand Up @@ -925,7 +928,8 @@ DllExport void MDD_RTSyncSynchronize(void * rtSyncObj, double simTime, double sc
}

/* Determine computation time */
ret = timespec_subtract(&t_elapsed, &t_now, &rtSync->t_last);
t_subtrahend_might_be_modified_for_carry = rtSync->t_last;
ret = timespec_subtract(&t_elapsed, &t_now, &t_subtrahend_might_be_modified_for_carry);
if (ret) {
ModelicaError("MDDRealtimeSynchronize.h: Uups, negative computing time?!\n");
}
Expand Down Expand Up @@ -959,15 +963,16 @@ DllExport void MDD_RTSyncSynchronize(void * rtSyncObj, double simTime, double sc
/* wait until (scaled) simulation time == real-time */
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t_abs, NULL);
if (ret) {
ModelicaError("MDDRealtimeSynchronize.h: clock_nanosleep(..) failed\n");
ModelicaFormatError("MDDRealtimeSynchronize.h: clock_nanosleep(..) failed\n");
}

/* get value the current time of the real-time clock (should be equal to t_abs if everything is OK) */
clock_gettime(CLOCK_MONOTONIC, &rtSync->t_last);
rtSync->lastSimTime = simTime;

/* wall clock time since start */
ret = timespec_subtract(&t_elapsed, &(rtSync->t_last), &(rtSync->t_start));
t_subtrahend_might_be_modified_for_carry = rtSync->t_start;
ret = timespec_subtract(&t_elapsed, &(rtSync->t_last), &t_subtrahend_might_be_modified_for_carry);
if (ret == 1) {
ModelicaFormatError("MDDRealtimeSynchronize.h: timespec_subtract returned negative number\n");
}
Expand Down

0 comments on commit 53f21d0

Please sign in to comment.