pyrcf.components.agents.torchscript_agent_base

Classes

TorchScriptAgentBase

A (abstract) base class that provides functionalities to use a torchscript model file

Module Contents

class pyrcf.components.agents.torchscript_agent_base.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.MLAgentBase

A (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
_model
_device
_dtype
_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
_default_kd
_rate
_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:

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]