simulation_carla.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import sys
  2. import os
  3. from multiprocessing import Process
  4. import time
  5. import signal
  6. import subprocess
  7. import psutil
  8. from datetime import datetime
  9. import shutil
  10. sys.path.append("/home/vangogh/software/FuzzScene/code/")
  11. import Constants
  12. mutation_name=sys.argv[1]
  13. print(mutation_name)
  14. # -RenderOffScreen ./CarlaUE4.sh -quality-level=low -fps=15 -windowed -Resx=600 -Resy=480
  15. def work1():
  16. os.system("timeout 150 ~/software/CARLA_0.9.13/CarlaUE4.sh -carla-streaming-port=0 -carla-rpc-port=3000")
  17. def work2():
  18. os.system("timeout 100 python3 ~/software/FuzzScene/code/scenario_runner-0.9.13/scenario_runner.py --output --openscenario ~/software/FuzzScene/code/seed_pool/"+mutation_name+" --sync --reloadWorld")
  19. def work3():
  20. os.system("timeout 100 python3 ~/software/FuzzScene/code/scenario_runner-0.9.13/manual_control.py -a")
  21. def work4():
  22. os.system("timeout 85 pkill -9 python && pkill -9 Carla")
  23. def work5():
  24. os.system("timeout 50 python3 ~/software/FuzzScene/code/scenario_runner-0.9.13/steering-curve.py --type ori_gif --name" + mutation_name)
  25. def timetask(times):
  26. time.sleep(times)
  27. print (time.localtime())
  28. # '-RenderOffScreen',
  29. def works():
  30. command = ['/home/vangogh/software/CARLA_0.9.13/CarlaUE4.sh', '-carla-streaming-port=0', '-carla-rpc-port=3000']
  31. sim_subprocessA = subprocess.Popen(command)
  32. print("*********************************")
  33. print("Carla Server Started")
  34. # proc_record.append(p1)
  35. time.sleep(10)
  36. commandB = ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/scenario_runner.py',
  37. '--output', '--openscenario', '/home/vangogh/software/FuzzScene/code/seed_pool/'+ mutation_name, '--sync', '--reloadWorld']
  38. sim_subprocessB = subprocess.Popen(commandB)
  39. # p2.start()
  40. print("*********************************")
  41. print("OpenScenario Simutation Started")
  42. time.sleep(10)
  43. # proc_record.append(p2)
  44. commandC = ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/manual_control.py', '-a', '--name', '', '--type', 'normal']
  45. sim_subprocessC = subprocess.Popen(commandC)
  46. print("*********************************")
  47. print("Collecting Data")
  48. timeout = 85
  49. start_time = time.time()
  50. print("TIMEOUT EVENT BEGIN AT:" + str(datetime.now()))
  51. while True:
  52. returncode = sim_subprocessC.poll()
  53. if returncode is None:
  54. elapsed_time = time.time() - start_time
  55. if (elapsed_time > timeout):
  56. print("!!! scenario runner TIMEOUT!!!")
  57. break
  58. time.sleep(5)
  59. else:
  60. print("!!! scenario runner FINISHED!!!")
  61. break
  62. # try:
  63. # # 从collecting Data到140帧时间为75秒左右,放宽限制至100秒的超时
  64. # print("TIMEOUT EVENT BEGIN AT:" + str(datetime.now()))
  65. # sim_subprocessB.wait(timeout=85)
  66. # except subprocess.TimeoutExpired:
  67. # print("!!! scenario runner TIMEOUT!!!" + str(datetime.now()))
  68. def clear_processes():
  69. # try:
  70. # output = subprocess.check_output(['pgrep', '-f', 'CarlaUE4-Linux-Shipping'])
  71. # if output:
  72. # carla_id = int(output.strip())
  73. # if carla_id:
  74. # os.kill(carla_id, signal.SIGTERM)
  75. # print("*******************")
  76. # print("KILL CarlaUE4-Linux-Shipping")
  77. # except subprocess.CalledProcessError:
  78. # pass
  79. # try:
  80. # output = subprocess.check_output(['pgrep', '-f', 'CarlaUE4.sh'])
  81. # if output:
  82. # carla_id = int(output.strip())
  83. # if carla_id:
  84. # os.kill(carla_id, signal.SIGTERM)
  85. # print("*******************")
  86. # print("KILL CarlaUE4.sh")
  87. # except subprocess.CalledProcessError:
  88. # pass
  89. os.system("pkill -9 Carla")
  90. print("*******************")
  91. print("KILL Carla")
  92. for proc in psutil.process_iter(['pid', 'cmdline']):
  93. if proc.info['cmdline'] == ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/scenario_runner.py',
  94. '--output', '--openscenario', '/home/vangogh/software/FuzzScene/code/seed_pool/'+ mutation_name, '--sync', '--reloadWorld']:
  95. proc.kill()
  96. print("*******************")
  97. print("KILL scenario_runner")
  98. if proc.info['cmdline'] == ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/manual_control.py', '-a', '--name', '', '--type', 'normal']:
  99. proc.kill()
  100. print("*******************")
  101. print("KILL manual_control")
  102. print("*******************")
  103. print("CLEAR output in simulation_carla")
  104. def clear_output():
  105. path = '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/_out/'
  106. shutil.rmtree(path + 'center') # 清空out
  107. os.mkdir(path + 'center')
  108. if os.path.exists(path + 'label_test.csv'): # 清空label_test_null.csv
  109. os.remove(path + 'label_test.csv')
  110. shutil.copy(path + 'label_test_null.csv', path + 'label_test.csv')
  111. # tensorflow 2.5 keras 2.4.3
  112. if __name__ == '__main__':
  113. for i in range(5):
  114. clear_output()
  115. works()
  116. clear_processes()
  117. with open('/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/_out/label_test.csv', 'r') as f:
  118. rows = len(f.readlines()) - 1
  119. print("\nsimulation_carla generated ### " + str(rows) + "_images")
  120. if (rows >= 100):
  121. print("\nsimulation_carla SUCCESS finished!\n")
  122. break
  123. else:
  124. if i == 4:
  125. print("\nsimulation_carla FAILED 5 times. EXIT WITH -1\n")
  126. else:
  127. print("\nsimulation_carla FAILED No." + str(i + 1) + " times, retrying...\n")