CommandManager#
- class genesis_forge.managers.command.CommandManager(env: GenesisEnv, range: Tuple[float, float] | dict[str, Tuple[float, float]], resample_time_sec: float = 5.0)[source]#
Bases:
BaseManagerGenerates a command from uniform distribution of values. You can use this to regularly sample a commands for each environment, such as a height command, a target destination, or a specific pose.
- Parameters:
env – The environment to control
range – The number range, or dict of ranges, to generate target command(s) for
resample_time_sec – The time interval between changing the command
Example:
class MyEnv(GenesisEnv): def config(self): # Create a height command self.height_command = CommandManager(self, range=(0.1, 0.2)) # Rewards RewardManager( self, logging_enabled=True, cfg={ "base_height_target": { "weight": -50.0, "fn": rewards.base_height, "params": { "height_command": self.height_command, }, }, # ... other rewards ... }, ) # Observations ObservationManager( self, cfg={ "height_cmd": {"fn": self.height_command.observation}, # ... other observations ... }, )
- build()#
Called when the scene is built
- get_command(range_key: str) torch.Tensor[source]#
If the range is a dict, get the command values for the given key.
- get_command_idx(key: str) int[source]#
If the range is a dict, get the command index for the given key.
- increment_range(range_key: str, increment: float | tuple[float, float], limit: float | tuple[float, float] = None)[source]#
Increment a command range target values by the given amount, with an optional limit.
Both increment and limit can be passed as a single float or a tuple of two floats. When they are tuples, they represent the increment and limit for both min and max. When they are a single float, they will be applied to both min and max as (-value, +value).
- Example::
# Increment the range by 0.5 (+/-) for both min and max command_manager.increment_range(“height”, 0.5)
# Increment the range min by -0.25 and max by 1.0 command_manager.increment_range(“height”, (-0.25, 1.0))
# Increment the range by (-0.25, 1.0) and keep min above -0.5 and max below 2.0 command_manager.increment_range(“height”, (-0.25, 1.0), (-0.5, 2.0))
- Parameters:
range_key – The key of the command range to increment.
increment – The amount to increment the min & max range values by (+/-). If this is a tuple, it will be: (min_increment, max_increment). If this is a single float, it will be applied to both min and max as (-increment, +increment).
limit – Do not set the values beyond this limit range. This is a tuple of (min_limit, max_limit).
- observation(env: GenesisEnv) torch.Tensor[source]#
Function that returns the current command for each environment.
- set_command(range_key: str, value: torch.Tensor, envs_idx: list[int] | None = None)[source]#
Update a command value for selected environments.
- use_external_controller(controller: Callable[[int], Tuple[float, float] | dict[str, Tuple[float, float]]])[source]#
Bypass the internal command controller, and generate the command values with an external control function. This can be used to connect a gamepad, joystick, or other external controller to the command manager.
- Parameters:
controller – A function that takes the step index and returns a tensor of command values with the shape (num_envs, num_ranges).
Example:
N_ENVS = 1 MIN_HEIGHT = 0.1 MAX_HEIGHT = 0.2 # Create environment class MyEnv(GenesisEnv): def config(self): self.height_command = CommandManager(self, range=(MIN_HEIGHT, MAX_HEIGHT)) # ... # Setup gamepad gamepad = Gamepad(GAMEPAD_PRODUCT) cmd_buffer = torch.zeros((N_ENVS, 1), device=gs.device) def gamepad_controller(_step): a_pressed = "a" in gamepad.state.buttons cmd_buffer[:, 0] = MAX_HEIGHT if a_pressed else MIN_HEIGHT return cmd_buffer # Create environment & connect gamepad env = MyEnv(num_envs=N_ENVS) env.build() env.command_manager.use_external_controller(gamepad_controller)
- use_gamepad(gamepad: Gamepad, range_axis: int | dict[str, int], invert_axis: bool | dict[str, bool] = False)[source]#
A wrapper around use_external_controller that converts a gamepad joystick axis to a command value.
- Parameters:
gamepad – The gamepad wrapper to use.
range_axis – The axis or dict of axes to use for the command value. This should match the range init param.
invert_axis – Whether to invert each mapped axis before converting it into the command range.
Example:
# Create environment class MyEnv(GenesisEnv): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.height_command = CommandManager(self, range=(0.1, 0.2)) # ... # Connect gamepad from genesis_forge.gamepads import Gamepad gamepad = Gamepad() # Create environment & connect gamepad env = MyEnv(num_envs=1) env.build() # Connect joystick axis 3 to the height command env.height_command.use_gamepad(gamepad, range_axis=3)
Example with multiple ranges:
# Create environment class MyEnv(GenesisEnv): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.height_command = CommandManager( self, range={ "cmd1": (-2.0, 2.0), "cmd2": (1.0, 5.0), } ) # ... # Connect gamepad from genesis_forge.gamepads import Gamepad gamepad = Gamepad() # Create environment & connect gamepad env = MyEnv(num_envs=1) env.build() # Connect joystick axis 2 and 3 to to the indvidual ranges env.height_command.use_gamepad( gamepad, range_axis={ "cmd1": 2, "cmd2": 3, })
- property command: torch.Tensor#
The desired command value. Shape is (num_envs, num_ranges).