controllers_ros2
ROS 2 controller interfaces and helpers.
This module defines ROS 2-backed controller wrappers used by RobotBlockSet robot interfaces. It provides controller-manager helpers, joint-trajectory interfaces, and Cartesian impedance command interfaces through ROS 2 topics, actions, and services.
- class robotblockset.ros2.controllers_ros2.controller_manager_helper(node: rclpy.node.Node, namespace: str = '', controller_manager_name: str | None = 'controller_manager')[source]
Bases:
objectHelper for ROS 2 controller-manager lifecycle operations.
- High-level lifecycle transitions:
load(name) -> ensure controller plugin instance exists (UNCONFIGURED) configure(name) -> UNCONFIGURED -> INACTIVE activate(name) -> INACTIVE -> ACTIVE (via switch_controllers) deactivate(name) -> ACTIVE -> INACTIVE unload(name) -> remove controller instance (must not be ACTIVE)
- Legacy wrappers:
load_controller(name) -> load + configure start_controller(name) -> activate stop_controller(name) -> deactivate switch_controllers(start, stop) -> activate/deactivate
- Utilities:
list_controllers(), get_state(name), is_active(name)
- Attributes:
cm_name (str) – Name of the controller-manager namespace.
list_client (Any) – Service client for listing controllers.
switch_client (Any) – Service client for switching controllers.
load_client (Any) – Service client for loading controllers.
unload_client (Any) – Service client for unloading controllers.
configure_client (Any) – Service client for configuring controllers.
Initialize the controller-manager helper.
- Parameters:
node (Node) – The ROS 2 node used to create service clients. The caller is responsible for spinning this node.
namespace (str, optional) – Namespace for the controller manager.
controller_manager_name (str, optional) – Name of the ROS 2 controller manager.
- __init__(node: rclpy.node.Node, namespace: str = '', controller_manager_name: str | None = 'controller_manager') None[source]
Initialize the controller-manager helper.
- Parameters:
node (Node) – The ROS 2 node used to create service clients. The caller is responsible for spinning this node.
namespace (str, optional) – Namespace for the controller manager.
controller_manager_name (str, optional) – Name of the ROS 2 controller manager.
- list_controllers() List[controller_manager_msgs.msg.ControllerState][source]
Query the controller_manager for all available controllers.
- Returns:
A list of controller state objects. If the service call fails, an empty list is returned and an error is logged.
- Return type:
list of controller_manager_msgs.msg.ControllerState
- get_state(name: str) str | None[source]
Get the lifecycle state of a specific controller.
- Parameters:
name (str) – Name of the controller.
- Returns:
The controller state string (e.g.
"active","inactive"), orNoneif the controller is not found.- Return type:
str or None
- is_active(name: str) bool[source]
Check whether a controller is in the
"active"state.- Parameters:
name (str) – Name of the controller.
- Returns:
Trueif the controller exists and is active, otherwiseFalse.- Return type:
bool
- load(name: str) bool[source]
Load a controller by name.
This requests that the controller_manager loads the specified controller plugin so that it can be configured and activated later.
- Parameters:
name (str) – Name of the controller to load.
- Returns:
Trueif the controller is already loaded or was loaded successfully,Falseif the load request failed.- Return type:
bool
- configure(name: str) bool[source]
Configure a controller, loading it first if necessary.
- Parameters:
name (str) – Name of the controller to configure.
- Returns:
Trueif the controller is already configured ("inactive"or"active") or was configured successfully, otherwiseFalse.- Return type:
bool
- activate(name: str) bool[source]
Activate a controller.
Ensures the controller is loaded and configured, then requests the controller_manager to start (activate) it.
- Parameters:
name (str) – Name of the controller to activate.
- Returns:
Trueif the controller is already active or was activated successfully,Falseotherwise.- Return type:
bool
- deactivate(name: str) bool[source]
Deactivate (stop) a controller.
- Parameters:
name (str) – Name of the controller to deactivate.
- Returns:
Trueif the controller is already not active or was deactivated successfully,Falseotherwise.- Return type:
bool
- unload(name: str) bool[source]
Unload a controller.
The controller must not be active when unloading. If it is active, this function first attempts to deactivate it.
- Parameters:
name (str) – Name of the controller to unload.
- Returns:
Trueif the controller is already absent or was unloaded successfully,Falseif deactivation or unload failed.- Return type:
bool
- start_controller(name: str) bool[source]
Backwards-compatible wrapper to start (activate) a controller.
- Parameters:
name (str) – Name of the controller to start.
- Returns:
Trueif activation succeeded or the controller is already active,Falseotherwise.- Return type:
bool
- load_controller(name: str) bool[source]
Backwards-compatible wrapper to load and configure a controller.
- Parameters:
name (str) – Name of the controller to load and configure.
- Returns:
Trueif the controller is loaded and configured,Falseotherwise.- Return type:
bool
- stop_controller(name: str) bool[source]
Backwards-compatible wrapper to stop (deactivate) a controller.
- Parameters:
name (str) – Name of the controller to stop.
- Returns:
Trueif the controller is not active or was deactivated successfully,Falseotherwise.- Return type:
bool
- switch_controllers(start_list: Iterable[str], stop_list: Iterable[str]) bool[source]
Switch sets of controllers in a single operation.
- Parameters:
start_list (Iterable[str]) – Names of controllers to activate.
stop_list (Iterable[str]) – Names of controllers to deactivate.
- Returns:
Trueif the switch request succeeded,Falseotherwise.- Return type:
bool
- get_active_controller(controller_names: Iterable[str] | None = None) str | None[source]
Get the name of the first active controller from a candidate list.
- Parameters:
controller_names (Iterable[str], optional) – Iterable of controller names to check. If
None, all known controllers are considered.- Returns:
Name of the first active controller from the list, or
Noneif none of them are active.- Return type:
str or None
- class robotblockset.ros2.controllers_ros2.RosControllerInterface[source]
Bases:
rbs_objectGeneric interface for a ROS2-based controller.
This class defines a skeleton for ROS2 controller interfaces used within Robotics Behavior Specification (RBS) systems. Derived classes must implement the _init_ros_interfaces method to create all ROS-specific communication elements such as publishers, subscribers, actions, or services.
- Attributes:
motion_result (int | None) – Last motion result code reported by the controller interface.
motion_done (bool | None) – Completion flag for the active motion command.
_robot (robot) – RobotBlockSet robot instance bound during activation.
_node (Node) – ROS 2 node used to create communication entities.
Initialize a RosControllerInterface instance.
Notes
This method only initializes the rbs_object base class.
ROS interfaces are initialized later via Activate().
- __init__() None[source]
Initialize a RosControllerInterface instance.
Notes
This method only initializes the rbs_object base class.
ROS interfaces are initialized later via Activate().
- Activate(robot: robot, node: rclpy.node.Node) None[source]
Activate the controller using the specified robot and ROS node.
This method initializes the controller with data from the robot, registers ROS interfaces, and marks the controller as active.
- Parameters:
robot (robot) – The robot instance providing joint and state data.
node (Node) – The ROS2 node used to create publishers, subscribers, or action clients.
- Return type:
None
Notes
This method should be called before issuing any control commands.
Derived classes typically call _init_ros_interfaces() to build ROS2 entities.
- class robotblockset.ros2.controllers_ros2.CsfCartesianImpedanceControllerInterface(robot: robot | None = None, ros_plugin_name: str = 'cartesian_impedance_controller', namespace: str = '', topic: str = 'cartesian_command', Kp: ndarray = array([2000.0000, 2000.0000, 2000.0000]), Kr: ndarray = array([30.0000, 30.0000, 30.0000]), R: ndarray = array([[1.0000, 0.0000, 0.0000], [0.0000, 1.0000, 0.0000], [0.0000, 0.0000, 1.0000]]), D: float = 2.0, command_frame: str = '')[source]
Bases:
RosControllerInterfaceInterface for a Cartesian impedance controller.
- Attributes:
Kp (np.ndarray) – Cartesian translational stiffness vector.
Kr (np.ndarray) – Cartesian rotational stiffness vector.
R (np.ndarray) – Rotation matrix used to orient the translational stiffness frame.
D (float) – Damping scaling factor.
Initialize the Cartesian impedance controller interface.
- Parameters:
robot (robot, optional) – Reserved robot argument kept for compatibility.
ros_plugin_name (str, optional) – Name of the ROS 2 controller plugin.
namespace (str, optional) – Namespace used for the controller topics.
topic (str, optional) – Cartesian command topic name.
Kp (np.ndarray, optional) – Translational stiffness gains.
Kr (np.ndarray, optional) – Rotational stiffness gains.
R (np.ndarray, optional) – Rotation matrix of the translational stiffness frame.
D (float, optional) – Damping scaling factor.
- __init__(robot: robot | None = None, ros_plugin_name: str = 'cartesian_impedance_controller', namespace: str = '', topic: str = 'cartesian_command', Kp: ndarray = array([2000.0000, 2000.0000, 2000.0000]), Kr: ndarray = array([30.0000, 30.0000, 30.0000]), R: ndarray = array([[1.0000, 0.0000, 0.0000], [0.0000, 1.0000, 0.0000], [0.0000, 0.0000, 1.0000]]), D: float = 2.0, command_frame: str = '') None[source]
Initialize the Cartesian impedance controller interface.
- Parameters:
robot (robot, optional) – Reserved robot argument kept for compatibility.
ros_plugin_name (str, optional) – Name of the ROS 2 controller plugin.
namespace (str, optional) – Namespace used for the controller topics.
topic (str, optional) – Cartesian command topic name.
Kp (np.ndarray, optional) – Translational stiffness gains.
Kr (np.ndarray, optional) – Rotational stiffness gains.
R (np.ndarray, optional) – Rotation matrix of the translational stiffness frame.
D (float, optional) – Damping scaling factor.
- GetCartesianCompliance() tuple[ndarray, ndarray, ndarray, float][source]
Return the active Cartesian impedance parameters.
- GetCartesianStiffness() tuple[ndarray, ndarray][source]
Return translational and rotational Cartesian stiffness.
- SetCartesianCompliance(Kp: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, Kr: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, R: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, D: float | None = None, hold_pose: bool = True) int[source]
Update Cartesian impedance parameters for subsequent CSF commands.
- SetCartesianStiffness(Kp: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, Kr: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, hold_pose: bool = True) int[source]
Update Cartesian stiffness while preserving orientation frame and damping.
- SetCartesianDamping(D: float | None = None, hold_pose: bool = True) int[source]
Update Cartesian damping while preserving stiffness.
- SetCartesianSoft(stiffness: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...], hold_pose: bool = True) int[source]
Scale Cartesian stiffness relative to this controller’s configured defaults.
- SetCartesianStiff(hold_pose: bool = True) int[source]
Set Cartesian impedance to the configured stiff defaults.
- SetCartesianCompliant(hold_pose: bool = True) int[source]
Set Cartesian impedance to zero stiffness.
- GoTo_X(x: ndarray, xdot: ndarray, trq: ndarray, wait: ndarray, Kp: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, Kr: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, R: ndarray | List[float] | List[int] | Tuple[float, ...] | Tuple[int, ...] | None = None, D: float | None = None, **kwargs: Any) int[source]
Publish a Cartesian impedance command.
- Parameters:
x (Pose3DType) – Desired task pose represented as position and quaternion.
xdot (Velocity3DType) – Desired task twist.
trq (WrenchType) – Desired feed-forward wrench.
wait (TimesType) – Kept for API compatibility; not used by the ROS 2 publisher.
Kp (ArrayLike, optional) – Translational stiffness vector.
Kr (ArrayLike, optional) – Rotational stiffness vector.
R (ArrayLike, optional) – Rotation matrix of the translational stiffness frame.
D (float, optional) – Damping scaling factor.
- Returns:
Motion result code.
- Return type:
int
Notes
The method updates the stored compliance parameters with the values used for the published command.
- class robotblockset.ros2.controllers_ros2.JointPositionControllerInterface(namespace: str = '', ros_plugin_name: str = 'command', topic: str = 'joint_position')[source]
Bases:
RosControllerInterfaceInterface for a joint-position controller.
- Attributes:
_namespace (str) – Namespace used for the controller topics.
_ros_plugin_name (str) – Name of the ROS 2 controller plugin.
_topic (str) – Topic used to publish joint commands.
_use_publisher (bool) – Indicates whether a publisher interface is active.
Initialize the joint-position controller interface.
This class provides a thin wrapper around a ROS 2 controller that accepts single-step joint commands through a trajectory_msgs/JointTrajectory topic. It is used for direct joint position setpoints without action goal management.
- Parameters:
namespace (str, optional) – Namespace prefix for the controller (for example
"arm"). If empty, no namespace prefix is used.ros_plugin_name (str, optional) – Name of the controller plugin (e.g.
"command"). If empty orNone, the controller is assumed to be directly under the namespace (no extra name component is added).topic (str, optional) – Topic name used when publishing joint-position commands.
Notes
Effective topic names are constructed as
<namespace>/<ros_plugin_name>/<topic>or<namespace>/<topic>.The actual ROS node is provided later through
RosControllerInterface.Activate().
- __init__(namespace: str = '', ros_plugin_name: str = 'command', topic: str = 'joint_position') None[source]
Initialize the joint-position controller interface.
This class provides a thin wrapper around a ROS 2 controller that accepts single-step joint commands through a trajectory_msgs/JointTrajectory topic. It is used for direct joint position setpoints without action goal management.
- Parameters:
namespace (str, optional) – Namespace prefix for the controller (for example
"arm"). If empty, no namespace prefix is used.ros_plugin_name (str, optional) – Name of the controller plugin (e.g.
"command"). If empty orNone, the controller is assumed to be directly under the namespace (no extra name component is added).topic (str, optional) – Topic name used when publishing joint-position commands.
Notes
Effective topic names are constructed as
<namespace>/<ros_plugin_name>/<topic>or<namespace>/<topic>.The actual ROS node is provided later through
RosControllerInterface.Activate().
- GoTo_q(q: ndarray, qdot: ndarray | None = None, trq: ndarray | None = None, wait: float | None = None, **kwargs: Any) int[source]
Publish a joint-position command.
- Parameters:
q (JointConfigurationType) – Desired joint positions (nj,).
qdot (JointVelocityType, optional) – Desired joint velocities (nj,).
trq (JointTorqueType, optional) – Desired joint torques (nj,).
wait (float, optional) – Kept for API compatibility; not used by the publisher interface.
- Returns:
Motion result code.
- Return type:
int
- Raises:
ValueError – If control is not implemented in controller.
- class robotblockset.ros2.controllers_ros2.JointTrajectoryControllerInterface(namespace: str = '', ros_plugin_name: str = 'joint_trajectory_controller', topic: str = 'joint_trajectory', action: str = 'follow_joint_trajectory')[source]
Bases:
RosControllerInterfaceInterface for a joint-trajectory controller.
- Attributes:
_namespace (str) – Namespace used for the controller topics and actions.
_ros_plugin_name (str) – Name of the ROS 2 controller plugin.
_topic (str) – Topic used for trajectory publishing.
_action (str) – Action used for trajectory execution.
_use_publisher (bool) – Indicates whether the publisher path is active.
_use_action (bool) – Indicates whether the action-client path is active.
Initialize the joint-trajectory controller interface.
This class provides a thin wrapper around a ROS 2 controller manager plugin exposing a control_msgs/FollowJointTrajectory action or a trajectory_msgs/JointTrajectory topic. It can be configured to either communicate via an action client or a plain publisher.
- Parameters:
namespace (str, optional) – Namespace prefix for the controller (for example
"arm"). If empty, no namespace prefix is used.ros_plugin_name (str, optional) – Name of the controller plugin (e.g.
"joint_trajectory_controller"). If empty orNone, the controller is assumed to be directly under the namespace (no extra name component is added).topic (str, optional) – Topic name used when publishing
JointTrajectorycommands.action (str, optional) – Action name used when creating a
FollowJointTrajectoryaction client. If notNone, this path is preferred over publishing.
Notes
Effective topic/action names are constructed as:
<namespace>/<ros_plugin_name>/<action>or<namespace>/<action><namespace>/<ros_plugin_name>/<topic>or<namespace>/<topic>
The actual ROS node is provided later through
RosControllerInterface.Activate().
- __init__(namespace: str = '', ros_plugin_name: str = 'joint_trajectory_controller', topic: str = 'joint_trajectory', action: str = 'follow_joint_trajectory') None[source]
Initialize the joint-trajectory controller interface.
This class provides a thin wrapper around a ROS 2 controller manager plugin exposing a control_msgs/FollowJointTrajectory action or a trajectory_msgs/JointTrajectory topic. It can be configured to either communicate via an action client or a plain publisher.
- Parameters:
namespace (str, optional) – Namespace prefix for the controller (for example
"arm"). If empty, no namespace prefix is used.ros_plugin_name (str, optional) – Name of the controller plugin (e.g.
"joint_trajectory_controller"). If empty orNone, the controller is assumed to be directly under the namespace (no extra name component is added).topic (str, optional) – Topic name used when publishing
JointTrajectorycommands.action (str, optional) – Action name used when creating a
FollowJointTrajectoryaction client. If notNone, this path is preferred over publishing.
Notes
Effective topic/action names are constructed as:
<namespace>/<ros_plugin_name>/<action>or<namespace>/<action><namespace>/<ros_plugin_name>/<topic>or<namespace>/<topic>
The actual ROS node is provided later through
RosControllerInterface.Activate().
- GoTo_qtraj(q: ndarray, qdot: ndarray, qddot: ndarray, time: ndarray) int[source]
Command the robot to follow a joint trajectory.
- Parameters:
q (JointPathType) – Desired joint positions for the trajectory (n, nj), where n is the number of trajectory points.
qdot (JointPathType) – Desired joint velocities for the trajectory (n, nj), where n is the number of trajectory points.
qddot (JointPathType) – Desired joint accelerations for the trajectory. The current interfaces do not use these values.
time (TimesType) – Time points for the trajectory (n,).
- Returns:
Motion result code.
- Return type:
int
Notes
The command is routed to either the publisher-based or action-based trajectory backend depending on the active ROS 2 interface.
- GoTo_qtraj_pub(q: ndarray, qdot: ndarray, qddot: ndarray, time: ndarray) int[source]
Publish a joint trajectory through the ROS 2 topic interface.
- Parameters:
q (JointPathType) – Desired joint positions for the trajectory (n, nj), where n is the number of trajectory points.
qdot (JointPathType) – Desired joint velocities for the trajectory (n, nj), where n is the number of trajectory points.
qddot (JointPathType) – Desired joint accelerations for the trajectory. The publisher path does not use these values.
time (TimesType) – Time points for the trajectory (n,).
- Returns:
Motion result code.
- Return type:
int
Notes
On success, the final trajectory point is copied into the robot command state and the motion is marked complete immediately after publishing.
- GoTo_qtraj_act(q: ndarray, qdot: ndarray, qddot: ndarray, time: ndarray) int[source]
Send a joint trajectory through the ROS 2 action interface.
- Parameters:
q (JointPathType) – Desired joint positions for the trajectory (n, nj), where n is the number of trajectory points.
qdot (JointPathType) – Desired joint velocities for the trajectory (n, nj), where n is the number of trajectory points.
qddot (JointPathType) – Desired joint accelerations for the trajectory. The action path does not use these values.
time (TimesType) – Time points for the trajectory (n,).
- Returns:
Motion result code.
- Return type:
int
Notes
The method waits for the action response and, when enabled, for the final motion-completion callback.
- qtraj_goal_response_callback(future: rclpy.task.Future) None[source]
Callback executed when the action server responds to a trajectory goal request.
- Parameters:
future (Future) – Future containing the
ClientGoalHandleresult from thesend_goal_asynccall.
Notes
If the goal is rejected,
motion_resultis set toINVALID_GOAL.If the goal is accepted, the completion callback is registered through
get_result_async.
- qtraj_get_result_callback(future: rclpy.task.Future) None[source]
Callback executed when the trajectory has finished execution.
- Parameters:
future (Future) – Future containing the result of the action execution.
Notes
Updates
motion_resultwith the final action result error code.Sets
motion_donetoTrueindicating motion completion.Logs success or error based on trajectory outcome.
If motion was aborted, sets
_abort = Trueon the robot instance (if available).
- Return type:
None
- qtraj_feedback_callback(feedback_msg: control_msgs.action.FollowJointTrajectory.Feedback) None[source]
Callback executed when feedback is received during trajectory execution.
- Parameters:
feedback_msg (FollowJointTrajectory.Feedback) – Feedback message containing intermediate trajectory updates.
Notes
Stores feedback data in
last_feedback.Feedback may include joint states and progress updates.
Logging is optional and typically kept disabled for performance reasons.
- Return type:
None
- abort_motion() int | None[source]
Attempt to abort the current trajectory motion by canceling the active FollowJointTrajectory goal.
This method sends a cancel request to the ROS2 action server for the active trajectory goal. If no goal is currently active, a warning is logged and the method exits without performing any action.
Notes
This does not immediately stop the robot; it requests the controller to perform a controlled stop.
After the cancel request is sent, the result is processed in
qtraj_cancel_done_callback().
- Returns:
Motion result code of the cancel operation, or
Noneif no active goal exists.- Return type:
int | None
- qtraj_cancel_done_callback(future: rclpy.task.Future) None[source]
Process the result of a cancel request for an active trajectory goal.
- Parameters:
future (Future) – Future object returned from
cancel_goal_asynccontaining the cancel response.
Notes
Logs whether cancellation was successful.
Does not raise exceptions; any failure is logged.
This callback does not wait for the robot to fully stop; it only indicates whether the cancel request was accepted.
Classes
Interface for a Cartesian impedance controller. |
|
Interface for a joint-position controller. |
|
Interface for a joint-trajectory controller. |
|
Generic interface for a ROS2-based controller. |
|
|
Helper for ROS 2 controller-manager lifecycle operations. |