Advertisement
silver2row

pwms.py

Nov 18th, 2022
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # import sys
  4. from Servo import *
  5. from time import sleep
  6.  
  7. # create instance of ServoCtrl on i2c bus 2 at address 64 using 60hz pwm frequency.
  8. # change 64 to 0x7f for servo cape
  9. servos = ServoCtrl(2, 0x7f, 50)
  10.  
  11.  
  12. # add a servo to controller, using channel 15. servo max rotation is 180 degrees.
  13. servos.addServos([Servo(name = 'servo1', channel = 0, max_angle = 180),
  14.                     Servo(name = 'servo2', channel = 1, max_angle = 180),
  15.                     Servo(name = 'servo3', channel = 2, max_angle = 180),
  16.                     Servo(name = 'servo4', channel = 3, max_angle = 180),
  17.                     Servo(name = 'servo5', channel = 4, max_angle = 180),
  18.                     Servo(name = 'servo6', channel = 5, max_angle = 180)
  19.                    ])
  20.  
  21.  
  22. # add some servos using JSON notation
  23. servos.addServosJSON([{'name':'servo9', 'channel':8, 'max_angle':180},
  24.                       {'name':'servo10', 'channel':9, 'max_angle':180},
  25.                       {'name':'servo11', 'channel':10, 'max_angle':180},
  26.                       {'name':'servo12', 'channel':11, 'max_angle':180},
  27.                       {'name':'servo13', 'channel':12, 'max_angle':180},
  28.                       {'name':'servo14', 'channel':13, 'max_angle':180}
  29.                     ])
  30.  
  31. servos.enablePWMs(True)
  32.  
  33. print(servos.servos)
  34.  
  35. for i in range(0, 30, 2):
  36.     # move servo15 to 0 degrees and servo2 to 90 degrees.
  37.     servos.moveServos([ServoPos(name = "servo1", angle = 0),
  38.                         ServoPos(name = "servo2", angle = 0),
  39.                         ServoPos(name = "servo3", angle = 0),
  40.                         ServoPos(name = "servo4", angle = 0),
  41.                         ServoPos(name = "servo5", angle = 0),
  42.                         ServoPos(name = "servo6", angle = 0)
  43.                      ])
  44.     sleep(0.25)
  45.  
  46.     # move both servos to 50%
  47.     servos.moveServosJSON([{'name':"servo9", 'percent':50},
  48.                            {'name':"servo10", 'percent':50},
  49.                            {'name':"servo11", 'percent':50},
  50.                            {'name':"servo12", 'percent':50},
  51.                            {'name':"servo13", 'percent':50},
  52.                            {'name':"servo14", 'percent':50}
  53.                         ])
  54.     sleep(0.25)
  55.  
  56.     #move servo15 to max rotation and servo2 to 0
  57.  
  58.     # create a list and then append moves to it.
  59.     #servolist = []
  60.     #servolist.append(ServoPos(name = "servo1", angle = servos.get_servo_max_angle("servo1")))
  61.     #servolist.append(ServoPos(name = "servo2", angle = 0) )
  62.  
  63.     #servos.moveServos(servolist)
  64.     #sleep(1)
  65.  
  66.  
  67. # use JSON to move servos
  68. #for l in range(0, 2):
  69.     servos.moveServosJSON([{'name':"servo9", 'angle':80},
  70.                              {'name':"servo10", 'angle':80},
  71.                              {'name':"servo11", 'angle':80},
  72.                              {'name':"servo12", 'angle':80},
  73.                              {'name':"servo13", 'angle':80},
  74.                              {'name':"servo14", 'angle':80}
  75.                          ])
  76.     sleep(0.25)
  77.  
  78.     # move both servos to 90%
  79.     servos.moveServos([ServoPos(name = "servo1", percent = 90),
  80.                              ServoPos(name = "servo2", percent = 90),
  81.                              ServoPos(name = "servo3", percent = 90),
  82.                              ServoPos(name = "servo4", percent = 90),
  83.                              ServoPos(name = "servo5", percent = 90),
  84.                              ServoPos(name = "servo6", percent = 90)
  85.                          ])
  86.     sleep(0.25)
  87.  
  88.     # move servo15 to max rotation and servo2 to 0
  89. #    servos.moveServosJSON([{'name':"servo1", 'angle':servos.get_servo_max_angle("servo1")},
  90. #                             {'name':"servo2", 'angle':0}
  91. #                        ])
  92. #    sleep(1)
  93.  
  94. servos.enablePWMs(False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement