Advertisement
silver2row

trying to understand why this works as is but not as a dict object, set object, etc.

Oct 30th, 2022
1,549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from ServoOne import *
  4. from time import sleep
  5. from flask import Flask, render_template
  6.  
  7. # create instance of ServoCtrl on i2c bus 2 at address 0x7f using a 60Hz or 50Hz pwm frequency.
  8. # 0x7f for servo cape
  9. servos = ServoCtrl(2, 0x7f, 60)
  10.  
  11. # add a servo to controller, using channel 0 - 15.  servo max rotation is 180 degrees;
  12. servos.add_servo("1a", 0, 180)
  13. servos.add_servo("1b", 1, 180)
  14.  
  15. servos.add_servo("2c", 3, 180)
  16. servos.add_servo("2d", 4, 180)
  17.  
  18. servos.add_servo("3e", 6, 180)
  19. servos.add_servo("3f", 7, 180)
  20.  
  21. servos.add_servo("4g", 9, 180)
  22. servos.add_servo("4h", 10, 180)
  23.  
  24. servos.enablePWMs(True)
  25.  
  26. #pwm_controller.set_pwm_frequency(50)
  27.  
  28. app = Flask(__name__)
  29. @app.route('/')
  30. @app.route('/<state>')
  31.  
  32. def updates(state = None):
  33.     if state == "0":
  34.         servos.set_servo_angle("1a", 0)
  35.         sleep(0.18)
  36.         servos.set_servo_angle("1b", 0)
  37.         sleep(0.18)
  38.         servos.set_servo_angle("2c", 0)
  39.         sleep(0.18)
  40.         servos.set_servo_angle("2d", 0)
  41.         sleep(0.18)
  42.         servos.set_servo_angle("3e", 0)
  43.         sleep(0.18)
  44.         servos.set_servo_angle("3f", 0)
  45.         sleep(0.18)
  46.         servos.set_servo_angle("4g", 0)
  47.         sleep(0.18)
  48.         servos.set_servo_angle("4h", 0)
  49.         sleep(0.18)
  50.  
  51.     # servos.set_servo_angle("another servo",servos.get_servo_max_angle("another servo")/2)
  52.     # Set the servo(s) to halfway of angle 0...
  53.     if state == "50":
  54.         servos.set_servo_percent("1a", 50)
  55.         sleep(0.18)
  56.         servos.set_servo_percent("1b", 50)
  57.         sleep(0.18)
  58.         servos.set_servo_percent("2c", 50)
  59.         sleep(0.18)
  60.         servos.set_servo_percent("2d", 50)
  61.         sleep(0.18)
  62.         servos.set_servo_percent("3e", 50)
  63.         sleep(0.18)
  64.         servos.set_servo_percent("3f", 50)
  65.         sleep(0.18)
  66.         servos.set_servo_percent("4g", 50)
  67.         sleep(0.18)
  68.         servos.set_servo_percent("4h", 50)
  69.         sleep(0.18)
  70.  
  71.     # move servo3 to max rotation of 180 degrees
  72.     if state == "max":
  73.         servos.set_servo_percent("1a", 180)
  74.         sleep(0.18)
  75.         servos.set_servo_percent("1b", 180)
  76.         sleep(0.18)
  77.         servos.set_servo_percent("2c", 180)
  78.         sleep(0.18)
  79.         servos.set_servo_percent("2d", 180)
  80.         sleep(0.18)
  81.         servos.set_servo_percent("3e", 180)
  82.         sleep(0.18)
  83.         servos.set_servo_percent("3f", 180)
  84.         sleep(0.18)
  85.         servos.set_servo_percent("4g", 180)
  86.         sleep(0.18)
  87.         servos.set_servo_percent("4h", 180)
  88.         sleep(0.18)
  89.  
  90. #        servos.set_servo_angle("1a", servos.get_servo_max_angle("1a"))
  91. #        sleep(1)
  92. #        servos.set_servo_angle("1b", servos.get_servo_max_angle("1b"))
  93. #        sleep(1)
  94.  
  95.     # Stop everything moving for good!
  96.     if state == "stop":
  97.         servos.enablePWMs(False)
  98.         sleep(1)
  99.  
  100.     # Start moving everything again!
  101.     if state == "start":
  102.         servos.enablePWMs(True)
  103.         sleep(1)
  104.  
  105.     template_data = {
  106.         "title" : state,
  107.     }
  108.     return render_template("ServoOne.html", **template_data)
  109.  
  110. if __name__ == "__main__":
  111.     app.run(host="0.0.0.0", port=5000, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement