binToCsv.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import numpy as np
  4. import pprint
  5. import sys, os, time, datetime
  6. sys.path.insert(0,'..')
  7. from functions import *
  8. import csv
  9. import argparse
  10. pp = pprint.PrettyPrinter(indent=4)
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument("input_file")
  13. parser.add_argument("output_file")
  14. args = parser.parse_args()
  15. #os.system('cls')
  16. size = os.stat(args.input_file).st_size
  17. print("file name: {} size: {} bytes".format(args.input_file, size))
  18. f = open(args.input_file, "rb")
  19. logFile = open(args.output_file, "w", newline='')
  20. dataset = """
  21. /*-------RAW-----------*/
  22. uint32_t pressure_cis_raw; // pack 1 x 4
  23. uint32_t temperature_cis_raw; // + 1 x 4
  24. uint32_t pressure_spi_raw;
  25. uint32_t temperature_i2c_raw;
  26. uint32_t temperature_spi_raw;
  27. uint32_t adc_temperature_raw;
  28. int32_t temperature_mpu_raw;
  29. uint32_t adc_temperature_internal_raw;
  30. uint32_t adc_voltage_raw;
  31. uint32_t adc_current_raw;
  32. struct vector_Uint accel_raw; // pack 3 x 4
  33. struct vector_Uint gyro_raw; // + 3 x 4
  34. struct vector_Uint mag_raw; // pack 3 x 4
  35. // + 1 x 4
  36. int32_t temperature_mag_raw;
  37. /*-----Computed--------*/
  38. double pressure_cis; // no packing needed 1 x 8
  39. double temperature_cis;
  40. double pressure_spi;
  41. double temperature_i2c;
  42. double temperature_spi;
  43. double adc_temperature;
  44. double adc_temperature_internal;
  45. double adc_voltage;
  46. double adc_current;
  47. double temperature_mpu;
  48. struct vector accel; // no packing needed 3 x 8
  49. struct vector gyro;
  50. struct vector rot;
  51. struct vector mag;
  52. double temperature_mag;
  53. struct vector global_accel;
  54. struct vector pos;
  55. /*-----------gps------------*/
  56. int32_t longitude;
  57. int32_t latitude;
  58. int32_t altitude;
  59. uint32_t time;
  60. uint32_t hdop;
  61. int32_t mag_head; //heading
  62. uint32_t count;
  63. uint32_t crc;
  64. """
  65. master = PktParser(dataset)
  66. slave = PktParser(dataset, master.end)
  67. fusion = PktParser(dataset, slave.end)
  68. length = fusion.end + 2
  69. print("expected length of row: {}".format(fusion.end))
  70. total = size/fusion.end
  71. print("expected row count: {}".format(total))
  72. print()
  73. print(master.keys())
  74. time.sleep(1)
  75. count = 0
  76. buf = b''
  77. obj = None
  78. cs = None
  79. def check():
  80. #pp.pprint(x)
  81. if buf[fusion.end:fusion.end+2] != b"\r\n":
  82. print("[{:8}/{:8}] error missing EOL!".format(count, total))
  83. return
  84. m = master.parse(buf)
  85. s = slave.parse(buf)
  86. f = fusion.parse(buf)
  87. crc_m = sum(buf[:master.end-4])
  88. crc_s = sum(buf[master.end:slave.end-4])
  89. crc_f = sum(buf[slave.end:fusion.end-4])
  90. if crc_m != m["crc"]:
  91. print("[{:8}/{:8}] crc error in master! diff = {:12}".format(count, total, crc_m - m["crc"]))
  92. return
  93. #if crc_s != s["crc"]:
  94. #print("[{:8}/{:8}] crc error in slave!".format(count, total))
  95. # return
  96. #if crc_f != f["crc"]:
  97. #print("[{:8}/{:8}] crc error in fusion!".format(count, total))
  98. # return
  99. data = m
  100. # for prop in slave.keys():
  101. # if s:
  102. # data["slave_" + prop] = s[prop]
  103. # if f:
  104. # data["fusion_" + prop] = f[prop]
  105. data["slave_count"] = s["count"]
  106. data["slave_time"] = s["time"]
  107. return data
  108. try:
  109. byte = b'A'
  110. while byte != b"":
  111. # Do stuff with byte.
  112. if len(buf) >= length:
  113. obj = check()
  114. if obj:
  115. if cs == None:
  116. cs = csv.DictWriter(logFile, obj.keys())
  117. cs.writeheader()
  118. cs.writerow(obj)
  119. if count % 10000 == 1:
  120. print("[{:8}/{:8}] ...{:.2%}".format(count, total, count/total))
  121. count += 1
  122. pos = buf.find(b"\r\n")
  123. if pos == -1:
  124. print("[{:8}/{:8}] can't find next line!".format(count, total))
  125. #if not obj and pos >= 0:
  126. #print("[{:8}] skipping {:4} chars".format(count, pos+2))
  127. if pos != fusion.end and not obj:
  128. print("[{:8}/{:8}] expected {:4} bytes but got {:4}".format(count, total, fusion.end+2, pos+2))
  129. buf = buf[pos+2:]
  130. else:
  131. byte = f.read(length)
  132. buf += byte
  133. finally:
  134. f.close()
  135. #fw.close()