key_read.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 
5 import rospy
6 
7 import geometry_msgs.msg
8 from geometry_msgs.msg import Twist
9 
10 import sys, pygame
11 
12 
13 startMsg = """
14 Hello User! Lookin' good today
15 Reading from Keyboard, Publishing to keys/cmd_vel
16 CTRL-C to quit
17 """
18 
19 screenMsg = """
20 NAVIGATION INSTRUCTIONS
21 
22 W : forward
23 S : back
24 Q : left
25 E : right
26 A : rotate left
27 D : rotate right
28 
29 J : increase speed
30 K : reset speed to default
31 L : lower speed
32 
33 P : quit
34 
35 Enjoy!
36 """
37 
38 screenMsg = screenMsg.split("\n")
39 
40 keyBindings = {
41  'd':(0,0,1), #right
42  'D':(0,0,1),#
43  'a':(0,0,-1),
44  'A':(0,0,-1),
45  's':(0,-1,0),
46  'S':(0,-1,0),
47  'w':(0,1,0),
48  'W':(0,1,0),
49  ## these are for the diagonal + rotational movement
50  # 'q':(0,1,-1),
51  # 'Q':(0,1,-1),
52  # 'e':(0,1,1),
53  # 'E':(0,1,1),
54  ## these are for the horizontal translation
55  'q':(-1,0,0),
56  'Q':(-1,0,0),
57  'e':(1,0,0),
58  'E':(1,0,0),
59 
60  }
61 
62 speedBindings = {
63  'j': 1.1,
64  'k': 1,
65  'l': 0.9,
66  }
67 
69  print ("Shutting Down!")
70 
71 if __name__=="__main__":
72 
73  pub = rospy.Publisher('keys/cmd_vel', Twist, queue_size = 3)
74  rospy.init_node('key_read_node')
75  pygame.init()
76  pygame.font.init()
77 
78  print(startMsg)
79  screen = pygame.display.set_mode((600,600))
80 
81  x = 0
82  y = 0
83  th = 0
84  running = True
85  n = len(screenMsg) + 1
86 
87 
88  r = rospy.Rate(100)
89 
90  ####### PARAMETERS ##########
91  speed = 1
92  turnspeed = 1
93 
94  while (not rospy.is_shutdown() and running):
95  screen.fill((254,128,195))
96  basicfont = pygame.font.SysFont(None, 38)
97  count = 1
98 
99  for word in screenMsg:
100  text = basicfont.render(word, True, (0, 0, 0))
101  textrect = text.get_rect()
102  textrect.centerx = screen.get_rect().centerx
103  textrect.centery = count * screen.get_rect().height / n
104  screen.blit(text, textrect)
105  count += 1
106 
107  pygame.display.flip()
108 
109 
110  for event in pygame.event.get():
111  #print(event)
112  if event.type == pygame.QUIT:
113  pygame.quit()
114  exit()
115  if event.type == pygame.KEYDOWN:
116  #print(event.unicode)
117  if event.key == pygame.K_p: # p to quit
118  pygame.quit()
119  running = False
120  rospy.on_shutdown(shutdownHook)
121  if event.unicode in keyBindings.keys():
122  x = keyBindings[event.unicode][0]
123  y = keyBindings[event.unicode][1]
124  th = keyBindings[event.unicode][2]
125  if (event.unicode in speedBindings.keys()):
126  if event.key == pygame.K_k:
127  speed = 1 # k acts as a reset button for the speed
128  else:
129  speed = speed * speedBindings[event.unicode]
130 
131  if event.type == pygame.KEYUP:
132  x = 0
133  y = 0
134  z = 0
135  th = 0
136 
137  twist = Twist()
138  # important parameters
139  twist.linear.x = x * speed; twist.linear.y = y * speed;
140  twist.angular.z = th*turnspeed;
141  # not important parameters, NO NEED TO CHANGE
142  twist.linear.z = 0;
143  twist.angular.x = 0; twist.angular.y = 0;
144  #print(twist)
145  pub.publish(twist)
146 
147  r.sleep()
148 
149  rospy.on_shutdown(shutdownHook)
150  # rospy.spin()
def shutdownHook()
Definition: key_read.py:68


hebiros_advanced_examples
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:13:22