app.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from comm import *
  2. import sys
  3. import time
  4. from struct import *
  5. class AppTest:
  6. def __init__(self, adr_module, adr_sensor):
  7. self.serial_port = SerialComm('/dev/ttyUSB0')
  8. self.module = adr_module
  9. self.sensor = adr_sensor
  10. def finish(self):
  11. self.serial_port.close()
  12. def cmd_version(self):
  13. resp = self.serial_port.request(self.module, 0, CMD_VERSION,[])
  14. version = chr(resp[3])+chr(resp[4])+chr(resp[5])
  15. print("Version: "+version)
  16. def cmd_echo(self, msg):
  17. resp = self.serial_port.request(self.module, 0, CMD_ECHO,msg)
  18. echo = ""
  19. if len(resp) == 0:
  20. print("No ECHO (0-size resp)")
  21. return 0
  22. for r in range(len(msg)):
  23. echo = echo + chr(resp[3+r])
  24. print("Echo:"+echo)
  25. return len(resp)
  26. def cmd_set_param(self, sensor, param, value):
  27. print("SET param:", PARAM_NAME[param], "[", param, "]=>", value)
  28. int_to_four_bytes = struct.Struct('<I').pack
  29. y1, y2, y3, y4 = int_to_four_bytes(value & 0xFFFFFFFF)
  30. resp = self.serial_port.request(self.module, sensor, (SET+CMD_PARAM), [param, y1, y2, y3, y4])
  31. def cmd_get_param_module(self, param):
  32. print("GET module param:", PARAM_NAME[param], "[", param, "]")
  33. resp = self.serial_port.request(self.module, 0, (CMD_PARAM),[param])
  34. def cmd_get_param(self, sensor, param):
  35. print("GET param:", PARAM_NAME[param], "[", param, "]")
  36. resp = self.serial_port.request(self.module, sensor, (CMD_PARAM),[param])
  37. val = None
  38. if len(resp) > 2:
  39. val = resp[4] + resp[5]*256 + resp[6]*256*256 + resp[7] * 256*256*256
  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. def cmd_sensor_cnt(self):
  45. print("SENSOR CNT")
  46. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_CNT,[])
  47. print(resp)
  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_params(self, index):
  59. resp = self.serial_port.request(self.module, index, CMD_PARAM,[])
  60. return resp
  61. def cmd_sensor_get_data(self, index):
  62. resp = self.serial_port.request(self.module, index, CMD_DATA,[])
  63. if len(resp)>1:
  64. h = resp[5] * 256 + resp[4]
  65. # print(hex(h))
  66. return h/4096*3.3
  67. return 0
  68. def cmd_sensor_get_data_IMU(self, sensor_index):
  69. resp = self.serial_port.request(self.module, sensor_index, CMD_DATA,[])
  70. #print(resp)
  71. x = 0
  72. y = 0
  73. z = 0
  74. if len(resp)>6:
  75. x = resp[5]*256 + resp[4]
  76. y = resp[7]*256 + resp[6]
  77. z = resp[9]*256 + resp[8]
  78. if x > 32768:
  79. x = x - 65535
  80. if y > 32768:
  81. y = y - 65535
  82. if z > 32768:
  83. z = z - 65535
  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_module_start(self):
  111. print("MODULE START")
  112. self.serial_port.requestBroadcast(CMD_START,[])
  113. def cmd_module_stop(self):
  114. print("MODULE STOP")
  115. self.serial_port.requestBroadcast(CMD_STOP,[])
  116. if __name__ == "__main__":
  117. app = AppTest(0x05, 0x0)
  118. #app.cmd_version()
  119. #app.cmd_version()
  120. # app.cmd_module_stop()
  121. if app.cmd_echo([97,98,99,100]) == 0:
  122. sys.exit()
  123. app.cmd_set_param(1, PARAM_SAMPLERATE, 10)
  124. app.cmd_set_param(1, PARAM_RANGE, 1)
  125. app.cmd_set_param(1, PARAM_FILTER, 2)
  126. for s in range(1):
  127. sr=app.cmd_get_param(s+1,PARAM_SAMPLERATE)
  128. r=app.cmd_get_param(s+1,PARAM_RANGE)
  129. lpf=app.cmd_get_param(s+1,PARAM_FILTER)
  130. ee=app.cmd_get_param(s+1,PARAM_GAIN)
  131. print(sr,r,lpf,ee)
  132. sys.exit()
  133. #time.sleep(0.5)
  134. #app.cmd_module_stop()
  135. time.sleep(0.5)
  136. app.cmd_module_start()
  137. #app.cmd_module_stop()
  138. #sys.exit()
  139. pocet = app.cmd_sensor_cnt()
  140. #sys.exit()
  141. print("pocet senzorov=", pocet)
  142. #app.cmd_sensor_get_data_FSR(pocet)
  143. for i in range(6):
  144. acc = app.cmd_sensor_get_data_IMU(1)
  145. gyr = app.cmd_sensor_get_data_IMU(2)
  146. print(acc)
  147. print(gyr)
  148. sys.exit()
  149. for i in range(pocet):
  150. d=app.cmd_sensor_get_data(i+1)
  151. print("Data", i+1, ":\t", d)
  152. time.sleep(1)
  153. app.cmd_module_stop()
  154. print("Sensor type")
  155. for i in range(pocet):
  156. print(app.cmd_sensor_type(i+1))
  157. print("Sensors type")
  158. print(app.cmd_sensors_type(pocet))
  159. par = app.cmd_sensor_get_params(1)
  160. print(par)
  161. r=app.cmd_module_info(0xE1)
  162. print('NAME:' , r)
  163. r=app.cmd_module_info(0xE2)
  164. print('TYPE', r)
  165. r=app.cmd_module_info(0xE4)
  166. print('FW', r)
  167. r=app.cmd_module_info(0xE5)
  168. print('HW', r)
  169. r=app.cmd_module_info(0xE3)
  170. print("UUID", r)
  171. r=app.cmd_module_info(0xE6)
  172. print("MEM ID", r)
  173. app.finish()