-
Notifications
You must be signed in to change notification settings - Fork 1
/
mano_oggetto.py
185 lines (154 loc) · 5.4 KB
/
mano_oggetto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from klampt import *
#Klampt v0.6.x
#from klampt import visualization as vis
# from klampt import resource
#from klampt import robotcollide as collide
#from klampt.simulation import *
#from klampt.glrobotprogram import *
#Klampt v0.7.x
from klampt import vis
from klampt.vis.glrobotprogram import * #Per il simulatore
# from klampt.math import *
from klampt.model import collide
# from klampt.io import resource
from klampt.sim import *
# # from moving_base_control import *
import importlib
# import os
# import time
import sys
# import grasp_chose
#import an object dataset
from klampt.math import so3
#Declare all variables
world = WorldModel()
dataset = sys.argv[1] #which dataset will be use
# set di oggetti
object_template_fn = 'object_template.obj'
objects_set = {
'primo':['primo/poisson_mesh.stl']
# 'secondo' : ['secondo/%n/poisson_mesh.stl']
# 'ycb':['data/objects/ycb/%s/meshes/tsdf_mesh.stl','data/objects/ycb/%s/meshes/poisson_mesh.stl'],
# 'apc2015':['data/objects/apc2015/%s/textured_meshes/optimized_tsdf_textured_mesh.ply']
}
world.loadElement("terrains/plane.env") #file che richiama la mesh del piano
moving_base_template_fn = 'moving_base_template.rob'
robotname = "reflex_col"
robot_files = {
'reflex_col':'reflex_col.rob'
}
#declare all functions
def import_object(pattern):
# for pattern in objects_set[dataset]:
objfile = 'primo/poisson_mesh.stl' #prendo il nome e il percorso di objectname
objmass = 0.05 #object_masses[object_set].get('mass',0.05) #definisco la massa
f = open(object_template_fn,'r') #apro il file 3d lo utilizzo come template per creare un file di mesh + massa
pattern = ''.join(f.readlines()) #leggi dentro f e unisci con '' (f= [a,b] pattern 'a' 'b')
f.close() #lo chiudo
f2 = open("temp_object.obj",'w') #creo se nn esiste o apro un file in scrittura
f2.write(pattern % (objfile,objmass)) #scrivo mesh dell'oggetto e la massa
f2.close() #chiuso
# f2 = open("temp_object.txt",'w') #creo se nn esiste o apro un file in scrittura
# f2.write(pattern % (objfile,objmass)) #scrivo mesh dell'oggetto e la massa
# f2.close() #chiuso
nobjs = world.numRigidObjects() #numero di oggetti
if world.loadElement('temp_object.obj') < 0 :
print("no load") #check se -1 load fallito
# continue
assert nobjs < world.numRigidObjects(),"Hmm... the object didn't load, but loadElement didn't return -1?"
obj = world.rigidObject(world.numRigidObjects()-1)
obj.setTransform(*se3.identity())
bmin,bmax = obj.geometry().getBB() #return axix-aligned della boundig box dell'oggetto (in pratica fornisce la posizione ossia la dimensione)
T = obj.getTransform() #prendo la trasformazione
spacing = 0.005 #altezza da cui cade??
T = (T[0],vectorops.add(T[1],(-(bmin[0]+bmax[0])*0.5,-(bmin[1]+bmax[1])*0.5,-bmin[2]+spacing)))
obj.setTransform(*T) #trasformazione
obj.appearance().setColor(0.2,0.5,0.7,1.0) #colore
obj.setName('BANANA') #do nome
print("****dove e' l'oggetto"), obj.getTransform()
print("****dove e' l'oggetto"), T[1][0]
return obj
def import_reflex():
f = open(moving_base_template_fn,'r')
pattern_2 = ''.join(f.readlines())
f.close()
f2 = open("temp.rob",'w')
f2.write(pattern_2 % (robot_files[robotname],robotname))
f2.close()
world.loadElement("temp.rob")
robot = world.robot(world.numRobots()-1)
return robot
def move_reflex(robot):
q = robot.getConfig()
q[0] = 0.05
q[1] = 0.01
q[2] = 0.18
q[3] = 0 #yaw
q[4] = 0#pitch
q[5] = math.radians(180) #roll
robot.setConfig(q)
print("******* Robot dove sono **********"), robot.getConfig()
def RelativePosition(robot,object):
robot_transform = robot.getConfig()
Robot_position = [robot_transform[0], robot_transform[1],robot_transform[2]]
object_transform = object.getTransform()
Pos = vectorops.distance(Robot_position,object_transform[1])
# print("Pos"), Pos
return Pos
def Differential(robot, object, Pos_prev, time):
Pos_actual = RelativePosition(robot,object)
Diff = (Pos_actual - Pos_prev) / time
# print("Derivate"), Diff
return Diff
def GraspValuate(diff,kindness):
if diff > 0:
print("No good grasp")
else:
print("good grasp")
objfile = 'primo/poisson_mesh.stl'
f = open('grasp_valuation_template.rob','r')
pattern_2 = ''.join(f.readlines())
f.close()
f2 = open("grasp_valuation.txt",'w')
pos = robot.getConfig()
f2.write(pattern_2 % (objfile,pos,kindness))
f2.close()
#Main
robot = import_reflex()
move_reflex(robot)
# for pattern in objects_set[dataset]:
object = import_object(objects_set[dataset])
#Simulation
#now the simulation is launched
program = GLSimulationProgram(world)
sim = program.sim
#create a hand emulator from the given robot name
module = importlib.import_module('plugins.'+robotname)
#emulator takes the robot index (0), start link index (6), and start driver index (6)
hand = module.HandEmulator(sim,0,6,6)
sim.addEmulator(0,hand)
import simple_controller
sim.setController(robot,simple_controller.make(sim,hand,program.dt))
#this code manually updates the visualization
vis.add("world",world)
vis.show()
t0 = time.time()
Pos_ = RelativePosition(robot,object)
kindness = 0
Td_prev = 0
while vis.shown():
vis.lock()
sim.simulate(0.01)
sim.updateWorld()
vis.unlock()
t1 = time.time()
time.sleep(max(0.01-(t1-t0),0.001))
t0 = t1
diff = Differential(robot, object,Pos_,sim.getTime())
print("getTime"), sim.getTime()
kindness += abs(diff*(sim.getTime()-Td_prev))
Td_prev = sim.getTime()
print("kindness"),kindness
if sim.getTime() > 3:
print("diff"),diff
GraspValuate(diff,kindness)