+ LED control

This commit is contained in:
elias 2025-04-03 10:22:47 +02:00
parent 01f9595a80
commit 27be5ce1bf

View File

@ -1,12 +1,12 @@
import tkinter as tk, robomasterpy as rm import tkinter as tk, robomasterpy as rm, datetime
from tkinter import ttk from tkinter import ttk
import datetime
class RobomasterUI: class RobomasterUI:
def __init__(self, master, cmd): def __init__(self, master, cmd):
self.master = master self.master = master
master.title("Robomaster Control") master.title("Robomaster Control")
self.cmd = cmd self.cmd = cmd
self._command_executor(cmd.led_control, comp="all", effect="solid", r=255, g=0, b=0)
master.configure(bg='#121212') master.configure(bg='#121212')
@ -19,7 +19,16 @@ class RobomasterUI:
style.map('TButton', style.map('TButton',
background=[('active', '#555555')], background=[('active', '#555555')],
foreground=[('active', 'white')]) foreground=[('active', 'white')])
style.configure('TCombobox',
background='#333333',
foreground='white',
fieldbackground='#333333',
selectbackground='#555555',
selectforeground='white',
insertcolor='white')
style.map('TCombobox',
fieldbackground=[('readonly', '#333333'), ('!readonly', '#333333')],
background=[('readonly', '#333333'), ('!readonly', '#333333')])
self.output_text = tk.Text(master, height=10, width=50, state=tk.DISABLED, fg='white', bg='#121212', insertbackground='white', selectbackground='#555555', selectforeground='white') self.output_text = tk.Text(master, height=10, width=50, state=tk.DISABLED, fg='white', bg='#121212', insertbackground='white', selectbackground='#555555', selectforeground='white')
self.output_text.grid(row=6, column=0, columnspan=4, padx=10, pady=10) self.output_text.grid(row=6, column=0, columnspan=4, padx=10, pady=10)
@ -54,8 +63,40 @@ class RobomasterUI:
ttk.Button(gripper_frame, text="Close Gripper", command=self.close_gripper).grid(row=2, column=0, columnspan=2, pady=5, sticky="we") ttk.Button(gripper_frame, text="Close Gripper", command=self.close_gripper).grid(row=2, column=0, columnspan=2, pady=5, sticky="we")
ttk.Button(gripper_frame, text="Gripper Status", command=self.get_gripper_status).grid(row=3, column=0, columnspan=2, pady=5, sticky="we") ttk.Button(gripper_frame, text="Gripper Status", command=self.get_gripper_status).grid(row=3, column=0, columnspan=2, pady=5, sticky="we")
chassis_frame = ttk.LabelFrame(master, text="Chassis Control") led_frame = ttk.LabelFrame(master, text="LED Control")
chassis_frame.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="n") led_frame.grid(row=0, column=2, padx=10, pady=10, sticky="n")
led_comp_values = ["all", "top_all", "top_right", "top_left", "bottom_all", "bottom_front", "bottom_back", "bottom_left", "bottom_right"]
tk.Label(led_frame, text="Comp:", fg='white', bg='#121212').grid(row=0, column=0, padx=5, pady=5, sticky="e")
self.comp_combo = ttk.Combobox(led_frame, values=led_comp_values, width=10, style='DarkCombobox.TCombobox')
self.comp_combo.set(led_comp_values[0])
self.comp_combo.grid(row=0, column=1, padx=5, pady=5)
led_effect_values = ["solid", "off", "pulse", "blink", "scrolling"]
tk.Label(led_frame, text="Effect:", fg='white', bg='#121212').grid(row=1, column=0, padx=5, pady=5, sticky="e")
self.effect_combo = ttk.Combobox(led_frame, values=led_effect_values, width=10, style='DarkCombobox.TCombobox')
self.effect_combo.set(led_effect_values[0])
self.effect_combo.grid(row=1, column=1, padx=5, pady=5)
tk.Label(led_frame, text="R:", fg='white', bg='#121212').grid(row=2, column=0, padx=5, pady=5, sticky="e")
self.r_entry = tk.Entry(led_frame, width=5, fg='white', bg='#333333', insertbackground='white')
self.r_entry.insert(0, "255")
self.r_entry.grid(row=2, column=1, padx=5, pady=5)
tk.Label(led_frame, text="G:", fg='white', bg='#121212').grid(row=3, column=0, padx=5, pady=5, sticky="e")
self.g_entry = tk.Entry(led_frame, width=5, fg='white', bg='#333333', insertbackground='white')
self.g_entry.insert(0, "255")
self.g_entry.grid(row=3, column=1, padx=5, pady=5)
tk.Label(led_frame, text="B:", fg='white', bg='#121212').grid(row=4, column=0, padx=5, pady=5, sticky="e")
self.b_entry = tk.Entry(led_frame, width=5, fg='white', bg='#333333', insertbackground='white')
self.b_entry.insert(0, "255")
self.b_entry.grid(row=4, column=1, padx=5, pady=5)
ttk.Button(led_frame, text="Set LED", command=self.led_control_ui).grid(row=5, column=0, columnspan=2, pady=5, sticky="we")
chassis_frame = ttk.LabelFrame(master, text="Chassis Control (WASDQE)")
chassis_frame.grid(row=1, column=0, columnspan=3, padx=10, pady=10, sticky="n")
tk.Label(chassis_frame, text="XY Speed (m/s):", fg='white', bg='#121212').grid(row=0, column=0, padx=5, pady=5, sticky="e") tk.Label(chassis_frame, text="XY Speed (m/s):", fg='white', bg='#121212').grid(row=0, column=0, padx=5, pady=5, sticky="e")
self.chassis_x_speed_entry = tk.Entry(chassis_frame, width=5, fg='white', bg='#333333', insertbackground='white') self.chassis_x_speed_entry = tk.Entry(chassis_frame, width=5, fg='white', bg='#333333', insertbackground='white')
@ -85,6 +126,7 @@ class RobomasterUI:
master.bind("<Button-1>", self.deselect_textbox) master.bind("<Button-1>", self.deselect_textbox)
master.columnconfigure(0, weight=1) master.columnconfigure(0, weight=1)
master.columnconfigure(1, weight=1) master.columnconfigure(1, weight=1)
master.columnconfigure(2, weight=1)
def deselect_textbox(self, event): def deselect_textbox(self, event):
focused_widget = self.master.focus_get() focused_widget = self.master.focus_get()
@ -161,10 +203,26 @@ class RobomasterUI:
except ValueError: except ValueError:
self._display_output("Error: Invalid speed value") self._display_output("Error: Invalid speed value")
def led_control_ui(self):
comp_str = self.comp_combo.get()
effect_str = self.effect_combo.get()
r_str = self.r_entry.get()
g_str = self.g_entry.get()
b_str = self.b_entry.get()
try:
r = int(r_str)
g = int(g_str)
b = int(b_str)
if not all(0 <= color <= 255 for color in [r, g, b]):
return self._display_output("Error: R, G, B values must be between 0 and 255")
except ValueError:
return self._display_output("Error: Invalid R, G, B values")
self._command_executor(self.cmd.led_control, comp=comp_str, effect=effect_str, r=r, g=g, b=b)
def main(): def main():
ip_address = "192.168.2.1" ip_address = "192.168.2.1"
try: try:
cmd = rm.Commander(ip=ip_address) cmd = rm.Commander(ip=ip_address, timeout=5)
print(f"Connected to Robomaster. Version: {cmd.version()}") print(f"Connected to Robomaster. Version: {cmd.version()}")
root = tk.Tk() root = tk.Tk()
root.resizable(False, False) root.resizable(False, False)