-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathviewer.py
More file actions
executable file
·48 lines (44 loc) · 1.46 KB
/
viewer.py
File metadata and controls
executable file
·48 lines (44 loc) · 1.46 KB
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
#!/usr/bin/env python
# license removed for brevity
import rospy
import numpy as np
#check world debug
import sys
from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage
from geometry_msgs.msg import Twist
import pygame
from pygame.locals import *
import cv2
import random
video_size = 700, 500
velocity_publisher = rospy.Publisher('space_invader/move', String, queue_size=10)
def key_action():
vel_msg = Twist()
#while i(var):
#x=
#return x
return (random.choice(["1","2","5","0","2","5","2","5","0","2","5","2","5","0","2","5","2","5","0","2","5","2","5","0","2","5","5","0","2","5"]))
def callback(ros_data):
np_arr = np.fromstring(ros_data.data, np.uint8)
image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
screen = pygame.display.set_mode(video_size)
surf = pygame.surfarray.make_surface(image_np)
screen.blit(surf, (0, 0))
pygame.display.update()
vel_msg = key_action()
velocity_publisher.publish(vel_msg)
def main(args):
'''Initializes and cleanup ros node'''
rospy.init_node('agent', anonymous=True)
subscriber = rospy.Subscriber('space_invader/image_raw', CompressedImage, callback)
try:
screen = pygame.display.set_mode(video_size)
vel_msg = key_action()
velocity_publisher.publish(vel_msg)
rospy.spin()
except KeyboardInterrupt:
print("Shutting down ROS Gym Image Viewer module")
cv2.destroyAllWindows()
if __name__ == '__main__':
main(sys.argv)