simulation_radar.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import csv
  11. sys.path.append("/home/vangogh/software/FuzzScene/code/")
  12. import Constants
  13. mutation_name = sys.argv[1]
  14. def timetask(times):
  15. time.sleep(times)
  16. print(time.localtime())
  17. def works():
  18. command = ['/home/vangogh/software/CARLA_0.9.13/CarlaUE4.sh', '-carla-streaming-port=0', '-carla-rpc-port=3000']
  19. sim_subprocessA = subprocess.Popen(command)
  20. print("*********************************")
  21. print("Carla Server Started")
  22. # proc_record.append(p1)
  23. time.sleep(10)
  24. commandB = ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/scenario_runner.py',
  25. '--output', '--openscenario', Constants.RADAR_SEED_POOL + mutation_name, '--sync', '--reloadWorld']
  26. sim_subprocessB = subprocess.Popen(commandB)
  27. # p2.start()
  28. print("*********************************")
  29. print("OpenScenario Simutation Started")
  30. time.sleep(10)
  31. # proc_record.append(p2)
  32. commandC = ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/manual_control.py', '-a', '--name', mutation_name, '--type', 'radar']
  33. sim_subprocessC = subprocess.Popen(commandC)
  34. print("*********************************")
  35. print("Collecting Data")
  36. timeout = 85
  37. start_time = time.time()
  38. print("TIMEOUT EVENT BEGIN AT:" + str(datetime.now()))
  39. while True:
  40. returncode = sim_subprocessC.poll()
  41. if returncode is None:
  42. elapsed_time = time.time() - start_time
  43. if (elapsed_time > timeout):
  44. print("!!! scenario runner TIMEOUT!!!")
  45. break
  46. time.sleep(5)
  47. else:
  48. print("!!! scenario runner FINISHED!!!")
  49. break;
  50. # try:
  51. # # 从collecting Data到140帧时间为75秒左右,放宽限制至100秒的超时
  52. # print("TIMEOUT EVENT BEGIN AT:" + str(datetime.now()))
  53. # sim_subprocessB.wait(timeout=85)
  54. # except subprocess.TimeoutExpired:
  55. # print("!!! scenario runner TIMEOUT!!!" + str(datetime.now()))
  56. def clear_processes():
  57. # try:
  58. # output = subprocess.check_output(['pgrep', '-f', 'CarlaUE4-Linux-Shipping'])
  59. # if output:
  60. # carla_id = int(output.strip())
  61. # if carla_id:
  62. # os.kill(carla_id, signal.SIGTERM)
  63. # print("*******************")
  64. # print("KILL CarlaUE4-Linux-Shipping")
  65. # except subprocess.CalledProcessError:
  66. # pass
  67. # try:
  68. # output = subprocess.check_output(['pgrep', '-f', 'CarlaUE4.sh'])
  69. # if output:
  70. # carla_id = int(output.strip())
  71. # if carla_id:
  72. # os.kill(carla_id, signal.SIGTERM)
  73. # print("*******************")
  74. # print("KILL CarlaUE4.sh")
  75. # except subprocess.CalledProcessError:
  76. # pass
  77. os.system("pkill -9 Carla")
  78. for proc in psutil.process_iter(['pid', 'cmdline']):
  79. if proc.info['cmdline'] == ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/scenario_runner.py',
  80. '--output', '--openscenario', Constants.RADAR_SEED_POOL + mutation_name, '--sync', '--reloadWorld']:
  81. proc.kill()
  82. print("*******************")
  83. print("KILL scenario_runner")
  84. if proc.info['cmdline'] == ['python3', '/home/vangogh/software/FuzzScene/code/scenario_runner-0.9.13/manual_control.py', '-a', '--name', mutation_name, '--type', 'radar']:
  85. proc.kill()
  86. print("*******************")
  87. print("KILL manual_control")
  88. print("*******************")
  89. print("CLEAR output")
  90. def clear_output():
  91. path = Constants.CARLA_RADAR_PNG_OUTPUT_PATH
  92. shutil.rmtree(path) # 清空out
  93. os.mkdir(path)
  94. seed_pool_path = Constants.RADAR_SEED_POOL
  95. if os.path.exists(seed_pool_path):
  96. shutil.rmtree(seed_pool_path) # 清空out
  97. shutil.copytree(Constants.RADAR_SEED_POOL_BAK, seed_pool_path)
  98. label_path = Constants.CARLA_RADAR_LABEL_OUTPUT_PATH
  99. if os.path.exists(label_path + 'label_test.csv'): # 清空label_test_null.csv
  100. os.remove(label_path + 'label_test.csv')
  101. shutil.copy(label_path + 'label_test_null.csv', label_path + 'label_test.csv')
  102. def clear_add_output(n):
  103. rows = []
  104. with open(Constants.CARLA_RADAR_LABEL_OUTPUT_PATH + 'label_test.csv', 'r') as csvfile:
  105. reader = csv.reader(csvfile)
  106. for row in reader:
  107. rows.append(row)
  108. last_n_rows = rows[n:]
  109. second_column = [row[1] for row in last_n_rows]
  110. for value in second_column:
  111. os.remove(Constants.CARLA_RADAR_PNG_OUTPUT_PATH + value)
  112. # 删除最后n行
  113. rows = rows[0 : n]
  114. # 将修改后的数据写回CSV文件
  115. with open(Constants.CARLA_RADAR_LABEL_OUTPUT_PATH + 'label_test.csv', 'w', newline='') as csvfile:
  116. writer = csv.writer(csvfile)
  117. writer.writerows(rows)
  118. # tensorflow 2.5 keras 2.4.3
  119. if __name__ == '__main__':
  120. # clear_output()
  121. begin_row = 1
  122. with open(Constants.CARLA_RADAR_LABEL_OUTPUT_PATH + 'label_test.csv', 'r') as f:
  123. begin_row = len(f.readlines())
  124. for i in range(5):
  125. works()
  126. clear_processes()
  127. with open(Constants.CARLA_RADAR_LABEL_OUTPUT_PATH + 'label_test.csv', 'r') as f:
  128. rows = len(f.readlines())
  129. add_row = rows - begin_row
  130. print("\nsimulation_carla generated ### " + str(add_row) + "_images")
  131. if (add_row >= 120):
  132. print("\nsimulation_carla SUCCESS finished!\n")
  133. break
  134. else:
  135. clear_add_output(begin_row)
  136. if i == 4:
  137. print("\nsimulation_carla FAILED 5 times. EXIT WITH -1\n")
  138. else:
  139. print("\nsimulation_carla FAILED No." + str(i + 1) + " times, retrying...\n")