test_tcp.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. from multiprocessing import Process
  2. # from event_storage import EventStorage
  3. from hard_disk_storage import HardDiskStorage
  4. from memory_storage import MemoryStorage
  5. from sonar_socket2 import Receiver
  6. from sonar_temp_tcp import ClientTemp
  7. from temp_sonar import Receiver1
  8. from sonar_udp import sender
  9. from sonar_command_tcp_once import Client
  10. import socket
  11. import struct
  12. import time
  13. import construct as c
  14. import redis
  15. servo_angle = 'servo1_angle'
  16. MIN_ANGLE = 1
  17. MAX_ANGLE = 358
  18. SYSTEM_MODE = 'once' # once,multiple
  19. hard_disk_db = HardDiskStorage()
  20. servo_data = c.Struct(
  21. header=c.Int16ul, # 固定不便,可能用于区分不同设备
  22. none_1=c.Int8ub, # 固定不便,可能用于区分不同设备
  23. servo1_flag=c.Int8ub,
  24. none_2=c.Int8ub, # 固定不便,可能用于区分不同设备
  25. servo2_flag=c.Int8ub,
  26. depth=c.Bytes(4),
  27. temp=c.Bytes(4),
  28. yaw=c.Bytes(4), # 航向
  29. roll=c.Bytes(4), # 横滚
  30. pitch=c.Bytes(4), # 俯仰
  31. servo1_angle=c.Bytes(4),
  32. servo2_angle=c.Bytes(4),
  33. water_leak1=c.Bytes(4),
  34. water_leak2=c.Bytes(4),
  35. )
  36. def get_program_status():
  37. sql = "SELECT time_end FROM measurement_data ORDER BY id desc limit 1"
  38. res = hard_disk_db.execute_sql(sql, None)
  39. if res:
  40. if res[0]['time_end']:
  41. return "off"
  42. else:
  43. return "on"
  44. else:
  45. return "off"
  46. class MyProcess(Process): # 继承Process类
  47. def __init__(self, name, config):
  48. super(MyProcess, self).__init__()
  49. self.name = name
  50. self.__sock = None
  51. self.__ip = config['ip']
  52. self.__port = config['port']
  53. self.__save_run_time = 0
  54. # self.__converter = converter
  55. # self.__storager = MemoryStorage({'ip':'127.0.0.1','port':4001})
  56. # self.__save_frequency = config['save_frequency']
  57. self.__save_frequency = 600 # 最小值120
  58. self.redis_db = None
  59. self.__connected = False
  60. self.run_count = 0
  61. self.servo_status = None
  62. self.proc_status = get_program_status()
  63. def run(self):
  64. self.__connect()
  65. self.__connected = True
  66. self.redis_db = MemoryStorage()
  67. self.redis_db.set_value_permanent({'operation_mode': 'auto'})
  68. self.redis_db.set_value_permanent({'operation_init_move': '0'})
  69. self.redis_db.set_value_permanent({'operation_move_flag': '0'})
  70. self.redis_db.set_value_permanent({'operation_move_angel': '0'})
  71. while True:
  72. speed = 30
  73. acceleration = 2000
  74. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  75. operation_init_move = self.redis_db.get_value(['operation_init_move'])['operation_init_move']
  76. status = self.get_sonar_status()
  77. # self.redis_db.set_value_permanent({'newset_angel': curr_angel})
  78. try:
  79. curr_angel = int(status['servo1_angle'])
  80. curr_depth = round(status['depth'])
  81. except:
  82. print('get sonar status error')
  83. continue
  84. info_dict = {'depth': curr_depth, 'servo_angle': curr_angel}
  85. self.redis_db.set_value(info_dict)
  86. print('fang --> operation_mode:', operation_mode)
  87. if operation_mode == 'manual':
  88. time_flag = 0
  89. temp_now_time = int(time.time())
  90. operation_save_time = self.redis_db.get_value(['operation_save_time'])['operation_save_time']
  91. print('fang --> operation_save_time:', operation_save_time)
  92. print('fang --> temp_now_time:', temp_now_time)
  93. print('fang --> operation_init_move:', operation_init_move)
  94. if operation_save_time != None:
  95. print('fang --> cha:', temp_now_time - int(operation_save_time))
  96. if operation_save_time != None and (temp_now_time - int(operation_save_time)) > 300:
  97. self.redis_db.set_value_permanent({'operation_mode': 'auto'})
  98. continue
  99. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  100. self.redis_db.set_value_permanent(dict1)
  101. if operation_init_move == '0':
  102. self.send_command(location=curr_angel, speed=speed, acceleration=acceleration)
  103. self.redis_db.set_value_permanent({'operation_init_move': '1'})
  104. operation_move_angel = self.redis_db.get_value(['operation_move_angel'])['operation_move_angel']
  105. operation_move_flag = self.redis_db.get_value(['operation_move_flag'])['operation_move_flag']
  106. print('fang --> operation_move_angel:', operation_move_angel)
  107. print('fang --> operation_move_flag:', operation_move_flag)
  108. print('fang --> curr_angel:', curr_angel)
  109. if operation_move_angel != None and operation_move_flag == '1':
  110. after_angel = int(operation_move_angel) + curr_angel
  111. if int(operation_move_angel) > 0:
  112. after_angel += 1
  113. if after_angel > 358:
  114. after_angel = 358
  115. elif after_angel < 1:
  116. after_angel = 1
  117. print('fang --> after_angel:', after_angel)
  118. self.send_command(location=after_angel, speed=speed, acceleration=acceleration)
  119. # time.sleep(0.2)
  120. time_flag = 1
  121. self.redis_db.set_value_permanent({'operation_move_angel': '0'})
  122. self.redis_db.set_value_permanent({'operation_move_flag': '0'})
  123. if time_flag == 0:
  124. time.sleep(1)
  125. print('fang --> --> --> --> --> --> --> --> -->')
  126. self.__save_run_time = 0
  127. continue
  128. status = self.get_sonar_status() # 发查询指令,当作心跳包。
  129. info_dict = {'depth': status['depth'], 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  130. 'pitch': status['pitch'], 'servo_angle': status[servo_angle],
  131. 'water_leak1': status['water_leak1'], 'water_leak2': status['water_leak2']}
  132. self.redis_db.set_value(info_dict)
  133. now_time = time.time()
  134. switch = get_program_status()
  135. print(self.proc_status, "-->", switch)
  136. if self.proc_status == 'on' and switch == 'off':
  137. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  138. self.redis_db.set_value_permanent(dict1)
  139. time.sleep(3)
  140. self.proc_status = switch
  141. continue
  142. elif self.proc_status == 'off' and switch == 'off':
  143. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  144. self.redis_db.set_value_permanent(dict1)
  145. time.sleep(3)
  146. continue
  147. elif self.proc_status == 'off' and switch == 'on':
  148. self.__save_run_time = 0
  149. self.proc_status = switch
  150. else:
  151. pass
  152. if now_time - self.__save_run_time >= self.__save_frequency - 10: # 提前开启声纳
  153. print('sonar_begin---------------------------', time.time())
  154. if SYSTEM_MODE == 'once':
  155. dict = {'sonar_open': 'open', 'range': 'small', 'flag': '0'}
  156. elif SYSTEM_MODE == 'multiple':
  157. dict = {'sonar_open': 'open', 'range': 'small', 'flag': '0'}
  158. self.redis_db.set_value(dict)
  159. self.rotate_zero(speed, acceleration)
  160. print('sonar_status:', self.redis_db.get_value(['sonar_status'])['sonar_status'])
  161. if now_time - self.__save_run_time >= self.__save_frequency and self.redis_db.get_value(['sonar_status'])[
  162. 'sonar_status'] == 'open':
  163. print('zz', self.redis_db.get_value(['sonar_status'])['sonar_status'])
  164. self.__save_run_time = time.time()
  165. if SYSTEM_MODE == 'once':
  166. print('duoji_runing---------------------------', time.time())
  167. self.rotate_once_small(speed, acceleration, self.redis_db, MAX_ANGLE, self.__save_run_time)
  168. self.redis_db.set_value({'range': 'big'})
  169. time.sleep(3)
  170. self.rotate_once_big(speed, acceleration, self.redis_db, MIN_ANGLE, self.__save_run_time)
  171. self.run_count = self.run_count + 1
  172. # print('run_count',self.run_count)
  173. elif SYSTEM_MODE == 'multiple':
  174. speed = 30
  175. angle = 10
  176. self.rotate_multiple_small(angle, speed, acceleration, self.__save_run_time, 's')
  177. self.redis_db.set_value({'range': 'big'})
  178. time.sleep(3)
  179. self.rotate_multiple_big(angle, speed, acceleration, self.__save_run_time, 'b')
  180. # 关闭声纳
  181. dict = {'sonar_open': 'close'}
  182. self.redis_db.set_value(dict)
  183. time.sleep(1)
  184. # data_dict = get_sonar_status(self.__sock)
  185. # self.__conn.set_value(data_dict)
  186. # find_data = b'\xF0\x0F\xFF\xFF\xFF\xFF\x03'
  187. # self.__sock.send(find_data)
  188. #
  189. # # 接收服务器返回的数据
  190. # response = self.__sock.recv(1024)
  191. # print(response)
  192. # #res = servo_data.parse(response)
  193. # while True:
  194. # if isinstance(self.__command, list):
  195. # for i in self.__command:
  196. # command_list = json.loads(i['command'])
  197. # self.command_polling(command_list=command_list)
  198. # time.sleep(1)
  199. # else:
  200. # self.command_polling()
  201. # time.sleep(1)
  202. #
  203. # if self.__stopped:
  204. # break
  205. # 建立socket连接
  206. def __connect(self):
  207. if self.__sock:
  208. self.__sock.close()
  209. self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  210. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 允许重用本地地址和端口
  211. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳维护
  212. self.__sock.settimeout(10) # 设置超时时间3mins
  213. try:
  214. self.__sock.connect((self.__ip, self.__port))
  215. # logger.info(f'Connect to [{self.name}]:[{self.__ip}]:[{self.__port}] success !')
  216. self.__connected = True
  217. except Exception as e:
  218. # logger.info(f'Connect to [{self.name}]:[{self.__ip}]:[{self.__port}] failed:{e} !!!')
  219. self.__connected = False
  220. self.__reconnect()
  221. def __reconnect(self):
  222. while True:
  223. try:
  224. if self.__sock:
  225. self.__sock.close()
  226. self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  227. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  228. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳维护
  229. self.__sock.settimeout(10) # 设置超时时间,单位:秒
  230. self.__sock.connect((self.__ip, self.__port))
  231. self.__connected = True
  232. # logger.info(f'Reconnect to [{self.name}]:[{self.__ip}]:[{self.__port}] success !')
  233. break
  234. except Exception as e:
  235. # logger.info(f'Reconnect to [{self.name}]:[{self.__ip}]:[{self.__port}] failed:{e} !!! Continue reconnect in 5s..')
  236. self.__connected = False
  237. time.sleep(5)
  238. def __send_command(self, command):
  239. if self.__connected:
  240. try:
  241. # send_data = bytes.fromhex(command)
  242. self.__sock.send(command)
  243. response = self.__sock.recv(1024)
  244. return response
  245. except Exception as e:
  246. time.sleep(5)
  247. self.__reconnect()
  248. print('send', e)
  249. return None
  250. # logger.info(f'Send command to [{self.name}]:[{self.__ip}]:[{self.__port}] error:{e}')
  251. else:
  252. self.__reconnect()
  253. return None
  254. def calculate_checksum(self, data):
  255. checksum = 0
  256. for byte in data:
  257. checksum += byte
  258. checksum = (~checksum) & 0xFF
  259. byte_data = bytes([checksum])
  260. hex_string = byte_data.hex()
  261. return hex_string
  262. def data_converter(self, hex_data):
  263. servo_data = c.Struct(
  264. header=c.Int16ul, # 固定不便,可能用于区分不同设备
  265. none_1=c.Int8ub, # 固定不便,可能用于区分不同设备
  266. servo1_flag=c.Int8ub,
  267. none_2=c.Int8ub, # 固定不便,可能用于区分不同设备
  268. servo2_flag=c.Int8ub,
  269. depth=c.Bytes(4),
  270. temp=c.Bytes(4),
  271. yaw=c.Bytes(4), # 航向
  272. roll=c.Bytes(4), # 横滚
  273. pitch=c.Bytes(4), # 俯仰
  274. servo1_angle=c.Bytes(4),
  275. servo2_angle=c.Bytes(4),
  276. )
  277. res = servo_data.parse(hex_data)
  278. return res
  279. def get_sonar_status(self):
  280. # find_data = b'\xF0\x0F\xFF\xFF\xFF\xFF\x03'
  281. # find_data = b'\xF0\x0F\xFF\xFF\xFF\xFF\x03'
  282. try:
  283. find_data = b'\xF0\x0F\xF0\xF0\xF0\xF0\x03' # 增加漏水检测传感器指令
  284. response = self.__send_command(find_data)
  285. result = {'depth': None, 'temp': None, 'yaw': None, 'roll': None, 'pitch': None, 'servo1_angle': None,
  286. 'servo2_angle': None, 'water_leak1': None, 'water_leak2': None}
  287. if response != None:
  288. if len(response) == 35:
  289. res = servo_data.parse(response)
  290. result['depth'] = struct.unpack('>f', res.depth)[0]
  291. result['temp'] = struct.unpack('>f', res.temp)[0]
  292. result['yaw'] = struct.unpack('>f', res.yaw)[0]
  293. result['roll'] = struct.unpack('>f', res.roll)[0]
  294. result['pitch'] = struct.unpack('>f', res.pitch)[0]
  295. result['servo1_angle'] = int(struct.unpack('>f', res.servo1_angle)[0])
  296. result['servo2_angle'] = int(struct.unpack('>f', res.servo2_angle)[0])
  297. if len(response) == 43: # 增加漏水检测传感器
  298. res = servo_data.parse(response)
  299. result['depth'] = struct.unpack('>f', res.depth)[0]
  300. result['temp'] = struct.unpack('>f', res.temp)[0]
  301. result['yaw'] = struct.unpack('>f', res.yaw)[0]
  302. result['roll'] = struct.unpack('>f', res.roll)[0]
  303. result['pitch'] = struct.unpack('>f', res.pitch)[0]
  304. result['servo1_angle'] = int(struct.unpack('>f', res.servo1_angle)[0])
  305. result['servo2_angle'] = int(struct.unpack('>f', res.servo2_angle)[0])
  306. result['water_leak1'] = struct.unpack('>f', res.water_leak1)[0]
  307. result['water_leak2'] = struct.unpack('>f', res.water_leak2)[0]
  308. return result
  309. except Exception as e:
  310. print('fang --> get sonar error :', e)
  311. #print(response)
  312. #print(len(response))
  313. #print(binascii.hexlify(response).decode('utf-8'))
  314. # time.sleep(2)
  315. # continue
  316. return {'depth': None, 'temp': None, 'yaw': None, 'roll': None, 'pitch': None, 'servo1_angle': None,
  317. 'servo2_angle': None, 'water_leak1': None, 'water_leak2': None}
  318. def send_command(self, location, speed, acceleration):
  319. header = 'f00f'
  320. servo1_location = struct.pack('>f', location).hex()
  321. servo2_location = struct.pack('>f', location).hex()
  322. servo1_speed = struct.pack('>f', speed).hex()
  323. servo2_speed = struct.pack('>f', speed).hex()
  324. servo1_acceleration = struct.pack('>f', acceleration).hex()
  325. servo2_acceleration = struct.pack('>f', acceleration).hex()
  326. data = servo1_location + servo2_location + servo1_speed + servo2_speed + servo1_acceleration + servo2_acceleration
  327. data_hex = bytes.fromhex(data)
  328. checksum = self.calculate_checksum(data_hex)
  329. send_data = header + data + checksum
  330. send_data = bytes.fromhex(send_data)
  331. # print(send_data) # 输出校验和的十六进制表示
  332. response = self.__send_command(send_data)
  333. def rotate_zero(self, speed, acceleration):
  334. while True:
  335. status = self.get_sonar_status()
  336. if status[servo_angle] != None:
  337. if status[servo_angle] > MIN_ANGLE + 5:
  338. print('rotate_zero', status[servo_angle])
  339. self.send_command(location=MIN_ANGLE, speed=speed, acceleration=acceleration)
  340. else:
  341. break
  342. def rotate_once_small(self, speed, acceleration, save_data, location, time_start):
  343. count = 0
  344. num = 0 # 图像编号
  345. angel = None
  346. save_flag = 0
  347. send_data = True
  348. while True:
  349. status = self.get_sonar_status()
  350. # print(status[servo_angle])
  351. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  352. if operation_mode == 'manual':
  353. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  354. self.redis_db.set_value_permanent(dict1)
  355. break
  356. if status[servo_angle] != None:
  357. if send_data == True:
  358. # time_start = time.time() # 记录开始时间
  359. save_flag = 1
  360. self.send_command(location=location, speed=speed, acceleration=acceleration)
  361. send_data = False
  362. if save_flag == 1:
  363. info_dict = {'flag': '1', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  364. 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  365. 'pitch': status['pitch'], 'img_type': 's',
  366. 'servo_angle': status[servo_angle],
  367. 'water_leak1': status['water_leak1'],
  368. 'water_leak2': status['water_leak2']}
  369. save_data.set_value(info_dict)
  370. num += 1
  371. if angel != status[servo_angle]:
  372. angel = status[servo_angle]
  373. count = 0
  374. else:
  375. count = count + 1
  376. if status[servo_angle] >= MAX_ANGLE - 1 or count > 3:
  377. save_data.set_value({'flag': '0'})
  378. break
  379. def rotate_once_big(self, speed, acceleration, save_data, location, time_start):
  380. count = 0
  381. num = 0 # 图像编号
  382. angel = None
  383. save_flag = 0
  384. send_data = True
  385. while True:
  386. status = self.get_sonar_status()
  387. print(status[servo_angle])
  388. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  389. # print('fang --> rotate --> model :', operation_mode)
  390. if operation_mode == 'manual':
  391. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  392. self.redis_db.set_value_permanent(dict1)
  393. break
  394. if status[servo_angle] != None and status[servo_angle]>0:
  395. if send_data == True:
  396. # time_start = time.time() # 记录开始时间
  397. save_flag = 1
  398. self.send_command(location=location, speed=speed, acceleration=acceleration)
  399. send_data = False
  400. if save_flag == 1:
  401. info_dict = {'flag': '1', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  402. 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  403. 'pitch': status['pitch'], 'img_type': 'b',
  404. 'servo_angle': status[servo_angle],
  405. 'water_leak1': status['water_leak1'],
  406. 'water_leak2': status['water_leak2']
  407. }
  408. save_data.set_value(info_dict)
  409. num += 1
  410. if angel != status[servo_angle]:
  411. angel = status[servo_angle]
  412. count = 0
  413. else:
  414. count = count + 1
  415. if status[servo_angle] <= MIN_ANGLE + 0.5 or count > 3:
  416. save_data.set_value({'flag': '0'})
  417. break
  418. def rotate_once_little(self, speed, acceleration, save_data, location, time_start, mode):
  419. count = 0
  420. angel = None
  421. while True:
  422. status = self.get_sonar_status()
  423. if status[servo_angle] != None and status[servo_angle]>0:
  424. if location >= MIN_ANGLE and location <= MAX_ANGLE:
  425. self.send_command(location=location, speed=speed, acceleration=acceleration)
  426. if angel != status[servo_angle]:
  427. angel = status[servo_angle]
  428. count = 0
  429. else:
  430. count = count + 1
  431. if count > 2:
  432. info_dict = {'flag': '1', 'timestamp': time_start, 'num': status[servo_angle],
  433. 'depth': status['depth'],
  434. 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  435. 'pitch': status['pitch'], 'img_type': mode,
  436. 'servo_angle': status[servo_angle],
  437. 'water_leak1': status['water_leak1'],
  438. 'water_leak2': status['water_leak2']
  439. }
  440. save_data.set_value(info_dict)
  441. break
  442. # def rotate_once(self,speed, acceleration,save_data):
  443. # status = self.get_sonar_status()
  444. # if status[servo_angle] != None:
  445. # if status[servo_angle] > 0.5:
  446. # self.send_command(location=0, speed=speed, acceleration=acceleration)
  447. # else:
  448. # count = 0
  449. # num = 0 # 图像编号
  450. # angel = None
  451. # save_image_flag = 0
  452. # while count < 5:
  453. # status = self.get_sonar_status()
  454. # if status[servo_angle] != None and save_image_flag == 0:
  455. # if status[servo_angle] < 0.5:
  456. # time_start = time.time() # 记录开始时间
  457. # save_image_flag = 1
  458. # self.send_command(location=359, speed=speed, acceleration=acceleration)
  459. # #print(angel,status[servo_angle])
  460. # if angel != status[servo_angle]:
  461. # angel = status[servo_angle]
  462. # count = 0
  463. # else:
  464. # count = count + 1
  465. # if save_image_flag == 2:
  466. # count = count + 2
  467. # if status[servo_angle] != None:
  468. # if count > 2 or status[servo_angle] >= 358:
  469. # save_image_flag = 0
  470. # save_data.set_value({'range':'big'})
  471. # if status[servo_angle] != None:
  472. # if count > 2 or status[servo_angle] >= 358:
  473. # save_image_flag = 2
  474. # time.sleep(5)
  475. # self.send_command(location=0, speed=speed, acceleration=acceleration)
  476. # time_now = time.time()
  477. # if save_image_flag == 1:
  478. # info_dict = {'flag': '1', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  479. # 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  480. # 'pitch': status['pitch'],
  481. # 'servo_angle': status[servo_angle]}
  482. # save_data.set_value(info_dict)
  483. # num += 1
  484. # print('asdasd',111111111111111111111)
  485. # elif save_image_flag == 2:
  486. # info_dict = {'flag': '1', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  487. # 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  488. # 'pitch': status['pitch'],
  489. # 'servo_angle': status[servo_angle]}
  490. # save_data.set_value(info_dict)
  491. # num += 1
  492. # print('asdasd', 22222222222222222222222222222222)
  493. # else:
  494. # info_dict = {'flag': '0', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  495. # 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  496. # 'pitch': status['pitch'],
  497. # 'servo_angle': status[servo_angle]}
  498. # save_data.set_value(info_dict)
  499. # # time_sum = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s
  500. # # print(time_sum)
  501. def rotate_multiple_small(self, angel, speed, acceleration, time_start, mode):
  502. t = 0
  503. while True:
  504. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  505. if operation_mode == 'manual':
  506. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  507. self.redis_db.set_value_permanent(dict1)
  508. break
  509. save_status = self.redis_db.get_value(['flag'])['flag']
  510. if save_status == '0':
  511. location_angel = MIN_ANGLE + angel * t
  512. if location_angel > MAX_ANGLE:
  513. break
  514. else:
  515. self.rotate_once_little(speed, acceleration, self.redis_db, location_angel, time_start, mode)
  516. t = t + 1
  517. def rotate_multiple_big(self, angel, speed, acceleration, time_start, mode):
  518. t = 0
  519. while True:
  520. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  521. if operation_mode == 'manual':
  522. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  523. self.redis_db.set_value_permanent(dict1)
  524. break
  525. save_status = self.redis_db.get_value(['flag'])['flag']
  526. if save_status == '0':
  527. location_angel = MAX_ANGLE - angel * t - 10
  528. if location_angel < MIN_ANGLE:
  529. break
  530. else:
  531. self.rotate_once_little(speed, acceleration, self.redis_db, location_angel, time_start, mode)
  532. t = t + 1
  533. def rotate_multiple(self, client_socket, angel, speed, acceleration):
  534. # 模式二,指定间隔,达到,停顿,继续,完成,停止
  535. status = self.get_sonar_status()
  536. if status[servo_angle] != None:
  537. if status[servo_angle] > 0.5:
  538. self.send_command(location=0, speed=speed, acceleration=acceleration)
  539. else:
  540. i = int(360 / angel)
  541. t = 0
  542. time_start = time.time() # 记录开始时间
  543. while t < i + 1:
  544. count = 0
  545. speed = 300
  546. acceleration = 2000
  547. last_angel = None
  548. while count < 4:
  549. status = self.get_sonar_status()
  550. location_angel = min(angel * t, 359)
  551. self.send_command(location=location_angel, speed=speed, acceleration=acceleration)
  552. if last_angel != status[servo_angle]:
  553. last_angel = status[servo_angle]
  554. count = 0
  555. else:
  556. print(t, last_angel)
  557. count = count + 1
  558. if count > 3:
  559. t = t + 1
  560. self.send_command(location=0, speed=speed, acceleration=acceleration)
  561. if __name__ == '__main__':
  562. servo_config = {'ip': '192.168.5.62', 'port': 4001, 'save_frequency': 0}
  563. awaken_gls10 = Process(target=sender, name='awaken_gls10', daemon=True)
  564. command_p = Client()
  565. temp = ClientTemp()
  566. save_image = Process(target=Receiver, name='save_image', daemon=True)
  567. save_temp = Process(target=Receiver1, name='save_temp', daemon=True)
  568. p = MyProcess(name='servo', config=servo_config) # 实例化进程对象
  569. awaken_gls10.start()
  570. command_p.daemon = True
  571. command_p.start()
  572. temp.daemon = True
  573. temp.start()
  574. save_image.start()
  575. save_temp.start()
  576. p.daemon = True
  577. p.start()
  578. while True:
  579. time.sleep(1)
  580. # print('main..............................', time.time())