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.

82 lines
2.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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. recieved = conn.recv(1024)
  55. if not recieved:
  56. pass
  57. else:
  58. recieved = recieved.decode().strip()[-1]
  59. print(recieved)
  60. if recieved == 's':
  61. stop()
  62. elif recieved == 'm':
  63. move(500)
  64. elif recieved == 'i':
  65. data = {
  66. "battery_voltage":power.measured_volts,
  67. "current_drawn":power.measured_amps,
  68. "lat":31,
  69. "lng":39
  70. }
  71. conn.send((json.dumps(data, ensure_ascii=False)+"\n").encode('gbk'))
  72. except BrokenPipeError:
  73. print("[INFO]: Client disconnected")
  74. stop()
  75. break
  76. except KeyboardInterrupt:
  77. stop()
  78. s.close()