Hi I have a project about robotics arm and I need to visualize motion of robotic arm. I have a code for taking coordinates and visualize it but I want a animation for it. I used for loop but I couldn’t success.
Here’s the code I’ve wrote:
import tkinter as tk
import matplotlib.pyplot as plt
import math
import numpy as np
import time
##########################################
from tkinter.ttk import Combobox
from ntpath import join
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.widgets import Slider
from matplotlib import animation
##########################################
#form=tk.Tk()
#form.geometry("500x500+100+100")
#label = tk.Label(text="Robot Kol arayüz",fg="black",bg="red").pack()
form=tk.Tk()
form.title('Robot Kol Arayüz v1"')
form.resizable(False,False)
form.config(bg='gray')
form.geometry('300x150+900+100')
servoderece=tk.StringVar()
secilenservo=tk.StringVar()
def yazdır():
#print(secilenservo.get(),',',servoderece.get(),'derece döndürüldü')
if secilenservo.get() == 'X':
Xguncelle(int(servoderece.get()))
elif secilenservo.get() == 'Y':
Yguncelle(int(servoderece.get()))
elif secilenservo.get() == 'Z':
Zguncelle(int(servoderece.get()))
print(secilenservo.get(), ',', servoderece.get(), 'konumuna getirildi.')
label=tk.Label(text='HANGİ SERVO?',bg='gray',fg='black').pack()
values=['X','Y','Z']
servosec=Combobox(form,values=values,height=3,textvariable=secilenservo,).pack()
label2=tk.Label(text='Kaç derece döndürmek istiyorsunuz?',bg='gray',fg='black').pack()
spin=tk.Spinbox(form,from_=-180,to=180,textvariable=servoderece).pack()
buton=tk.Button(form,text='DÖNDÜR',command=yazdır,activebackground='blue',activeforeground='green').pack()
home = [0,0,0];
alt_kısım = 0
uzun_kol_uzunluk = 15
kucuk_kol_uzunluk = 10
eklem1 = 0
eklem2 = 0
eklem3 = 0
nokta1 = [0,0,0]
nokta2 = [0,-5,0]
nokta3 = [0,0,0]
figure = plt.figure(figsize=(7,7)) ##koordinat sisteminin ilk açıldığında ne kadarlık bir alan kaplayacağı
Axes = figure.add_subplot(111,projection='3d')
xSlider = plt.axes([0.2, 0.1, 0.65, 0.03] )
ySlider = plt.axes([0.2, 0.065, 0.65, 0.03] )
zSlider = plt.axes([0.2, 0.03, 0.65, 0.03] )
sliderX = Slider(xSlider, 'X', 0, 180.0, valinit=0, valstep = 1)
sliderY = Slider(ySlider, 'Y', -180, 15.0, valinit=-90, valstep = 1)
sliderZ = Slider(zSlider, 'Z', -155, 155.0, valinit=-155, valstep = 1)
def forwKinematik(aci1,aci2,aci3):
# 1.nokta
trans = np.dot(np.matrix([[1,0,0,alt_kısım],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([home[0],home[1],home[2],1]).transpose())
nokta1[0] = trans[(0,0)]
nokta1[1] = trans[(1,0)]
nokta1[2] = trans[(2,0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci1)),-math.sin(math.radians(aci1)),0,0],
[math.sin(math.radians(aci1)),math.cos(math.radians(aci1)),0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([nokta1[0],nokta1[1],nokta1[2],1]).transpose())
nokta1[0] = rot[(0,0)]
nokta1[1] = rot[(1,0)]
nokta1[2] = rot[(2,0)]
# 2.nokta
trans = np.dot(np.matrix([[1,0,0,uzun_kol_uzunluk],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([home[0],home[1],home[2],1]).transpose())
nokta2[0] = trans[(0,0)]
nokta2[1] = trans[(1,0)]
nokta2[2] = trans[(2,0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci2)),0,math.sin(math.radians(aci2)),0],
[0,1,0,0],
[-math.sin(math.radians(aci2)),0,math.cos(math.radians(aci2)),0],
[0,0,0,1]]),np.matrix([nokta2[0],nokta2[1],nokta2[2],1]).transpose())
nokta2[0] = rot[(0,0)]
nokta2[1] = rot[(1,0)]
nokta2[2] = rot[(2,0)]
trans = np.dot(np.matrix([[1,0,0,alt_kısım],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([nokta2[0],nokta2[1],nokta2[2],1]).transpose())
nokta2[0] = trans[(0,0)]
nokta2[1] = trans[(1,0)]
nokta2[2] = trans[(2,0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci1)),-math.sin(math.radians(aci1)),0,0],
[math.sin(math.radians(aci1)),math.cos(math.radians(aci1)),0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([nokta2[0],nokta2[1],nokta2[2],1]).transpose())
nokta2[0] = rot[(0,0)]
nokta2[1] = rot[(1,0)]
nokta2[2] = rot[(2,0)]
# 3.nokta
trans = np.dot(np.matrix([[1,0,0,kucuk_kol_uzunluk],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([home[0],home[1],home[2],1]).transpose())
nokta3[0] = trans[(0,0)]
nokta3[1] = trans[(1,0)]
nokta3[2] = trans[(2,0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci3)),0,math.sin(math.radians(aci3)),0],
[0,1,0,0],
[-math.sin(math.radians(aci3)),0,math.cos(math.radians(aci3)),0],
[0,0,0,1]]),np.matrix([nokta3[0],nokta3[1],nokta3[2],1]).transpose())
nokta3[0] = rot[(0,0)]
nokta3[1] = rot[(1,0)]
nokta3[2] = rot[(2,0)]
trans = np.dot(np.matrix([[1,0,0,uzun_kol_uzunluk],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([nokta3[0],nokta3[1],nokta3[2],1]).transpose())
nokta3[0] = trans[(0,0)]
nokta3[1] = trans[(1,0)]
nokta3[2] = trans[(2,0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci2)),0,math.sin(math.radians(aci2)),0],
[0,1,0,0],
[-math.sin(math.radians(aci2)),0,math.cos(math.radians(aci2)),0],
[0,0,0,1]]),np.matrix([nokta3[0],nokta3[1],nokta3[2],1]).transpose())
nokta3[0] = rot[(0,0)]
nokta3[1] = rot[(1,0)]
nokta3[2] = rot[(2,0)]
trans = np.dot(np.matrix([[1,0,0,alt_kısım],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([nokta3[0],nokta3[1],nokta3[2],1]).transpose())
nokta3[0] = trans[(0,0)]
nokta3[1] = trans[(1,0)]
nokta3[2] = trans[(2,0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci1)),-math.sin(math.radians(aci1)),0,0],
[math.sin(math.radians(aci1)),math.cos(math.radians(aci1)),0,0],
[0,0,1,0],
[0,0,0,1]]),np.matrix([nokta3[0],nokta3[1],nokta3[2],1]).transpose())
nokta3[0] = rot[(0,0)]
nokta3[1] = rot[(1,0)]
nokta3[2] = rot[(2,0)]
return nokta1, nokta2, nokta3
def Xguncelle(value=servoderece.get()):
global eklem1
if value>180: #servo 180 dereceden fazla ve 0dan az değer alırsa değeri max min alabileceği değere sabitle
value=180
elif value<0:
value=0
eklem1 = value
Axes.clear()
nokta1,nokta2,nokta3 = forwKinematik(eklem1 ,eklem2, eklem3)
Axes.plot([home[0],nokta1[0]],[home[1],nokta1[1]],[home[2],nokta1[2]])
Axes.plot([nokta1[0],nokta2[0]],[nokta1[1],nokta2[1]],[nokta1[2],nokta2[2]])
Axes.plot([nokta2[0],nokta3[0]],[nokta2[1],nokta3[1]],[nokta2[2],nokta3[2]])
Axes.plot([-10,10],[0,0],[0,0], color='red')
Axes.plot([0,0],[-10,10],[0,0], color='green')
Axes.plot([0,0],[0,0],[-10,10], color='blue')
plt.draw()
def Yguncelle(value=servoderece.get()):
global eklem2
if value>15: #servo 15 dereceden fazla ve -180den az değer alırsa değeri max min alabileceği değere sabitle
value=15
elif value<-180:
value=180
eklem2 = value
Axes.clear()
nokta1,nokta2,nokta3 = forwKinematik(eklem1,eklem2,eklem3)
Axes.plot([home[0],nokta1[0]],[home[1],nokta1[1]],[home[2],nokta1[2]])
Axes.plot([nokta1[0],nokta2[0]],[nokta1[1],nokta2[1]],[nokta1[2],nokta2[2]])
Axes.plot([nokta2[0],nokta3[0]],[nokta2[1],nokta3[1]],[nokta2[2],nokta3[2]])
Axes.plot([-10,10],[0,0],[0,0], color='red')
Axes.plot([0,0],[-10,10],[0,0], color='green')
Axes.plot([0,0],[0,0],[-10,10], color='blue')
plt.draw()
def Zguncelle(value=servoderece.get()):
global eklem3
if value>155: #servo 155 dereceden fazla ve -155den az değer alırsa değeri max min alabileceği değere sabitle
value=155
elif value<-155:
value=-155
for i in range (0,value,1):
eklem3 = i
Axes.clear()
nokta1,nokta2,nokta3 = forwKinematik(eklem1,eklem2,eklem3)
Axes.plot([home[0],nokta1[0]],[home[1],nokta1[1]],[home[2],nokta1[2]])
Axes.plot([nokta1[0],nokta2[0]],[nokta1[1],nokta2[1]],[nokta1[2],nokta2[2]])
Axes.plot([nokta2[0],nokta3[0]],[nokta2[1],nokta3[1]],[nokta2[2],nokta3[2]])
Axes.plot([-10,10],[0,0],[0,0], color='red')
Axes.plot([0,0],[-10,10],[0,0], color='green')
Axes.plot([0,0],[0,0],[-10,10], color='blue')
plt.draw()
sliderX.on_changed(Xguncelle)
sliderY.on_changed(Yguncelle)
sliderZ.on_changed(Zguncelle)
plt.show()
form.mainloop()
It’s the old version of the code because I am using some raspberry pi librarys and this one is only for visualize GUI. I just need help in animation part.
I wanna make animation when robot arms are changing the coordinate.
Halil İbrahim ÇETİN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have added the animation part to your code
ani = animation.FuncAnimation(figure, animate, frames=100, interval=100)
at the end and also changed the width of the arm.
import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.widgets import Slider
from matplotlib import animation
home = [0, 0, 0]
alt_kısım = 0
uzun_kol_uzunluk = 15
kucuk_kol_uzunluk = 10
eklem1 = 0
eklem2 = 0
eklem3 = 0
nokta1 = [0, 0, 0]
nokta2 = [0, -5, 0]
nokta3 = [0, 0, 0]
figure = plt.figure(figsize=(7, 7))
Axes = figure.add_subplot(111, projection='3d')
xSlider = plt.axes([0.2, 0.1, 0.65, 0.03])
ySlider = plt.axes([0.2, 0.065, 0.65, 0.03])
zSlider = plt.axes([0.2, 0.03, 0.65, 0.03])
sliderX = Slider(xSlider, 'X', 0, 180.0, valinit=0, valstep=1)
sliderY = Slider(ySlider, 'Y', -180, 15.0, valinit=-90, valstep=1)
sliderZ = Slider(zSlider, 'Z', -155, 155.0, valinit=-155, valstep=1)
def forwKinematik(aci1, aci2, aci3):
trans = np.dot(np.matrix([[1, 0, 0, alt_kısım],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([home[0], home[1], home[2], 1]).transpose())
nokta1[0] = trans[(0, 0)]
nokta1[1] = trans[(1, 0)]
nokta1[2] = trans[(2, 0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci1)), -math.sin(math.radians(aci1)), 0, 0],
[math.sin(math.radians(aci1)), math.cos(math.radians(aci1)), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([nokta1[0], nokta1[1], nokta1[2], 1]).transpose())
nokta1[0] = rot[(0, 0)]
nokta1[1] = rot[(1, 0)]
nokta1[2] = rot[(2, 0)]
trans = np.dot(np.matrix([[1, 0, 0, uzun_kol_uzunluk],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([home[0], home[1], home[2], 1]).transpose())
nokta2[0] = trans[(0, 0)]
nokta2[1] = trans[(1, 0)]
nokta2[2] = trans[(2, 0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci2)), 0, math.sin(math.radians(aci2)), 0],
[0, 1, 0, 0],
[-math.sin(math.radians(aci2)), 0, math.cos(math.radians(aci2)), 0],
[0, 0, 0, 1]]), np.matrix([nokta2[0], nokta2[1], nokta2[2], 1]).transpose())
nokta2[0] = rot[(0, 0)]
nokta2[1] = rot[(1, 0)]
nokta2[2] = rot[(2, 0)]
trans = np.dot(np.matrix([[1, 0, 0, alt_kısım],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([nokta2[0], nokta2[1], nokta2[2], 1]).transpose())
nokta2[0] = trans[(0, 0)]
nokta2[1] = trans[(1, 0)]
nokta2[2] = trans[(2, 0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci1)), -math.sin(math.radians(aci1)), 0, 0],
[math.sin(math.radians(aci1)), math.cos(math.radians(aci1)), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([nokta2[0], nokta2[1], nokta2[2], 1]).transpose())
nokta2[0] = rot[(0, 0)]
nokta2[1] = rot[(1, 0)]
nokta2[2] = rot[(2, 0)]
trans = np.dot(np.matrix([[1, 0, 0, kucuk_kol_uzunluk],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([home[0], home[1], home[2], 1]).transpose())
nokta3[0] = trans[(0, 0)]
nokta3[1] = trans[(1, 0)]
nokta3[2] = trans[(2, 0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci3)), 0, math.sin(math.radians(aci3)), 0],
[0, 1, 0, 0],
[-math.sin(math.radians(aci3)), 0, math.cos(math.radians(aci3)), 0],
[0, 0, 0, 1]]), np.matrix([nokta3[0], nokta3[1], nokta3[2], 1]).transpose())
nokta3[0] = rot[(0, 0)]
nokta3[1] = rot[(1, 0)]
nokta3[2] = rot[(2, 0)]
trans = np.dot(np.matrix([[1, 0, 0, uzun_kol_uzunluk],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([nokta3[0], nokta3[1], nokta3[2], 1]).transpose())
nokta3[0] = trans[(0, 0)]
nokta3[1] = trans[(1, 0)]
nokta3[2] = trans[(2, 0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci2)), 0, math.sin(math.radians(aci2)), 0],
[0, 1, 0, 0],
[-math.sin(math.radians(aci2)), 0, math.cos(math.radians(aci2)), 0],
[0, 0, 0, 1]]), np.matrix([nokta3[0], nokta3[1], nokta3[2], 1]).transpose())
nokta3[0] = rot[(0, 0)]
nokta3[1] = rot[(1, 0)]
nokta3[2] = rot[(2, 0)]
trans = np.dot(np.matrix([[1, 0, 0, alt_kısım],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([nokta3[0], nokta3[1], nokta3[2], 1]).transpose())
nokta3[0] = trans[(0, 0)]
nokta3[1] = trans[(1, 0)]
nokta3[2] = trans[(2, 0)]
rot = np.dot(np.matrix([[math.cos(math.radians(aci1)), -math.sin(math.radians(aci1)), 0, 0],
[math.sin(math.radians(aci1)), math.cos(math.radians(aci1)), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]), np.matrix([nokta3[0], nokta3[1], nokta3[2], 1]).transpose())
nokta3[0] = rot[(0, 0)]
nokta3[1] = rot[(1, 0)]
nokta3[2] = rot[(2, 0)]
return nokta1, nokta2, nokta3
def update_plot(aci1, aci2, aci3):
Axes.clear()
nokta1, nokta2, nokta3 = forwKinematik(aci1, aci2, aci3)
Axes.plot([home[0], nokta1[0]], [home[1], nokta1[1]], [home[2], nokta1[2]], linewidth=25)
Axes.plot([nokta1[0], nokta2[0]], [nokta1[1], nokta2[1]], [nokta1[2], nokta2[2]], linewidth=5)
Axes.plot([nokta2[0], nokta3[0]], [nokta2[1], nokta3[1]], [nokta2[2], nokta3[2]], linewidth=5)
Axes.plot([-10, 10], [0, 0], [0, 0], color='red')
Axes.plot([0, 0], [-10, 10], [0, 0], color='green')
Axes.plot([0, 0], [0, 0], [-10, 10], color='blue')
plt.draw()
def animate(i):
global eklem1, eklem2, eklem3
eklem1 = (eklem1 + 1) % 180
eklem2 = (eklem2 + 1) % 15
eklem3 = (eklem3 + 1) % 155
update_plot(eklem1, eklem2, eklem3)
def Xguncelle(value):
global eklem1
eklem1 = int(value)
def Yguncelle(value):
global eklem2
eklem2 = int(value)
def Zguncelle(value):
global eklem3
eklem3 = int(value)
sliderX.on_changed(Xguncelle)
sliderY.on_changed(Yguncelle)
sliderZ.on_changed(Zguncelle)
ani = animation.FuncAnimation(figure, animate, frames=100, interval=100)
plt.show()
which gives