示例#1
0
def _make_model(n_poles):
    """Generates an xml string defining a cart with `n_poles` bodies."""
    xml_string = common.read_model('cartpole.xml')
    if n_poles == 1:
        return xml_string
    mjcf = etree.fromstring(xml_string)
    parent = mjcf.find('./worldbody/body/body')  # Find first pole.
    # Make chain of poles.
    for pole_index in range(2, n_poles + 1):
        child = etree.Element('body',
                              name='pole_{}'.format(pole_index),
                              pos='0 0 1',
                              childclass='pole')
        etree.SubElement(child, 'joint', name='hinge_{}'.format(pole_index))
        etree.SubElement(child, 'geom', name='pole_{}'.format(pole_index))
        parent.append(child)
        parent = child
    # Move plane down.
    floor = mjcf.find('./worldbody/geom')
    floor.set('pos', '0 0 {}'.format(1 - n_poles - .05))
    # Move cameras back.
    cameras = mjcf.findall('./worldbody/camera')
    cameras[0].set('pos', '0 {} 1'.format(-1 - 2 * n_poles))
    cameras[1].set('pos', '0 {} 2'.format(-2 * n_poles))
    return etree.tostring(mjcf, pretty_print=True)
示例#2
0
def make_model(n_boxes):
    """Returns a tuple containing the model XML string and a dict of assets."""
    xml_string = common.read_model('stacker.xml')
    parser = etree.XMLParser(remove_blank_text=True)
    mjcf = etree.XML(xml_string, parser)

    # Remove unused boxes
    for b in range(n_boxes, 4):
        box = xml_tools.find_element(mjcf, 'body', 'box' + str(b))
        box.getparent().remove(box)

    return etree.tostring(mjcf, pretty_print=True), common.ASSETS
示例#3
0
def _make_model(n_bodies):
    """Generates an xml string defining a swimmer with `n_bodies` bodies."""
    if n_bodies < 3:
        raise ValueError(
            'At least 3 bodies required. Received {}'.format(n_bodies))
    mjcf = etree.fromstring(common.read_model('swimmer.xml'))
    head_body = mjcf.find('./worldbody/body')
    actuator = etree.SubElement(mjcf, 'actuator')
    sensor = etree.SubElement(mjcf, 'sensor')

    parent = head_body
    for body_index in range(n_bodies - 1):
        site_name = 'site_{}'.format(body_index)
        child = _make_body(body_index=body_index)
        child.append(etree.Element('site', name=site_name))
        joint_name = 'joint_{}'.format(body_index)
        joint_limit = 360.0 / n_bodies
        joint_range = '{} {}'.format(-joint_limit, joint_limit)
        child.append(
            etree.Element('joint', {
                'name': joint_name,
                'range': joint_range
            }))
        motor_name = 'motor_{}'.format(body_index)
        actuator.append(
            etree.Element('motor', name=motor_name, joint=joint_name))
        velocimeter_name = 'velocimeter_{}'.format(body_index)
        sensor.append(
            etree.Element('velocimeter', name=velocimeter_name,
                          site=site_name))
        gyro_name = 'gyro_{}'.format(body_index)
        sensor.append(etree.Element('gyro', name=gyro_name, site=site_name))
        parent.append(child)
        parent = child

    # Move tracking cameras further away from the swimmer according to its length.
    cameras = mjcf.findall('./worldbody/body/camera')
    scale = n_bodies / 6.0
    for cam in cameras:
        if cam.get('mode') == 'trackcom':
            old_pos = cam.get('pos').split(' ')
            new_pos = ' '.join([str(float(dim) * scale) for dim in old_pos])
            cam.set('pos', new_pos)

    return etree.tostring(mjcf, pretty_print=True)
示例#4
0
def make_model(use_peg, insert):
    """Returns a tuple containing the model XML string and a dict of assets."""
    xml_string = common.read_model('manipulator.xml')
    parser = etree.XMLParser(remove_blank_text=True)
    mjcf = etree.XML(xml_string, parser)

    # Select the desired prop.
    if use_peg:
        required_props = ['peg', 'target_peg']
        if insert:
            required_props += ['slot']
    else:
        required_props = ['ball', 'target_ball']
        if insert:
            required_props += ['cup']

    # Remove unused props
    for unused_prop in _ALL_PROPS.difference(required_props):
        prop = xml_tools.find_element(mjcf, 'body', unused_prop)
        prop.getparent().remove(prop)

    return etree.tostring(mjcf, pretty_print=True), common.ASSETS
示例#5
0
def get_model_and_assets():
  """Returns a tuple containing the model XML string and a dict of assets."""
  return common.read_model('reacher.xml'), common.ASSETS