11#include " AppConfig.h"
2+ #include " Logger.h"
23#include " mbed.h"
34
45Thread periodic_10s_thread (osPriorityNormal1);
@@ -12,39 +13,69 @@ void periodic_10s(void) {
1213 for (Module* module : gModules ) {
1314 module ->periodic_10s ();
1415 }
15- ThisThread::sleep_until (startTime + 10s);
16+
17+ auto nextStartTime = startTime + 10s;
18+ if (Kernel::Clock::now () > nextStartTime) {
19+ Utility::logger << " Reseting periodic 10s task timing as it failed to hit the deadline!\n " ;
20+ nextStartTime = Kernel::Clock::now () + 10s;
21+ }
22+ ThisThread::sleep_until (nextStartTime);
1623}
1724
1825void periodic_1s (void ) {
1926 auto startTime = Kernel::Clock::now ();
2027 for (Module* module : gModules ) {
2128 module ->periodic_1s ();
2229 }
23- ThisThread::sleep_until (startTime + 1s);
30+
31+ auto nextStartTime = startTime + 1s;
32+ if (Kernel::Clock::now () > nextStartTime) {
33+ Utility::logger << " Reseting periodic 1s task timing as it failed to hit the deadline!\n " ;
34+ nextStartTime = Kernel::Clock::now () + 1s;
35+ }
36+ ThisThread::sleep_until (nextStartTime);
2437}
2538
2639void periodic_100ms (void ) {
2740 auto startTime = Kernel::Clock::now ();
2841 for (Module* module : gModules ) {
2942 module ->periodic_100ms ();
3043 }
31- ThisThread::sleep_until (startTime + 100ms);
44+
45+ auto nextStartTime = startTime + 100ms;
46+ if (Kernel::Clock::now () > nextStartTime) {
47+ Utility::logger << " Reseting periodic 100ms task timing as it failed to hit the deadline!\n " ;
48+ nextStartTime = Kernel::Clock::now () + 100ms;
49+ }
50+ ThisThread::sleep_until (nextStartTime);
3251}
3352
3453void periodic_10ms (void ) {
3554 auto startTime = Kernel::Clock::now ();
3655 for (Module* module : gModules ) {
3756 module ->periodic_10ms ();
3857 }
39- ThisThread::sleep_until (startTime + 10ms);
58+
59+ auto nextStartTime = startTime + 10ms;
60+ if (Kernel::Clock::now () > nextStartTime) {
61+ Utility::logger << " Reseting periodic 10ms task timing as it failed to hit the deadline!\n " ;
62+ nextStartTime = Kernel::Clock::now () + 10ms;
63+ }
64+ ThisThread::sleep_until (nextStartTime);
4065}
4166
4267void periodic_1ms (void ) {
4368 auto startTime = Kernel::Clock::now ();
4469 for (Module* module : gModules ) {
4570 module ->periodic_1ms ();
4671 }
47- ThisThread::sleep_until (startTime + 1ms);
72+
73+ auto nextStartTime = startTime + 1ms;
74+ if (Kernel::Clock::now () > nextStartTime) {
75+ Utility::logger << " Reseting periodic 1ms task timing as it failed to hit the deadline!\n " ;
76+ nextStartTime = Kernel::Clock::now () + 1ms;
77+ }
78+ ThisThread::sleep_until (nextStartTime);
4879}
4980
5081int main () {
@@ -54,6 +85,6 @@ int main() {
5485 periodic_1s_thread.start (periodic_1s);
5586 periodic_10s_thread.start (periodic_10s);
5687
57- while (true ) {
58- }
88+ while (true )
89+ ;
5990}
0 commit comments