feat: CommandPublishGate - chi publish mot lenh zero Twist khi tha dieu khien, them icon cua so

Truoc: publish lien tuc kha ca zero Twist theo timer -> de ghi de /cmd_vel cua publisher khac
khi joystick khong dieu khien. CommandPublishGate: lenh khac-zero publish binh thuong theo chu
ky; ve zero thi publish DUNG MOT LAN roi im, chi publish lai khi co lenh khac-zero moi.
UI: dat className "VirtualJoystickTeleop" cho WM_CLASS (truoc dung chung "Tk" voi moi app Tk
khac, bi gom lan trong taskbar/window picker) + ve icon joystick 32x32 khong can file ngoai.
This commit is contained in:
2026-09-22 15:32:00 +07:00
parent 4d0ade7c6d
commit 6c9f77eaae
7 changed files with 98 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ from tkinter import ttk
import rospy
from geometry_msgs.msg import Twist
from virtual_joystick_teleop import TeleopModel
from virtual_joystick_teleop import CommandPublishGate, TeleopModel
class VirtualJoystickApp:
@@ -59,13 +59,19 @@ class VirtualJoystickApp:
)
self._updating_velocity_controls = False
self._limit_editors = {}
self._publish_gate = CommandPublishGate()
self.publisher = rospy.Publisher(self._cmd_vel_topic, Twist, queue_size=1)
rospy.on_shutdown(self._publish_stop)
self.root = tk.Tk()
# className sets X11 WM_CLASS: without it every Tk app shares the generic
# "Tk" class, so window pickers/taskbars group and icon this the same as
# any other Tkinter tool and it gets lost among them.
self.root = tk.Tk(className="VirtualJoystickTeleop")
self.root.title("ROS Virtual Joystick - Điều khiển robot")
self.root.resizable(False, False)
self._icon_image = self._build_icon()
self.root.iconphoto(True, self._icon_image)
self.root.protocol("WM_DELETE_WINDOW", self._request_shutdown)
self.root.bind_all("<KeyPress>", self._on_key_press, add="+")
self.root.bind_all("<KeyRelease>", self._on_key_release, add="+")
@@ -96,6 +102,23 @@ class VirtualJoystickApp:
raise ValueError("{} must be a positive finite number".format(name))
return value
def _build_icon(self, size=32):
"""Draw a small joystick-knob icon for the window/taskbar, no external file needed."""
icon = tk.PhotoImage(width=size, height=size)
bg, ring, knob = "#111827", "#1f2937", "#22c55e"
center = size / 2.0
outer_r = size * 0.46
knob_r = size * 0.22
icon.put(bg, to=(0, 0, size, size))
for y in range(size):
for x in range(size):
dist = math.hypot(x - center, y - center)
if dist <= knob_r:
icon.put(knob, (x, y))
elif dist <= outer_r:
icon.put(ring, (x, y))
return icon
def _configure_style(self):
style = ttk.Style(self.root)
if "clam" in style.theme_names():
@@ -740,17 +763,17 @@ class VirtualJoystickApp:
self._publish_stop()
def _emergency_stop(self):
self._clear_motion("STOP - đã xóa lệnh chuyển động")
self._clear_motion("STOP - đã xóa lệnh chuyển động", force_stop=True)
self._focus_window()
def _clear_motion(self, status):
def _clear_motion(self, status, force_stop=False):
for job in self._release_jobs.values():
self.root.after_cancel(job)
self._release_jobs.clear()
self.model.clear_motion()
self.status_var.set(status)
self._refresh_view()
self._publish_stop()
self._publish_stop(force=force_stop)
def _refresh_view(self):
linear_x, angular_z = self.model.command()
@@ -831,10 +854,15 @@ class VirtualJoystickApp:
return message
def _publish_current(self):
if not rospy.is_shutdown():
self.publisher.publish(self._twist_from_model())
if rospy.is_shutdown():
return
message = self._twist_from_model()
if self._publish_gate.allow_command(message.linear.x, message.angular.z):
self.publisher.publish(message)
def _publish_stop(self):
def _publish_stop(self, force=False):
if not self._publish_gate.allow_stop(force=force):
return
try:
self.publisher.publish(Twist())
except (AttributeError, rospy.ROSException):
@@ -855,7 +883,7 @@ class VirtualJoystickApp:
return
self._closed = True
self.model.clear_motion()
self._publish_stop()
self._publish_stop(force=True)
rospy.signal_shutdown("Virtual joystick window closed")
self.root.destroy()