main.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from socket import *
  2. import time
  3. import subprocess
  4. # import pigpio
  5. # from controlServer import ControlServer
  6. from videoServer import VideoServer
  7. # 控制服务配置
  8. HOST = '0.0.0.0'
  9. PORT = 49800
  10. ADDR = (HOST, PORT)
  11. # 电调+舵机配置
  12. MSLIST = [
  13. {
  14. "name": 'MOTOR',
  15. "pinList": [13, 16]
  16. },
  17. {
  18. "name": 'SERVO',
  19. "pinList": [12, 19]
  20. },
  21. ]
  22. # 用户列表配置
  23. USERLIST = []
  24. # PI配置
  25. PI = None
  26. # 视频服务配置
  27. VPORT = 49700
  28. VADDR = (HOST, VPORT)
  29. FX = 0.5 # 缩放
  30. SPD = 0.1 # fps=>越小,FPS越高,cpu消耗越大
  31. # def Calibration():
  32. # # 自动校准
  33. # global PI, MSLIST
  34. # # 启动pigpiod服务
  35. # port = subprocess.check_output(
  36. # "ps -ef |grep pigpiod |awk '{print $2}'", shell=True).decode().split('\n')
  37. # if '' in port and len(port) <= 3:
  38. # subprocess.run('pigpiod', shell=True)
  39. # time.sleep(0.5)
  40. # PI = pigpio.pi()
  41. # print('开始自动校准')
  42. # # 校准服务
  43. # speed = 0
  44. # while True:
  45. # speed += 1
  46. # for s in MSLIST:
  47. # for d in s['pinList']:
  48. # if s['name'] == 'MOTOR':
  49. # # Set ESC speed via PWM
  50. # PI.set_servo_pulsewidth(
  51. # d, speed * 1000 / 7 + 1000)
  52. # else:
  53. # PI.set_servo_pulsewidth(d, 1500)
  54. # time.sleep(2)
  55. # print(speed)
  56. # if speed > 2:
  57. # break
  58. def loopServer(SOCK):
  59. # 服务监听
  60. global USERLIST
  61. while True:
  62. print('Waiting For Contorl Client Connection ...')
  63. client, addr = SOCK.accept() # 会阻塞
  64. print('New control User', addr)
  65. # 增加线程
  66. if client not in USERLIST and len(USERLIST) < 2:
  67. USERLIST.append(client)
  68. # 接收
  69. # contrl = ControlServer(client, USERLIST, MSLIST, PI)
  70. # contrl.start()
  71. else:
  72. # 不允许连接
  73. print('不允许连接')
  74. client.close()
  75. if __name__ == '__main__':
  76. # 启动校准服务
  77. # Calibration()
  78. print('1S后启动视频服务')
  79. time.sleep(1)
  80. # 视频服务
  81. VideoServer(VADDR, SPD, FX).start()
  82. print('1S后启动控制服务')
  83. time.sleep(1)
  84. # 启动socket服务
  85. SOCK = socket(AF_INET, SOCK_STREAM)
  86. SOCK.bind(ADDR)
  87. SOCK.listen(1)
  88. loopServer(SOCK)