-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpybullet_hand.py
136 lines (103 loc) · 2.98 KB
/
pybullet_hand.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
#PARAMETERS TO CHANGE: ____________________________
N=800
urfdName="SURENA/surenaNOTWorld.urdf" #surenaFIXEDtoWorld.urdf
txtName="qscenario.txt"
RightArm=False
Plot=True
#__________________________________________________
from os import MFD_HUGE_64KB
import numpy as np
import pybullet as p
import time
import pybullet_data
#Reading inputs from the file
with open(txtName) as f:
lines = f.readlines()
f.close()
M=np.zeros(N*7)
i=0
for number in lines:
M[i]=float(number)
i+=1
#_____________
def Reset():
startPos = [0,0,0]
startOrientation = p.getQuaternionFromEuler([0,0,0])
p.resetSimulation()
planeId = p.loadURDF("plane.urdf")
Sid=p.loadURDF(urfdName, useFixedBase=True)
p.setGravity(0,0,-9.81)
p.setTimeStep(1./200.)
p.stepSimulation()
return Sid,planeId
#_____________
physicsClient = p.connect(p.GUI)
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally
joints=list(range(14,21)) if RightArm else list(range(21,28))
feedback_theta=np.zeros([N,7])
feedback_thetaDot=np.zeros([N,7])
actions=np.zeros([N,7])
Vrate=1
#_____________
i=0
j=0
flag=True
Sid,planeId=Reset()
while flag:
time.sleep((1./200.)/Vrate)
p.setJointMotorControlArray(bodyUniqueId=Sid,
jointIndices=joints,
controlMode=p.POSITION_CONTROL,
targetPositions = M[j:j+7]
) #forces=np.full((7,),10.)
p.stepSimulation()
actions[i]=M[j:j+7]
JointStates=p.getJointStates(Sid,joints)
for ii in range(7):
feedback_theta[i][ii]=JointStates[ii][0] #theta
feedback_thetaDot[i][ii]=JointStates[ii][1] #theta_dot
j+=7
i+=1
if i==N:
print("_____ Press Y if you want to replay, N if you want to continue, a number if you want to change the rate _____")
inp=input()
try:
Vrate = float(inp)
Reset()
i,j=0,0
except ValueError:
if (inp=="Y" or inp=="y"):
Reset()
i,j,Vrate=0,0,1
else:
flag=False
p.disconnect()
def cal_derivative(param):
der=np.zeros((N,7))
for i in range(1,N):
for j in range(7):
der[i][j]=(param[i][j]-param[i-1][j])*200.
return der
def plot_actions():
plt.figure()
plt.plot(actions)
plt.legend([1,2,3,4,5,6,7])
plt.title("Actions")
def plot_feedbacks():
for fb in feed_backs:
plt.figure()
plt.plot(feed_backs[fb])
plt.legend([1,2,3,4,5,6,7])
plt.title(fb)
if Plot:
import matplotlib.pyplot as plt
accelerations=cal_derivative(feedback_thetaDot)
jerks=cal_derivative(accelerations)
feed_backs={"FeedBackTheta":feedback_theta,
"FeedBackTheta_Dot":feedback_thetaDot,
"Accelerations":accelerations,
"Jerks":jerks}
plot_actions()
plot_feedbacks()
plt.show()
print("^_^")