You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.1 KiB

6 years ago
  1. #!/usr/bin/env python3
  2. print("[INFO]: Interpreter started")
  3. from ev3dev.core import *
  4. import ev3dev.ev3 as ev3
  5. print("[INFO]: Imported ev3dev.ev3")
  6. import socket
  7. print("[INFO]: Imported socket")
  8. import os
  9. print("[INFO]: Imported os")
  10. import json
  11. print("[INFO]: Imported json")
  12. state = True
  13. motor1 = ev3.LargeMotor("outA")
  14. motor2 = ev3.LargeMotor("outD")
  15. cam = ev3.MediumMotor('outB')
  16. def move(speed):
  17. if time:
  18. motor1.run_forever(speed_sp=speed)
  19. motor2.run_forever(speed_sp=speed)
  20. cam.run_to_abs_pos(position_sp=0, speed_sp=500)
  21. def stop():
  22. motor1.stop()
  23. motor2.stop()
  24. cam.stop()
  25. HOST = '0.0.0.0'
  26. PORT = 3131
  27. pid=os.fork()
  28. if pid==0: # new process
  29. mjpeg_loc = "/home/robot/mjpg-streamer/"
  30. os.system('{0}mjpg_streamer -i "{0}input_uvc.so -f 15 -y" -o "{0}output_http.so -w {0}www"'.format(mjpeg_loc))
  31. exit()
  32. power = PowerSupply()
  33. if power.measured_volts < 7.1:
  34. print("Change the battery.")
  35. exit()
  36. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  37. try:
  38. s.bind((HOST, PORT))
  39. while True:
  40. s.listen()
  41. print("[INFO]: Listening...")
  42. conn, addr = s.accept()
  43. with conn:
  44. print('[INFO]: Connected by', addr)
  45. move(500)
  46. while True:
  47. if cam.speed == 0 and (motor1.speed > 0 and motor2.speed > 0):
  48. if state:
  49. cam.run_to_abs_pos(position_sp=4000, speed_sp=500)
  50. else:
  51. cam.run_to_abs_pos(position_sp=0, speed_sp=500)
  52. state = not state
  53. try:
  54. received = conn.recv(1024)
  55. if not received:
  56. raise BrokenPipeError("Client disconnect")
  57. else:
  58. received = received.decode().strip()[-1]
  59. print(received)
  60. if received == 's':
  61. stop()
  62. elif received == 'm':
  63. move(500)
  64. elif received == 'i':
  65. data = {
  66. "battery_voltage":str(round(power.measured_volts,1)),
  67. "current_drawn":str(round(power.measured_amps,1)),
  68. "lat":"31",
  69. "lng":"39"
  70. }
  71. conn.send((json.dumps(data, ensure_ascii=False)+"\n").encode('gbk'))
  72. elif recieved == '+':
  73. cam.run_timed(time_sp=50, speed_sp=750)
  74. elif recieved == '-':
  75. cam.run_timed(time_sp=50, speed_sp=-750)
  76. except BrokenPipeError:
  77. print("[INFO]: Client disconnected")
  78. stop()
  79. break
  80. except KeyboardInterrupt:
  81. stop()
  82. s.close()