app.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import sys
  2. from nbus_slave import *
  3. from nbus_system import *
  4. import matplotlib.pyplot as plt
  5. from imu_slave import *
  6. import numpy as np
  7. def show_data(data):
  8. fig, ax = plt.subplots()
  9. for d in data:
  10. ax.plot(data[d])
  11. ax.grid()
  12. plt.show()
  13. if __name__ == "__main__":
  14. nbus = NbusSystem(True)
  15. s1_adr = 5
  16. s2_adr = 6
  17. adrs = [s1_adr, s2_adr]
  18. adrs = [200]
  19. foundSlaves = {}
  20. for adr in adrs:
  21. slave1 = nbus.create_slave(ImuSlave, adr)
  22. foundSlaves[adr] = True
  23. # r = nbus.get_slave(adr).cmd_calibrate(0)
  24. # print(r)
  25. # slave2 = nbus.create_slave(ImuSlave, s2_adr)
  26. # sys.exit(0)
  27. startCheck = True
  28. for adr in adrs:
  29. if nbus.get_slave(adr).cmd_echo([97, 98, 99, 100]) == 0:
  30. foundSlaves[adr] = False
  31. for s in foundSlaves:
  32. startCheck = startCheck and foundSlaves[s]
  33. print("Module presence:", foundSlaves)
  34. if not startCheck:
  35. sys.exit()
  36. # app.cmd_reset()
  37. store_param = False
  38. if store_param:
  39. nbus.get_slave(s1_adr).cmd_set_param(1, PARAM_SAMPLERATE, 1)
  40. nbus.get_slave(s1_adr).cmd_set_param(1, PARAM_RANGE, 0)
  41. nbus.get_slave(s1_adr).cmd_set_param(1, PARAM_FILTER, 2)
  42. nbus.get_slave(s1_adr).cmd_store(1)
  43. sys.exit()
  44. # !!!! slave1.cmd_set_param(1, PARAM_SAMPLERATE, 10)
  45. # !!!! slave1.cmd_set_param(2, PARAM_SAMPLERATE, 10)
  46. for adr in adrs:
  47. for s in range(1):
  48. sr = nbus.get_slave(adr).cmd_get_param(s + 1, PARAM_SAMPLERATE)
  49. r = nbus.get_slave(adr).cmd_get_param(s + 1, PARAM_RANGE)
  50. lpf = nbus.get_slave(adr).cmd_get_param(s + 1, PARAM_FILTER)
  51. ee = nbus.get_slave(adr).cmd_get_param(s + 1, PARAM_GAIN)
  52. print(sr, r, lpf, ee)
  53. # sr = nbus.get_slave(adr).cmd_get_param(1, PARAM_SAMPLERATE)
  54. # nbus.get_slave(adr).cmd_set_param(1, PARAM_RANGE, 1)
  55. # print(nbus.get_slave(adr).slave_params)
  56. pocet = {}
  57. for adr in adrs:
  58. pocet[adr] = nbus.get_slave(adr).cmd_sensor_cnt()
  59. # sys.exit()
  60. print("pocet senzorov=", pocet)
  61. nbus.cmd_start()
  62. time.sleep(0.1)
  63. #nbus.cmd_stop()
  64. #sys.exit()
  65. data_acc = {}
  66. for adr in adrs:
  67. data_acc[adr] = []
  68. print(data_acc)
  69. for i in range(64):
  70. for adr in adrs:
  71. acc = nbus.get_slave(adr).cmd_sensor_get_data_IMU(1)
  72. # gyr = nbus.get_slave(adr).cmd_sensor_get_data_IMU(2)
  73. data_acc[adr].append(acc)
  74. # print(acc)
  75. print(".", end=" ")
  76. nbus.cmd_stop()
  77. show_data(data_acc)
  78. # sys.exit()
  79. for adr in adrs:
  80. for i in range(pocet[adr]):
  81. d = nbus.get_slave(adr).cmd_sensor_get_data(i + 1)
  82. print("Data", i + 1, ":\t", d)
  83. print("Sensor type")
  84. for i in range(pocet[adr]):
  85. print(nbus.get_slave(adr).cmd_sensor_type(i + 1))
  86. print("Sensors type")
  87. print(nbus.get_slave(adr).cmd_sensors_type(pocet[adr]))
  88. par = nbus.get_slave(adr).cmd_get_params(1)
  89. print(par)
  90. r = nbus.get_slave(adr).cmd_module_info(0xE1)
  91. print('NAME:', r)
  92. r = nbus.get_slave(adr).cmd_module_info(0xE2)
  93. print('TYPE', r)
  94. r = nbus.get_slave(adr).cmd_module_info(0xE4)
  95. print('FW', r)
  96. r = nbus.get_slave(adr).cmd_module_info(0xE5)
  97. print('HW', r)
  98. r = nbus.get_slave(adr).cmd_module_info(0xE3)
  99. print("UUID", r)
  100. r = nbus.get_slave(adr).cmd_module_info(0xE6)
  101. print("MEM ID", r)
  102. nbus.finish()