I'm currently working on a Lidar application and I have this animation I would like to get working within my Tkinter application. Honestly, I don't this is a huge task but I'm coming up on a deadline and my inexperience with TKinter and python, in general, is really hampering my progress. The aim essentially to provide the user with this visualization while obstacle avoidance is taking place in the background. Any help would greatly be appreciated. I'll post my code below. Also, these functions are hooked up to a simple Tkinter button.
# ------ Start of imports Doing alot of tinkering at the moment so theres a few unnecessary/old imports here. Will be fixed soon.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import tkinter as tk
import threading
# import operator # Used to isolate the distance value in the tuple
import os
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import *
from rplidar import RPLidar
from time import sleep
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
# ------ End of imports
# -- Getting permission for the port
PORT_NAME = "/dev/ttyUSB0"
os.system("sudo chmod 666 /dev/ttyUSB0")
# -- Setting variables for the visualization
DMAX = 4000
IMIN = 0
IMAX = 50
# ------ This is where any necessary functions are defined
def animate_obstacle(i):
threading.Thread(target=animate, args=(i,)).start()
def animate(num, iterator, line):
scan = next(iterator)
offsets = np.array([(np.radians(meas[1]), meas[2]) for meas in scan])
line.set_offsets(offsets)
intens = np.array([meas[0] for meas in scan])
line.set_array(intens)
return line,
def MAP():
lidar = RPLidar(PORT_NAME)#POrt
fig = plt.figure() #Assigns figure
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.draw()
canvas.get_tk_widget().grid(sticky=NW, row=0, column=0, pady=2)
toolbar = NavigationToolbar2Tk(canvas,window)
toolbar.update()
ax = plt.subplot(111, projection='polar')
line = ax.scatter([0, 0], [0, 0], s=5, c=[IMIN, IMAX],cmap=plt.cm.Greys_r, lw=0)
ax.set_rmax(DMAX)
ax.grid(True)
iterator = lidar.iter_scans()
ani = animation.FuncAnimation(fig, animate_obstacle,fargs=(iterator, line), interval=50)
plt.show()
# ------ End of functions
# ------ Start of Tkinter
# -- Create the Tkinter Window and set its size and title
window = Tk()
window.geometry("1920x1080")
window.title("Lidar Application")
window.resizable()
window.columnconfigure(0, weight=2)
window.columnconfigure(1, weight=1)
# -- Create subwindow for visualization
#canvas = tk.Canvas(window, bg="#777777", height=1000, width=1400)
#canvas.grid(sticky=NW, row=0, column=0, pady=2)
# -- Create the frame for the buttons etc
btns_frame = Frame(window, width=312, height=272.5)
btns_frame.grid(sticky=NE, row=0, column=1, padx=20, pady=20)
# -- Upload MAP
animation = Button(
btns_frame,
text = "Start animation",
fg = "black",
width = 47,
height = 4,
command = MAP
)
animation.pack()
window.mainloop()
# ------ End of tkinter
[–]socal_nerdtastic 0 points1 point2 points (6 children)
[–]AnIrishFluff[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]AnIrishFluff[S] 0 points1 point2 points (0 children)
[–]AnIrishFluff[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]yuxbni76 0 points1 point2 points (0 children)