nbus_slave.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from abc import abstractmethod
  2. from comm import *
  3. class NbusSlave():
  4. slave_params = {1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}, 10: {}, 11: {}, 12: {}, 13: {},
  5. 14: {}, 15: {}, 16: {}}
  6. def __init__(self, module, comm_port):
  7. self.serial_port = comm_port
  8. self.module = module
  9. @abstractmethod
  10. def evaluate_param_value(self, sensor, param, val):
  11. pass
  12. def cmd_echo(self, msg):
  13. resp = self.serial_port.request(self.module, 0, CMD_ECHO, msg)
  14. echo = ""
  15. if len(resp) == 0:
  16. print("No ECHO (0-size resp)")
  17. return 0
  18. # print(resp)
  19. for r in range(len(msg)):
  20. echo = echo + chr(resp[3 + r])
  21. # print("Echo:" + echo)
  22. return len(resp)
  23. def cmd_set_param(self, sensor, param, value):
  24. print("SET param:", PARAM_NAME[param], "[", param, "]=>", value)
  25. int_to_four_bytes = struct.Struct('<I').pack
  26. y1, y2, y3, y4 = int_to_four_bytes(value & 0xFFFFFFFF)
  27. resp = self.serial_port.request(self.module, sensor, (SET + CMD_PARAM), [param, y1, y2, y3, y4])
  28. self.evaluate_param_value(sensor, param, value)
  29. return resp
  30. def cmd_get_param_module(self, param):
  31. print("GET module param:", PARAM_NAME[param], "[", param, "]")
  32. resp = self.serial_port.request(self.module, 0, CMD_PARAM, [param])
  33. def cmd_get_param(self, sensor, param):
  34. # print("GET param:", PARAM_NAME[param], "[", param, "]")
  35. resp = self.serial_port.request(self.module, sensor, CMD_PARAM, [param])
  36. val = None
  37. if len(resp) > 2:
  38. val = resp[4] + resp[5] * 256 + resp[6] * 256 * 256 + resp[7] * 256 * 256 * 256
  39. return self.evaluate_param_value(sensor, param, val)
  40. return val
  41. def cmd_get_params(self, sensor):
  42. print("GET params:")
  43. resp = self.serial_port.request(self.module, sensor, (CMD_PARAM), [])
  44. return resp
  45. def cmd_sensor_cnt(self):
  46. print("SENSOR CNT")
  47. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_CNT, [])
  48. return resp[3]
  49. def cmd_sensor_type(self, index):
  50. resp = self.serial_port.request(self.module, index, CMD_SENSOR_TYPE, [])
  51. return resp[3]
  52. def cmd_sensors_type(self, pocet):
  53. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_TYPE, [])
  54. typy = [];
  55. for i in range(pocet):
  56. typy.append([resp[3 + 2 * i], resp[4 + 2 * i]])
  57. return typy
  58. def cmd_sensor_get_data(self, index):
  59. resp = self.serial_port.request(self.module, index, CMD_DATA, [])
  60. if len(resp) > 1:
  61. h = resp[5] * 256 + resp[4]
  62. # print(hex(h))
  63. return h / 4096 * 3.3
  64. return 0
  65. def get_real_value_imu(self, sensor, value):
  66. # value = value/
  67. # if self.slave_params[sensor] == 1:
  68. pass
  69. def _cmd_sensor_get_data(self, sensor_index):
  70. '''
  71. sensor_index:
  72. 1 - ACC
  73. 2 - GYRO
  74. '''
  75. resp = self.serial_port.request(self.module, sensor_index, CMD_DATA, [])
  76. # print(resp)
  77. x = 0
  78. y = 0
  79. z = 0
  80. if len(resp) > 6:
  81. x = resp[5] * 256 + resp[4]
  82. y = resp[7] * 256 + resp[6]
  83. z = resp[9] * 256 + resp[8]
  84. return [x, y, z]
  85. def cmd_sensor_get_data_FSR(self, cnt):
  86. resp = self.serial_port.request(self.module, 0, CMD_DATA, [])
  87. print(resp)
  88. if len(resp) > 1:
  89. for i in range(cnt):
  90. sen = resp[3 + 3 * i]
  91. h = resp[5 + 3 * i] * 256 + resp[4 + 3 * i]
  92. print("FSR", sen, ":\t", h / 4096.0 * 3.3)
  93. def cmd_module_info(self, param):
  94. resp = self.serial_port.request(self.module, 0, CMD_INFO, [param])
  95. if param == 0xE3:
  96. data = 0
  97. for i in range(4):
  98. data = data + resp[i + 3] * pow(256, i)
  99. return hex(data)
  100. if param == 0xE6:
  101. data = 0
  102. for i in range(8):
  103. data = data + resp[i + 3] * pow(256, 7 - i)
  104. return hex(data)
  105. l = len(resp)
  106. data = ""
  107. for i in range(l - 4):
  108. data = data + chr(resp[i + 3])
  109. return data
  110. def cmd_reset(self):
  111. print("MODULE RESET")
  112. resp = self.serial_port.request(self.module, 0, (SET + CMD_RESET), [], long_answer=0.3)
  113. def cmd_store(self, sensor):
  114. print("MODULE STORE PARAM")
  115. resp = self.serial_port.request(self.module, sensor, (SET + CMD_STORE), [], long_answer=0.1)
  116. def cmd_calibrate(self, sensor):
  117. print(f"MODULE [{self.module}/{sensor}] RUN CALIBRATION")
  118. resp = self.serial_port.request(self.module, sensor, (SET + CMD_CALIBRATE), [], long_answer=3 if sensor == 0 else 1.5)
  119. if len(resp) == 1:
  120. return 0
  121. return resp[3]