drive_csv.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Dec 25 17:35:56 2023
  5. @author: dell
  6. """
  7. # coding: utf-8
  8. # !/usr/bin/env python2
  9. import os
  10. import rosbag
  11. import csv
  12. import math
  13. import sys
  14. import time
  15. def parsebag(input_dir, output_dir):
  16. dic_object_detection = ['Time', 'Strg_Angle', 'Accel_Pos', 'BrkPel_Pos', 'Real_Speed', 'latitude', 'longitude',
  17. 'angular_velocity_z', 'accel_x']
  18. object_detection_file = open(output_dir + "/" + "drive.csv", 'w')
  19. writer_object_detection = csv.writer(object_detection_file)
  20. writer_object_detection.writerow(dic_object_detection)
  21. frame_max = sys.maxsize
  22. count_data_read = 1
  23. count_cicv_location = 1
  24. with rosbag.Bag(input_dir, 'r') as bag:
  25. latitude = 0
  26. longitude = 0
  27. speed = 0
  28. ActStrWhAng = 0
  29. AccPed1 = 0
  30. BrkPedSts = 0
  31. angular_velocity_z = 0
  32. accel_x = 0
  33. for topic, msg, t in bag.read_messages(topics=['/cicv_location', '/data_read']): # t代表时间
  34. # print(topic)
  35. # print('-------------------------------------------------')
  36. if topic == "/cicv_location": # /pji_gps #/cicv_location
  37. count_cicv_location += 1
  38. latitude = msg.latitude
  39. longitude = msg.longitude
  40. speed = pow(pow(msg.velocity_x, 2) + pow(msg.velocity_y, 2), 0.5) * 3.6
  41. accel_x = msg.accel_x * 9.8
  42. angular_velocity_z = msg.angular_velocity_z
  43. if count_cicv_location % 10 == 0 and latitude != 0 and longitude != 0:
  44. message_location = [str(t)[:-6], ActStrWhAng, AccPed1, BrkPedSts, speed, latitude, longitude,
  45. angular_velocity_z, accel_x]
  46. writer_object_detection.writerow(message_location)
  47. if topic == "/data_read": # 10hz
  48. ActStrWhAng = msg.ActStrWhAng
  49. AccPed1 = msg.AccPed1
  50. BrkPedSts = msg.BrkPedSts
  51. count_data_read += 1
  52. if count_data_read % 10 == 0 and latitude != 0 and longitude != 0:
  53. message_data = [str(t)[:-6], msg.ActStrWhAng, msg.AccPed1, msg.BrkPedSts, speed, latitude,
  54. longitude, angular_velocity_z, accel_x]
  55. writer_object_detection.writerow(message_data)
  56. object_detection_file.close()
  57. # location_file.close()
  58. def parse(input_dir, output_dir):
  59. bagname = input_dir.split('/')[-1].split('.bag')[0]
  60. output_dir = os.path.join(output_dir, bagname)
  61. if not os.path.exists(output_dir):
  62. os.makedirs(output_dir)
  63. parsebag(input_dir, output_dir)
  64. return output_dir