Skip to content

Commit ee203b3

Browse files
committed
pbio/util: Move time comparison util.
This is not only useful for motor control code.
1 parent e8e6bbf commit ee203b3

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

lib/pbio/include/pbio/control_settings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ int32_t pbio_control_settings_app_to_ctl(const pbio_control_settings_t *s, int32
135135
void pbio_control_settings_app_to_ctl_long(const pbio_control_settings_t *s, int32_t input, pbio_angle_t *output);
136136
int32_t pbio_control_settings_actuation_ctl_to_app(int32_t input);
137137
int32_t pbio_control_settings_actuation_app_to_ctl(int32_t input);
138-
bool pbio_control_settings_time_is_later(uint32_t sample, uint32_t base);
139138

140139
// Scale values by given constants:
141140

lib/pbio/include/pbio/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ void pbio_uuid128_reverse_copy(uint8_t *dst, const uint8_t *src);
130130

131131
bool pbio_oneshot(bool value, bool *state);
132132

133+
bool pbio_util_time_has_passed(uint32_t sample, uint32_t base);
134+
133135
#endif // _PBIO_UTIL_H_
134136

135137
/** @} */

lib/pbio/src/control.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <pbio/int_math.h>
1515
#include <pbio/trajectory.h>
1616
#include <pbio/integrator.h>
17+
#include <pbio/util.h>
1718

1819
/**
1920
* Gets the wall time in control unit time ticks (1e-4 seconds).
@@ -108,7 +109,7 @@ static bool pbio_control_check_completion(const pbio_control_t *ctl, uint32_t ti
108109
}
109110

110111
// Check if we are passed the nominal maneuver time.
111-
bool time_completed = pbio_control_settings_time_is_later(time, end->time);
112+
bool time_completed = pbio_util_time_has_passed(time, end->time);
112113

113114
if (pbio_control_type_is_time(ctl)) {
114115
// Infinite maneuvers are always done (should never block).
@@ -354,7 +355,7 @@ void pbio_control_update(
354355
// without resetting any controllers. This avoids accumulating errors
355356
// in sequential relative maneuvers.
356357
(pbio_control_on_completion_is_passive_smart(ctl->on_completion) &&
357-
!pbio_control_settings_time_is_later(ref->time, ref_end.time + ctl->settings.smart_passive_hold_time))) {
358+
!pbio_util_time_has_passed(ref->time, ref_end.time + ctl->settings.smart_passive_hold_time))) {
358359
// Keep actuating, so apply calculated PID torque value.
359360
*actuation = PBIO_DCMOTOR_ACTUATION_TORQUE;
360361
*control = torque;

lib/pbio/src/control_settings.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,6 @@ int32_t pbio_control_settings_mul_by_loop_time(int32_t input) {
165165
return input / (1000 / PBIO_CONFIG_CONTROL_LOOP_TIME_MS);
166166
}
167167

168-
/**
169-
* Checks if a time sample is equal to or newer than a given base time stamp.
170-
*
171-
* @param [in] sample Sample time.
172-
* @param [in] base Base time to compare to.
173-
* @return True if sample time is equal to or newer than base time, else false.
174-
*/
175-
bool pbio_control_settings_time_is_later(uint32_t sample, uint32_t base) {
176-
return sample - base < UINT32_MAX / 2;
177-
}
178-
179168
/**
180169
* Gets the control limits for movement and actuation, in application units.
181170
*

lib/pbio/src/integrator.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <pbio/control_settings.h>
1212
#include <pbio/integrator.h>
1313
#include <pbio/int_math.h>
14+
#include <pbio/util.h>
1415

1516
/**
1617
* Pauses the speed integrator at the current position error.
@@ -124,7 +125,7 @@ bool pbio_speed_integrator_stalled(const pbio_speed_integrator_t *itg, uint32_t
124125
}
125126

126127
// If the integrator is paused for less than the stall time, we're still not stalled for now.
127-
if (!pbio_control_settings_time_is_later(time_now, itg->time_pause_begin + itg->settings->stall_time)) {
128+
if (!pbio_util_time_has_passed(time_now, itg->time_pause_begin + itg->settings->stall_time)) {
128129
return false;
129130
}
130131

@@ -299,7 +300,7 @@ bool pbio_position_integrator_stalled(const pbio_position_integrator_t *itg, uin
299300
}
300301

301302
// If the integrator is paused for less than the stall time, we're still not stalled for now.
302-
if (!pbio_control_settings_time_is_later(time_now, itg->time_pause_begin + itg->settings->stall_time)) {
303+
if (!pbio_util_time_has_passed(time_now, itg->time_pause_begin + itg->settings->stall_time)) {
303304
return false;
304305
}
305306

lib/pbio/src/util.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@ bool pbio_oneshot(bool value, bool *state) {
8080

8181
return ret;
8282
}
83+
84+
/**
85+
* Checks if a time sample is equal to or newer than a given base time stamp.
86+
*
87+
* @param [in] sample Sample time.
88+
* @param [in] base Base time to compare to.
89+
* @return True if sample time is equal to or newer than base time, else false.
90+
*/
91+
bool pbio_util_time_has_passed(uint32_t sample, uint32_t base) {
92+
return sample - base < UINT32_MAX / 2;
93+
}

0 commit comments

Comments
 (0)