Skip to content

Commit 5388fe3

Browse files
committed
uavobjectmanager: improve timer math
1 parent cd292b9 commit 5388fe3

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

flight/UAVObjects/uavobjectmanager.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct ObjectEventEntry {
7474
struct ObjectEventEntryThrottled {
7575
struct ObjectEventEntry entry; // MUST be first! So throttled entry can be interpreted as ObjectEventEntry
7676

77-
uint32_t due;
77+
uint32_t when;
7878
uint16_t interval;
7979
volatile uint8_t inhibited;
8080
};
@@ -1735,12 +1735,14 @@ static int32_t pumpOneEvent(UAVObjEvent msg, void *obj_data, int len) {
17351735
(struct ObjectEventEntryThrottled *) event;
17361736

17371737
uint32_t now = PIOS_Thread_Systime();
1738-
if (throtInfo->due > now) {
1738+
1739+
if (!PIOS_Thread_Period_Elapsed(throtInfo->when,
1740+
throtInfo->interval)) {
17391741
continue;
17401742
}
17411743

17421744
// Set time for next callback
1743-
throtInfo->due += ((now - throtInfo->due) / throtInfo->interval + 1) * throtInfo->interval;
1745+
throtInfo->when = now;
17441746

17451747
if (throtInfo->inhibited) {
17461748
continue;
@@ -2052,7 +2054,7 @@ static int32_t connectObj(UAVObjHandle obj_handle, struct pios_queue *queue,
20522054
throttled = (struct ObjectEventEntryThrottled *) event;
20532055

20542056
throttled->interval = interval;
2055-
throttled->due = PIOS_Thread_Systime() + randomize_int(throttled->interval);
2057+
throttled->when = PIOS_Thread_Systime() + randomize_int(throttled->interval);
20562058
}
20572059

20582060
LL_APPEND(obj->next_event, event);

0 commit comments

Comments
 (0)