pyrcf.components.agents
Defines agents that can be used to compute control commands to be sent to a robot given a global plan (target/task). When using ‘classical’ methods, an agent may just be a local planner + a controller (use the PlannerControllerAgent in this case). When using machine-learned controller, this could be a policy that direcly uses the global plan and outputs a robot command (implement an agent from MLAgentBase).
Submodules
Classes
An agent is an entity that observes the state of the robot and responds with a control |
|
Dummy Agent for testing pipeline. |
|
A simple implementation of Agent using a local planner object and controller. |
|
An abstract base class machine-learned controller agents. |
|
A (abstract) base class that provides functionalities to use a torchscript model file |
Package Contents
- class pyrcf.components.agents.AgentBase
Bases:
pyrcf.components.pyrcf_component.PyRCFComponentAn agent is an entity that observes the state of the robot and responds with a control action.
With this in mind, an agent for classical control cases would be an entity that combines the functions of a ‘local planner’ and ‘controller’ into a single entity called ‘Agent’, which observes the state of the robot interface and responds by sending commands to the robot, so as to follow the objective of following the ‘global plan’ from the global planner.
- abstract get_action(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float, dt: float) pyrcf.core.types.RobotCmd
Compute the control command to be executed given the current state and the global plan.
- Parameters:
robot_state (RobotState) – Current robot state. This is equivalent to ‘observation’ in standard learning-based agents.
global_plan (GlobalMotionPlan) – The reference global plan to follow.
t (float, optional) – the current time signature of the control loop. Defaults to None (controllers may or may not need this).
dt (float, optional) – the time since the last control loop. Defaults to None (controllers may or may not need this).
- Returns:
The output control command (action) to be sent to the robot.
- Return type:
- Raises:
NotImplementedError – Raised if this method is not implemented by the child class.
- abstract get_last_output() Tuple[pyrcf.core.types.LocalMotionPlan, pyrcf.core.types.RobotCmd]
Should return the last computed outputs by this agent.
- Returns:
- Output local plan and control command from this agent.
NOTE: Either of these can be None, if the agent does not compute them.
- Return type:
Tuple[LocalMotionPlan, RobotCmd]
- class pyrcf.components.agents.DummyAgent(squawk: bool = True)
Bases:
AgentBaseDummy Agent for testing pipeline.
- _squawk = True
- get_action(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float, dt: float) pyrcf.core.types.RobotCmd
This is a dummy method for sanity checking the control loop.
- get_last_output() Tuple[pyrcf.core.types.LocalMotionPlan | pyrcf.core.types.RobotCmd]
Should return the last computed outputs by this agent.
- Returns:
- Output local plan and control command from this agent.
NOTE: Either of these can be None, if the agent does not compute them.
- Return type:
Tuple[LocalMotionPlan, RobotCmd]
- class pyrcf.components.agents.PlannerControllerAgent(local_planner: pyrcf.components.local_planners.LocalPlannerBase, controller: pyrcf.components.controllers.ControllerBase)
Bases:
pyrcf.components.agents.agent_base.AgentBaseA simple implementation of Agent using a local planner object and controller.
This agent simply calls the local planner update and controller update steps in sequence.
- local_planner
- controller
- _latest_local_plan = None
- _latest_ctrl_cmd = None
- get_action(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float = None, dt: float = None) pyrcf.core.types.RobotCmd
Compute the control command to be executed given the current state and the global plan.
- Parameters:
robot_state (RobotState) – Current robot state. This is equivalent to ‘observation’ in standard learning-based agents.
global_plan (GlobalMotionPlan) – The reference global plan to follow.
t (float, optional) – the current time signature of the control loop. Defaults to None (controllers may or may not need this).
dt (float, optional) – the time since the last control loop. Defaults to None (controllers may or may not need this).
- Returns:
The output control command (action) to be sent to the robot.
- Return type:
- Raises:
NotImplementedError – Raised if this method is not implemented by the child class.
- get_latest_local_plan() pyrcf.core.types.LocalMotionPlan
- get_latest_ctrl_cmd() pyrcf.core.types.RobotCmd
- get_last_output() Tuple[pyrcf.core.types.LocalMotionPlan, pyrcf.core.types.RobotCmd]
Should return the last computed outputs by this agent.
- Returns:
- Output local plan and control command from this agent.
NOTE: Either of these can be None, if the agent does not compute them.
- Return type:
Tuple[LocalMotionPlan, RobotCmd]
- shutdown()
Cleanly shutdown the PyRCF component. Override in child class if required. The base class implements an empty function.
- get_class_info() str
Override with custom string if needed.
- class pyrcf.components.agents.MLAgentBase
Bases:
pyrcf.components.agents.agent_base.AgentBase,abc.ABCAn abstract base class machine-learned controller agents.
See docstrings for each method to be implemented.
- abstract initialise_robot_cmd(joint_states: pyrcf.core.types.JointStates)
Override this method in child class if custom initilisation is required (e.g. joint name order). By default, this method sets the joint name and position values to be equal to the input joint states object, with kp and kd set to be the default (in constructor).
- abstract update_input_to_model(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float, dt: float) numpy.ndarray
Should update (self._input_ndarray) using appropriate values (input to model). This method has access to self._latest_ctrl_cmd (type RobotCmd) as well if needed. (NOTE: self._latest_ctrl_cmd is set to be the initial robot joint positions (zero velocities and efforts commands) with default_kp and default_kd at start).
- abstract update_cmd_from_model_output(model_output: numpy.ndarray, robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float, dt: float) None
Should update self._latest_robot_cmd (type RobotCmd) using the output from the NN model.
- Parameters:
model_output (np.ndarray) – the numpy array created from the output tensor from the model after the inference query was done. This is the output of the neural network. This method should use this object to update self._latest_robot_cmd to be sent to the robot.
- abstract get_action(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float = None, dt: float = None) pyrcf.core.types.RobotCmd
Should return the control command given the current robot state and global plan.
- Parameters:
robot_state (RobotState) – The current state information from the robot.
global_plan (GlobalMotionPlan) – the latest global plan generated by the global planner used in the loop.
t (float, optional) – the current time signature of the control loop. Defaults to None (controllers may or may not need this).
dt (float, optional) – the time since the last control loop. Defaults to None (controllers may or may not need this).
- Returns:
The output control command to be sent to the robot.
- Return type:
- class pyrcf.components.agents.TorchScriptAgentBase(model_file: str, input_dims: int, device: Literal['cpu', 0] = 'cpu', warmup_iterations: int = 10, default_kp: numpy.ndarray | float = 20, default_kd: numpy.ndarray | float = 1.0, update_rate: float = None, clock: pyrcf.utils.time_utils.ClockBase = PythonPerfClock(), dtype: torch.dtype = None)
Bases:
pyrcf.components.agents.ml_agent_base.MLAgentBaseA (abstract) base class that provides functionalities to use a torchscript model file and use it’s inference mode to perform control update.
- Child class has to override two methods:
update_input_to_model
update_cmd_from_model_output
See docstrings for each method in MLAgentBase class.
- _torch_device
- _model
- _device = 'cpu'
- _dtype = None
- _dims
- _tensor_inp
- _input_ndarray
The input array to be updated by the child class using the current robot state and global plan commands. This will be passed to the model as input during inference
- _latest_ctrl_cmd: pyrcf.core.types.RobotCmd = None
The RobotCmd object that can be use in the update_input_to_model method (if required) as the last sent command to the robot. This object has to be updated by the child class’s update_cmd_from_model_output using the output from the model inference step.
- _default_kp = 20
- _default_kd = 1.0
- _rate = None
- _should_run()
- initialise_robot_cmd(joint_states: pyrcf.core.types.JointStates)
Override this method in child class if custom initilisation is required (e.g. joint name order). By default, this method sets the joint name and position values to be equal to the input joint states object, with kp and kd set to be the default (in constructor).
- abstract update_input_to_model(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float, dt: float) numpy.ndarray
Should update (self._input_ndarray) using appropriate values (input to model). This method has access to self._latest_ctrl_cmd (type RobotCmd) as well if needed. (NOTE: self._latest_ctrl_cmd is set to be the initial robot joint positions (zero velocities and efforts commands) with default_kp and default_kd at start).
- abstract update_cmd_from_model_output(model_output: numpy.ndarray, robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float, dt: float) None
Should update self._latest_robot_cmd (type RobotCmd) using the output from the NN model.
- Parameters:
model_output (np.ndarray) – the numpy array created from the output tensor from the model after the inference query was done. This is the output of the neural network. This method should use this object to update self._latest_robot_cmd to be sent to the robot.
- get_action(robot_state: pyrcf.core.types.RobotState, global_plan: pyrcf.core.types.GlobalMotionPlan, t: float = None, dt: float = None) pyrcf.core.types.RobotCmd
Should return the control command given the current robot state and global plan.
- Parameters:
robot_state (RobotState) – The current state information from the robot.
global_plan (GlobalMotionPlan) – the latest global plan generated by the global planner used in the loop.
t (float, optional) – the current time signature of the control loop. Defaults to None (controllers may or may not need this).
dt (float, optional) – the time since the last control loop. Defaults to None (controllers may or may not need this).
- Returns:
The output control command to be sent to the robot.
- Return type:
- get_last_output() Tuple[pyrcf.core.types.LocalMotionPlan, pyrcf.core.types.RobotCmd]
Should return the last computed outputs by this agent.
- Returns:
- Output local plan and control command from this agent.
NOTE: Either of these can be None, if the agent does not compute them.
- Return type:
Tuple[LocalMotionPlan, RobotCmd]