def init_servos(address): i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() pwm = PWM(bus, address) pwm.set_pwm_freq(60) pwm.output_enable() # cycle the arms on channels 0, 2, 4 print('cycling through all valid positions') for x in (175, 300, 425, 550): pwm.set_pwm(0, 0, x) sleep(1) pwm.set_pwm(2, 0, x) sleep(1) pwm.set_pwm(4, 0, x) sleep(2) return pwm
def init_servos(address): i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() pwm = PWM(bus, address) pwm.set_pwm_freq(60) pwm.output_enable() # cycle the arms on channels 0, 2, 4 print("cycling through all valid positions") for x in (175, 300, 425, 550): pwm.set_pwm(0, 0, x) sleep(1) pwm.set_pwm(2, 0, x) sleep(1) pwm.set_pwm(4, 0, x) sleep(2) return pwm
def main(): monitor = PeakMonitor(SINK_NAME, METER_RATE) i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() pwm = PWM(bus, 0x40) # Set PWM frequency to 60 Hz pwm.set_pwm_freq(60) pwm.output_enable() for sample in monitor: sample = sample >> DISPLAY_SCALE step = ((4000 - 0) / 100.0) sample = int(step * sample) print ' %3d' % sample sys.stdout.flush() pwm.set_pwm(8, 0, (randint(0,1) * sample)) pwm.set_pwm(7, 0, (randint(0,1) * sample)) pwm.set_pwm(6, 0, (randint(0,1) * sample))
import subprocess import oracle limits = { "left-eye":(14, 200, 400), "right-eye":(15, 180, 360), "left-brow":(12, 260, 460), "right-brow":(13, 380, 560), "mouth1":(6, 0, 4000), "mouth2":(7, 0, 4000), "mouth3":(8, 0, 4000) } bus = ABEHelpers().get_smbus() pwm = PWM(bus, 0x40) pwm.set_pwm_freq(60) pwm.output_enable() app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" @app.route("/turn/<name>/<int:value>", methods=['GET']) def turn_eye(name, value): index = limits[name] scaled = scale_value(name, value) pwm.set_pwm(index[0], 0, scaled) return "Turn %s %d Scaled value: %s" % (name, value, scaled)
================================================ ABElectronics Servo Pi pwm controller | PWM output demo Version 1.0 18/11/2014 run with: sudo python demo-pwm.py ================================================ This demo shows how to set a 1KHz output frequency and change the pulse width between the minimum and maximum values """ # create an instance of the ABEHelpers class and use it # to find the correct i2c bus i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() # create an instance of the PWM class on i2c address 0x40 pwm = PWM(bus, 0x40) # Set PWM frequency to 1 Khz and enable the output pwm.set_pwm_freq(1000) pwm.output_enable() while (True): for x in range(1, 4095, 5): pwm.set_pwm(0, 0, x) #time.sleep(0.01) for x in range(4095, 1, -5): pwm.set_pwm(0, 0, x) #time.sleep(0.01)
================================================ This demo shows how to set the limits of movement on a servo and then move between those positions """ i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() pwm = PWM(bus, 0x40) # set the servo minimum, centre and maximum limits servoMin = 250 # Min pulse length out of 4096 servoMed = 400 # Min pulse length out of 4096 servoMax = 500 # Max pulse length out of 4096 # Set PWM frequency to 60 Hz pwm.set_pwm_freq(60) pwm.output_enable() while (True): # Move servo on port 0 between three points pwm.set_pwm(0, 0, servoMin) time.sleep(0.5) pwm.set_pwm(0, 0, servoMed) time.sleep(0.5) pwm.set_pwm(0, 0, servoMax) time.sleep(0.5) # use set_all_pwm to set PWM on all outputs
Version 1.0 Created 29/02/2015 Requires python 3 smbus to be installed run with: sudo python3 demo-pwm.py ================================================ This demo shows how to set a 1KHz output frequency and change the pulse width between the minimum and maximum values """ # create an instance of the ABEHelpers class and use it # to find the correct i2c bus i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() # create an instance of the PWM class on i2c address 0x40 pwm = PWM(bus, 0x40) # Set PWM frequency to 1 Khz and enable the output pwm.set_pwm_freq(1000) pwm.output_enable() while (True): for x in range(1, 4095, 5): pwm.set_pwm(0, 0, x) #time.sleep(0.01) for x in range(4095, 1, -5): pwm.set_pwm(0, 0, x) #time.sleep(0.01)
class servo_robot(object): def __init__(self): pass self.set_robot(ergo_servo_config) def set_robot(self, servo_configuration_robot): self.servo_robot = servo_configuration_robot self.i2c_helper = ABEHelpers() self.bus = self.i2c_helper.get_smbus() self.pwm = PWM(self.bus, 0x6f) # take care to know the real port self.pwm.output_enable() self.pwm.set_pwm_freq(60) #self.pwm.set_pwm(0, 0, servoMed) #self.goto_dic_position(a) def example_move(self): while (True): a = [ 300, 450 ]# pulses self.goto_dic_position(a) time.sleep(1) a = [ 450, 450 ] self.goto_dic_position(a) time.sleep(1) a = [ 600, 450 ] self.goto_dic_position(a) time.sleep(1) def gira(self , elemento , angulo): pass def set_position(self , a ): pass #t = [a[0],a[1],a[2],a[3],a[4], a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13],a[14],0,0,0] #goto_dic_position(robot, t , 0.5) #self.goto_dic_position( t , 0.5) #time.sleep(2) def goto_dic_position_anu( self, my_array, f = 0 ): item = 0 for key, value in self.servo_robot["motors"].iteritems(): grade_servo = my_array[item] # en grados new_value_servo = 400 max_value_servo = value["max"] min_value_servo = value["min"] if grade_servo < 0: new_value_servo= 300/180*(grade_servo + 180) + 100 if new_value_servo < min_value_servo: new_value_servo = min_value_servo if grade_servo > 0: new_value_servo = 300/180*( grade_servo) + 400 if new_value_servo > max_value_servo: new_value_servo = max_value_servo # -180 es 100 # 0 es 400 # 180 es 700 item += 1 num_servo = value["servo"] #print new_value_servo self.pwm.set_pwm(num_servo, 0, new_value_servo) def goto_dic_position( self, my_array, f = 0 ): #my_arry [ pulse length out of 4096 , ... ] item = 0 for key, value in self.servo_robot["motors"].iteritems(): grade_servo = my_array[item] # en grados max_value_servo = value["max"] min_value_servo = value["min"] if grade_servo < min_value_servo: grade_servo = min_value_servo if grade_servo > max_value_servo: grade_servo = max_value_servo # -180 es 100 # 0 es 400 # 180 es 700 item += 1 num_servo = value["servo"] #print new_value_servo self.pwm.set_pwm(num_servo, 0, grade_servo)