kennyho-rass

flaskservo.py

Nov 11th, 2021
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. from flask import Flask, render_template_string, request
  2. from time import sleep
  3. import pigpio
  4. from OpenSSL import SSL
  5.  
  6. # This HTML page + JS will be served when the root path is requested with a GET
  7. TPL = '''
  8. <html>
  9. <body>
  10. <button id="connectbtn" onclick="requestPermission()">Connect</button>
  11. <div id="outputdiv"></div>
  12. </body>
  13. <script>
  14. var output = document.getElementById("outputdiv");
  15. var btn = document.getElementById("connectbtn");
  16. var socket = false;
  17. var vertical = 0;
  18. var horizontal = 0;
  19. function sendToFlask()
  20. {
  21. const xmlhttpreq = new XMLHttpRequest();
  22. const data = new FormData();
  23. data.append("updown", vertical);
  24. data.append("leftright", horizontal);
  25. xmlhttpreq.open("POST", "campan");
  26. xmlhttpreq.send(data);
  27. }
  28. function handleRotation(event)
  29. {
  30. vertical = Math.round(event.gamma);
  31. horizontal = Math.round(event.beta);
  32. }
  33. function requestPermission()
  34. {
  35. if (typeof(DeviceMotionEvent) !== 'undefined' && typeof(DeviceMotionEvent.requestPermission) ===
  36. 'function')
  37. {
  38. DeviceMotionEvent.requestPermission().
  39. then(response => {
  40. if (response === 'granted') {
  41. window.addEventListener('deviceorientation', handleRotation);
  42. }
  43. finishRequest();
  44. }).catch(console.error);
  45. }
  46. else
  47. {
  48. window.addEventListener('deviceorientation', handleRotation);
  49. finishRequest();
  50. }
  51. }
  52. function finishRequest()
  53. {
  54. setInterval(Flask, 200);
  55. }
  56. // schedule the first one.
  57. setTimeout(processVideo, 0);
  58. </script>
  59. </html>
  60. '''
  61.  
  62. # The servos are connected on these GPIO pins (BCM numbering)
  63. HORIZ_SERVO_PORT = 13
  64. VERT_SERVO_PORT= 11
  65.  
  66. HORIZ_SERVO_CENTER = 1750
  67. VERT_SERVO_CENTER = 1500
  68.  
  69. # Create Flask App
  70. app = Flask(__name__)
  71.  
  72. # GPIO Info
  73. gpio = pigpio.pi()
  74.  
  75. def setupGPIO():
  76.     gpio.set_servo_pulsewidth(HORIZ_SERVO_PORT, 0)
  77.     gpio.set_servo_pulsewidth(VERT_SERVO_PORT, 0)
  78.  
  79. def setServoDuty(servo, duty):
  80.     gpio.set_servo_pulsewidth(servo, duty)
  81.  
  82. def clamp(num, minimum, maximum):
  83.     return max(min(num, maximum), minimum)
  84.  
  85. # Serve the HTML file when the root path is requested
  86. @app.route("/")
  87. def serveRoot():
  88.     return render_template_string(TPL)
  89.  
  90. # Expose an endpoint for sending the servo coordinates
  91. # from the JS to the Flask Backend
  92. @app.route("/moveservos", methods=["POST"])
  93. def moveServos():
  94.     # Get the values from the request
  95.     horizontal = 25 * int(request.form["updown"])
  96.     vertical = 25 * int(request.form["leftright"])
  97.     print(str(horizontal) + ", " + str(vertical))
  98.  
  99.     # Move the Servos
  100.     setServoDuty(HORIZ_SERVO_PORT, clamp(HORIZ_SERVO_CENTER - horizontal, 500, 2500))
  101.     setServoDuty(VERT_SERVO_PORT, clamp(VERT_SERVO_CENTER - vertical, 500, 2500))
  102.  
  103.     # Wait for 0.2s so that the servos have time to move
  104.     sleep(0.2)
  105.  
  106.     # Stop the servo motors to save energy and reduce noise
  107.     gpio.set_servo_pulsewidth(HORIZ_SERVO_PORT, 0)
  108.     gpio.set_servo_pulsewidth(VERT_SERVO_PORT, 0)
  109.  
  110.     # Return empty request (Should return a 200 OK with an empty body)
  111.     return ""
  112.  
  113. # Run the app on the local development server
  114.  
  115. # Accept any IP address
  116. # Create ad-hoc SSL encryption (needed for iOS 13 support)
  117. if __name__ == "__main__":
  118.     app.run(host="192.168.0.157", port=5001, ssl_context='')
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment