test_tcp_kaoji.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. import logging
  16. from logging.handlers import TimedRotatingFileHandler
  17. servo_angle = 'servo1_angle'
  18. MIN_ANGLE = 1
  19. MAX_ANGLE = 358
  20. SYSTEM_MODE = 'once' # once,multiple
  21. hard_disk_db = HardDiskStorage()
  22. servo_data = c.Struct(
  23. header=c.Int16ul, # 固定不便,可能用于区分不同设备
  24. none_1=c.Int8ub, # 固定不便,可能用于区分不同设备
  25. servo1_flag=c.Int8ub,
  26. none_2=c.Int8ub, # 固定不便,可能用于区分不同设备
  27. servo2_flag=c.Int8ub,
  28. depth=c.Bytes(4),
  29. temp=c.Bytes(4),
  30. yaw=c.Bytes(4), # 航向
  31. roll=c.Bytes(4), # 横滚
  32. pitch=c.Bytes(4), # 俯仰
  33. servo1_angle=c.Bytes(4),
  34. servo2_angle=c.Bytes(4),
  35. water_leak1=c.Bytes(4),
  36. water_leak2=c.Bytes(4),
  37. )
  38. def get_program_status():
  39. sql = "SELECT time_end FROM measurement_data ORDER BY id desc limit 1"
  40. res = hard_disk_db.execute_sql(sql, None)
  41. if res:
  42. if res[0]['time_end']:
  43. return "off"
  44. else:
  45. return "on"
  46. else:
  47. return "off"
  48. class MyProcess(Process): # 继承Process类
  49. def __init__(self, name, config):
  50. super(MyProcess, self).__init__()
  51. self.name = name
  52. self.__sock = None
  53. self.__ip = config['ip']
  54. self.__port = config['port']
  55. self.__save_run_time = 0
  56. # self.__converter = converter
  57. # self.__storager = MemoryStorage({'ip':'127.0.0.1','port':4001})
  58. # self.__save_frequency = config['save_frequency']
  59. self.__save_frequency = 300 # 最小值120
  60. self.redis_db = None
  61. self.__connected = False
  62. self.run_count = 0
  63. self.servo_status = None
  64. self.proc_status = get_program_status()
  65. def run(self):
  66. self.__connect()
  67. self.__connected = True
  68. self.redis_db = MemoryStorage()
  69. self.redis_db.set_value_permanent({'operation_mode': 'auto'})
  70. self.redis_db.set_value_permanent({'operation_init_move': '0'})
  71. self.redis_db.set_value_permanent({'operation_move_flag': '0'})
  72. self.redis_db.set_value_permanent({'operation_move_angel': '0'})
  73. while True:
  74. speed = 30
  75. acceleration = 2000
  76. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  77. operation_init_move = self.redis_db.get_value(['operation_init_move'])['operation_init_move']
  78. status = self.get_sonar_status()
  79. # self.redis_db.set_value_permanent({'newset_angel': curr_angel})
  80. try:
  81. curr_angel = int(status['servo1_angle'])
  82. curr_depth = round(status['depth'])
  83. except:
  84. print('get sonar status error')
  85. continue
  86. info_dict = {'depth': curr_depth, 'servo_angle': curr_angel}
  87. self.redis_db.set_value(info_dict)
  88. print('fang --> operation_mode:', operation_mode)
  89. if operation_mode == 'manual':
  90. time_flag = 0
  91. temp_now_time = int(time.time())
  92. operation_save_time = self.redis_db.get_value(['operation_save_time'])['operation_save_time']
  93. print('fang --> operation_save_time:', operation_save_time)
  94. print('fang --> temp_now_time:', temp_now_time)
  95. print('fang --> operation_init_move:', operation_init_move)
  96. if operation_save_time != None:
  97. print('fang --> cha:', temp_now_time - int(operation_save_time))
  98. if operation_save_time != None and (temp_now_time - int(operation_save_time)) > 300:
  99. self.redis_db.set_value_permanent({'operation_mode': 'auto'})
  100. continue
  101. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  102. self.redis_db.set_value_permanent(dict1)
  103. if operation_init_move == '0':
  104. self.send_command(location=curr_angel, speed=speed, acceleration=acceleration)
  105. self.redis_db.set_value_permanent({'operation_init_move': '1'})
  106. operation_move_angel = self.redis_db.get_value(['operation_move_angel'])['operation_move_angel']
  107. operation_move_flag = self.redis_db.get_value(['operation_move_flag'])['operation_move_flag']
  108. print('fang --> operation_move_angel:', operation_move_angel)
  109. print('fang --> operation_move_flag:', operation_move_flag)
  110. print('fang --> curr_angel:', curr_angel)
  111. if operation_move_angel != None and operation_move_flag == '1':
  112. after_angel = int(operation_move_angel) + curr_angel
  113. if int(operation_move_angel) > 0:
  114. after_angel += 1
  115. if after_angel > 358:
  116. after_angel = 358
  117. elif after_angel < 1:
  118. after_angel = 1
  119. print('fang --> after_angel:', after_angel)
  120. self.send_command(location=after_angel, speed=speed, acceleration=acceleration)
  121. # time.sleep(0.2)
  122. time_flag = 1
  123. self.redis_db.set_value_permanent({'operation_move_angel': '0'})
  124. self.redis_db.set_value_permanent({'operation_move_flag': '0'})
  125. if time_flag == 0:
  126. time.sleep(1)
  127. print('fang --> --> --> --> --> --> --> --> -->')
  128. self.__save_run_time = 0
  129. continue
  130. status = self.get_sonar_status() # 发查询指令,当作心跳包。
  131. info_dict = {'depth': status['depth'], 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  132. 'pitch': status['pitch'], 'servo_angle': status[servo_angle],
  133. 'water_leak1': status['water_leak1'], 'water_leak2': status['water_leak2']}
  134. self.redis_db.set_value(info_dict)
  135. now_time = time.time()
  136. switch = get_program_status()
  137. print(self.proc_status, "-->", switch)
  138. if self.proc_status == 'on' and switch == 'off':
  139. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  140. self.redis_db.set_value_permanent(dict1)
  141. time.sleep(3)
  142. self.proc_status = switch
  143. continue
  144. elif self.proc_status == 'off' and switch == 'off':
  145. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  146. self.redis_db.set_value_permanent(dict1)
  147. time.sleep(3)
  148. continue
  149. elif self.proc_status == 'off' and switch == 'on':
  150. self.__save_run_time = 0
  151. self.proc_status = switch
  152. else:
  153. pass
  154. if now_time - self.__save_run_time >= self.__save_frequency - 10: # 提前开启声纳
  155. print('sonar_begin---------------------------', time.time())
  156. if SYSTEM_MODE == 'once':
  157. dict = {'sonar_open': 'open', 'range': 'small', 'flag': '0'}
  158. elif SYSTEM_MODE == 'multiple':
  159. dict = {'sonar_open': 'open', 'range': 'small', 'flag': '0'}
  160. self.redis_db.set_value(dict)
  161. self.rotate_zero(speed, acceleration)
  162. print('sonar_status:', self.redis_db.get_value(['sonar_status'])['sonar_status'])
  163. if now_time - self.__save_run_time >= self.__save_frequency and self.redis_db.get_value(['sonar_status'])[
  164. 'sonar_status'] == 'open':
  165. print('zz', self.redis_db.get_value(['sonar_status'])['sonar_status'])
  166. self.__save_run_time = time.time()
  167. if SYSTEM_MODE == 'once':
  168. print('duoji_runing---------------------------', time.time())
  169. self.rotate_once_small(speed, acceleration, self.redis_db, MAX_ANGLE, self.__save_run_time)
  170. self.redis_db.set_value({'range': 'big'})
  171. time.sleep(3)
  172. self.rotate_once_big(speed, acceleration, self.redis_db, MIN_ANGLE, self.__save_run_time)
  173. self.run_count = self.run_count + 1
  174. # print('run_count',self.run_count)
  175. elif SYSTEM_MODE == 'multiple':
  176. speed = 30
  177. angle = 10
  178. self.rotate_multiple_small(angle, speed, acceleration, self.__save_run_time, 's')
  179. self.redis_db.set_value({'range': 'big'})
  180. time.sleep(3)
  181. self.rotate_multiple_big(angle, speed, acceleration, self.__save_run_time, 'b')
  182. # 关闭声纳
  183. dict = {'sonar_open': 'close'}
  184. self.redis_db.set_value(dict)
  185. time.sleep(1)
  186. # data_dict = get_sonar_status(self.__sock)
  187. # self.__conn.set_value(data_dict)
  188. # find_data = b'\xF0\x0F\xFF\xFF\xFF\xFF\x03'
  189. # self.__sock.send(find_data)
  190. #
  191. # # 接收服务器返回的数据
  192. # response = self.__sock.recv(1024)
  193. # print(response)
  194. # #res = servo_data.parse(response)
  195. # while True:
  196. # if isinstance(self.__command, list):
  197. # for i in self.__command:
  198. # command_list = json.loads(i['command'])
  199. # self.command_polling(command_list=command_list)
  200. # time.sleep(1)
  201. # else:
  202. # self.command_polling()
  203. # time.sleep(1)
  204. #
  205. # if self.__stopped:
  206. # break
  207. # 建立socket连接
  208. def __connect(self):
  209. if self.__sock:
  210. self.__sock.close()
  211. self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  212. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 允许重用本地地址和端口
  213. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳维护
  214. self.__sock.settimeout(10) # 设置超时时间3mins
  215. try:
  216. self.__sock.connect((self.__ip, self.__port))
  217. # logger.info(f'Connect to [{self.name}]:[{self.__ip}]:[{self.__port}] success !')
  218. self.__connected = True
  219. except Exception as e:
  220. # logger.info(f'Connect to [{self.name}]:[{self.__ip}]:[{self.__port}] failed:{e} !!!')
  221. self.__connected = False
  222. self.__reconnect()
  223. def __reconnect(self):
  224. while True:
  225. try:
  226. if self.__sock:
  227. self.__sock.close()
  228. self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  229. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  230. self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳维护
  231. self.__sock.settimeout(10) # 设置超时时间,单位:秒
  232. self.__sock.connect((self.__ip, self.__port))
  233. self.__connected = True
  234. # logger.info(f'Reconnect to [{self.name}]:[{self.__ip}]:[{self.__port}] success !')
  235. break
  236. except Exception as e:
  237. # logger.info(f'Reconnect to [{self.name}]:[{self.__ip}]:[{self.__port}] failed:{e} !!! Continue reconnect in 5s..')
  238. self.__connected = False
  239. time.sleep(5)
  240. def __send_command(self, command):
  241. if self.__connected:
  242. try:
  243. # send_data = bytes.fromhex(command)
  244. self.__sock.send(command)
  245. response = self.__sock.recv(1024)
  246. return response
  247. except Exception as e:
  248. time.sleep(5)
  249. self.__reconnect()
  250. print('send', e)
  251. return None
  252. # logger.info(f'Send command to [{self.name}]:[{self.__ip}]:[{self.__port}] error:{e}')
  253. else:
  254. self.__reconnect()
  255. return None
  256. def calculate_checksum(self, data):
  257. checksum = 0
  258. for byte in data:
  259. checksum += byte
  260. checksum = (~checksum) & 0xFF
  261. byte_data = bytes([checksum])
  262. hex_string = byte_data.hex()
  263. return hex_string
  264. def data_converter(self, hex_data):
  265. servo_data = c.Struct(
  266. header=c.Int16ul, # 固定不便,可能用于区分不同设备
  267. none_1=c.Int8ub, # 固定不便,可能用于区分不同设备
  268. servo1_flag=c.Int8ub,
  269. none_2=c.Int8ub, # 固定不便,可能用于区分不同设备
  270. servo2_flag=c.Int8ub,
  271. depth=c.Bytes(4),
  272. temp=c.Bytes(4),
  273. yaw=c.Bytes(4), # 航向
  274. roll=c.Bytes(4), # 横滚
  275. pitch=c.Bytes(4), # 俯仰
  276. servo1_angle=c.Bytes(4),
  277. servo2_angle=c.Bytes(4),
  278. )
  279. res = servo_data.parse(hex_data)
  280. return res
  281. def get_sonar_status(self):
  282. # find_data = b'\xF0\x0F\xFF\xFF\xFF\xFF\x03'
  283. # find_data = b'\xF0\x0F\xFF\xFF\xFF\xFF\x03'
  284. while True:
  285. try:
  286. find_data = b'\xF0\x0F\xF0\xF0\xF0\xF0\x03' # 增加漏水检测传感器指令
  287. response = self.__send_command(find_data)
  288. result = {'depth': None, 'temp': None, 'yaw': None, 'roll': None, 'pitch': None, 'servo1_angle': None,
  289. 'servo2_angle': None, 'water_leak1': None, 'water_leak2': None}
  290. if response != None:
  291. if len(response) == 35:
  292. res = servo_data.parse(response)
  293. result['depth'] = struct.unpack('>f', res.depth)[0]
  294. result['temp'] = struct.unpack('>f', res.temp)[0]
  295. result['yaw'] = struct.unpack('>f', res.yaw)[0]
  296. result['roll'] = struct.unpack('>f', res.roll)[0]
  297. result['pitch'] = struct.unpack('>f', res.pitch)[0]
  298. result['servo1_angle'] = int(struct.unpack('>f', res.servo1_angle)[0])
  299. result['servo2_angle'] = int(struct.unpack('>f', res.servo2_angle)[0])
  300. if len(response) == 43: # 增加漏水检测传感器
  301. res = servo_data.parse(response)
  302. result['depth'] = struct.unpack('>f', res.depth)[0]
  303. result['temp'] = struct.unpack('>f', res.temp)[0]
  304. result['yaw'] = struct.unpack('>f', res.yaw)[0]
  305. result['roll'] = struct.unpack('>f', res.roll)[0]
  306. result['pitch'] = struct.unpack('>f', res.pitch)[0]
  307. result['servo1_angle'] = int(struct.unpack('>f', res.servo1_angle)[0])
  308. result['servo2_angle'] = int(struct.unpack('>f', res.servo2_angle)[0])
  309. result['water_leak1'] = struct.unpack('>f', res.water_leak1)[0]
  310. result['water_leak2'] = struct.unpack('>f', res.water_leak2)[0]
  311. return result
  312. except Exception as e:
  313. print('fang --> get sonar error :', e)
  314. time.sleep(2)
  315. continue
  316. def send_command(self, location, speed, acceleration):
  317. header = 'f00f'
  318. servo1_location = struct.pack('>f', location).hex()
  319. servo2_location = struct.pack('>f', location).hex()
  320. servo1_speed = struct.pack('>f', speed).hex()
  321. servo2_speed = struct.pack('>f', speed).hex()
  322. servo1_acceleration = struct.pack('>f', acceleration).hex()
  323. servo2_acceleration = struct.pack('>f', acceleration).hex()
  324. data = servo1_location + servo2_location + servo1_speed + servo2_speed + servo1_acceleration + servo2_acceleration
  325. data_hex = bytes.fromhex(data)
  326. checksum = self.calculate_checksum(data_hex)
  327. send_data = header + data + checksum
  328. send_data = bytes.fromhex(send_data)
  329. # print(send_data) # 输出校验和的十六进制表示
  330. response = self.__send_command(send_data)
  331. def rotate_zero(self, speed, acceleration):
  332. while True:
  333. status = self.get_sonar_status()
  334. if status[servo_angle] != None:
  335. if status[servo_angle] > MIN_ANGLE + 5:
  336. print('rotate_zero', status[servo_angle])
  337. self.send_command(location=MIN_ANGLE, speed=speed, acceleration=acceleration)
  338. else:
  339. break
  340. def rotate_once_small(self, speed, acceleration, save_data, location, time_start):
  341. count = 0
  342. num = 0 # 图像编号
  343. angel = None
  344. save_flag = 0
  345. send_data = True
  346. while True:
  347. status = self.get_sonar_status()
  348. # print(status[servo_angle])
  349. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  350. if operation_mode == 'manual':
  351. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  352. self.redis_db.set_value_permanent(dict1)
  353. break
  354. if status[servo_angle] != None:
  355. if send_data == True:
  356. # time_start = time.time() # 记录开始时间
  357. save_flag = 1
  358. self.send_command(location=location, speed=speed, acceleration=acceleration)
  359. send_data = False
  360. if save_flag == 1:
  361. info_dict = {'flag': '1', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  362. 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  363. 'pitch': status['pitch'], 'img_type': 's',
  364. 'servo_angle': status[servo_angle],
  365. 'water_leak1': status['water_leak1'],
  366. 'water_leak2': status['water_leak2']}
  367. save_data.set_value(info_dict)
  368. num += 1
  369. if angel != status[servo_angle]:
  370. angel = status[servo_angle]
  371. count = 0
  372. else:
  373. count = count + 1
  374. if status[servo_angle] >= MAX_ANGLE - 1 or count > 4:
  375. save_data.set_value({'flag': '0'})
  376. logger.warning('angel error, save image stop , curr range: s . ')
  377. break
  378. def rotate_once_big(self, speed, acceleration, save_data, location, time_start):
  379. count = 0
  380. num = 0 # 图像编号
  381. angel = None
  382. save_flag = 0
  383. send_data = True
  384. while True:
  385. status = self.get_sonar_status()
  386. print(status[servo_angle])
  387. operation_mode = self.redis_db.get_value(['operation_mode'])['operation_mode']
  388. print('fang --> rotate --> model :', operation_mode)
  389. if operation_mode == 'manual':
  390. dict1 = {'sonar_open': 'open', 'range': 'big', 'flag': '0'}
  391. self.redis_db.set_value_permanent(dict1)
  392. break
  393. if status[servo_angle] != None:
  394. if send_data == True:
  395. # time_start = time.time() # 记录开始时间
  396. save_flag = 1
  397. self.send_command(location=location, speed=speed, acceleration=acceleration)
  398. send_data = False
  399. if save_flag == 1:
  400. info_dict = {'flag': '1', 'timestamp': time_start, 'num': num, 'depth': status['depth'],
  401. 'temp': status['temp'], 'yaw': status['yaw'], 'roll': status['roll'],
  402. 'pitch': status['pitch'], 'img_type': 'b',
  403. 'servo_angle': status[servo_angle],
  404. 'water_leak1': status['water_leak1'],
  405. 'water_leak2': status['water_leak2']
  406. }
  407. save_data.set_value(info_dict)
  408. num += 1
  409. if angel != status[servo_angle]:
  410. angel = status[servo_angle]
  411. count = 0
  412. else:
  413. count = count + 1
  414. if status[servo_angle] <= MIN_ANGLE + 0.5 or count > 4:
  415. save_data.set_value({'flag': '0'})
  416. logger.warning('angel error, save image stop , curr range: b . ')
  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:
  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.1.61', 'port': 4001, 'save_frequency': 0}
  563. log_handler = TimedRotatingFileHandler(filename='/home/sencott/sonar/log/test_tcp.log', when='midnight', interval=30, backupCount=5,
  564. encoding="utf8")
  565. # 配置日志记录器
  566. logging.basicConfig(level=logging.WARNING, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
  567. handlers=[log_handler])
  568. # 获取日志记录器
  569. logger = logging.getLogger()
  570. awaken_gls10 = Process(target=sender, name='awaken_gls10', daemon=True)
  571. command_p = Client()
  572. temp = ClientTemp()
  573. save_image = Process(target=Receiver, name='save_image', daemon=True)
  574. save_temp = Process(target=Receiver1, name='save_temp', daemon=True)
  575. p = MyProcess(name='servo', config=servo_config) # 实例化进程对象
  576. awaken_gls10.start()
  577. command_p.daemon = True
  578. command_p.start()
  579. temp.daemon = True
  580. temp.start()
  581. save_image.start()
  582. save_temp.start()
  583. p.daemon = True
  584. p.start()
  585. while True:
  586. time.sleep(1)
  587. # print('main..............................', time.time())