bullet3
bullet3 copied to clipboard
Cartpole environment: something wrong with the physic engine
I'm trying to create my own OpenAI compatible environment for the "cartpole.urdf" robot .
The problem: Even running the cart on one direction (+ 15m on the x axis), the pole stays straight in its 0, 0, 0 position without falling down in any direction. But this is exactly the opposite behavior that the cartpole should have: as far as the cart starts going in one direction, the instability of the system should let the pole falling down in one direction:
Here the code, to reproduce the problem:
import pybullet as p
import numpy as np
import torch
import time
import gym
import os
from collections import UserDict
registry = UserDict(gym.envs.registration.registry)
registry.env_specs = gym.envs.registration.registry
gym.envs.registration.registry = registry
import pybullet_data
import pybullet_envs
robot_name = "cartpole.urdf"
class EnvironmentRL:
def __init__(self):
pass
def make(self, name_env):
self.name_env = name_env
self.client = p.connect(p.GUI)
p.resetSimulation()
p.setAdditionalSearchPath(pybullet_data.getDataPath())
self.init_env()
def init_env(self):
p.setGravity(0, 0, -9.81)
p.setRealTimeSimulation(0)
p.loadURDF("plane.urdf", [0, 0, 0], [0, 0, 0, 1])
self.robot_id = p.loadURDF(fileName = self.name_env, basePosition = [0, 0, 0], baseOrientation = [0, 0, 0, 1], useFixedBase = True)
self.active = True
self.termination = False
self.truncation = False
def reset(self):
p.resetSimulation(self.client)
self.init_env(self.name_env)
position = np.asarray(p.getLinkState(self.robot_id, 0)[0])
orientation = p.getLinkState(self.robot_id, 1)[1]
orientation = np.asarray(p.getEulerFromQuaternion(orientation))
return position[0], orientation[1]
def step(self, action):
p.setJointMotorControlArray(self.robot_id, [0], p.POSITION_CONTROL, targetPositions = [action], physicsClientId = self.client)
p.configureDebugVisualizer(p.COV_ENABLE_SINGLE_STEP_RENDERING)
p.stepSimulation()
position = np.asarray(p.getLinkState(self.robot_id, 0)[0])
orientation = p.getLinkState(self.robot_id, 1)[1]
orientation = np.asarray(p.getEulerFromQuaternion(orientation))
reward = -1
if (position[0] > 2.4) or (position[0] < -2.4):
self.termination = True
if (orientation[1] < -12) or (orientation[1] > 12):
self.termination = True
return position[0], orientation[1], reward, self.termination
def close(self):
p.disconnect(self.client)
env = EnvironmentRL()
env.make(robot_name)
for i in range(200):
action = 15
print(env.step(action))
time.sleep(0.1)
Do I need to actively move the pole in one way or the other? Or it is suppose, that the pole starts moving "automatically" due to the instability once the cart starts moving?
My system is: Pybullet: 3.25
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.04 DISTRIB_CODENAME=jammy DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS" PRETTY_NAME="Ubuntu 22.04.3 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.3 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy