示例#1
0
文件: povray.py 项目: smeng9/Klampt
def add_multiple_lights(properties,object,dist,numLight,gravity=[0,0,-9.81],tgt=None,color=[1.,1.,1.], \
              spotlight=False,radius=15.,falloff=20.,tightness=10., \
              area=0.,sample=9,adaptive=True,jitter=True):
    """This is a convenient function that calls add_light multiple times"""
    #normalize gravity
    g = op.mul(gravity, -1 / op.norm(gravity))

    #compute frame
    gabs = [abs(gi) for gi in g]
    id = gabs.index(min(gabs))
    t0 = [1. if i == id else 0. for i in range(3)]
    t1 = op.cross(t0, g)
    t1 = op.mul(t1, 1 / op.norm(t1))
    t0 = op.cross(t1, g)

    #find highest direction
    bb = compute_bb(object)
    ctr = op.mul(op.add(bb[0], bb[1]), 0.5)
    distg = sum([abs((bb[1][i] - bb[0][i]) / 2 * g[i]) for i in range(3)])

    #add each light
    for i in range(numLight):
        angle = math.pi * 2 * i / numLight
        d0 = op.mul(g, distg)
        d1 = op.mul(t0, math.sin(angle) * dist)
        d2 = op.mul(t1, math.cos(angle) * dist)
        add_light(properties, op.add(d0, op.add(d1,
                                                d2)), ctr, color, spotlight,
                  radius, falloff, tightness, area, sample, adaptive, jitter)
示例#2
0
    def df_dz(self, x, y, z):
        n = len(y)
        m = len(z) // n
        result = np.zeros((6, len(z)))
        T = self.env_geom.getTransform()
        CoM = se3.apply(T, self.env_obj.getMass().getCom())

        for i in range(n):
            point = y[i][1]
            Normal_normal = normal(self.env_geom, point)
            n1 = so3.canonical(Normal_normal)[3:6]
            n2 = so3.canonical(Normal_normal)[6:9]
            Normal_friction = []
            for j in range(6):
                n_tmp = (math.cos((math.pi / 3) * j) * np.array(n1) + math.sin(
                    (math.pi / 3) * j) * np.array(n2)).tolist()
                Normal_friction.append(vectorops.unit(n_tmp))
            Cross_Normal = list(
                vectorops.cross(vectorops.sub(point, CoM), Normal_normal))
            Cross_Normal_friction = []
            for j in range(6):
                cross_Normal_v = list(
                    vectorops.cross(vectorops.sub(point, CoM),
                                    Normal_friction[j]))
                Cross_Normal_friction.append(cross_Normal_v)

            result[0:3, i * m:(i + 1) * m - 1] = np.asarray(Normal_friction).T
            result[0:3, (i + 1) * m - 1:(i + 1) *
                   m] = np.asarray(Normal_normal).reshape((3, 1))
            result[3:6,
                   i * m:(i + 1) * m - 1] = np.asarray(Cross_Normal_friction).T
            result[3:6, (i + 1) * m - 1:(i + 1) *
                   m] = np.asarray(Cross_Normal).reshape((3, 1))

        return result
示例#3
0
def segment_triangle_intersection(seg, tri, umin=0, umax=1):
    """Given a 3D segment (p,q) and a triangle (a,b,c), returns the point of
	intersection if the region of the segment in interval [umin,umax] intersects the
	triangle. Otherwise returns None"""
    p, q = seg
    a, b, c = tri
    d = vectorops.sub(b, a)
    e = vectorops.sub(c, a)
    n = vectorops.cross(d, e)
    if vectorops.norm(n) < 1e-7:  #degenerate
        return None
    n = vectorops.unit(n)
    ofs = vectorops.dot(n, a)
    #Solve for n^T(p + u(q-p)) = ofs
    denom = vectorops.dot(n, vectorops.sub(q, p))
    numer = ofs - vectorops.dot(n, p)
    if abs(denom) < 1e-7:  #near parallel
        return None
    u = numer / denom
    if u < umin or u > umax:
        return None
    #now check whether point of intersection lies in triangle
    x = vectorops.madd(p, vectorops.sub(q, p), u)
    xloc = vectorops.sub(x, a)

    #solve for coordinates [r,s,t] such that xloc = r*n + s*d + t*e
    try:
        rst = np.dot(np.linalg.inv(np.array([n, d, e]).T), xloc)
    except np.linalg.linalg.LinAlgError:
        return None
    r, s, t = rst
    assert abs(r) < 1e-4
    if s >= 0 and t >= 0 and s + t <= 1:
        return x
    return None
示例#4
0
def draw_hull(hull,scale=0.01):
    """Draws a ConvexHull with 3D texture coordinates"""
    if len(hull.simplices)==0:
        print "Hull with no simplices?"
        return
    centroid = sum([hull.points[v] for v in hull.vertices],[0.0]*3) / len(hull.vertices) * scale
    vmin = [min([hull.points[v][i] for v in hull.vertices])*scale for i in range(3)]
    vmax = [max([hull.points[v][i] for v in hull.vertices])*scale for i in range(3)]
    uvwscale = [1.0/(b-a) if b != a else 1.0 for (a,b) in zip(vmin,vmax)]

    for simplex in hull.simplices:
        ia,ib,ic = simplex
        a = vectorops.mul(hull.points[ia].tolist(),scale)
        b = vectorops.mul(hull.points[ib].tolist(),scale)
        c = vectorops.mul(hull.points[ic].tolist(),scale)
        n = vectorops.cross(vectorops.sub(b,a),vectorops.sub(c,a))
        if vectorops.dot(n,vectorops.sub(centroid,a)) > 0:
            b,c = c,b
            n = vectorops.mul(n,-1)
        try:
            n = vectorops.mul(n,1.0/vectorops.norm(n))
            glNormal3f(*n)
        except ZeroDivisionError:
            pass
        glBegin(GL_TRIANGLES)
        glTexCoord3f(uvwscale[0]*(a[0]-vmin[0]),uvwscale[1]*(a[1]-vmin[1]),uvwscale[2]*(a[2]-vmin[2]))
        glVertex3f(*a)
        glTexCoord3f(uvwscale[0]*(b[0]-vmin[0]),uvwscale[1]*(b[1]-vmin[1]),uvwscale[2]*(b[2]-vmin[2]))
        glVertex3f(*b)
        glTexCoord3f(uvwscale[0]*(c[0]-vmin[0]),uvwscale[1]*(c[1]-vmin[1]),uvwscale[2]*(c[2]-vmin[2]))
        glVertex3f(*c)
        glEnd()
 def get_left_dir(self, zeroZ=False):
     dir = op.cross([0, 0, 1], self.get_camera_dir())
     if zeroZ:
         dir = (dir[0], dir[1], 0)
     if op.norm(dir) > 1e-6:
         dir = op.mul(dir, 1 / op.norm(dir))
     return dir
    def apply_tendon_forces(self,i,link1,link2,rest_length):
        tendon_c2 = 30000.0
        tendon_c1 = 10000.0
        b0 = self.sim.body(self.model.robot.link(self.model.proximal_links[0]-3))
        b1 = self.sim.body(self.model.robot.link(link1))
        b2 = self.sim.body(self.model.robot.link(link2))
        p0w = se3.apply(b1.getTransform(),self.tendon0_local)
        p1w = se3.apply(b1.getTransform(),self.tendon1_local)
        p2w = se3.apply(b2.getTransform(),self.tendon2_local)

        d = vectorops.distance(p1w,p2w)
        if d > rest_length:
            #apply tendon force
            direction = vectorops.unit(vectorops.sub(p2w,p1w))
            f = tendon_c2*(d - rest_length)**2+tendon_c1*(d - rest_length)
            #print d,rest_length
            #print "Force magnitude",self.model.robot.link(link1).getName(),":",f
            f = min(f,100)
            #tendon routing force
            straight = vectorops.unit(vectorops.sub(p2w,p0w))
            pulley_direction = vectorops.unit(vectorops.sub(p1w,p0w))
            pulley_axis = so3.apply(b1.getTransform()[0],(0,1,0))
            tangential_axis = vectorops.cross(pulley_axis,pulley_direction)
            cosangle = vectorops.dot(straight,tangential_axis)
            #print "Cosine angle",self.model.robot.link(link1).getName(),cosangle
            base_direction = so3.apply(b0.getTransform()[0],[0,0,-1])
            b0.applyForceAtLocalPoint(vectorops.mul(base_direction,-f),vectorops.madd(p0w,base_direction,0.04))
            b1.applyForceAtLocalPoint(vectorops.mul(tangential_axis,cosangle*f),self.tendon1_local)
            b1.applyForceAtLocalPoint(vectorops.mul(tangential_axis,-cosangle*f),self.tendon0_local)
            b2.applyForceAtLocalPoint(vectorops.mul(direction,-f),self.tendon2_local)
            self.forces[i][1] = vectorops.mul(tangential_axis,cosangle*f)
            self.forces[i][2] = vectorops.mul(direction,f)
        else:
            self.forces[i] = [None,None,None]
        return
示例#7
0
文件: povray.py 项目: smeng9/Klampt
def add_light(properties,pos,tgt=None,color=[1.,1.,1.], \
              spotlight=False,radius=15.,falloff=20.,tightness=10., \
              area=0.,sample=9,adaptive=True,jitter=True):
    if 'lights' not in properties:
        properties['lights'] = []

    if isinstance(pos, list) and isinstance(pos[0], list):
        if isinstance(tgt, list) and isinstance(tgt[0], list):
            for p, t in zip(pos, tgt):
                add_light(properties,pos=p,tgt=t,color=color,   \
                          spotlight=spotlight,radius=radius,falloff=falloff,tightness=tightness,    \
                          area=area,sample=sample,adaptive=adaptive,jitter=jitter)
        else:
            for p in pos:
                add_light(properties,pos=p,tgt=tgt,color=color, \
                          spotlight=spotlight,radius=radius,falloff=falloff,tightness=tightness,    \
                          area=area,sample=sample,adaptive=adaptive,jitter=jitter)
    else:
        light_params = [list(pos), 'color', list(color)]
        if spotlight:
            light_params += [
                'spotlight', 'radius', radius, 'falloff', falloff, 'tightness',
                tightness, 'point_at', tgt
            ]
        if area > 0.:
            d = op.sub(tgt, pos)
            d = op.mul(d, 1 / op.norm(d))
            minId = None
            for i in range(3):
                if abs(d[i]) <= abs(d[(i + 1) % 3]) and abs(d[i]) <= abs(
                        d[(i + 2) % 3]):
                    minId = i
            t0 = [0, 0, 0]
            t0[minId] = 1.
            t1 = op.cross(d, t0)
            t1 = op.mul(t1, 1 / op.norm(t1))
            t0 = op.cross(d, t1)
            light_params += [
                'area_light',
                list(op.mul(t0, area)),
                list(op.mul(t1, area)), sample, sample, 'adaptive', 1, 'jitter'
            ]
        properties['lights'].append(vp.LightSource(*light_params))
示例#8
0
    def value(self, x, y, z):
        result = np.zeros(6)

        if len(y) == 0:
            result[0:3] += self.mass * self.gravity_coefficient * np.asarray(
                self.gravity_direction)
            return result

        n = len(y)
        m = len(z) // n
        assert m == 7
        T = self.env_geom.getTransform()
        CoM = se3.apply(T, self.env_obj.getMass().getCom())

        for i in range(n):
            point = y[i][1]
            f_normal = z[m * (i + 1) - 1]
            f_friction = z[m * i:m * (i + 1) - 1]
            Normal_normal = normal(self.env_geom, point)
            n1 = so3.canonical(Normal_normal)[3:6]
            n2 = so3.canonical(Normal_normal)[6:9]
            Normal_friction = []
            for j in range(6):
                n_tmp = (math.cos((math.pi / 3) * j) * np.array(n1) + math.sin(
                    (math.pi / 3) * j) * np.array(n2)).tolist()
                Normal_friction.append(vectorops.unit(n_tmp))
            Cross_Normal = list(
                vectorops.cross(vectorops.sub(point, CoM), Normal_normal))
            Cross_Normal_friction = []
            for j in range(6):
                cross_Normal_v = list(
                    vectorops.cross(vectorops.sub(point, CoM),
                                    Normal_friction[j]))
                Cross_Normal_friction.append(cross_Normal_v)

            result[0:3] += np.asarray(Normal_normal) * f_normal + np.asarray(
                f_friction) @ np.asarray(Normal_friction)
            result[3:6] += np.asarray(Cross_Normal) * f_normal + np.asarray(
                f_friction) @ np.asarray(Cross_Normal_friction)

        result[0:3] += self.mass * self.gravity_coefficient * np.asarray(
            self.gravity_direction)
        return result
示例#9
0
    def apply_tendon_forces(self, i, link1, link2, rest_length):
        tendon_c2 = 30000.0
        tendon_c1 = 10000.0
        b0 = self.sim.body(
            self.model.robot.link(self.model.proximal_links[0] - 3))
        b1 = self.sim.body(self.model.robot.link(link1))
        b2 = self.sim.body(self.model.robot.link(link2))
        p0w = se3.apply(b1.getTransform(), self.tendon0_local)
        p1w = se3.apply(b1.getTransform(), self.tendon1_local)
        p2w = se3.apply(b2.getTransform(), self.tendon2_local)

        d = vectorops.distance(p1w, p2w)
        if d > rest_length:
            #apply tendon force
            direction = vectorops.unit(vectorops.sub(p2w, p1w))
            f = tendon_c2 * (d - rest_length)**2 + tendon_c1 * (d -
                                                                rest_length)
            #print d,rest_length
            #print "Force magnitude",self.model.robot.link(link1).getName(),":",f
            f = min(f, 100)
            #tendon routing force
            straight = vectorops.unit(vectorops.sub(p2w, p0w))
            pulley_direction = vectorops.unit(vectorops.sub(p1w, p0w))
            pulley_axis = so3.apply(b1.getTransform()[0], (0, 1, 0))
            tangential_axis = vectorops.cross(pulley_axis, pulley_direction)
            cosangle = vectorops.dot(straight, tangential_axis)
            #print "Cosine angle",self.model.robot.link(link1).getName(),cosangle
            base_direction = so3.apply(b0.getTransform()[0], [0, 0, -1])
            b0.applyForceAtLocalPoint(
                vectorops.mul(base_direction, -f),
                vectorops.madd(p0w, base_direction, 0.04))
            b1.applyForceAtLocalPoint(
                vectorops.mul(tangential_axis, cosangle * f),
                self.tendon1_local)
            b1.applyForceAtLocalPoint(
                vectorops.mul(tangential_axis, -cosangle * f),
                self.tendon0_local)
            b2.applyForceAtLocalPoint(vectorops.mul(direction, -f),
                                      self.tendon2_local)
            self.forces[i][1] = vectorops.mul(tangential_axis, cosangle * f)
            self.forces[i][2] = vectorops.mul(direction, f)
        else:
            self.forces[i] = [None, None, None]
        return
def predict_line(lineStarts,lineEnds,lineNormals,lineTorqueAxes,pcd,param,discretization,num_iter,queryDList,vis,world):
    vision_task_1 = False#True means we are doing the collision detection.
    o3d = False
    #create a pcd in open3D
    equilibriumPcd = open3d.geometry.PointCloud()
    xyz = []
    rgb = []
    normals = []
    for ele in pcd:
        xyz.append(ele[0:3])
        rgb.append(ele[3:6])
        normals.append(ele[6:9])
    equilibriumPcd.points = open3d.utility.Vector3dVector(np.asarray(xyz,dtype=np.float32))
    equilibriumPcd.colors = open3d.utility.Vector3dVector(np.asarray(rgb,dtype=np.float32))
    equilibriumPcd.normals = open3d.utility.Vector3dVector(np.asarray(normals,dtype=np.float32))
    # equilibriumPcd,ind=statistical_outlier_removal(equilibriumPcd,nb_neighbors=20,std_ratio=0.75) 
    if o3d:
        vis3 = open3d.visualization.Visualizer()
        vis3.create_window()
        open3dPcd = open3d.geometry.PointCloud()
        vis3.add_geometry(open3dPcd)

    probe = world.rigidObject(0)
    klampt_pcd_object = world.rigidObject(1) #this is a rigid object
    klampt_pcd = klampt_pcd_object.geometry().getPointCloud() #this is now a PointCloud()
    N_pts = klampt_pcd.numPoints()
    dt = 0.1
   

    for pointNumber in num_iter:

        ####Preprocess the pcd ####
        lineStart0 = lineStarts[pointNumber]
        lineEnd0 =lineEnds[pointNumber]
        lineTorqueAxis = lineTorqueAxes[pointNumber]
        N = 1 + round(vo.norm(vo.sub(lineStart0,lineEnd0))/discretization)
        s = np.linspace(0,1,N)
        lineNormal = lineNormals[pointNumber]
        localXinW = vo.sub(lineEnd0,lineStart0)/np.linalg.norm(vo.sub(lineEnd0,lineStart0))
        localYinW = (lineTorqueAxis)/np.linalg.norm(lineTorqueAxis)
        lineStartinLocal = [0,0]
        lineEndinLocal = [np.dot(vo.sub(lineEnd0,lineStart0),localXinW),
                        np.dot(vo.sub(lineEnd0,lineStart0),localYinW)]
        #################first project the pcd to the plane of the line##############
        #The start point is the origin of the plane...
        projectedPcd = [] #Each point is R^10 
        projectedPcdLocal = [] #localXY coordinate
        #the projected Pcd is in local frame....
        for i in range(len(pcd)):
            p = pcd[i][0:3]
            projectedPt = vo.sub(p,vo.mul(lineNormal,vo.dot(vo.sub(p,lineStart0),lineNormal))) ##world origin
            projectedPt2D = vo.sub(projectedPt,lineStart0)#world origin
            projectedPt2DinLocal = [vo.dot(projectedPt2D,localXinW),vo.dot(projectedPt2D,localYinW)]
            #%make sure point is in the "box" defined by the line
            if ((projectedPt2DinLocal[0]<0.051) and (projectedPt2DinLocal[0] > -0.001)
                and (projectedPt2DinLocal[1]<0.001) and (projectedPt2DinLocal[1]>-0.001)):
                projectedPcdLocal.append(projectedPt2DinLocal)
                projectedPcd.append(pcd[i])
        #Create a KDTree for searching
        projectedPcdTree = spatial.KDTree(projectedPcdLocal)
        ###############Find the corresponding point on the surface to the line############
        surfacePtsAll = []# %These are the surface pts that will be displaced.
        #%part of the probe not in contact with the object...
        #average 3 neighbors
        NofN = 3
        #rigidPointsFinal = []   ##we have a potential bug here for not using rigidPointsFinal....      
        for i in range(int(N)):
            tmp =vo.mul(vo.sub(lineEnd0,lineStart0),s[i])#%the point on the line, projected 
            linePt = [vo.dot(tmp,localXinW),vo.dot(tmp,localYinW)]
            d,Idx = projectedPcdTree.query(linePt[0:2],k=NofN)
            #We might end up having duplicated pts...
            #We should make sure that the discretization is not too fine..
            #or should average a few neighbors
            if d[0] < 0.002:
                surfacePt = [0]*10
                for j in range(NofN):
                    surfacePt = vo.add(surfacePt,projectedPcd[Idx[j]][0:10])
                surfacePt = vo.div(surfacePt,NofN)
                surfacePtsAll.append(surfacePt) #position in the global frame..
                #rigidPointsFinal.append(linePts)

        N = len(surfacePtsAll)

        ##### Preprocesss done
        if not o3d:
            vis.show()
            time.sleep(15.0)
        ############# Go through a list of displacements
        totalFinNList = []
        #for queryD = -0.003:0.001:0.014
        for queryD in queryDList:
            lineStart = vo.sub(lineStart0,vo.mul(lineNormal,queryD))
            lineEnd = vo.sub(lineEnd0,vo.mul(lineNormal,queryD))
            lineCenter = vo.div(vo.add(lineStart,lineEnd),2)
            torqueCenter = vo.add(lineCenter,vo.mul(lineNormal,0.09))


            if vision_task_1:

                vis.lock()
                print(queryD)
                x_axis = vo.mul(vo.unit(vo.sub(lineEnd,lineStart)),-1.0)
                y_axis = [lineTorqueAxis[0],lineTorqueAxis[1],lineTorqueAxis[2]]
                z_axis = vo.cross(x_axis,y_axis)
                z_axis = [z_axis[0],z_axis[1],z_axis[2]]
                R = x_axis+y_axis+z_axis
                t = vo.add(lineCenter,vo.mul(lineNormal,0.002))
                
                print(R,t)
                probe.setTransform(R,t)
                vis.unlock()
                time.sleep(dt)
            else:
                if not o3d:
                    vis.lock()
                print(queryD)
                x_axis = vo.mul(vo.unit(vo.sub(lineEnd,lineStart)),-1.0)
                y_axis = [lineTorqueAxis[0],lineTorqueAxis[1],lineTorqueAxis[2]]
                z_axis = vo.cross(x_axis,y_axis)
                z_axis = [z_axis[0],z_axis[1],z_axis[2]]
                R = x_axis+y_axis+z_axis
                t = vo.add(lineCenter,vo.mul(lineNormal,0.003))
                probe.setTransform(R,t)
                

                ####calculate the nominal displacements
                surfacePts = [] #These are the surface pts that will be displaced...
                rigidPtsInContact = []
                nominalD = [] #Column Vector..
                for i in range(int(N)): ##we have a potential bug here for keep interating trhough N, since about, if d[0] < 0.002, the points is not added...
                    #weird no bug occured...
                    linePt = vo.add(vo.mul(vo.sub(lineEnd,lineStart),s[i]),lineStart)              
                    surfacePt = surfacePtsAll[i][0:3]
                    normal = surfacePtsAll[i][6:9]
                    nominalDisp = -vo.dot(vo.sub(linePt,surfacePt),normal)
                    if nominalDisp > 0:
                        surfacePts.append(surfacePtsAll[i][0:10])#position in the global frame..
                        rigidPtsInContact.append(linePt)
                        nominalD.append(nominalDisp)
                originalNominalD = deepcopy(nominalD)
                NofSurfacePts = len(surfacePts)
                if NofSurfacePts > 0:
                    negativeDisp = True
                    while negativeDisp:
                        NofSurfacePts = len(surfacePts)
                        K = np.zeros((NofSurfacePts,NofSurfacePts))
                        for i in range(NofSurfacePts):
                            for j in range(NofSurfacePts):
                                K[i][j] = invKernel(surfacePts[i][0:3],surfacePts[j][0:3],param)
                        #print K
                        #startTime = time.time()
                        actualD =  np.dot(np.linalg.inv(K),nominalD)
                        #print('Time spent inverting matrix',time.time()- startTime)
                        #print nominalD,actualD
                        negativeIndex = actualD < 0
                        if np.sum(negativeIndex) > 0:
                            actualD = actualD.tolist()
                            surfacePts = [surfacePts[i] for i in range(len(surfacePts)) if actualD[i]>=0]
                            nominalD = [nominalD[i] for i in range(len(nominalD)) if actualD[i]>=0]
                            rigidPtsInContact = [rigidPtsInContact[i] for i in range(len(rigidPtsInContact)) if actualD[i]>=0]
                        else:
                            negativeDisp = False
                    
                    
                    #hsv's  hue: 0.25 = 0.0002 displacement 1 == 10 displacement
                    #generate the entire displacement field
                    #surfacePts are the equlibrium pts that provide 
                    entireDisplacedPcd = []
                    colorThreshold = 0.0005
                    colorUpperBound = 0.012
                    forceColorUpperBound = 0.004
                    colorRange = colorUpperBound - colorThreshold
                    greyColor = 0.38
                    
                    xyz = []
                    rgb = []
                    xyzForce = []
                    rgbForce = []
                    maxDisp = 0
                    for pt,ptNormal in zip(equilibriumPcd.points,equilibriumPcd.normals):
                        shapeVector = []
                        for pt2 in surfacePts:
                            shapeVector.append(invKernel(pt[0:3],pt2[0:3],param))
                        nominalDisplacement = vo.dot(shapeVector,actualD)
                        if nominalDisplacement > maxDisp:
                            maxDisp = nominalDisplacement

                        color = colorsys.hsv_to_rgb(nominalDisplacement/colorRange*0.75+0.25,1,0.75)
                        color = [color[0],color[1],color[2]]
                        displacedDeformablePoint = vo.sub(pt[0:3],vo.mul(ptNormal,nominalDisplacement))
                        xyz.append(displacedDeformablePoint)
                        rgb.append(color)
                        

                    counter = 0 
                    for (p,c) in zip(xyz,equilibriumPcd.colors):#rgb):
                        klampt_pcd.setProperty(counter,'r',c[0])
                        klampt_pcd.setProperty(counter,'g',c[1])
                        klampt_pcd.setProperty(counter,'b',c[2])
                        klampt_pcd.setPoint(counter,p)
                        counter = counter + 1
                    klampt_pcd_object.geometry().setPointCloud(klampt_pcd)
                    if o3d:
                        open3dPcd.points = open3d.utility.Vector3dVector(np.asarray(xyz,dtype=np.float32))
                        open3dPcd.colors = open3d.utility.Vector3dVector(np.asarray(rgb,dtype=np.float32))
                        open3dPcd.estimate_normals(search_param = open3d.geometry.KDTreeSearchParamHybrid(radius = 0.1, max_nn = 30))
                        ###open3dPcd,ind=statistical_outlier_removal(open3dPcd,nb_neighbors=10,std_ratio=2) 
                        #open3d.visualization.draw_geometries([open3dPcd])#_box_bottom])
                        vis3.add_geometry(open3dPcd)
                        vis3.poll_events()
                        vis3.update_renderer()
                    if not o3d:
                        vis.unlock()
                    time.sleep(dt)
                else:
                    pass

        
        while vis.shown():
            time.sleep(0.1)
    return
示例#11
0
def run_poking_point_probe(config,tableHeight,probeLength,forceLimit,dt,moveStep,shortServoTime,longServoTime,
								IKErrorTolerence,maxDev,EEZLimit,probe_transform,point_probe_to_local,world,res,
								robot,link,CONTROLLER,collider,intermediateConfig):
	"""
	this is the main function of poking object. - point probe
	"""
	# Read In the pcd 
	points, normals = load_pcd(config.exp_path+'exp_'+str(config.exp_number)+'/probePcd.txt')
	
	# control interface
	if CONTROLLER == 'physical':
		robotControlApi = UR5WithGripperController(host=config.robot_host,gripper=False)
		robotControlApi.start()
		time.sleep(2)
		print '---------------------robot started -----------------------------'
		constantVServo(robotControlApi,4,intermediateConfig,dt)#controller format

	# in simulation ,set
	robot.setConfig(controller_2_klampt(robot,intermediateConfig))
	print '---------------------at home configuration -----------------------------'

	if CONTROLLER == 'debugging':
		differences=[]
		print('[*]Debug: Poking process start!')

		for i in range(len(points)):
			print('point %d, pos: %s, normals: %s'%(i,points[i],normals[i]))
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0)) #get unit vector in the direction '- normals'

			## perform IK
			local_NY_UnitV=vectorops.unit(vectorops.cross([0,1,0],approachVector))			
			pt1=goalPosition
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength)) # use 1m in normals direction.			
			pt3=vectorops.add(pt1,local_NY_UnitV)

			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],maxDev,
											IKErrorTolerence,EEZLimit,collider,use_collision_detect=True)
			differences.append(difference)
			print('difference: %f'%difference)

			### now start colecting data..
			travel = 0.0
			stepVector = vectorops.mul(approachVector,moveStep)
			
			while travel<0.0001: #just try 0.1mm?
				pt1=vectorops.add(pt1,stepVector)
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
				pt3=vectorops.add(pt1,local_NY_UnitV)
				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],maxDev,
												IKErrorTolerence,EEZLimit,collider,use_const=False)
				travel = travel + moveStep
				
			### move the probe away, note: a bit different to physical mode
			pt1=vectorops.add(points[i],vectorops.mul(approachVector,-0.05))  ## move the probe 5 cm from the object surface
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
			pt3=vectorops.add(pt1,local_NY_UnitV)
			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],maxDev,
											IKErrorTolerence,EEZLimit,collider)

			### move back to intermediate config
			robot.setConfig(controller_2_klampt(robot,intermediateConfig))
		
		print('[*]Debug: Poking process done, with max difference:%f'%max(differences))
		
		vis.show()
		while vis.shown():
			time.sleep(1.0)

	elif CONTROLLER == 'physical':
		input('There are %d poking point, go?'%len(points))
		point_list = range(112,116) # !delete 85, 90
		#point_list = random.sample(range(97),97)

		#point_list = [40,37,67,68]
		for i in point_list:	
			print('point %d, pos: %s, normals: %s'%(i,points[i],normals[i]))
			
			travel = -0.01
			#init record file
			forceData=open(config.exp_path+'exp_'+str(config.exp_number)+'/point/force_'+str(i)+'.txt','w')

			robotCurrentConfig=robotControlApi.getConfig()
			robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
			#calculate start position
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0))
			#### Make sure no contact, backup 0.01m
			local_NY_UnitV=vectorops.unit(vectorops.cross([0,1,0],approachVector))

			pt1=vectorops.add(goalPosition,vectorops.mul(approachVector,travel))
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))			
			pt3=vectorops.add(pt1,local_NY_UnitV)

			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime-1,dt) #TODO:

			time.sleep(0.2)

			# Zero the sensor before straight line push, Note that the force is recorded in the global frame..
			counter = 0.0
			totalF = [0,0,0]
			startTime=time.time()
			while (time.time()-startTime) < 1: # use 1s to cal the Force
				totalF = vectorops.add(totalF,robotControlApi.getWrench()[0:3])
				counter = counter + 1.0
				time.sleep(dt)
			forceBias = vectorops.mul(totalF,1.0/float(counter)) # when probe no touch the obj, F_avr = sum(F)/n

			### now start collecting data..
			wrench = robotControlApi.getWrench()
			Force = vectorops.sub(wrench[0:3],forceBias)
			Force_normal = math.fabs(vectorops.dot(Force,approachVector)) #|F||n|cos(theta) = F dot n, set it >= 0 
						
			forceHistory = [Force]			
			force_normalHistory = [Force_normal]
			displacementHistory = [travel]
			stepVector = vectorops.mul(approachVector,moveStep)

			while Force_normal < forceLimit:
				robotCurrentConfig=robotControlApi.getConfig()
				robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
				pt1=vectorops.add(pt1,stepVector)
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
				pt3=vectorops.add(pt1,local_NY_UnitV)
				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
												maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime,dt,use_const=False)
				time.sleep(dt)

				Force = vectorops.sub(robotControlApi.getWrench()[0:3],forceBias)
				Force_normal = math.fabs(vectorops.dot(Force,approachVector))
				travel = travel + moveStep

				forceHistory.append([Force[0],Force[1],Force[2]])				
				force_normalHistory.append(Force_normal)
				displacementHistory.append(travel)
			
			#record all the data in 2 files, one N*2 containts all the force data collected at various locations, another
			#file specifies the number of datapoints at each detected point
			for (f,fn,d) in zip(forceHistory,force_normalHistory,displacementHistory):
				forceData.write(str(f[0])+' '+str(f[1])+' '+str(f[2])+' '+str(fn)+' '+str(d)+'\n')
			forceData.close()

			### move the probe away
			robotCurrentConfig=robotControlApi.getConfig()
			robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
			pt1=vectorops.add(points[i],vectorops.mul(approachVector,-0.10))  ## move the probe 8 cm from the object surface
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
			pt3=vectorops.add(pt1,local_NY_UnitV)
			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,shortServoTime,dt)
			
			#constantVServo(robotControlApi,longServoTime,intermediateConfig,dt)

			print'----------------------- pt '+str(i)+' completed -------------------------------'
		
		#### move back to intermediate config
		constantVServo(robotControlApi,shortServoTime,intermediateConfig,dt)	
		robotControlApi.stop()
示例#12
0
def run_poking_ellipse_probe(config,tableHeight,probeLength,forceLimit,dt,moveStep,shortServoTime,longServoTime,
								IKErrorTolerence,maxDev,EEZLimit,probe_transform,point_probe_to_local,world,res,
								robot,link,CONTROLLER,collider,intermediateConfig):
	"""
	this is the main function of poking object. - point probe
	"""
	########################## Read In the pcd ######################################
	points, normals = load_pcd(config.exp_path+'exp_'+str(config.exp_number)+'/probePcd.txt')
	
	# control interface
	if CONTROLLER == 'physical':
		robotControlApi = UR5WithGripperController(host=config.robot_host,gripper=False)
		robotControlApi.start()
		time.sleep(2)
	print '---------------------robot started -----------------------------'

	## Record some home configuration
	intermediateConfig = config.intermediateConfig
	intermediateConfig = intermediateConfig

	if CONTROLLER == "physical":
		constantVServo(robotControlApi,4,intermediateConfig,dt)#controller format

	robot.setConfig(controller_2_klampt(robot,intermediateConfig))
	print '---------------------at home configuration -----------------------------'

	if CONTROLLER == 'debugging':
		differences=[]
		print('[*]Debug: Poking process start!')

		for i in range(len(points)):
			print('point %d, pos: %s, normals: %s'%(i,points[i],normals[i]))

			#robotCurrentConfig=intermediateConfig # TODO: compare to the intermediateConfig, I comment it 
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0)) #get unit vector in the direction '- normals'

			## perform IK
			local_NY_UnitV=vectorops.unit(vectorops.cross([0,1,0],approachVector))			
			pt1=goalPosition
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength)) # use 1m in normals direction.			
			pt3=vectorops.add(pt1,local_NY_UnitV)

			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],maxDev,
											IKErrorTolerence,EEZLimit,collider,use_collision_detect=True,use_ik_detect=True)
			differences.append(difference)
			print('difference: %f'%difference)

			### now start colecting data..
			travel = 0.0
			stepVector = vectorops.mul(approachVector,moveStep)
			
			while travel<0.0001: #just try 0.1mm?
				pt1=vectorops.add(pt1,stepVector)
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
				pt3=vectorops.add(pt1,local_NY_UnitV)
				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],maxDev,
												IKErrorTolerence,EEZLimit,collider,use_const=False)
				travel = travel + moveStep
				
			### move the probe away, note: a bit different to physical mode
			pt1=vectorops.add(points[i],vectorops.mul(approachVector,-0.05))  ## move the probe 5 cm from the object surface
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
			pt3=vectorops.add(pt1,local_NY_UnitV)
			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],maxDev,
											IKErrorTolerence,EEZLimit,collider)

			### move back to intermediate config
			robot.setConfig(controller_2_klampt(robot,intermediateConfig))
		
		print('[*]Debug: Poking process done, with max difference:%f'%max(differences))
		
		vis.show()
		while vis.shown():
			time.sleep(1.0)

	elif CONTROLLER == 'physical':
		######################################## Ready to Take Measurements ################################################
		input('[!]Warning: There are %d poking point, Robot act!:'%len(points))
		point_list = range(65,120) #64
		#point_list = random.sample(range(0,94),94)

		#finish_list = [0,1,4,9,10,11,12,13,14,15,17,18,19,20,25,26,28,30,33,34,35,37,42,43,44,46,47,50,51,53,54,57,58,59,64,69,72,73,74,75,76,77,78,79,81,83,86,95]
		#point_list = [x for x in point_list if x not in finish_list]
		
		point_list = [64]
		
		for i in point_list:	
			print('point %d, pos: %s, normals: %s'%(i,points[i],normals[i]))
			
			#init record file
			forceData=open(config.exp_path+'exp_'+str(config.exp_number)+'/ellipse/force_'+str(i)+'.txt','w')
			torqueData=open(config.exp_path+'exp_'+str(config.exp_number)+'/ellipse/torque_'+str(i)+'.txt','w')

			#init the backforward distance
			travel = -0.018

			robotCurrentConfig=robotControlApi.getConfig()
			robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
			#calculate start position
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0))
			#### Make sure no contact, backup 0.01m
			local_NY_UnitV=vectorops.unit(vectorops.cross([0,1,0],approachVector))

			pt1=vectorops.add(goalPosition,vectorops.mul(approachVector,travel))
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))			
			pt3=vectorops.add(pt1,local_NY_UnitV)

			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime-1,dt,
											use_ik_detect=False,use_collision_detect=False)

			time.sleep(0.2)

			## Zero the sensor before straight line push
			#
			# Note that the force is recorded in the global frame..
			# And the global frame has x and y axis flipped w.r.t the URDF....

			counter = 0.0
			totalF = [0,0,0]
			totalTorque = [0,0,0]

			startTime=time.time()
			while (time.time()-startTime) < 1: # use 1s to cal the Force
				totalF = vectorops.add(totalF,robotControlApi.getWrench()[0:3])
				totalTorque = vectorops.add(totalTorque,robotControlApi.getWrench()[3:6])
				counter = counter + 1.0
				time.sleep(dt)
			forceBias = vectorops.mul(totalF,1.0/float(counter)) # when probe no touch the obj, F_avr = sum(F)/n
			torqueBias = vectorops.mul(totalTorque,1.0/float(counter))


			### now start collecting data..
			# Force direction x, y inverse, refer to correct force.py
			wrench = robotControlApi.getWrench()
			Force = fix_direction(vectorops.sub(wrench[0:3],forceBias))
			Force_normal = math.fabs(vectorops.dot(Force,approachVector)) #|F||n|cos(theta) = F dot n, set it >= 0 
			Torque = vectorops.sub(wrench[3:6],torqueBias)
			Torque = fix_direction(Torque)
						
			forceHistory = [Force]
			torqueHistory = [Torque]			
			force_normalHistory = [Force_normal]
			displacementHistory = [travel]
			stepVector = vectorops.mul(approachVector,moveStep)

			while Force_normal < forceLimit:
				robotCurrentConfig=robotControlApi.getConfig()
				robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
				pt1=vectorops.add(pt1,stepVector)
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
				pt3=vectorops.add(pt1,local_NY_UnitV)
				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
												maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime,dt,
												use_const=False,use_ik_detect=False)
				time.sleep(dt)

				Force = fix_direction(vectorops.sub(robotControlApi.getWrench()[0:3],forceBias))
				Force_normal = math.fabs(vectorops.dot(Force,approachVector))
				Torque = vectorops.sub(robotControlApi.getWrench()[3:6],torqueBias)

				travel = travel + moveStep

				forceHistory.append([Force[0],Force[1],Force[2]])
				torqueHistory.append([Torque[0],Torque[1],Torque[2]])					
				force_normalHistory.append(Force_normal)
				displacementHistory.append(travel)
			
			#record all the data in 2 files, one N*2 containts all the force data collected at various locations, another
			#file specifies the number of datapoints at each detected point
			for (f,fn,d) in zip(forceHistory,force_normalHistory,displacementHistory):
				forceData.write(str(f[0])+' '+str(f[1])+' '+str(f[2])+' '+str(fn)+' '+str(d)+'\n')		
			for (t,d) in zip(torqueHistory,displacementHistory):
				torqueData.write(str(t[0])+' '+str(t[1])+' '+str(t[2])+' '+str(d)+'\n')
			forceData.close()
			torqueData.close()

			### move the probe away
			robotCurrentConfig=robotControlApi.getConfig()
			robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
			pt1=vectorops.add(points[i],vectorops.mul(approachVector,-0.10))  ## move the probe 5 cm from the object surface
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
			pt3=vectorops.add(pt1,local_NY_UnitV)
			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,shortServoTime,dt,
											use_ik_detect=False)
			
			#constantVServo(robotControlApi,longServoTime,intermediateConfig,dt)

			print'----------------------- pt '+str(i)+' completed -------------------------------'
		
		#### move back to intermediate config
		constantVServo(robotControlApi,shortServoTime,intermediateConfig,dt)	
		robotControlApi.stop()
示例#13
0
def run_poking_line_probe(config,tableHeight,probeLength,forceLimit,dt,moveStep,shortServoTime,longServoTime,
							IKErrorTolerence,maxDev,EEZLimit,probe_transform,point_probe_to_local,world,res,
							robot,link,CONTROLLER,collider,intermediateConfig):
	"""
	this is the main function of poking object. - line probe
	"""
	# reconstruct probepcd.txt
	if input('[*]Reconstruct probe pcd?') == 1:
		theta_list_num = input('---need theta list number: ')
		reconstruct_pcd(config.exp_path+'exp_'+str(config.exp_number)+'/probePcd.txt',
						config.exp_path+'exp_'+str(config.exp_number)+'/probePcd_theta.txt',
						theta_list_num)
		print('---New probe list done')

	# Read In the pcd
	points, normals, theta_list, theta, pti = load_pcd(config.exp_path+'exp_'+str(config.exp_number)+'/probePcd_theta.txt', pcdtype='xyzrgbntheta')
	
	# control interface
	if CONTROLLER == 'physical':
		robotControlApi = UR5WithGripperController(host=config.robot_host,gripper=False)
		robotControlApi.start()
		time.sleep(2)
		print '---------------------robot started -----------------------------'
		constantVServo(robotControlApi,4,intermediateConfig,dt)#controller format

	# set in simul model
	robot.setConfig(controller_2_klampt(robot,intermediateConfig))
	print '---------------------at home configuration -----------------------------'

	if CONTROLLER == 'debugging':
		differences=[]
		print('[*]Debug: Poking process start')

		i = 0 # use this to catch points
		pti_ = pti[i]
		while(i < len(points)):
			robotCurrentConfig=intermediateConfig
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0)) #get unit vector in the direction '- normals'
			_pti = pti_

			if pti[i] == _pti:
				print('point %d, pos: %s, normals: %s, theta: %s, -> %f'%(i,points[i],normals[i],theta_list[i],theta[i]))
				## perform IK				
				local_NY_UnitV = vectorops.unit(back_2_line(approachVector,theta_list[i])) # the probe's line direction

				pt1=goalPosition
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength)) # use 1m in normals direction.				
				pt3=vectorops.add(pt1,local_NY_UnitV)

				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
												maxDev,IKErrorTolerence,EEZLimit,collider,use_collision_detect=False,use_ik_detect=True)
				differences.append(difference)
				print('difference: %f'%difference)

				### now start colecting data..
				travel = 0.0
				stepVector = vectorops.mul(approachVector,moveStep)
				
				while travel<0.0001:
					robotCurrentConfig=klampt_2_controller(robot.getConfig())
					pt1=vectorops.add(pt1,stepVector)
					pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
					pt3=vectorops.add(pt1,local_NY_UnitV)

					[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
													maxDev,IKErrorTolerence,EEZLimit,collider,use_collision_detect=False)
					travel = travel + moveStep

				### move the probe away, note: a bit different to physical mode
				robotCurrentConfig=klampt_2_controller(robot.getConfig())
				pt1=vectorops.add(points[i],vectorops.mul(approachVector,-0.05))  ## move the probe 5 cm from the object surface
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
				pt3=vectorops.add(pt1,local_NY_UnitV)

				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
												maxDev,IKErrorTolerence,EEZLimit,collider,use_collision_detect=False)

				### move back to intermediate config
				robot.setConfig(controller_2_klampt(robot,intermediateConfig))

				i = i + 1 # important
			else:
				pti_ = pti[i]
		
		print('[*]Debug: Poking process done, with max difference:%f'%max(differences))
		
		vis.show()
		while vis.shown():
			time.sleep(1.0)

	elif CONTROLLER == 'physical':
		exe_number = input('There are %d poking point, go?'%len(points))

		start_i = 0 #72,93,94,97,99,100,101,108,116,125,128,147,148,150,151,152,189,194~197,207~210   !40,37,67,68 -> 111 112 113 120 121 122 201 206
 		end_i = 1 #len(points)
		i = start_i # use this to catch points, set manully! # TODO:
		pti_ = pti[i]
		
		probe_list = random.sample(range(282),282) #18,15
		finish_list= range(16)+range(17,21)+range(25,44)+range(45,51)+range(52,57)+[58,59,60,63,67,68,71,73,75,76,78]+range(81,96)\
					+[97,99,101,103,104,108,110,112,114,115,117,118,120,124,125,128,129,130]+range(132,138)+[141,142,144,147,149,150,151]+range(153,156)\
					+[159,160,161,167,168,170,172,175,176,177,178]+range(180,186)\
					+[189,192,195,196,199,200,201,203]+range(204,210)+range(211,217)+[219]+range(221,229)+range(230,236)+range(237,241)\
					+range(244,250)+[251]+range(254,261)+[262]+range(264,276)+range(277,282)
		probe_list = [x for x in probe_list if x not in finish_list] +[227]

		probe_list = [95]
		
		for i in probe_list:
			print('point %d, pos: %s, normals: %s, theta: %s, -> %f'%(i,points[i],normals[i],theta_list[i],theta[i]))
			
			robotCurrentConfig=robotControlApi.getConfig()
			robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
			# calculate start position
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0))

			# init record file
			forceData=open(config.exp_path+'exp_'+str(config.exp_number)+'/line/force_'+str(i)+'.txt','w')
			torqueData=open(config.exp_path+'exp_'+str(config.exp_number)+'/line/torque_'+str(i)+'.txt','w')

			travel = -0.025

			## perform IK
			local_NY_UnitV = vectorops.unit(back_2_line(approachVector,theta_list[i])) # the probe's line direction
			#### Make sure no contact, backup 0.01m
			pt1=vectorops.add(goalPosition,vectorops.mul(approachVector,travel))
			pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength)) # use 1m in normals direction.				
			pt3=vectorops.add(pt1,local_NY_UnitV)
			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
										maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,shortServoTime,dt) #TODO:
			time.sleep(0.2)

			# Zero the sensor before straight line push, Note that the force is recorded in the global frame..
			counter = 0.0
			totalF = [0,0,0]
			totalTorque = [0,0,0]
			startTime=time.time()
			while (time.time()-startTime) < 1: # use 1s to cal the Force
				totalF = vectorops.add(totalF,robotControlApi.getWrench()[0:3])
				totalTorque = vectorops.add(totalTorque,robotControlApi.getWrench()[3:6])
				counter = counter + 1.0
				time.sleep(dt)
			forceBias = vectorops.mul(totalF,1.0/float(counter)) # when probe no touch the obj, F_avr = sum(F)/n
			torqueBias = vectorops.mul(totalTorque,1.0/float(counter))

			### now start collecting data..
			wrench = robotControlApi.getWrench()
			Force = vectorops.sub(wrench[0:3],forceBias)
			Torque = vectorops.sub(wrench[3:6],torqueBias)
			Force_normal = math.fabs(vectorops.dot(Force,approachVector)) #|F||n|cos(theta) = F dot n, set it >= 0 
			
			local_Z_UnitV = vectorops.cross(normals[i],local_NY_UnitV)
			Torque_normal = vectorops.dot(Torque,local_Z_UnitV) #TODO:
			
			forceHistory = [Force]			
			force_normalHistory = [Force_normal]
			torqueHistory = [Torque]
			torque_normalHistory = [Torque_normal]

			displacementHistory = [travel]
			stepVector = vectorops.mul(approachVector,moveStep)

			while Force_normal < forceLimit:
				robotCurrentConfig=robotControlApi.getConfig()
				robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
				pt1=vectorops.add(pt1,stepVector)
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
				pt3=vectorops.add(pt1,local_NY_UnitV)

				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
										maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime,dt,use_const=False)
				time.sleep(dt)

				Force = vectorops.sub(robotControlApi.getWrench()[0:3],forceBias)
				Torque = vectorops.sub(robotControlApi.getWrench()[3:6],torqueBias)
				Force_normal = math.fabs(vectorops.dot(Force,approachVector))

				local_Z_UnitV = vectorops.cross(normals[i],local_NY_UnitV)
				Torque_normal = vectorops.dot(Torque,local_Z_UnitV)

				travel = travel + moveStep

				forceHistory.append([Force[0],Force[1],Force[2]])				
				force_normalHistory.append(Force_normal)
				torqueHistory.append([Torque[0],Torque[1],Torque[2]])				
				torque_normalHistory.append(Torque_normal)

				displacementHistory.append(travel)
			
			#record all the data in 2 files, one N*2 containts all the force data collected at various locations, another
			#file specifies the number of datapoints at each detected point				
			for (f,fn,d) in zip(forceHistory,force_normalHistory,displacementHistory):
				forceData.write(str(f[0])+' '+str(f[1])+' '+str(f[2])+' '+str(fn)+' '+str(d)+'\n')
			
			for (t,tn,d) in zip(torqueHistory,torque_normalHistory,displacementHistory):
				torqueData.write(str(t[0])+' '+str(t[1])+' '+str(t[2])+' '+str(tn)+' '+str(d)+'\n')

			### move the probe away, sometimes z up 5cm is better than normal direction up 5cm...
			pt1=vectorops.add(pt1,[0,0,0.05])  ## move the probe 10 cm up-z-axis, find another point
			pt2=vectorops.add(pt2,[0,0,0.05])
			pt3=vectorops.add(pt3,[0,0,0.05])
			[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,shortServoTime-1,dt)

			constantVServo(robotControlApi,longServoTime,intermediateConfig,dt)#TODO:
			# close record file for point i
			forceData.close()
			torqueData.close()
			print'----------------------- pt '+str(i)+' completed -------------------------------'
		'''
		while(i < end_i):
					
			robotCurrentConfig=robotControlApi.getConfig()
			robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
			# calculate start position
			goalPosition=deepcopy(points[i])
			approachVector=vectorops.unit(vectorops.mul(normals[i],-1.0))

			# init record file
			forceData=open(config.exp_path+'exp_'+str(config.exp_number)+'/line/force_'+str(i)+'.txt','w')
			torqueData=open(config.exp_path+'exp_'+str(config.exp_number)+'/line/torque_'+str(i)+'.txt','w')

			_pti = pti_
			if pti[i] == _pti:
				print('point %d, pos: %s, normals: %s, theta: %s, -> %f'%(i,points[i],normals[i],theta_list[i],theta[i]))
				
				travel = -0.015

				## perform IK
				local_NY_UnitV = vectorops.unit(back_2_line(approachVector,theta_list[i])) # the probe's line direction
				#### Make sure no contact, backup 0.01m
				pt1=vectorops.add(goalPosition,vectorops.mul(approachVector,travel))
				pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength)) # use 1m in normals direction.				
				pt3=vectorops.add(pt1,local_NY_UnitV)
				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime-1,dt) #TODO:
				time.sleep(0.2)

				# Zero the sensor before straight line push, Note that the force is recorded in the global frame..
				counter = 0.0
				totalF = [0,0,0]
				totalTorque = [0,0,0]
				startTime=time.time()
				while (time.time()-startTime) < 1: # use 1s to cal the Force
					totalF = vectorops.add(totalF,robotControlApi.getWrench()[0:3])
					totalTorque = vectorops.add(totalTorque,robotControlApi.getWrench()[3:6])
					counter = counter + 1.0
					time.sleep(dt)
				forceBias = vectorops.mul(totalF,1.0/float(counter)) # when probe no touch the obj, F_avr = sum(F)/n
				torqueBias = vectorops.mul(totalTorque,1.0/float(counter))

				### now start collecting data..
				wrench = robotControlApi.getWrench()
				Force = vectorops.sub(wrench[0:3],forceBias)
				Torque = vectorops.sub(wrench[3:6],torqueBias)
				Force_normal = math.fabs(vectorops.dot(Force,approachVector)) #|F||n|cos(theta) = F dot n, set it >= 0 
				
				local_Z_UnitV = vectorops.cross(normals[i],local_NY_UnitV)
				Torque_normal = vectorops.dot(Torque,local_Z_UnitV) #TODO:
				
				forceHistory = [Force]			
				force_normalHistory = [Force_normal]
				torqueHistory = [Torque]
				torque_normalHistory = [Torque_normal]

				displacementHistory = [travel]
				stepVector = vectorops.mul(approachVector,moveStep)

				while Force_normal < forceLimit:
					robotCurrentConfig=robotControlApi.getConfig()
					robot.setConfig(controller_2_klampt(robot,robotCurrentConfig))
					pt1=vectorops.add(pt1,stepVector)
					pt2=vectorops.add(pt1,vectorops.mul(approachVector,1.0-probeLength))
					pt3=vectorops.add(pt1,local_NY_UnitV)

					[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
											maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,longServoTime,dt,use_const=False)
					time.sleep(dt)

					Force = vectorops.sub(robotControlApi.getWrench()[0:3],forceBias)
					Torque = vectorops.sub(robotControlApi.getWrench()[3:6],torqueBias)
					Force_normal = math.fabs(vectorops.dot(Force,approachVector))

					local_Z_UnitV = vectorops.cross(normals[i],local_NY_UnitV)
					Torque_normal = vectorops.dot(Torque,local_Z_UnitV)

					travel = travel + moveStep

					forceHistory.append([Force[0],Force[1],Force[2]])				
					force_normalHistory.append(Force_normal)
					torqueHistory.append([Torque[0],Torque[1],Torque[2]])				
					torque_normalHistory.append(Torque_normal)

					displacementHistory.append(travel)
				
				#record all the data in 2 files, one N*2 containts all the force data collected at various locations, another
				#file specifies the number of datapoints at each detected point				
				for (f,fn,d) in zip(forceHistory,force_normalHistory,displacementHistory):
					forceData.write(str(f[0])+' '+str(f[1])+' '+str(f[2])+' '+str(fn)+' '+str(d)+'\n')
				
				for (t,tn,d) in zip(torqueHistory,torque_normalHistory,displacementHistory):
					torqueData.write(str(t[0])+' '+str(t[1])+' '+str(t[2])+' '+str(tn)+' '+str(d)+'\n')

				### move the probe away, sometimes z up 5cm is better than normal direction up 5cm...
				pt1=vectorops.add(pt1,[0,0,0.05])  ## move the probe 10 cm up-z-axis, find another point
				pt2=vectorops.add(pt2,[0,0,0.05])
				pt3=vectorops.add(pt3,[0,0,0.05])
				[robot,difference] = robot_move(CONTROLLER,world,robot,link,point_probe_to_local,[pt1,pt2,pt3],
												maxDev,IKErrorTolerence,EEZLimit,collider,robotControlApi,shortServoTime-0.5,dt)

				#constantVServo(robotControlApi,longServoTime,intermediateConfig,dt)#TODO:
				i = i + 1
				# close record file for point i
				forceData.close()
				torqueData.close()
				print'----------------------- pt '+str(i)+' completed -------------------------------'
			
			else:
				# up 10cm is faster but not good.
				# since the points are close, no need to go back home
			
				constantVServo(robotControlApi,longServoTime,intermediateConfig,dt)
				pti_ = pti[i]
		'''
			
		#### move back to intermediate config
		constantVServo(robotControlApi,shortServoTime,intermediateConfig,dt)
		# finish all points
		robotControlApi.stop()
示例#14
0
def cal_curvature(pcdSegmented):
    NofPts = len(pcdSegmented.points)
    NofNN = 30
    searchR = 0.01  #the points are 2mm apart...

    pcdTree = KDTreeFlann(pcdSegmented)
    curvatures = []

    print 'calculating curvatures......'
    for i in range(len(pcdSegmented.points)):
        #for i in [0]:
        [k, idx, _] = pcdTree.search_knn_vector_3d(pcdSegmented.points[i],
                                                   NofNN + 1)
        #[k,idx,_] = pcdTree.search_radius_vector_3d(pcdSegmented.points[i],searchR)
        #k is the total Number, while idx is the list of indices
        #for debugging
        #for ii in idx:
        #	pcdSegmente.colors[ii] = [0,1,0]
        #draw_geometries([pcdSegmented])

        #if k < 3.5:
        #	[k,idx,_] = pcdTree.search_knn_vector_3d(pcdSegmented.points[i],NofNN+1)
        nearbyPts = np.asarray(pcdSegmented.points)[idx[1:], :]
        nearbyPtsNormal = np.asarray(pcdSegmented.normals)[idx[1:], :]

        normalCurvatures = []
        ## Define the local coordinates
        N = vectorops.unit(pcdSegmented.normals[i])  # also the "z"
        Y = vectorops.cross(N, [1, 0, 0])  # pq = vectorops.(q,p)
        X = vectorops.cross(Y, N)
        MMatrix = []
        for j in range(k - 1):
            #estimate the point normal, in Local corrdinate
            qGlobal = vectorops.sub(nearbyPts[j], pcdSegmented.points[i])
            qLocal = [
                vectorops.dot(qGlobal, X),
                vectorops.dot(qGlobal, Y),
                vectorops.dot(qGlobal, N)
            ]
            MGlobal = nearbyPtsNormal[j]
            MLocal = [
                vectorops.dot(MGlobal, X),
                vectorops.dot(MGlobal, Y),
                vectorops.dot(MGlobal, N)
            ]
            [x_i, y_i, z_i] = qLocal
            [n_xi, n_yi, n_zi] = MLocal

            #print x_i,n_xi,y_i,n_yi
            n_xy = (x_i * n_xi + y_i * n_yi) / math.sqrt(x_i * x_i + y_i * y_i)
            #print n_xy
            k_i_n = -(n_xy) / (math.sqrt(n_xy * n_xy + n_zi * n_zi) +
                               math.sqrt(x_i * x_i + y_i * y_i))
            normalCurvatures.append(k_i_n)

            ## calculate the M matrix
            pQ = [qLocal[0], qLocal[1], 0]
            angle = np.arccos(
                np.dot(X, pQ) / (np.linalg.norm(X) * np.linalg.norm(pQ)))
            row = [
                math.cos(angle) * math.cos(angle),
                2 * math.cos(angle) * math.sin(angle),
                math.sin(angle) * math.sin(angle)
            ]
            MMatrix.append(row)
        #perform least squres fitting
        tmp = np.dot(np.transpose(MMatrix), MMatrix)
        tmp2 = np.dot(np.linalg.inv(tmp), np.transpose(MMatrix))
        x = tmp2.dot(normalCurvatures)
        eigenValues, v = np.linalg.eig([[x[0], x[1]], [x[1], x[2]]])
        curvatures.append(vectorops.norm_L1(eigenValues))
        #print vectorops.norm_L1(eigenValues)
        print 'progress', float(i) / float(NofPts)

    print "max and min of curvatures: ", np.max(curvatures), np.min(curvatures)
    return curvatures
示例#15
0
	def find_target(self):
		self.current_target=self.waiting_list[0]
		best_p=self.sim.world.rigidObject(self.current_target).getTransform()[1]
		self.current_target_pos=best_p
		for i in self.waiting_list:
			p=self.sim.world.rigidObject(i).getTransform()[1]
			# print i,p[2],vectorops.distance([0,0],[p[0],p[1]])
			if i not in self.failedpickup:
				if p[2]>best_p[2]+0.05:
					self.current_target=i
					self.current_target_pos=p
					# print 'higher is easier!'
					best_p=p
				elif p[2]>best_p[2]-0.04 and vectorops.distance([0,0],[p[0],p[1]])<vectorops.distance([0,0],[best_p[0],best_p[1]]):
					self.current_target=i
					self.current_target_pos=p		
					best_p=p
				elif self.current_target in self.failedpickup:
					self.current_target=i
					self.current_target_pos=p		
					best_p=p
		d_y=-0.02
		face_down=face_down_forward
		self.tooclose = False;
		if best_p[1]>0.15:
			d_y=-0.02
			face_down=face_down_backward
			self.tooclose = True;
			print 'too close to the wall!!'
		elif best_p[1]<-0.15:
			d_y=0.02
			print best_p
			self.tooclose = True;
			print 'too close to the wall!!'
		#here is hardcoding the best relative position for grasp the ball

		target=(face_down,vectorops.add(best_p,[0,d_y,0.14]))
		if self.tooclose:
			if best_p[0]<0:
				self.boxside=boxleft
				aready_pos=start_pos_left
			else:
				self.boxside=boxright
				aready_pos=start_pos_right
			aready_pos[1][0]=best_p[0]
			balllocation = best_p
			handballdiff = vectorops.sub(aready_pos[1],best_p)
			axis = vectorops.unit(vectorops.cross(handballdiff,aready_pos[1]))
			axis = (1,0,0)
			if best_p[1]>0:
				axis=(-1,0,0)
			angleforaxis = -1*math.acos(vectorops.dot(aready_pos[1],handballdiff)/vectorops.norm(aready_pos[1])/vectorops.norm(handballdiff))
			angleforaxis = so2.normalize(angleforaxis)
			#if angleforaxis>math.pi:
			#	angleforaxis=angleforaxis-2*math.pi
			adjR = so3.rotation(axis, angleforaxis)
			print balllocation
			print vectorops.norm(aready_pos[1]),vectorops.norm(handballdiff),angleforaxis
			target=(adjR,vectorops.add(best_p,vectorops.div(vectorops.unit(handballdiff),5)))

		# print self.current_target	
		# print 'find target at:',self.current_target_pos	
		return target