keyboard_user_commands.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 
5 import Tkinter as tk
7 
8 from actionlib import *
9 
10 
11 USER_COMMANDS = {
12  's': "start",
13  'r': "reset",
14  'f': "fold",
15  'q': "quit"
16 }
17 
18 def on_key_press(event):
19  # Clean text and reinsert the header
20  text.delete('1.0', 'end')
21  text.insert('end', text_header)
22 
23  try:
24  rospy.loginfo("Key pressed: %s -> command: %s", event.char, USER_COMMANDS[event.char])
25 
26  # Show the valid command about to execute
27  text.insert('end', "\n\n'%s' command selected" % USER_COMMANDS[event.char])
28 
29  # Creates a goal to send to the action server.
30  goal = UserCommandGoal(command=USER_COMMANDS[event.char])
31 
32  # Sends the goal to the action server.
33  client.send_goal(goal)
34 
35  # TODO: Maybe I should call this in a separated thread... just for information
36  # # Waits for the server to finish performing the action.
37  # client.wait_for_result()
38  #
39  # # Prints out the result of executing the action
40  # rospy.loginfo("SM result: %s", client.get_result()) # probably empty... not implemented
41  except KeyError:
42  # Invalid command; nothing to do
43  text.insert('end', "\n\n'%s' is not a valid command" % event.char)
44 
45 
46 
47 if __name__ == '__main__':
48  try:
49  rospy.init_node('keyboard_user_commands')
50 
51  # Creates the SimpleActionClient, passing the type of the action
52  # (UserCommandAction) to the constructor.
53  client = SimpleActionClient('user_commands_action_server', UserCommandAction)
54 
55  # Waits until the action server has started up and started listening for goals.
56  client.wait_for_server()
57 
58  # Create a simple GUI to show available commands and capture keyboard inputs
59  window = tk.Tk()
60  window.geometry(rospy.get_param('~window_geometry', '300x200'))
61  window.wm_title(rospy.get_param('~window_caption', 'User input'))
62 
63  text = tk.Text(window, background='black', foreground='white',
64  font=(rospy.get_param('~text_font', 'Comic Sans MS'), rospy.get_param('~font_size', 12)))
65  text_header = rospy.get_param('~shown_text', 'Press command key')
66  text.insert('end', text_header)
67  text.pack()
68 
69  window.bind('<KeyPress>', on_key_press)
70  window.mainloop()
71  except rospy.ROSInterruptException:
72  print "program interrupted before completion"


turtlebot_arm_object_manipulation
Author(s): Jorge Santos
autogenerated on Fri Feb 7 2020 03:56:40