app.py 4.9 KB

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