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.

99 lines
3.6 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
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. moving = False
  14. motor1 = ev3.LargeMotor("outA")
  15. motor2 = ev3.LargeMotor("outD")
  16. cam = ev3.MediumMotor('outB')
  17. def move(speed):
  18. moving = True
  19. motor1.run_forever(speed_sp=speed)
  20. motor2.run_forever(speed_sp=speed)
  21. cam.run_to_abs_pos(position_sp=0, speed_sp=500)
  22. def stop():
  23. moving = False
  24. motor1.stop()
  25. motor2.stop()
  26. cam.stop()
  27. HOST = '0.0.0.0'
  28. PORT = 3131
  29. pid=os.fork()
  30. if pid==0: # new process
  31. mjpeg_loc = "/home/robot/mjpg-streamer/"
  32. os.system('{0}mjpg_streamer -i "{0}input_uvc.so -f 5" -o "{0}output_http.so -w {0}www"'.format(mjpeg_loc))
  33. exit()
  34. power = PowerSupply()
  35. if power.measured_volts < 7.1:
  36. print("Change the battery.")
  37. exit()
  38. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  39. try:
  40. s.bind((HOST, PORT))
  41. while True:
  42. s.listen()
  43. print("[INFO]: Listening...")
  44. conn, addr = s.accept()
  45. with conn:
  46. print('[INFO]: Connected by', addr)
  47. while True:
  48. if cam.speed == 0 and moving:
  49. if state:
  50. cam.run_to_abs_pos(position_sp=4000, speed_sp=500)
  51. else:
  52. cam.run_to_abs_pos(position_sp=0, speed_sp=500)
  53. state = not state
  54. try:
  55. recieved = conn.recv(1024)
  56. if not recieved:
  57. raise BrokenPipeError
  58. else:
  59. recieved = recieved.decode().strip()[-1]
  60. print(recieved)
  61. if recieved == 's':
  62. stop()
  63. elif recieved == 'm':
  64. move(200)
  65. elif recieved == 'i':
  66. data = {
  67. "battery_voltage":power.measured_volts,
  68. "current_drawn":power.measured_amps,
  69. "lat":31,
  70. "lng":39
  71. }
  72. conn.send((json.dumps(data, ensure_ascii=False)+"\n").encode('gbk'))
  73. elif recieved == '+':
  74. cam.run_timed(time_sp=100, speed_sp=750)
  75. elif recieved == '-':
  76. cam.run_timed(time_sp=100, speed_sp=-750)
  77. elif recieved == 'f':
  78. stop()
  79. motor1.run_forever(speed_sp=400)
  80. motor2.run_forever(speed_sp=400)
  81. elif recieved == 'b':
  82. stop()
  83. motor1.run_forever(speed_sp=-400)
  84. motor2.run_forever(speed_sp=-400)
  85. elif recieved == 'l':
  86. motor1.run_forever(speed_sp=400)
  87. elif recieved == 'r':
  88. motor2.run_forever(speed_sp=400)
  89. except BrokenPipeError:
  90. print("[INFO]: Client disconnected")
  91. stop()
  92. break
  93. except KeyboardInterrupt:
  94. stop()
  95. s.close()