auswertung.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import socket
  2. import time
  3. import pprint
  4. import argparse
  5. import sys, os
  6. sys.path.insert(0,'..')
  7. from functions import *
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. pp = pprint.PrettyPrinter(indent=4)
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument("input_file")
  13. args = parser.parse_args()
  14. size = os.stat(args.input_file).st_size
  15. print("file name: {} size: {} bytes".format(args.input_file, size))
  16. f = open(args.input_file, "rb")
  17. #fw = open("data.csv", "w")
  18. buf = b''
  19. length = 1024
  20. t = []
  21. result = {
  22. "mag_x": [],
  23. "temperature_mpu": [],
  24. #"adc_temperature": [],
  25. }
  26. axis = [0, 1, 1]
  27. count = 0
  28. colors = ["tab:red", "tab:blue", "tab:green"]
  29. dataset = """
  30. /*-------RAW-----------*/
  31. uint32_t pressure_cis_raw; // pack 1 x 4
  32. uint32_t temperature_cis_raw; // + 1 x 4
  33. uint32_t pressure_i2c_raw;
  34. uint32_t temperature_i2c_raw;
  35. uint32_t temperature_spi_raw;
  36. uint32_t adc_temperature_raw;
  37. int32_t temperature_mpu_raw;
  38. uint32_t adc_temperature_internal_raw;
  39. uint32_t adc_voltage_raw;
  40. uint32_t adc_current_raw;
  41. struct vector_Uint accel_raw; // pack 3 x 4
  42. struct vector_Uint gyro_raw; // + 3 x 4
  43. /*-----Computed--------*/
  44. double pressure_cis; // no packing needed 1 x 8
  45. double temperature_cis;
  46. double pressure_spi;
  47. double temperature_i2c;
  48. double temperature_spi;
  49. double adc_temperature;
  50. double adc_temperature_internal;
  51. double adc_voltage;
  52. double adc_current;
  53. double temperature_mpu;
  54. struct vector accel; // no packing needed 3 x 8
  55. struct vector gyro;
  56. struct vector rot;
  57. /*-----------gps------------*/
  58. int32_t longitude;
  59. int32_t longitude_mod2;
  60. int32_t latitude;
  61. int32_t latitude_mod2;
  62. int32_t altitude;
  63. int32_t altitude_mod2;
  64. uint32_t time;
  65. uint32_t time_mod2;
  66. uint32_t hdop;
  67. uint32_t hdop_mod2;
  68. /*--------------------------*/
  69. /*---------magnet-----------*/
  70. struct vector_Uint mag_raw; // pack 3 x 4
  71. int32_t temperature_mag_raw; // + 1 x 4
  72. struct vector mag;
  73. double temperature_mag;
  74. uint32_t count;
  75. uint32_t crc;
  76. """
  77. master = PktParser(dataset)
  78. slave = PktParser(dataset, master.end)
  79. fusion = PktParser(dataset, slave.end)
  80. def check():
  81. global count
  82. #if buf[length-2] != ord("\r") or buf[length-1] != ord("\n"):
  83. if buf.find(b"\r\n") == -1:
  84. print("[{:8}] error missing EOL! bufLen={}".format(count, len(buf)))
  85. return
  86. obj = master.parse(buf)
  87. obj2 = slave.parse(buf)
  88. #if count == 0:
  89. #fw.write(",".join(obj.keys()) + "\n")
  90. if obj["count"] != 0: #obj2["count"]:
  91. print("[{:8}] count error! {} != {}".format(count, obj["count"], obj2["count"]))
  92. return
  93. if obj["crc"] != 1094795585: #obj2["crc"]:
  94. print("[{:8}] crc error! crc={}".format(count, obj["crc"]))
  95. return
  96. #if count != obj["count"] - 1:
  97. # print("[{0:8}] missing data between row {0} and {1}!".format(count, obj["count"]))
  98. #count = obj["count"]
  99. count += 1
  100. t.append(count/10)
  101. for key in result:
  102. result[key].append(obj[key])
  103. if count % 10000 == 0:
  104. print("[{:8}] Reading...".format(count))
  105. return obj
  106. try:
  107. byte = b'A'
  108. while byte != b"":
  109. # Do stuff with byte.
  110. if len(buf) >= length:
  111. obj = check()
  112. #if obj:
  113. #fw.write(",".join(str(x) for x in obj.values()) + "\n")
  114. pos = buf.find(b"\r\n")
  115. if pos == -1:
  116. print("[{:8}] can't find next line! bufLen={}".format(count, len(buf)))
  117. buf = buf[pos+2:]
  118. else:
  119. byte = f.read(length)
  120. buf += byte
  121. #if obj != None:
  122. # pp.pprint(obj)
  123. finally:
  124. f.close()
  125. #fw.close()
  126. fig, ax1 = plt.subplots()
  127. ax1.set_xlabel('time (s)')
  128. n = 0
  129. ax2 = ax1.twinx()
  130. axes = [ax1, ax2]
  131. for key in result:
  132. axes[axis[n]].plot(t, result[key], label=key, color=colors[n])
  133. axes[axis[n]].set_ylabel(key, color=colors[n])
  134. axes[axis[n]].tick_params(axis='y', labelcolor=colors[n])
  135. n += 1
  136. #plt.legend()
  137. fig.tight_layout()
  138. plt.show()