forked from huiyi1990/rlTORCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Torcs.py
136 lines (104 loc) · 3.83 KB
/
Torcs.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from enum import Enum
import numpy as np
from ctypes import *
import scipy.misc
import matplotlib.pyplot as plt
lib = cdll.LoadLibrary('./libPythonCtrl.so')
lib.pyctrl_getRGBImage.restype = POINTER((c_uint8) * 3 * 640 * 480)
lib.pyctrl_getSpeed.restype = c_double
lib.pyctrl_getPos.restype = c_double
lib.pyctrl_getDist.restype = c_double
lib.pyctrl_getFrontCarDist.restype = c_double
lib.pyctrl_getRadius.restype = c_double
lib.pyctrl_getAngle.restype = c_double
im_count = 0
class Options(Enum):
server = 'server'
game_config = 'game_config'
use_RGB = 'use_RGB'
mkey = 'mkey'
auto_back = 'auto_back'
class Torcs(object):
def __init__(self, opts):
# [self.obj]; memorizing PythonCtrl.cpp object address to be sent as an arg to .so lib function calls.
self.obj = lib.pyctrl_new()
self.mkey = opts[Options.mkey] if Options.mkey in opts else 98
self.damage = 0
self.distance = -9999
self.distance_gap = 0
self.frontNum = -1
self.observation_gray = None
self.observation_RGB = None
self.server = opts[Options.server] if Options.server in opts else False
self.isStarted = False
self.game_config = opts[Options.game_config] if Options.game_config in opts else 'quickrace_discrete_single.xml'
self.auto_back = opts[Options.auto_back] if Options.auto_back in opts else False
self.use_RGB = opts[Options.use_RGB] if Options.use_RGB in opts else False
self.wrapperPid = -1
self.nan_count = 0
self.stuck_count = 0
self.end_step = 10000
def start(self):
lib.pyctrl_setUp(self.obj)
lib.pyctrl_initializeMem(self.obj)
lib.pyctrl_newGame(self.obj)
def connect(self, accel, brake, steer):
# accel_1, brake_0, steer_0
lib.pyctrl_setAccelCmd(self.obj, c_double(accel))
lib.pyctrl_setBrakeCmd(self.obj, c_double(brake))
lib.pyctrl_setSteerCmd(self.obj, c_double(steer))
lib.pyctrl_setWritten(self.obj, 0)
count = 0
while lib.pyctrl_getWritten(self.obj) != 1:
lib.pyctrl_sleep(self.obj, 1)
count += 1
if count == 20000:
print('failed to connect to torcs!')
# blank_image = np.zeros((480, 640, 3), np.uint8)
print("POSITION: ", lib.pyctrl_getPos(self.obj))
rgb_image = lib.pyctrl_getRGBImage(self.obj)
rgb_image_matrix = np.array(rgb_image.contents)
img = np.flipud(rgb_image_matrix)
# also flipping image, but more expensive
# rgbImageMatrix = rgbImageMatrix[::-1]
# converting to gray image
grayImg = Torcs.rgb2gray(img)
# plotting image
# plt.imshow(grayImg, cmap=plt.get_cmap('gray'))
global im_count
im_count = im_count + 1
img_file_name = "/home/ibrahim/Pictures/py_rlTORCS_imgs/outfile%d.png" % im_count
scipy.misc.imsave(img_file_name, grayImg)
def step(self, accel, brake, steer):
self.connect(accel, brake, steer)
terminal = lib.pyctrl_getIsEnd(self.obj)
if terminal == 1:
return True
else:
if terminal == 0:
return False
@staticmethod
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
def main():
options = {
Options.server: False,
Options.game_config: 'quickrace_discrete_single_ushite-city.xml',
Options.mkey: 817
}
cr = Torcs(options)
cr.start()
cr.connect(0, 0, 0)
terminal = False
totalSteps = 200
episodes = 100
stepsCounter = 0
while stepsCounter < totalSteps:
while not terminal:
steer_val = np.tanh(np.random.randn(1))
terminal = cr.step(1, 0, steer_val)
stepsCounter += 1
if terminal:
cr.start()
if __name__ == "__main__":
main()