pyrcf.utils.time_utils
Utilities for timer and rate-related functionalities.
Classes
Base class for any Clock implementation for pyrcf control loop and components. |
|
ClockBase implementation to query system time using time.time(). |
|
ClockBase implementation to query counter using time.perf_counter(). Only |
|
Modified from https://github.com/upkie/loop-rate-limiters/blob/main/loop_rate_limiters/rate_limiter.py. |
|
Can be used to execute something at a specific rate. Checking |
Module Contents
- class pyrcf.utils.time_utils.ClockBase
Bases:
abc.ABCBase class for any Clock implementation for pyrcf control loop and components.
- abstract get_time() float
Get the latest time from this clock.
- Raises:
NotImplementedError – Raised if this method is not implemented by the child class.
- Returns:
the current time in seconds.
- Return type:
float
- class pyrcf.utils.time_utils.PythonEpochClock
Bases:
ClockBaseClockBase implementation to query system time using time.time().
- get_time() float
Get the latest time from this clock.
- Raises:
NotImplementedError – Raised if this method is not implemented by the child class.
- Returns:
the current time in seconds.
- Return type:
float
- class pyrcf.utils.time_utils.PythonPerfClock
Bases:
ClockBaseClockBase implementation to query counter using time.perf_counter(). Only meaningful when comparing values from other time.perf_counter() calls.
- get_time() float
Get the latest time from this clock.
- Raises:
NotImplementedError – Raised if this method is not implemented by the child class.
- Returns:
the current time in seconds.
- Return type:
float
- class pyrcf.utils.time_utils.RateLimiter(frequency: float, name: str = 'rate limiter', warn: bool = False, clock: ClockBase = PythonPerfClock())
Modified from https://github.com/upkie/loop-rate-limiters/blob/main/loop_rate_limiters/rate_limiter.py.
Original License terms below:
Regulate the frequency between calls to the same instruction.
This rate limniter is meant to be used in e.g. a loop or callback function. It is, in essence, the same as rospy.Rate. It assumes Python’s performance counter never jumps backward nor forward, so that it does not handle such cases contrary to rospy.Rate.
- name
Human-readable name used for logging.
- warn
If set (default), warn when the time between two calls exceeded the rate clock.
# # Copyright 2022 Stéphane Caron # Copyright 2023 Inria # # Licensed under the Apache License, Version 2.0 (the “License”); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an “AS IS” BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
- __period: float
- __slack: float
- __next_tick: float
- __freq: float
- name: str
- warn: bool
- set_frequency(frequency: float)
- property clock: ClockBase
The clock used by this class.
Use get_time() on this attribute to get current time in seconds.
- Returns:
The clock object used by this class.
- Return type:
- property next_tick: float
Time of next clock tick.
- property slack: float
Slack duration computed at the last call to
sleep().This duration is in seconds.
- remaining() float
Get the time remaining until the next expected clock tick.
- Returns:
Time remaining, in seconds, until the next expected clock tick.
- sleep()
Sleep for the duration required to regulate inter-call frequency.
- class pyrcf.utils.time_utils.RateTrigger(rate: float, clock: ClockBase = PythonPerfClock())
Can be used to execute something at a specific rate. Checking the .triggered() method of this class will return True or False depending on whether the specified rate has reached.
NOTE: Warning: When using this with RateLimiter in the same loop, try to keep the rate of this class a factor of the rate of the RateLimiter object. Otherwise, the RateTrigger will not be able to keep the required rate (because RateLimiter enforces a sleep). Make sure to use same clock as well.
- __period
- __next_tick = None
- property clock: ClockBase
The clock used by this class.
Use get_time() on this attribute to get current time in seconds.
- Returns:
The clock object used by this class.
- Return type:
- property dt: float
Desired period between two calls to
sleep(), in seconds.
- property next_tick: float
Time of next clock tick.
- property period: float
Desired period between two calls to
sleep(), in seconds.
- triggered() bool
Check if the specified rate has been triggered.
- property has_triggered: bool
Attribute to check if the specified rate has been triggered.
- __call__() bool
Calling this class object directly will check if the specified rate has been triggered.