app.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. print(resp)
  23. for r in range(len(msg)):
  24. echo = echo + chr(resp[3+r])
  25. print("Echo:"+echo)
  26. return len(resp)
  27. def cmd_set_param(self, sensor, param, value):
  28. print("SET param:", PARAM_NAME[param], "[", param, "]=>", value)
  29. int_to_four_bytes = struct.Struct('<I').pack
  30. y1, y2, y3, y4 = int_to_four_bytes(value & 0xFFFFFFFF)
  31. resp = self.serial_port.request(self.module, sensor, (SET+CMD_PARAM), [param, y1, y2, y3, y4])
  32. def cmd_get_param_module(self, param):
  33. print("GET module param:", PARAM_NAME[param], "[", param, "]")
  34. resp = self.serial_port.request(self.module, 0, (CMD_PARAM),[param])
  35. def cmd_get_param(self, sensor, param):
  36. print("GET param:", PARAM_NAME[param], "[", param, "]")
  37. resp = self.serial_port.request(self.module, sensor, (CMD_PARAM),[param])
  38. val = None
  39. if len(resp) > 2:
  40. val = resp[4] + resp[5]*256 + resp[6]*256*256 + resp[7] * 256*256*256
  41. return val
  42. def cmd_get_params(self, sensor):
  43. print("GET params:")
  44. resp = self.serial_port.request(self.module, sensor, (CMD_PARAM),[])
  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_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. '''
  70. sensor_index:
  71. 1 - ACC
  72. 2 - GYRO
  73. '''
  74. resp = self.serial_port.request(self.module, sensor_index, CMD_DATA,[])
  75. #print(resp)
  76. x = 0
  77. y = 0
  78. z = 0
  79. if len(resp)>6:
  80. x = resp[5]*256 + resp[4]
  81. y = resp[7]*256 + resp[6]
  82. z = resp[9]*256 + resp[8]
  83. if x > 32768:
  84. x = x - 65535
  85. if y > 32768:
  86. y = y - 65535
  87. if z > 32768:
  88. z = z - 65535
  89. return [x,y,z]
  90. def cmd_sensor_get_data_FSR(self, cnt):
  91. resp = self.serial_port.request(self.module, 0, CMD_DATA,[])
  92. print(resp)
  93. if len(resp)>1:
  94. for i in range(cnt):
  95. sen = resp[3+3*i]
  96. h = resp[5+3*i] * 256 + resp[4+3*i]
  97. print("FSR",sen,":\t", h/4096.0*3.3)
  98. def cmd_module_info(self, param):
  99. resp = self.serial_port.request(self.module, 0, CMD_INFO,[param])
  100. if param == 0xE3:
  101. data=0
  102. for i in range(4):
  103. data=data+resp[i+3]*pow(256,i)
  104. return hex(data)
  105. if param == 0xE6:
  106. data=0
  107. for i in range(8):
  108. data=data+resp[i+3]*pow(256,7-i)
  109. return hex(data)
  110. l = len(resp)
  111. data=""
  112. for i in range(l-4):
  113. data=data+chr(resp[i+3])
  114. return data
  115. def cmd_module_start(self):
  116. print("MODULE START")
  117. self.serial_port.requestBroadcast(CMD_START,[])
  118. def cmd_module_stop(self):
  119. print("MODULE STOP")
  120. self.serial_port.requestBroadcast(CMD_STOP,[])
  121. def cmd_reset(self):
  122. print("MODULE RESET")
  123. resp = self.serial_port.request(self.module, 0, (SET+CMD_RESET),[], long_answer=0.3)
  124. def cmd_store(self, sensor):
  125. print("MODULE STORE PARAM")
  126. resp = self.serial_port.request(self.module, sensor, (SET+CMD_STORE),[], long_answer=0.1)
  127. if __name__ == "__main__":
  128. app = AppTest(0x05, 0x0)
  129. #app.cmd_version()
  130. #app.cmd_version()
  131. # app.cmd_module_stop()
  132. if app.cmd_echo([97,98,99,100]) == 0:
  133. sys.exit()
  134. #app.cmd_reset()
  135. store_param = False
  136. if store_param:
  137. app.cmd_set_param(1, PARAM_SAMPLERATE, 10)
  138. app.cmd_set_param(1, PARAM_RANGE, 1)
  139. app.cmd_set_param(1, PARAM_FILTER, 2)
  140. app.cmd_store(1)
  141. sys.exit()
  142. for s in range(1):
  143. sr=app.cmd_get_param(s+1,PARAM_SAMPLERATE)
  144. r=app.cmd_get_param(s+1,PARAM_RANGE)
  145. lpf=app.cmd_get_param(s+1,PARAM_FILTER)
  146. ee=app.cmd_get_param(s+1,PARAM_GAIN)
  147. print(sr,r,lpf,ee)
  148. # sys.exit()
  149. #time.sleep(0.5)
  150. #app.cmd_module_stop()
  151. app.cmd_module_start()
  152. time.sleep(0.5)
  153. #app.cmd_module_stop()
  154. #sys.exit()
  155. pocet = app.cmd_sensor_cnt()
  156. #sys.exit()
  157. print("pocet senzorov=", pocet)
  158. #app.cmd_sensor_get_data_FSR(pocet)
  159. for i in range(50):
  160. acc = app.cmd_sensor_get_data_IMU(1)
  161. gyr = app.cmd_sensor_get_data_IMU(2)
  162. print(acc)
  163. print(gyr)
  164. app.cmd_module_stop()
  165. sys.exit()
  166. for i in range(pocet):
  167. d=app.cmd_sensor_get_data(i+1)
  168. print("Data", i+1, ":\t", d)
  169. time.sleep(1)
  170. app.cmd_module_stop()
  171. print("Sensor type")
  172. for i in range(pocet):
  173. print(app.cmd_sensor_type(i+1))
  174. print("Sensors type")
  175. print(app.cmd_sensors_type(pocet))
  176. par = app.cmd_sensor_get_params(1)
  177. print(par)
  178. r=app.cmd_module_info(0xE1)
  179. print('NAME:' , r)
  180. r=app.cmd_module_info(0xE2)
  181. print('TYPE', r)
  182. r=app.cmd_module_info(0xE4)
  183. print('FW', r)
  184. r=app.cmd_module_info(0xE5)
  185. print('HW', r)
  186. r=app.cmd_module_info(0xE3)
  187. print("UUID", r)
  188. r=app.cmd_module_info(0xE6)
  189. print("MEM ID", r)
  190. app.finish()