Skip to content

Commit

Permalink
Add option to globally set desired update rate by calling set_default…
Browse files Browse the repository at this point in the history
…_rate from behavior
  • Loading branch information
David Conner committed Jul 2, 2024
1 parent 15a6756 commit ad286a6
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions flexbe_core/flexbe_core/core/ros_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
class RosState(State):
"""A state to interface with ROS."""

_node = None
_breakpoints = None
_default_rate_hz = 10.0 # Default best effort update rate
_node = None

@staticmethod
def initialize_ros(node):
Expand All @@ -65,12 +66,12 @@ def __init__(self, *args, **kwargs):
"""Initialize RosState instance."""
super().__init__(*args, **kwargs)

self._desired_period_ns = (1 / 10) * 1e9

if 'desired_rate' in kwargs:
Logger.localinfo('RosState: Set desired state update '
f"rate to {kwargs['desired_rate']} Hz.")
self._desired_period_ns = (1 / kwargs['desired_rate']) * 1e9
self.set_rate(kwargs['desired_rate'])
else:
self.set_rate(RosState._default_rate_hz)

self._is_controlled = False

Expand Down Expand Up @@ -103,6 +104,31 @@ def set_rate(self, desired_rate):
"""
self._desired_period_ns = (1 / desired_rate) * 1e9

@classmethod
def set_default_rate(cls, desired_rate):
"""
Set the desired best effort execution rate of all states.
i.e., the rate with which the execute method is being called.
Note: The rate is best-effort, real-time support is not yet available.
This must be called at behavior level PRIOR to any states being created.
Typically, add import to behavior MANUAL_IMPORT section
from flexbe_core.core import RosState
The, in the MANUAL_INIT section
RosState.set_default_rate(5.0)
Note: This will change the default update rate for any states created afterwards,
so beware when importing sub-behaviors with different rates defined!
@type desired_rate: float
@param desired_rate: The desired rate in Hz.
"""
cls._default_rate_hz = desired_rate
Logger.localinfo('RosState: Set the default state update '
f'rate for behavior to {desired_rate} Hz.')

def _enable_ros_control(self):
self._is_controlled = True

Expand Down

0 comments on commit ad286a6

Please sign in to comment.