-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFootTouchdown.py
299 lines (240 loc) · 9.77 KB
/
FootTouchdown.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import pybullet as p
import time
import pybullet_data
import math
import matplotlib.pyplot as plt
import numpy as np
physicsClient = p.connect(p.GUI)#or p.DIRECT for non-graphical version
print("hi")
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally
p.setGravity(0,0,-9.8)
planeId = p.loadURDF("plane.urdf") #where is plane.urdf
startPos = [0,0,0]
#sphereId = p.loadURDF("sphere.urdf")
startOrientation = p.getQuaternionFromEuler([0,0,0])
robotId = p.loadURDF("olympian.urdf",startPos, startOrientation,
# useMaximalCoordinates=1, ## New feature in Pybullet
flags=p.URDF_USE_INERTIA_FROM_FILE)
print("==========SIMULATION ENABLED================")
#GET JOINT INFO
print(p.getNumJoints(robotId))
for i in range(0, p.getNumJoints(robotId)-1):
print(p.getJointInfo(robotId, i)[0:13])
listOfJointIndeces = []
for i in range(0, p.getNumJoints(robotId)):
listOfJointIndeces.append(i)
#why not use list(range())?
#also why have 0
def calcCOM():
#CALCULATE COM
masstimesxpossum = 0.0
masstimesypossum = 0.0
masstimeszpossum = 0.0
masssum = 0.0
for i in range(0, p.getNumJoints(robotId) -1):
# if(i >= 0):
# print(p.getJointInfo(robotId, i)[0:13])
wheight = p.getDynamicsInfo(robotId, i)[0]
xpos = p.getLinkState(robotId, i)[0][0]
ypos = p.getLinkState(robotId, i)[0][1]
zpos = p.getLinkState(robotId, i)[0][2]
masstimesxpossum += (wheight * xpos)
masstimesypossum += (wheight * ypos)
masstimeszpossum += (wheight * zpos)
masssum += wheight
# print(wheight)
# print(xpos)
# print(ypos)
# print(zpos)
# print("\n")
p.stepSimulation()
com = (masstimesxpossum/masssum, masstimesypossum/masssum, masstimeszpossum/masssum)
#print("mass: " + str(masssum))
#print("center of mass: " + str(com))
#print("\n")
return com
#INVERSE KINEMATICS
#GET JOINT ANGLES NEEDED TO MOVE LINK 10 to (0. 0.6, 2)
#FORCE IS 25NM for every link bc im too lazy to custom set it for every link
#IT FALLS BECAUSE its a lot of torque exerted in a small amount of time
torque = 100
newi = []
comPos = []
comPos2 = []
holdingTorque = 100
actuationTorque = 10
torqueList = [holdingTorque]*23
file = open("xValues.txt", "r")
xValues = []
for word in file.readlines():
xValues.append(float(word.rstrip('\n')))
file = open("zValues.txt", "r")
zValues = []
for word in file.readlines():
zValues.append(float(word.rstrip('\n')))
def shiftFoot():
leftAnkle = p.getJointState(robotId, 21)[0]
leftThigh = p.getJointState(robotId, 18)[0]
print(leftAnkle)
print(leftThigh)
for i in range(0, 50):
positionsList = [0]*23
angleNeeded = calculateAngleNeeded()
comPos.append(calcCOM())
newi.insert(0, i)
positionsList[21] = angleNeeded/50 * i #left ankle
positionsList[15] = angleNeeded/50 * i #right ankle
positionsList[18] = angleNeeded/50 * i#left thigh
positionsList[12] = angleNeeded/50 * i#right thigh
#positionsList[14] = 0.3/50 * i
forceArray = [torque]*23
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL, targetPositions = positionsList, forces = forceArray)
p.stepSimulation()
for i in range(0, 50):
comPos.append(calcCOM())
newi.insert(0, i)
angleNeeded = 0.5
positionsList[11] = 0.2598371070140097/50 * i #thigh pitch
positionsList[14] = 0.8123194567657278/50 * i #knee
positionsList[16] = 0.4891906071331436/50 * i#ankle
#positionsList[14] = 0.3/50 * i
forceArray = [torque]*23
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL, targetPositions = positionsList, forces = forceArray)
p.stepSimulation()
positionsList = [0]*23
firstY = p.getLinkState(robotId, 15)[0][1]
firstZ = p.getLinkState(robotId, 15)[0][2]
for i in range(0, len(xValues)//20):
"""
comPos.append(calcCOM())
newi.insert(0, i)
angleNeeded = calculateAngleNeeded()
positionsList[21] = angleNeeded/len(xValues) * i #left ankle
#positionsList[15] = angleNeeded/len(xValues) * i #right ankle
positionsList[18] = angleNeeded/len(xValues) * i#left thigh
#positionsList[12] = angleNeeded/len(xValues) * i#right thigh
forceArray = [torque]*23
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL, targetPositions = positionsList, forces = forceArray)
p.stepSimulation()
"""
#thighs are 12 and 18
angleNeeded = calculateAngleNeeded()
ikList = p.calculateInverseKinematics(robotId, 15, [xValues[20*i], firstY, zValues[20*i]+(len(xValues)-20*i)/len(xValues)*firstZ])
positionsList = list(ikList)
#print("uwu")
#print(positionsList[11])
#print(positionsList[14])
#print(positionsList[16])
#positionsList[11] = 0.5*(len(xValues)-i)/len(xValues)+positionsList[11]*(i/len(xValues))
#positionsList[14] = 1*(len(xValues)-i)/len(xValues)+positionsList[14]*(i/len(xValues))
#positionsList[16] = 0.5*(len(xValues)-i)/len(xValues)+positionsList[16]*(i/len(xValues))
positionsList[21] = angleNeeded*(len(xValues)-8*i)/len(xValues)
positionsList[18] = angleNeeded*(len(xValues)-8*i)/len(xValues)
ankleAngle = calculateAnkleAngle()
positionsList[15] = (math.pi/2-ankleAngle*np.sign(ankleAngle))*np.sign(ankleAngle)/len(xValues)*i*20
ankleAngle = calculateOtherAnkleAngle()
positionsList[16] = 0.4891906071331436*(len(xValues)-(i*20))/len(xValues)+ankleAngle/len(xValues)*i*20
pelvisAngle = calculatePelvisAngle()
pelvisAngle += 1.1
positionsList[0] = -1*pelvisAngle
#positionsList[15] = (math.pi-ankleAngle*np.sign(ankleAngle))*np.sign(ankleAngle)/len(xValues)*i
'''
if((ankleAngle)<0):
positionsList[15] = -1*(math.pi-(abs(ankleAngle)))/len(xValues)*i
else:
positionsList[15] = math.pi-(abs(ankleAngle))/len(xValues)*i
'''
#positionsList[16] =
#positionsList[15] = (math.pi-calculateAnkleAngle())/len(xValues)*i
# positionsList[0] = math.pi / 4
forceArray = [torque]*23
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL, targetPositions = positionsList, forces = forceArray)
p.stepSimulation()
for i in range(50000):
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL, targetPositions = positionsList, forces = forceArray)
p.stepSimulation()
def calculateAngleNeeded():
anklePos = p.getLinkState(robotId, 21)[0]
comPos = calcCOM()
hipPos = p.getLinkState(robotId, 18)[0]
ankleCom = (comPos[1]-anklePos[1], comPos[2]-anklePos[2])
ankleHip = (hipPos[1]-anklePos[1], hipPos[2]-anklePos[2])
cosAngle = (ankleCom[0]*ankleHip[0] + ankleCom[1]*ankleHip[1])/(np.sqrt((ankleCom[0]**2+ankleCom[1]**2))*np.sqrt((ankleHip[0]**2 + ankleHip[1]**2)))
angle = np.arccos(cosAngle)
#print(angle)
return angle
def calculateAnkleAngle():
anklePos = p.getLinkState(robotId, 15)[0]
kneePos = p.getLinkState(robotId, 14)[0]
angle = np.arctan((kneePos[2]-anklePos[2])/(kneePos[1]-anklePos[1]))
print("thing " + str(angle))
return angle
def calculateOtherAnkleAngle():
anklePos = p.getLinkState(robotId, 15)[0]
kneePos = p.getLinkState(robotId, 14)[0]
angle = np.arctan((kneePos[0]-anklePos[0])/(kneePos[2]-anklePos[2]))
print("thing " + str(angle))
return angle
def calculatePelvisAngle():
pelvisPos = p.getLinkState(robotId, 0)[0]
chestPos = p.getLinkState(robotId, 2)[0]
print(chestPos)
print(pelvisPos)
angle = np.arctan((chestPos[0]-pelvisPos[0])/(chestPos[2]-pelvisPos[2]))
return angle
shiftFoot()
print(p.getLinkState(robotId, 21)[0][1]-calcCOM()[1])
print(str(p.getLinkState(robotId, 21)) + "HIIII")
for i in range(1000000):
p.stepSimulation()
"""
for i in range(0, len(xValues), 1):
ikList = p.calculateInverseKinematics(robotId, 15, [xValues[i], 0, zValues[i]])
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL,
targetPositions = ikList, forces = torqueList)
p.stepSimulation()
time.sleep(1./240.)
for i in range(10000):
p.stepSimulation()
time.sleep(1./240.)
"""
z = list(range(len(xValues)))
area = 3 # 0 to 15 point radii
plt.scatter(z, zValues, s=area, alpha=0.5)
#plt.show()
#ikList = p.calculateInverseKinematics(robotId, 10, [0,0.6,2] )
"""
p.setJointMotorControlArray(robotId, listOfJointIndeces, p.POSITION_CONTROL,
targetPositions = ikList, forces = torqueList)
#CALCULATE COM
masstimesxpossum = 0.0
masstimesypossum = 0.0
masstimeszpossum = 0.0
masssum = 0.0
for i in range(0, p.getNumJoints(robotId) -1):
if(i >= 0):
print(p.getJointInfo(robotId, i)[0:13])
wheight = p.getDynamicsInfo(robotId, i)[0]
xpos = p.getLinkState(robotId, i)[0][0]
ypos = p.getLinkState(robotId, i)[0][1]
zpos = p.getLinkState(robotId, i)[0][2]
masstimesxpossum += (wheight * xpos)
masstimesypossum += (wheight * ypos)
masstimeszpossum += (wheight * zpos)
masssum += wheight
print(wheight) #what is wheight
print(xpos)
print(ypos)
print(zpos)
print("\n")
com = (masstimesxpossum/masssum, masstimesypossum/masssum, masstimeszpossum/masssum)
print("==========COM APROX EQUALS===========")
print(com)
print("\n")
#STEP SIMULATION
for i in range(0,10000):
p.stepSimulation()
time.sleep(1./240.)
print("========REALTIME SIMULATION DISABLED===============")
# p.calculateInverseKinematics(robotId, )
"""