Example #1
0
def draw_robot_at_conf(conf, transparency, name, robot, env, color=None):
    held_obj = robot.GetGrabbed()
    before = get_body_xytheta(robot)
    newrobot = RaveCreateRobot(env, '')
    newrobot.Clone(robot, 0)
    newrobot.SetName(name)
    env.Add(newrobot, True)
    set_active_config(conf, newrobot)
    newrobot.Enable(False)
    if color is not None:
        set_color(newrobot, color)
    print get_body_xytheta(robot)

    if len(held_obj) > 0:
        print get_body_xytheta(held_obj[0])
        set_robot_config(conf)
        held_obj = robot.GetGrabbed()[0]
        held_obj_trans = held_obj.GetTransform()
        release_obj()
        new_obj = RaveCreateKinBody(env, '')
        new_obj.Clone(held_obj, 0)
        new_obj.SetName(name + '_obj')
        env.Add(new_obj, True)
        new_obj.SetTransform(held_obj_trans)
        for link in new_obj.GetLinks():
            for geom in link.GetGeometries():
                geom.SetTransparency(transparency)
        print get_body_xytheta(robot)
        grab_obj(held_obj)
        set_robot_config(before)

    for link in newrobot.GetLinks():
        for geom in link.GetGeometries():
            geom.SetTransparency(transparency)
Example #2
0
def main(env,options):
    "Main example code."
    if options.iktype is not None:
        # cannot use .names due to python 2.5 (or is it boost version?)
        for value,type in IkParameterization.Type.values.iteritems():
            if type.name.lower() == options.iktype.lower():
                iktype = type
                break
    else:
        iktype = IkParameterizationType.Transform6D
        
    env.Load(options.scene)
    with env:
        # load the Transform6D IK models
        ikmodels = []
        for robot in env.GetRobots():
            for manip in robot.GetManipulators():
                print manip
                try:
                    robot.SetActiveManipulator(manip)
                    ikmodel = databases.inversekinematics.InverseKinematicsModel(robot, iktype=iktype)
                    if not ikmodel.load():
                        ikmodel.autogenerate()
                        if not ikmodel.has():
                            continue
                    ikmodels.append(ikmodel)
                except Exception, e:
                    print 'failed manip %s'%manip, e
                
        if len(ikmodels) == 0:
            raveLogWarn('no manipulators found that can be loaded with iktype %s'%str(iktype))
            
        # create the pickers
        pickers = []
        for ikmodel in ikmodels:
            raveLogInfo('creating picker for %s'%str(ikmodel.manip))
            picker = RaveCreateKinBody(env,'')
            picker.InitFromBoxes(array([ [0.05,0,0,0.05,0.01,0.01], [0,0.05,0,0.01,0.05,0.01], [0,0,0.05,0.01,0.01,0.05] ]),True)
            for igeom,geom in enumerate(picker.GetLinks()[0].GetGeometries()):
                color = zeros(3)
                color[igeom] = 1
                geom.SetDiffuseColor(color)
            picker.SetName(ikmodel.manip.GetName())
            env.Add(picker,True)
            # have to disable since not part of collision 
            picker.Enable(False)
            picker.SetTransform(ikmodel.manip.GetTransform())
            pickers.append([picker,ikmodel.manip])
Example #3
0
    def _add_obstacle(self, geom):
        obstacles = np.matrix('-0.576036866359447, 0.918128654970760, 1;\
                        -0.806451612903226,-1.07017543859649, 1;\
                        1.01843317972350,-0.988304093567252, 1;\
                        0.640552995391705,0.906432748538011, 1;\
                        -0.576036866359447, 0.918128654970760, -1;\
                        -0.806451612903226,-1.07017543859649, -1;\
                        1.01843317972350,-0.988304093567252, -1;\
                        0.640552995391705,0.906432748538011, -1')

        body = RaveCreateKinBody(self._env, '')
        vertices = np.array(obstacles)
        indices = np.array([[0, 1, 2], [2, 3, 0], [4, 5, 6], [6, 7, 4],
                            [0, 4, 5], [0, 1, 5], [1, 2, 5], [5, 6, 2],
                            [2, 3, 6], [6, 7, 3], [0, 3, 7], [0, 4, 7]])
        body.InitFromTrimesh(trimesh=TriMesh(vertices, indices), draw=True)
        body.SetName(self.name)
        for link in body.GetLinks():
            for geom in link.GetGeometries():
                geom.SetDiffuseColor((.9, .9, .9))
        self.env_body = body
        self._env.AddKinBody(body)
def main(env,options):
    "Main example code."
    if options.iktype is not None:
        # cannot use .names due to python 2.5 (or is it boost version?)
        for value,type in IkParameterization.Type.values.items():
            if type.name.lower() == options.iktype.lower():
                iktype = type
                break
    else:
        iktype = IkParameterizationType.Transform6D
        
    env.Load(options.scene)
    with env:
        # load the Transform6D IK models
        ikmodels = []
        for robot in env.GetRobots():
            for manip in robot.GetManipulators():
                print(manip)
                try:
                    robot.SetActiveManipulator(manip)
                    ikmodel = databases.inversekinematics.InverseKinematicsModel(robot, iktype=iktype)
                    if not ikmodel.load():
                        ikmodel.autogenerate()
                        if not ikmodel.has():
                            continue
                    ikmodels.append(ikmodel)
                except Exception as e:
                    print('failed manip %s %r'%(manip, e))
                
        if len(ikmodels) == 0:
            raveLogWarn('no manipulators found that can be loaded with iktype %s'%str(iktype))
            
        # create the pickers
        pickers = []
        for ikmodel in ikmodels:
            raveLogInfo('creating picker for %s'%str(ikmodel.manip))
            picker = RaveCreateKinBody(env,'')
            picker.InitFromBoxes(array([ [0.05,0,0,0.05,0.01,0.01], [0,0.05,0,0.01,0.05,0.01], [0,0,0.05,0.01,0.01,0.05] ]),True)
            for igeom,geom in enumerate(picker.GetLinks()[0].GetGeometries()):
                color = zeros(3)
                color[igeom] = 1
                geom.SetDiffuseColor(color)
            picker.SetName(ikmodel.manip.GetName())
            env.Add(picker,True)
            # have to disable since not part of collision 
            picker.Enable(False)
            picker.SetTransform(ikmodel.manip.GetTransform())
            pickers.append([picker,ikmodel.manip])
              
    raveLogInfo('pickers loaded, try moving them')
    def PickerThread(env,pickers,iktype):
        """this function runs in a separate thread monitoring each of the pickers
        """
        Tpickers = [None]*len(pickers)
        while True:
            if env.GetViewer() is None:
                break
            with env:
                for ipicker,(picker,manip) in enumerate(pickers):
                    T=picker.GetTransform()
                    if Tpickers[ipicker] is not None and sum(abs(Tpickers[ipicker]-T).flatten()) <= 1e-10:
                        continue
                    data = None
                    if iktype == IkParameterizationType.Direction3D:
                        data = T[0:3,2]
                    elif iktype == IkParameterizationType.Lookat3D:
                        data = T[0:3,3]
                    elif iktype == IkParameterizationType.Ray4D:
                        data = Ray(T[0:3,3],T[0:3,2])
                    elif iktype == IkParameterizationType.Rotation3D:
                        data = T[0:3,0:3]
                    elif iktype == IkParameterizationType.Transform6D:
                        data = T
                    elif iktype == IkParameterizationType.Translation3D:
                        data = T[0:3,3]
                    elif iktype == IkParameterizationType.TranslationDirection5D:
                        ikparam = Ray(T[0:3,3],T[0:3,2])
                    elif iktype == IkParameterizationType.TranslationLocalGlobal6D:
                        ikparam = [T[0:3,3],zeros(3)]
                    else:
                        raveLogWarn('iktype %s unsupported'%str(iktype))
                        continue
                    sol = manip.FindIKSolution(IkParameterization(data,iktype),IkFilterOptions.CheckEnvCollisions)
                    if sol is not None:
                        robot.SetDOFValues(sol,manip.GetArmIndices())
                        # have to update all other pickers since two manipulators can be connected to the same joints (torso)
                        for ipicker2, (picker2,manip2) in enumerate(pickers):
                            Tpickers[ipicker2] = manip2.GetTransform()
                            picker2.SetTransform(manip2.GetTransform())
                    # changing color produces bugs with qtcoin
#                     for igeom,geom in enumerate(picker.GetLinks()[0].GetGeometries()):
#                         color = zeros(3)
#                         color[igeom] = 0.4 if sol is None else 1.0
#                         geom.SetDiffuseColor(color)
                    Tpickers[ipicker] = T

            # update rate of 0.05s
            time.sleep(0.05)

    # create the thread
    t = threading.Thread(target=PickerThread,args=[env,pickers,iktype])
    t.start()
    while True:
        t.join(1)