-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroboticArm4.py
More file actions
269 lines (210 loc) · 7.44 KB
/
roboticArm4.py
File metadata and controls
269 lines (210 loc) · 7.44 KB
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
# copiada de kinematics el 29/12/2024
# adaptada a autofore
# deriva de clock2 y es backpropagation2->neuronalprogrammig4
import pygame
import math
from autoforenumpy import AutoFore
import time
class Parameters:
def __init__(self):
self.width = 600
self.height = 600
self.white = (255, 255, 255)
self.red=(255,0,0)
self.green=(0,255,0)
self.blue=(0,0,255)
self.yellow=(255,255,0)
self.black=(0,0,0)
self.circle_radius = 5 # Radio del círculo
self.max_angle_velocity = 0.01
class Transform:
def __init__(self,nn):
self.nn=nn
self.matrix = None
# [
# [nn.const(1), nn.const(0), nn.const(0)],
# [nn.const(0), nn.const(1), nn.const(0)],
# [nn.const(0), nn.const(0), nn.const(1)]
# ]
def rotate(self, angle):
nn=self.nn
if self.matrix==None:
self.matrix= [
[nn.const(0), nn.const(0), nn.const(0)],
[nn.const(0), nn.const(0), nn.const(0)],
[nn.const(0), nn.const(0), nn.const(1)]
]
self.matrix[0][0].assign(angle.cos())
self.matrix[0][1].assign(-angle.sin())
self.matrix[1][0].assign(angle.sin())
self.matrix[1][1].assign(angle.cos())
def translate(self, translation):
nn=self.nn
self.matrix = [
[nn.const(1), nn.const(0), translation[0]],
[nn.const(0), nn.const(1), translation[1]],
[nn.const(0), nn.const(0), nn.const(1)]
]
class Arm:
def __init__(self,p,nn,segment_length,color):
self.p=p
self.nn=nn
self.color=color
self.size=Transform(nn)
self.segment_length=segment_length
self.size.translate((0,self.segment_length))
self.rota=Transform(nn)
self.children=[]
self.angle=nn.val(0)
def setAngle(self,angle):
#nn=self.nn
self.angle.assign(angle)
self.rota.rotate(self.angle)
def draw(self,screen,center,id,tono=1):
b=self.matrix_multiplication(center,self.rota.matrix)
c= self.matrix_multiplication(b,self.size.matrix)
self.x=c[0][2]
self.y=c[1][2]
pygame.draw.line(screen, (self.color[0]*tono,self.color[1]*tono,self.color[2]*tono), self._fromPoint(center,id), self._fromPoint(c,id) , 5)
for child in self.children:
child.draw(screen,c,id,tono)
def _fromPoint(self,point,id):
return [point[0][2].value(id),point[1][2].value(id)]
def matrix_multiplication(self,A, B):
if len(A[0]) != len(B):
raise ValueError("Number of columns in A should be equal to the number of rows in B")
# Obtener dimensiones
rows_A, cols_A = len(A), len(A[0])
rows_B, cols_B = len(B), len(B[0])
# Inicializar matriz de resultado con ceros
result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]
return result
def addChildren(self,child):
self.children.append(child)
class RoboticArm:
def __init__(self, p):
self.p = p
nn=AutoFore(gradientes=6,variables=500,poblacion=2)
# Inicializar pygame
pygame.init()
screen = pygame.display.set_mode((p.width, p.height))
pygame.display.set_caption("Robotic Arm")
center=Transform(nn)
center.translate((nn.const(p.width//3),nn.const(p.height//2)))
radio_ojo=100
focus_cam=(p.width,p.height//2)
ma=nn.random(50,200).differentiable()
aa=nn.random(0,math.pi*2).differentiable()
md=nn.random(50,200).differentiable()
ad=nn.random(0,math.pi*2).differentiable()
mb=nn.random(50,200).differentiable()
ab=nn.random(0,math.pi*2).differentiable()
a=Arm(p,nn,ma,p.red)
a.setAngle(aa)
d=Arm(p,nn,md,p.green)
d.setAngle(ad)
a.addChildren(d)
b=Arm(p,nn,mb,p.blue)
b.setAngle(ab)
d.addChildren(b)
circle_position = (100,100)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
click_x, click_y = event.pos
if focus_cam==None:
focus_cam=(click_x,click_y)
else:
circle_position = (click_x, click_y) # Guardar posición del clic para dibujar el círculo
screen.fill(p.white)
since=time.time()
a.draw(screen,center.matrix,0,tono=1)
a.draw(screen,center.matrix,1,tono=0.5)
if focus_cam:
pygame.draw.circle(screen, p.black, focus_cam, 5)
pygame.draw.circle(screen, p.black, focus_cam, radio_ojo, 1)
for c in [a,d,b]:
angle_grad_y=b.y.get(c.angle,0)
angle_grad_x=b.x.get(c.angle,0)
norm=math.sqrt(angle_grad_x**2+angle_grad_y**2)/20
norm=1
#print(angle_grad_x,angle_grad_y)
#pygame.draw.line(screen, c.color, (b.x.value(0),b.y.value(0)), (b.x.value(0)+angle_grad_x/norm,b.y.value(0)+angle_grad_y/norm) , 1)
if focus_cam:
pygame.draw.line(screen,c.color,(c.x.value(0),c.y.value(0)),focus_cam,1)
m=(c.y-focus_cam[1])/(c.x-focus_cam[0])
# pendiente a ángulo
angle=m.atan()
#angle=math.atan2(c.y.value(0)-focus_cam[1],c.x.value(0)-focus_cam[0])
#if c==a:
error=angle-angle.value(0)
error2=error*error
error2.error2Delta()
# calcula el vector normalizado focus->c
x_n=c.x.value(0)-focus_cam[0]
y_n=c.y.value(0)-focus_cam[1]
# Normaliza el vector
norm=math.sqrt(x_n**2+y_n**2)
x_n=x_n/norm*radio_ojo+focus_cam[0]
y_n=y_n/norm*radio_ojo+focus_cam[1]
# calcula el punto medio
#middle=((c.x.value(0)+focus_cam[0])//2,(c.y.value(0)+focus_cam[1])//2)
# draw a label m in the middle
# font = pygame.font.Font(None, 36)
# text = font.render(str(round(angle.value(0),2)), True, c.color)
# screen.blit(text, middle)
derivate=angle.get(c.angle,0)*100
#derivate=angle.get(c.segment_length)*10000
# draw a ortogonal line from the middle with module derivate
# Calcula el vector ortogonal (derivada perpendicular)
orthogonal_angle = angle.value(0) - math.pi / 2 # Ángulo perpendicular
length = derivate # Longitud del vector ortogonal
end_point = (
x_n + length * math.cos(orthogonal_angle),
y_n + length * math.sin(orthogonal_angle)
)
# Dibuja la línea ortogonal desde el punto medio
pygame.draw.line(screen, c.color, (x_n,y_n), end_point, 1)
nn.applyDelta(0.001)
if circle_position:
# halla el vector normalizado
x_n=circle_position[0]-b.x.value(0)
y_n=circle_position[1]-b.y.value(0)
norm=math.sqrt(x_n**2+y_n**2)
#norm=20000
x_n=x_n/norm
y_n=y_n/norm
# lo dibuja
#pygame.draw.line(screen, p.black, (b.x.value(0),b.y.value(0)), (b.x.value(0)+x_n*30,b.y.value(0)+y_n*30) , 1)
# calcula el producto escalar
# mide el tiempo
# star=time.time()
for c in [a,d,b]:
angle_grad_y=b.y.get(c.angle,0)
angle_grad_x=b.x.get(c.angle,0)
producto_escalar=x_n*angle_grad_x+y_n*angle_grad_y
angle_velocity=p.max_angle_velocity*norm/100
#angle_velocity=p.max_angle_velocity
if producto_escalar>angle_velocity:
producto_escalar=angle_velocity
if producto_escalar<-angle_velocity:
producto_escalar=-angle_velocity
c.setAngle(c.angle+producto_escalar)
# calcula el tiempo que tardó
# end=time.time()
# print(end-star)
pygame.draw.circle(screen, p.black, circle_position, p.circle_radius)
print("Tiempo:",time.time()-since)
# Actualizar la ventana
pygame.display.flip()
nn.noMoreConst()
pygame.quit()
if __name__ == '__main__':
RoboticArm(Parameters())