From 944a03b280df083800c8aea9b206de0c6539e14a Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 16 Apr 2011 02:57:44 +0200 Subject: Use precise time intervals. --- main.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 14f8a8a..27fab50 100644 --- a/main.cpp +++ b/main.cpp @@ -12,11 +12,14 @@ class LEDThread : public BaseThread { public: noreturn_t thread_main() { - while (1) { + systime_t time = chTimeNow(); // T0 + while (TRUE) { + time += MS2ST(1000); // Next deadline palClearPad(GPIOA, 5); - chThdSleepMilliseconds(500); + chThdSleepUntil(time); + time += MS2ST(1000); // Next deadline palSetPad(GPIOA, 5); - chThdSleepMilliseconds(500); + chThdSleepUntil(time); } } }; @@ -84,9 +87,8 @@ class I2CThread : public BaseThread { acc.init(); magn.init(); - systime_t nexttime = chTimeNow(); + systime_t time = chTimeNow(); while (1) { - nexttime += MS2ST(100); gyro.update(); acc.update(); magn.update(); @@ -126,7 +128,10 @@ class I2CThread : public BaseThread { int(q0 * 10000), int(q1 * 10000), int(q2 * 10000), int(q3 * 10000), int(pitch * 10000), int(roll * 10000), int(yaw * 10000));*/ - //chThdSleepUntil(nexttime); + time += MS2ST(10); + if(time > chTimeNow()) { + chThdSleepUntil(time); + } } } }; -- cgit v1.2.3