Skip to content

Commit

Permalink
Support multiple torcs processes
Browse files Browse the repository at this point in the history
  • Loading branch information
muupan committed Aug 30, 2016
1 parent 92278d8 commit 34585e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
30 changes: 19 additions & 11 deletions autostart.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#!/bin/bash
xte 'key Return'
xte 'usleep 100000'
xte 'key Return'
xte 'usleep 100000'
xte 'key Up'
xte 'usleep 100000'
xte 'key Up'
xte 'usleep 100000'
xte 'key Return'
xte 'usleep 100000'
xte 'key Return'

set -eu

This comment has been minimized.

Copy link
@bryan078

bryan078 Oct 13, 2016

-xte 'key Return'
-xte 'usleep 100000'
-xte 'key Return'
-xte 'usleep 100000'
-xte 'key Up'
-xte 'usleep 100000'
-xte 'key Up'
-xte 'usleep 100000'
-xte 'key Return'
-xte 'usleep 100000'
-xte 'key Return'


This comment has been minimized.

Copy link
@bryan078

bryan078 Oct 13, 2016

+window=xdotool search --name $1 | head -n 1
+
+xdotool key --window $window Return
+sleep 0.1
+xdotool key --window $window Return
+sleep 0.1
+xdotool key --window $window Up
+sleep 0.1
+xdotool key --window $window Up
+sleep 0.1
+xdotool key --window $window Return
+sleep 0.1
+xdotool key --window $window Return
+# Uncomment for using vision as input
+# sleep 0.1
+# xdotool key --window $window F2

window=`xdotool search --name $1 | head -n 1`

xdotool key --window $window Return
sleep 0.1
xdotool key --window $window Return
sleep 0.1
xdotool key --window $window Up
sleep 0.1
xdotool key --window $window Up
sleep 0.1
xdotool key --window $window Return
sleep 0.1
xdotool key --window $window Return
# Uncomment for using vision as input
# sleep 0.1
# xdotool key --window $window F2
42 changes: 24 additions & 18 deletions gym_torcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import copy
import collections as col
import os
import subprocess
import time
import signal


class TorcsEnv:
Expand All @@ -17,29 +19,38 @@ class TorcsEnv:

initial_reset = True

def start_torcs_process(self):
if self.torcs_proc is not None:
os.killpg(os.getpgid(self.torcs_proc.pid), signal.SIGKILL)
time.sleep(0.5)
self.torcs_proc = None
window_title = str(self.port)
command = 'torcs -nofuel -nodamage -nolaptime -title {} -p {}'.format(window_title, self.port)
if self.vision is True:
command += ' -vision'
self.torcs_proc = subprocess.Popen([command], shell=True, preexec_fn=os.setsid)
time.sleep(0.5)
os.system('sh autostart.sh {}'.format(window_title))
time.sleep(0.5)

def __init__(self, vision=False, throttle=False, gear_change=False):
def __init__(self, vision=False, throttle=False, gear_change=False, port=3101):
#print("Init")
self.vision = vision
self.throttle = throttle
self.gear_change = gear_change
self.port = port
self.torcs_proc = None

self.initial_run = True

##print("launch torcs")
os.system('pkill torcs')
time.sleep(0.5)
if self.vision is True:
os.system('torcs -nofuel -nodamage -nolaptime -vision &')
else:
os.system('torcs -nofuel -nodamage -nolaptime &')
time.sleep(0.5)
os.system('sh autostart.sh')
self.start_torcs_process()
time.sleep(0.5)

"""
# Modify here if you use multiple tracks in the environment
self.client = snakeoil3.Client(p=3101, vision=self.vision) # Open new UDP in vtorcs
self.client = snakeoil3.Client(p=3101) # Open new UDP in vtorcs
self.client.MAX_STEPS = np.inf
client = self.client
Expand Down Expand Up @@ -180,7 +191,7 @@ def reset(self, relaunch=False):
print("### TORCS is RELAUNCHED ###")

# Modify here if you use multiple tracks in the environment
self.client = snakeoil3.Client(p=3101, vision=self.vision) # Open new UDP in vtorcs
self.client = snakeoil3.Client(self.start_torcs_process, p=self.port) # Open new UDP in vtorcs
self.client.MAX_STEPS = np.inf

client = self.client
Expand All @@ -195,21 +206,16 @@ def reset(self, relaunch=False):
return self.get_obs()

def end(self):
os.system('pkill torcs')
os.killpg(os.getpgid(self.torcs_proc.pid), signal.SIGKILL)

def get_obs(self):
return self.observation

def reset_torcs(self):
#print("relaunch torcs")
os.system('pkill torcs')
time.sleep(0.5)
if self.vision is True:
os.system('torcs -nofuel -nodamage -nolaptime -vision &')
else:
os.system('torcs -nofuel -nodamage -nolaptime &')
self.torcs_proc.terminate()
time.sleep(0.5)
os.system('sh autostart.sh')
self.start_torcs_process()
time.sleep(0.5)

def agent_to_torcs(self, u):
Expand Down
15 changes: 3 additions & 12 deletions snakeoil3_gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def bargraph(x,mn,mx,w,c='X'):
return '[%s]' % (nnc+npc+ppc+pnc)

class Client():
def __init__(self,H=None,p=None,i=None,e=None,t=None,s=None,d=None,vision=False):
def __init__(self,relaunch_torcs,H=None,p=None,i=None,e=None,t=None,s=None,d=None,vision=False):
# If you don't like the option defaults, change them here.
self.vision = vision

Expand All @@ -128,7 +128,7 @@ def __init__(self,H=None,p=None,i=None,e=None,t=None,s=None,d=None,vision=False)
self.stage= 3 # 0=Warm-up, 1=Qualifying 2=Race, 3=unknown <Default=3>
self.debug= False
self.maxSteps= 100000 # 50steps/second
self.parse_the_command_line()
self.relaunch_torcs = relaunch_torcs
if H: self.host= H
if p: self.port= p
if i: self.sid= i
Expand Down Expand Up @@ -171,16 +171,7 @@ def setup_connection(self):
print("Waiting for server on %d............" % self.port)
print("Count Down : " + str(n_fail))
if n_fail < 0:
print("relaunch torcs")
os.system('pkill torcs')
time.sleep(1.0)
if self.vision is False:
os.system('torcs -nofuel -nodamage -nolaptime &')
else:
os.system('torcs -nofuel -nodamage -nolaptime -vision &')

time.sleep(1.0)
os.system('sh autostart.sh')
self.relaunch_torcs()
n_fail = 5
n_fail -= 1

Expand Down

0 comments on commit 34585e6

Please sign in to comment.