예제 #1
0
    def gazebo(self) -> bindings.GazeboWrapper:
        if self._gazebo_wrapper:
            assert self._gazebo_wrapper.getPhysicsData().rtf == self._rtf, \
                "The RTF of gazebo does not match the configuration"
            assert 1 / self._gazebo_wrapper.getPhysicsData().maxStepSize == \
                self._physics_rate, \
                "The physics rate of gazebo does not match the configuration"

            return self._gazebo_wrapper

        # =================
        # INITIALIZE GAZEBO
        # =================

        assert self._rtf, "Real Time Factor was not set"
        assert self.agent_rate, "Agent rate was not set"
        assert self._physics_rate, "Physics rate was not set"

        logger.debug("Starting gazebo")
        logger.debug(f"Agent rate: {self.agent_rate} Hz")
        logger.debug(f"Physics rate: {self._physics_rate} Hz")
        logger.debug(f"Simulation RTF: {self._rtf}")

        # Compute the number of physics iteration to execute at every environment step
        num_of_iterations_per_gazebo_step = self._physics_rate / self.agent_rate

        if num_of_iterations_per_gazebo_step != int(
                num_of_iterations_per_gazebo_step):
            logger.warn(
                "Rounding the number of iterations to {} from the nominal {}".
                format(int(num_of_iterations_per_gazebo_step),
                       num_of_iterations_per_gazebo_step))
        else:
            logger.debug("Setting {} iteration per simulator step".format(
                int(num_of_iterations_per_gazebo_step)))

        # Create the GazeboWrapper object
        self._gazebo_wrapper = bindings.GazeboWrapper(
            int(num_of_iterations_per_gazebo_step), self._rtf,
            self._physics_rate)

        # Set the verbosity
        logger.set_level(gym.logger.MIN_LEVEL)

        # Initialize the world
        world_ok = self._gazebo_wrapper.setupGazeboWorld(self._world)
        assert world_ok, "Failed to initialize the gazebo world"

        # Initialize the ignition gazebo wrapper
        gazebo_initialized = self._gazebo_wrapper.initialize()
        assert gazebo_initialized, "Failed to initialize ignition gazebo"

        return self._gazebo_wrapper
예제 #2
0
    def __init__(self,
                 physics_rate: float,
                 iterations: int = int(1),
                 rtf=float(np.finfo(np.float32).max)):
        self.simulator = bindings.GazeboWrapper(iterations, rtf, physics_rate)
        assert self.simulator

        self.simulator.setVerbosity(4)

        empty_world = resource_finder.find_resource("DefaultEmptyWorld.world")
        ok_world = self.simulator.setupGazeboWorld(worldFile=empty_world)
        assert ok_world

        ok_initialize = self.simulator.initialize()
        assert ok_initialize
예제 #3
0
def test_joint_controller():
    # ==========
    # PARAMETERS
    # ==========

    agent_rate = 100.0
    physics_rate = 1000.0
    controller_rate = 500.0

    plugin_data = bindings.PluginData()
    plugin_data.libName = "RobotController"
    plugin_data.className = "gympp::plugins::RobotController"

    # Find and load the model SDF file
    model_sdf_file = resource_finder.find_resource("CartPole/CartPole.urdf")
    with open(model_sdf_file, "r") as stream:
        model_sdf_string = stream.read()

    # Initialize the model data
    model_data = bindings.ModelInitData()
    model_data.sdfString = model_sdf_string

    # Get the model name
    model_name = bindings.GazeboWrapper.getModelNameFromSDF(model_sdf_string)
    robot_name = model_name

    # =============
    # CONFIGURATION
    # =============

    # Create the gazebo wrapper
    num_of_iterations = int(physics_rate / agent_rate)
    desired_rtf = float(np.finfo(np.float32).max)
    gazebo = bindings.GazeboWrapper(num_of_iterations, desired_rtf, physics_rate)
    assert gazebo, "Failed to get the gazebo wrapper"

    # Set the verbosity
    logger.set_level(gym.logger.DEBUG)

    # Initialize the world
    world_ok = gazebo.setupGazeboWorld("DefaultEmptyWorld.world")
    assert world_ok, "Failed to initialize the gazebo world"

    # Initialize the ignition gazebo wrapper (creates a paused simulation)
    gazebo_initialized = gazebo.initialize()
    assert gazebo_initialized, "Failed to initialize ignition gazebo"

    # Insert the model
    model_ok = gazebo.insertModel(model_data, plugin_data)
    assert model_ok, "Failed to insert the model in the simulation"

    assert bindings.RobotSingleton_get().exists(robot_name), \
        "The robot interface was not registered in the singleton"

    # Extract the robot interface from the singleton
    robot_weak_ptr = bindings.RobotSingleton_get().getRobot(robot_name)
    assert not robot_weak_ptr.expired(), "The Robot object has expired"
    assert robot_weak_ptr.lock(), \
        "The returned Robot object does not contain a valid interface"
    assert robot_weak_ptr.lock().valid(), "The Robot object is not valid"

    # Get the pointer to the robot interface
    robot = robot_weak_ptr.lock()

    # Set the default update rate
    robot.setdt(1 / controller_rate)

    # Control the cart joint in position
    ok_cm = robot.setJointControlMode("linear", bindings.JointControlMode_Position)
    assert ok_cm, "Failed to control the cart joint in position"

    # Set the PID of the cart joint
    pid = bindings.PID(10000, 1000, 1000)
    pid_ok = robot.setJointPID("linear", pid)
    assert pid_ok, "Failed to set the PID of the cart joint"

    # Reset the robot state
    robot.resetJoint("pivot", 0, 0)
    robot.resetJoint("linear", 0, 0)

    # Generate the cart trajectory
    cart_ref = np.fromiter(
        (0.2 * np.sin(2 * np.pi * 0.5 * t) for t in np.arange(0, 5, 1 / agent_rate)),
        dtype=np.float)

    # Initialize the cart position buffer
    pos_cart_buffer = np.zeros(np.shape(cart_ref))

    for (i, ref) in enumerate(cart_ref):
        # Set the references
        ok1 = robot.setJointPositionTarget("linear", ref)
        assert ok1, "Failed to set joint references"

        # Step the simulator
        gazebo.run()

        # Read the position
        pos_cart_buffer[i] = robot.jointPosition("linear")

    # Check that the trajectory was followed correctly
    assert np.abs(pos_cart_buffer - cart_ref).sum() / cart_ref.size < 5E-3, \
        "The reference trajectory was not tracked correctly"
예제 #4
0
def get_gazebo_and_robot(rtf: float,
                         physics_rate: float,
                         agent_rate: float,
                         # controller_rate: float = None,
                         ) \
        -> Tuple[bindings.GazeboWrapper, bindings.Robot]:

    # ==========
    # PARAMETERS
    # ==========

    model_sdf = "CartPole/CartPole.sdf"
    world_sdf = "DefaultEmptyWorld.world"

    plugin_data = bindings.PluginData()
    plugin_data.setLibName("RobotController")
    plugin_data.setClassName("gympp::plugins::RobotController")

    # Find and load the model SDF file
    model_sdf_file = resource_finder.find_resource(model_sdf)
    with open(model_sdf_file, "r") as stream:
        model_sdf_string = stream.read()

    # Initialize the model data
    model_data = bindings.ModelInitData()
    model_data.setSdfString(model_sdf_string)

    # Create a unique model name
    letters_and_digits = string.ascii_letters + string.digits
    prefix = ''.join(random.choice(letters_and_digits) for _ in range(6))

    # Get the model name
    model_name = bindings.GazeboWrapper.getModelNameFromSDF(model_sdf_string)
    model_data.modelName = prefix + "::" + model_name
    robot_name = model_data.modelName

    num_of_iterations_per_gazebo_step = physics_rate / agent_rate
    assert num_of_iterations_per_gazebo_step == int(
        num_of_iterations_per_gazebo_step)

    # =============
    # CONFIGURATION
    # =============

    # Create the gazebo wrapper
    gazebo = bindings.GazeboWrapper(int(num_of_iterations_per_gazebo_step),
                                    rtf, physics_rate)
    assert gazebo, "Failed to get the gazebo wrapper"

    # Set verbosity
    logger.set_level(gym.logger.DEBUG)

    # Initialize the world
    world_ok = gazebo.setupGazeboWorld(world_sdf)
    assert world_ok, "Failed to initialize the gazebo world"

    # Initialize the ignition gazebo wrapper (creates a paused simulation)
    gazebo_initialized = gazebo.initialize()
    assert gazebo_initialized, "Failed to initialize ignition gazebo"

    # Insert the model
    model_ok = gazebo.insertModel(model_data, plugin_data)
    assert model_ok, "Failed to insert the model in the simulation"

    assert bindings.RobotSingleton_get().exists(robot_name), \
        "The robot interface was not registered in the singleton"

    # Extract the robot interface from the singleton
    robot_weak_ptr = bindings.RobotSingleton_get().getRobot(robot_name)
    assert not robot_weak_ptr.expired(), "The Robot object has expired"
    assert robot_weak_ptr.lock(), \
        "The returned Robot object does not contain a valid interface"
    assert robot_weak_ptr.lock().valid(), "The Robot object is not valid"

    # Get the pointer to the robot interface
    robot = robot_weak_ptr.lock()

    # Set the joint controller period
    robot.setdt(0.001 if physics_rate < 1000 else physics_rate)

    # Return the simulator and the robot object
    return gazebo, robot