-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tutorial_11_Teleop_GUI.py
69 lines (55 loc) · 1.93 KB
/
Tutorial_11_Teleop_GUI.py
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
#Bizbot Technology
#Tutorial 11
#Teleop Bveeta Mini with GUI Button
import rospy
from geometry_msgs.msg import Twist
import tkinter as tk
# Create a ROS publisher for sending velocity commands to the robot
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
rospy.init_node('gui_control_node')
# Create a Twist message for velocity commands
twist = Twist()
# Create a Tkinter GUI window
window = tk.Tk()
window.title("Robot Control")
# Define a function for sending velocity commands to the robot
def send_velocity_command(linear_vel, angular_vel):
twist.linear.x = linear_vel
twist.angular.z = angular_vel
pub.publish(twist)
print("Sent command: linear=%s, angular=%s" % (linear_vel, angular_vel))
# Define the GUI button functions
def move_forward():
send_velocity_command(0.5, 0)
def move_backward():
send_velocity_command(-0.5, 0)
def turn_left():
send_velocity_command(0, 0.5)
def turn_right():
send_velocity_command(0, -0.5)
# Create the GUI buttons
btn_forward = tk.Button(window, text="Forward", command=move_forward)
btn_backward = tk.Button(window, text="Backward", command=move_backward)
btn_left = tk.Button(window, text="Left", command=turn_left)
btn_right = tk.Button(window, text="Right", command=turn_right)
# Add the buttons to the GUI window
btn_forward.pack()
btn_backward.pack()
btn_left.pack()
btn_right.pack()
# Define a function to handle keyboard interrupt (Ctrl+C)
def on_keyboard_interrupt(event):
print("Keyboard interrupt, stopping program...")
window.quit()
# Bind the keyboard interrupt function to Ctrl+C
window.bind("<Control-c>", on_keyboard_interrupt)
# Start the Tkinter main loop
try:
window.mainloop()
except KeyboardInterrupt:
print("Keyboard interrupt, stopping program...")
finally:
# Stop the robot before exiting the program
send_velocity_command(0, 0)
rospy.sleep(1) # wait for the robot to stop
rospy.signal_shutdown("GUI control program stopped")