app.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. for r in range(len(msg)):
  20. echo = echo + chr(resp[3+r])
  21. print("Echo:"+echo)
  22. def cmd_set_param(self, sensor, param, value):
  23. print("SET param:", PARAM_NAME[param], "[", param, "]=>", value)
  24. int_to_four_bytes = struct.Struct('<I').pack
  25. y1, y2, y3, y4 = int_to_four_bytes(value & 0xFFFFFFFF)
  26. resp = self.serial_port.request(self.module, sensor, (SET+CMD_PARAM), [param, y1, y2, y3, y4])
  27. def cmd_get_param_module(self, param):
  28. print("GET module param:", PARAM_NAME[param], "[", param, "]")
  29. resp = self.serial_port.request(self.module, 0, (CMD_PARAM),[param])
  30. def cmd_get_param(self, sensor, param):
  31. print("GET param:", PARAM_NAME[param], "[", param, "]")
  32. resp = self.serial_port.request(self.module, sensor, (CMD_PARAM),[param])
  33. val = None
  34. if len(resp) > 2:
  35. val = resp[4] + resp[5]*256 + resp[6]*256*256 + resp[7] * 256*256*256
  36. return val
  37. def cmd_get_params(self, sensor):
  38. print("GET params:")
  39. resp = self.serial_port.request(self.module, sensor, (CMD_PARAM),[])
  40. def cmd_sensor_cnt(self):
  41. print("SENSOR CNT")
  42. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_CNT,[])
  43. print(resp)
  44. return resp[3]
  45. def cmd_sensor_type(self, index):
  46. resp = self.serial_port.request(self.module, index, CMD_SENSOR_TYPE,[])
  47. return resp[3]
  48. def cmd_sensors_type(self, pocet):
  49. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_TYPE,[])
  50. typy = [];
  51. for i in range(pocet):
  52. typy.append([resp[3+2*i],resp[4+2*i]])
  53. return typy
  54. def cmd_sensor_get_params(self, index):
  55. resp = self.serial_port.request(self.module, index, CMD_PARAM,[])
  56. return resp
  57. def cmd_sensor_get_data(self, index):
  58. resp = self.serial_port.request(self.module, index, CMD_DATA,[])
  59. if len(resp)>1:
  60. h = resp[5] * 256 + resp[4]
  61. # print(hex(h))
  62. return h/4096*3.3
  63. return 0
  64. def cmd_sensor_get_data_IMU(self, sensor_index):
  65. resp = self.serial_port.request(self.module, sensor_index, CMD_DATA,[])
  66. #print(resp)
  67. x = 0
  68. y = 0
  69. z = 0
  70. if len(resp)>6:
  71. x = resp[5]*256 + resp[4]
  72. y = resp[7]*256 + resp[6]
  73. z = resp[9]*256 + resp[8]
  74. if x > 32768:
  75. x = x - 65535
  76. if y > 32768:
  77. y = y - 65535
  78. if z > 32768:
  79. z = z - 65535
  80. return [x,y,z]
  81. def cmd_sensor_get_data_FSR(self, cnt):
  82. resp = self.serial_port.request(self.module, 0, CMD_DATA,[])
  83. print(resp)
  84. if len(resp)>1:
  85. for i in range(cnt):
  86. sen = resp[3+3*i]
  87. h = resp[5+3*i] * 256 + resp[4+3*i]
  88. print("FSR",sen,":\t", h/4096.0*3.3)
  89. def cmd_module_info(self, param):
  90. resp = self.serial_port.request(self.module, 0, CMD_INFO,[param])
  91. if param == 0xE3:
  92. data=0
  93. for i in range(4):
  94. data=data+resp[i+3]*pow(256,i)
  95. return hex(data)
  96. if param == 0xE6:
  97. data=0
  98. for i in range(8):
  99. data=data+resp[i+3]*pow(256,7-i)
  100. return hex(data)
  101. l = len(resp)
  102. data=""
  103. for i in range(l-4):
  104. data=data+chr(resp[i+3])
  105. return data
  106. def cmd_module_start(self):
  107. print("MODULE START")
  108. self.serial_port.requestBroadcast(CMD_START,[])
  109. def cmd_module_stop(self):
  110. print("MODULE STOP")
  111. self.serial_port.requestBroadcast(CMD_STOP,[])
  112. if __name__ == "__main__":
  113. app = AppTest(0x05, 0x0)
  114. #app.cmd_version()
  115. #app.cmd_version()
  116. # app.cmd_module_stop()
  117. app.cmd_echo([65])
  118. app.cmd_echo([97,98,99,100])
  119. app.cmd_set_param(1, PARAM_SAMPLERATE, 10)
  120. app.cmd_set_param(1, PARAM_RANGE, 1)
  121. app.cmd_set_param(1, PARAM_FILTER, 2)
  122. for s in range(1):
  123. sr=app.cmd_get_param(s+1,PARAM_SAMPLERATE)
  124. r=app.cmd_get_param(s+1,PARAM_RANGE)
  125. lpf=app.cmd_get_param(s+1,PARAM_FILTER)
  126. ee=app.cmd_get_param(s+1,PARAM_GAIN)
  127. print(sr,r,lpf,ee)
  128. sys.exit()
  129. #time.sleep(0.5)
  130. #app.cmd_module_stop()
  131. time.sleep(0.5)
  132. app.cmd_module_start()
  133. #app.cmd_module_stop()
  134. #sys.exit()
  135. pocet = app.cmd_sensor_cnt()
  136. #sys.exit()
  137. print("pocet senzorov=", pocet)
  138. #app.cmd_sensor_get_data_FSR(pocet)
  139. for i in range(6):
  140. acc = app.cmd_sensor_get_data_IMU(1)
  141. gyr = app.cmd_sensor_get_data_IMU(2)
  142. print(acc)
  143. print(gyr)
  144. sys.exit()
  145. for i in range(pocet):
  146. d=app.cmd_sensor_get_data(i+1)
  147. print("Data", i+1, ":\t", d)
  148. time.sleep(1)
  149. app.cmd_module_stop()
  150. print("Sensor type")
  151. for i in range(pocet):
  152. print(app.cmd_sensor_type(i+1))
  153. print("Sensors type")
  154. print(app.cmd_sensors_type(pocet))
  155. par = app.cmd_sensor_get_params(1)
  156. print(par)
  157. r=app.cmd_module_info(0xE1)
  158. print('NAME:' , r)
  159. r=app.cmd_module_info(0xE2)
  160. print('TYPE', r)
  161. r=app.cmd_module_info(0xE4)
  162. print('FW', r)
  163. r=app.cmd_module_info(0xE5)
  164. print('HW', r)
  165. r=app.cmd_module_info(0xE3)
  166. print("UUID", r)
  167. r=app.cmd_module_info(0xE6)
  168. print("MEM ID", r)
  169. app.finish()