-
Notifications
You must be signed in to change notification settings - Fork 31
/
key2joy.py
75 lines (65 loc) · 2.21 KB
/
key2joy.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
70
71
72
73
74
75
import os
import pygame
from pygame.locals import *
import time
import sys
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy
def main():
# initialize pygame to get keyboard event
pygame.init()
window_size = Rect(0, 0, 438, 344)
screen = pygame.display.set_mode(window_size.size)
path = os.path.dirname(os.path.abspath(__file__))
print(path)
img = pygame.image.load(path+"/figs/key2joy.png")
# initialize ros publisher
twist_pub = rospy.Publisher('keyboard/twist', Twist, queue_size=10)
joy_pub = rospy.Publisher('/joy', Joy, queue_size=10)
rospy.init_node('key2joy')
rate = rospy.Rate(50)
# init joy message
joy_ = Joy()
joy_.header.frame_id = 'world'
for i in range(8):
joy_.axes.append(0.0)
for i in range(11):
joy_.buttons.append(0)
while not rospy.is_shutdown():
rate.sleep()
screen.blit(img, (1,1))
pygame.display.flip()
for event in pygame.event.get():
# ---------------------- key dowm message ----------------------
if event.type == KEYDOWN:
if event.key == pygame.K_w:
# print 'up'
joy_.axes[1] = 1.0
if event.key == pygame.K_s:
# print 'down'
joy_.axes[1] = -1.0
if event.key == pygame.K_a:
# print 'left'
joy_.axes[0] = -1.0
if event.key == pygame.K_d:
# print 'right'
joy_.axes[0] = 1.0
joy_pub.publish(joy_)
# when keyup, reset wind velocity
elif event.type == pygame.KEYUP:
# wind direction
if event.key == pygame.K_w:
joy_.axes[1] = 0.0
if event.key == pygame.K_s:
joy_.axes[1] = -0.0
if event.key == pygame.K_a:
joy_.axes[0] = 0.0
if event.key == pygame.K_d:
joy_.axes[0] = -0.0
joy_pub.publish(joy_)
if __name__ == '__main__':
try:
main()
except rospy.ROSInterruptException:
pass