auswertung.py 3.6 KB

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