upkie 6.1.0
Open-source wheeled biped robots
|
Base class for Upkie environments. More...
Public Member Functions | |
None | __init__ (self, float fall_pitch=1.0, Optional[float] frequency=200.0, bool frequency_checks=True, Optional[RobotState] init_state=None, bool regulate_frequency=True, str shm_name="/upkie", Optional[dict] spine_config=None, int spine_retries=10) |
Initialize environment. More... | |
def | __del__ (self) |
Stop the spine when deleting the environment instance. | |
None | close (self) |
Stop the spine properly. | |
Optional[float] | dt (self) |
Regulated period of the control loop in seconds, or None if there is no loop frequency regulation. | |
Optional[float] | frequency (self) |
Regulated frequency of the control loop in Hz, or None if there is no loop frequency regulation. | |
None | update_init_rand (self, **kwargs) |
Update initial-state randomization. More... | |
Tuple[np.ndarray, dict] | reset (self, *Optional[int] seed=None, Optional[dict] options=None) |
Resets the spine and get an initial observation. More... | |
Tuple[np.ndarray, float, bool, bool, dict] | step (self, np.ndarray action) |
Run one timestep of the environment's dynamics. More... | |
bool | detect_fall (self, dict spine_observation) |
Detect a fall based on the base-to-world pitch angle. More... | |
def | get_env_observation (self, dict spine_observation) |
Extract environment observation from spine observation dictionary. More... | |
dict | get_spine_action (self, action) |
Convert environment action to a spine action dictionary. More... | |
None | log (self, str name, Any entry) |
Log a new entry to the "log" key of the action dictionary. More... | |
None | bullet_extra (self, dict bullet_action) |
Prepend for the next step an extra action for the Bullet spine. More... | |
Base class for Upkie environments.
This environment base class provides a number of features:
It otherwise defines three abstract methods to be defined by derived environments to:
None upkie.envs.upkie_base_env.UpkieBaseEnv.__init__ | ( | self, | |
float | fall_pitch = 1.0 , |
||
Optional[float] | frequency = 200.0 , |
||
bool | frequency_checks = True , |
||
Optional[RobotState] | init_state = None , |
||
bool | regulate_frequency = True , |
||
str | shm_name = "/upkie" , |
||
Optional[dict] | spine_config = None , |
||
int | spine_retries = 10 |
||
) |
Initialize environment.
fall_pitch | Fall detection pitch angle, in radians. |
frequency | Regulated frequency of the control loop, in Hz. Can be prescribed even when regulate_frequency is unset, in which case self.dt will be defined but the loop frequency will not be regulated. |
frequency_checks | If regulate_frequency is set and this parameter is true (default), a warning is issued every time the control loop runs slower than the desired frequency . Set this parameter to false to disable these warnings. |
init_state | Initial state of the robot, only used in simulation. |
regulate_frequency | If set (default), the environment will regulate the control loop frequency to the value prescribed in frequency . |
shm_name | Name of shared-memory file to exchange with the spine. |
spine_config | Additional spine configuration overriding the default upkie.config.SPINE_CONFIG . The combined configuration dictionary is sent to the spine at every reset. |
spine_retries | Number of times to try opening the shared-memory file to communicate with the spine. |
SpineError | If the spine did not respond after the prescribed number of trials. |
Reimplemented in upkie.envs.upkie_ground_velocity.UpkieGroundVelocity, upkie.envs.upkie_servo_positions.UpkieServoPositions, upkie.envs.upkie_servo_torques.UpkieServoTorques, and upkie.envs.upkie_servos.UpkieServos.
None upkie.envs.upkie_base_env.UpkieBaseEnv.bullet_extra | ( | self, | |
dict | bullet_action | ||
) |
Prepend for the next step an extra action for the Bullet spine.
This extra action can be for instance a set of external forces applied to some robot bodies.
bullet_action | Action dictionary processed by the Bullet spine. |
bool upkie.envs.upkie_base_env.UpkieBaseEnv.detect_fall | ( | self, | |
dict | spine_observation | ||
) |
Detect a fall based on the base-to-world pitch angle.
spine_observation | Observation dictionary with a "base_orientation" key. This requires the upkie::cpp::observers::BaseOrientation observer in the spine's observer pipeline. |
def upkie.envs.upkie_base_env.UpkieBaseEnv.get_env_observation | ( | self, | |
dict | spine_observation | ||
) |
Extract environment observation from spine observation dictionary.
spine_observation | Spine observation dictionary. |
Reimplemented in upkie.envs.upkie_ground_velocity.UpkieGroundVelocity, and upkie.envs.upkie_servos.UpkieServos.
dict upkie.envs.upkie_base_env.UpkieBaseEnv.get_spine_action | ( | self, | |
action | |||
) |
Convert environment action to a spine action dictionary.
action | Environment action. |
Reimplemented in upkie.envs.upkie_servos.UpkieServos, and upkie.envs.upkie_ground_velocity.UpkieGroundVelocity.
None upkie.envs.upkie_base_env.UpkieBaseEnv.log | ( | self, | |
str | name, | ||
Any | entry | ||
) |
Log a new entry to the "log" key of the action dictionary.
name | Name of the entry. |
entry | Dictionary to log along with the actual action. |
Tuple[np.ndarray, dict] upkie.envs.upkie_base_env.UpkieBaseEnv.reset | ( | self, | |
*Optional[int] | seed = None , |
||
Optional[dict] | options = None |
||
) |
Resets the spine and get an initial observation.
seed | Number used to initialize the environment’s internal random number generator. |
options | Currently unused. |
observation
: Initial vectorized observation, i.e. an element of the environment's observation_space
.info
: Dictionary with auxiliary diagnostic information. For Upkie this is the full observation dictionary sent by the spine. Reimplemented in upkie.envs.upkie_ground_velocity.UpkieGroundVelocity.
Tuple[np.ndarray, float, bool, bool, dict] upkie.envs.upkie_base_env.UpkieBaseEnv.step | ( | self, | |
np.ndarray | action | ||
) |
Run one timestep of the environment's dynamics.
When the end of the episode is reached, you are responsible for calling reset()
to reset the environment's state.
action | Action from the agent. |
observation
: Observation of the environment, i.e. an element of its observation_space
.reward
: Reward returned after taking the action.terminated
: Whether the agent reached a terminal state, which may be a good or a bad thing. When true, the user needs to call reset()
.truncated
: Whether the episode is reaching max number of steps. This boolean can signal a premature end of the episode, i.e. before a terminal state is reached. When true, the user needs to call reset()
.info
: Dictionary with auxiliary diagnostic information. For us this is the full observation dictionary coming from the spine. None upkie.envs.upkie_base_env.UpkieBaseEnv.update_init_rand | ( | self, | |
** | kwargs | ||
) |
Update initial-state randomization.
Keyword arguments are forwarded as is to upkie.utils.robot_state_randomization.RobotStateRandomization.update.