-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeFootTrajectoryThing.py
239 lines (190 loc) · 7.56 KB
/
theFootTrajectoryThing.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
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
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally
p.setGravity(0,0,-1)
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 = 20
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():
for i in range(0, 50):
positionsList = [0]*23
comPos.append(calcCOM())
newi.insert(0, i)
angleNeeded = calculateAngleNeeded()
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.2
positionsList[11] = angleNeeded/50 * i #thigh pitch
positionsList[13] = 2*angleNeeded/50 * i #knee
positionsList[14] = angleNeeded/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)):
"""
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[i], firstY, zValues[i]+(len(xValues)-i)/len(xValues)*firstZ])
positionsList = list(ikList)
positionsList[21] = angleNeeded
positionsList[18] = angleNeeded
# positionsList[0] = math.pi / 4
forceArray = [torque]*23
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
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, )
"""