generated from hariharan382/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleg_simukation.py
190 lines (160 loc) · 5.49 KB
/
leg_simukation.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
import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d
from math import sin, cos
import fk
import math
limit=10
fig = plt.figure()
ax=fig.add_subplot(111,projection='3d')
ax.set_xlim(-limit,limit)
ax.set_ylim(-limit,limit)
ax.set_zlim(-limit,limit)
ax.set_xlabel('"X')
ax.set_ylabel("Y")
ax.set_zlabel("Z")
o=np.matrix([[0],
[0],
[0],
[1]])
def point(x):
df=x@o
print(df)
ax.scatter(df.item(0),df.item(1),df.item(2),cmap='Greens')
#print(df.item(0),"------",df.item(1),"------",df.item(2))
return df
def dh(q, a, d, t):
z = np.matrix([[np.cos(math.radians(t)), -np.sin(math.radians(t)), 0, q],
[np.sin(math.radians(t)) * np.cos(math.radians(a)), np.cos(math.radians(t)) * np.cos(math.radians(a)),
-np.sin(math.radians(a)), -d * np.sin(math.radians(a))],
[np.sin(math.radians(t)) * np.sin(math.radians(a)), np.cos(math.radians(t)) * np.sin(math.radians(a)),
np.cos(math.radians(a)), d * np.cos(math.radians(a))],
[0, 0, 0, 1]])
return z
def ik(x, y, z):
alpha=math.degrees(math.atan(y/z))
phi = math.degrees(math.atan(x / z))
r=math.sqrt(z**2+x**2)
w=r*np.cos(math.radians(phi))
#r0=math.sqrt(h2**2-l1**2)
h=math.sqrt(y**2+z**2)
P=(l1**2+h**2-w**2)/(2*l1*h)
Q=(l2**2 + r**2-l3**2)/(2*l2*r)
R=(r**2 -l2**2-l3**2)/(2*l2*l3)
theta1=alpha+math.degrees(math.atan2(P,math.sqrt(1-P**2)))
theta2=phi+math.degrees(math.atan2(Q,math.sqrt(1-Q**2)))
theta3=math.degrees(math.atan2(math.sqrt(1-R**2),R))
return theta1,theta2,theta3
l1=2
l2=4
l3=4
m=np.matrix([[2,-2,1,1]
,[-3,3,-2,-1]
,[0,0,1,0]
,[1,0,0,0]])
def lin_plot(x,y):
a=x
b=y
plt.plot([a.item(0),b.item(0)],[a.item(1),b.item(1)],[a.item(2),b.item(2)],linewidth=0.5)
def rotx(alpha):
rx = np.array([[1, 0, 0, 0 ],
[0, cos(alpha), -sin(alpha),0],
[0, sin(alpha), cos(alpha),0 ],
[0,0,0,1]])
return rx
def roty(beta):
ry = np.array([[cos(beta), 0, sin(beta),0],
[0, 1, 0 ,0],
[-sin(beta), 0, cos(beta),0],
[0,0,0,1]])
return ry
def rotz(gamma):
rz = np.array([[cos(gamma), -sin(gamma), 0,0],
[sin(gamma), cos(gamma), 0,0],
[0, 0, 1,0],
[0,0,0,1]])
return rz
def rotxyz(alpha, beta, gamma):
return rotx(alpha).dot(roty(beta)).dot(rotz(gamma))
q=rotxyz(0,3.14/2,0)
fg=dh(0,0,0,0)
a = dh(0, -90, 2, 45)
b=dh(l2,0,0,90)
c=dh(l3,0,0,45)
"""print(fg)
print("===================")
print(point(np.eye(4)))
print(o)
print(np.transpose(point(np.eye(4)))@np.transpose(o))"""
#print("/////")
w=np.array([[0,0,0,1]])
z=np.matrix([[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]])
"""lin_plot(point(z), point(fg))
print("------------------111111111111--------")
lin_plot(point(fg), point(fg @ a))
print("------------------22222222222-----------")
lin_plot(point(fg @ a), point(fg @ a @ b))
print("---------------33333333333--------------")
lin_plot(point(fg @ a @ b), point(fg @ a @ b @ c))
print("----------------444444444444444---------------------")
print(np.around(fg @ a @ b @ c))"""
ef = fg @ a @ b @ c
st_x=ef.item(3)
st_y=ef.item(7)
st_z=np.round(ef.item(11),2)
#print(st_x,st_y,st_z)
sl=4
bl=np.matrix([[ef.item(3),ef.item(3)-sl,-6/np.sqrt(2),6/np.sqrt(2)]
,[2,2,0,0]
,[-5.66,-5.66,6/np.sqrt(2),-6/np.sqrt(2)]])
mat=np.matrix([[1,-6,15,-20,15,-6,1],
[-6,30,-60,60,-30,6,0],
[15,-60,90,-60,15,0,0],
[-20,60,-60,20,0,0,0],
[15,-30,15,0,0,0,0],
[-6,6,0,0,0,0,0],
[1,0,0,0,0,0,0]])
h=3
s=5
points=np.matrix([[0,2,-5.65],
[0+(s/2),2,-5.65],
[0+(s/2),2,-5.65+h],
[0,2,-5.65+h],
[0-(s/2),2,-5.65+h],
[0-(s/2),2,-5.65],
[0,2,-5.65],
])
x=points[:,0]
y=points[:,1]
z=points[:,2]
print(z.shape)
for t in np.arange(0,1,0.1):
p = np.matrix([t ** 6, t ** 5, t ** 4, t ** 3, t ** 2, t, 1])
x1=p@mat@x
y1=p@mat@y
z1=p@mat@z
print(z1)
ax.scatter(np.asscalar(x1),np.asscalar(y1),np.asscalar(z1),cmap='Greens')
#print((df.item(0)," --- ",df.item(1)," ----- ",df.item(2)))
print(np.asscalar(x1)," ",np.asscalar(y1)," ",np.asscalar(z1))
theta1,theta2,theta3=ik(np.asscalar(x1),np.asscalar(y1),np.asscalar(z1))
g = fk.FK(round(theta1,0),round(theta2,0),round(theta3,0))
#print(round(theta1,0)," --- ",round(theta2,0)," ----- ",round(theta3,0))
g.fk(theta1, theta2,theta3)
print("IK")
print(ik(0,2,-5.67))
plt.show()
print("hello")
#[ 0. 2. -5.657]
"""for u in np.arange(0,1,0.1):
Y = np.matrix([u ** 3, u ** 2, u, 1])
df=Y@[email protected](bl)
ax.scatter(df.item(0),df.item(1),df.item(2),cmap='Greens')
#print((df.item(0)," --- ",df.item(1)," ----- ",df.item(2)))
theta1,theta2,theta3=ik(df.item(0),df.item(1),df.item(2))
z = fk.FK(round(theta1,0),round(theta2,0),round(theta3,0))
print(round(theta1,0)," --- ",round(theta2,0)," ----- ",round(theta3,0))
z.fk(theta1, theta2,theta3)"""