def __init__(self, i_min, i_max, freq, L298s=[], Dios=[], Inv=[]): PCA9685.__init__(self) self.freq = freq self.set_pwm_freq(self.freq) self.i_min = i_min self.i_max = i_max self.L298Chs = L298s self.Dios = Dios self.Inv = Inv self.FSave = [] self.imp_tab = [] self.invert_input = [] self.ChTidef = [], [] # Fills the fail_safe position list with default values for i in range(16): self.FSave.append(127) (self.ix_min, self.ix_diff) = range(2) # Fills the channel time behavior list with default values for i in range(16): self.ChTidef[self.ix_min].append(self.imp_val(self.i_min)) self.ChTidef[self.ix_diff].append( self.diff_val(self.i_min, self.i_max)) self.calc_puls_table() # update of pulstable for LM298 purposes i = 0 for i in range(len(self.L298Chs)): if (i % 3 == 0): self.calc_puls_table_L298(self.L298Chs[i]) self.create_invert_table()
def __init__(self, i2c, address=0x40, freq=50, min_us=600, max_us=2400, degrees=180): PCA9685.__init__( self, i2c=i2c, address=address) self.period = 1000000 / freq self.min_duty = self._us2duty(min_us) self.max_duty = self._us2duty(max_us) self.degrees = degrees # Init the freq on PCA9685 self.freq(freq)
def __init__(self): self.i2c = I2C(id=self.id, sda=self.sda, scl=self.scl) self.pca9685 = PCA9685(self.i2c) back_left = Leg(self.i2c, pca9685=self.pca9685, name="BACK_LEFT", shoulder_pin=Limbs.BACK_LEFT_LEG, foot_pin=Limbs.BACK_LEFT_FOOT) back_right = Leg(self.i2c, pca9685=self.pca9685, name="BACK_RIGHT", shoulder_pin=Limbs.BACK_RIGHT_LEG, foot_pin=Limbs.BACK_RIGHT_FOOT) front_left = Leg(self.i2c, pca9685=self.pca9685, name='FRONT_LEFT', shoulder_pin=Limbs.FRONT_LEFT_LEG, foot_pin=Limbs.FRONT_LEFT_FOOT) front_right = Leg(self.i2c, pca9685=self.pca9685, name='FRONT_RIGHT', shoulder_pin=Limbs.FRONT_RIGHT_LEG, foot_pin=Limbs.FRONT_RIGHT_FOOT) self.legs.append(front_left) self.legs.append(front_right) self.legs.append(back_left) self.legs.append(back_right) print("***", self.name, "is Online ***")
def init_hardware(self): # init Servo-Treiber self.pca = PCA9685() self.pca.frequeny = 50 self.pca.init_servo(self.id_left, 320, 2140, 177) self.pca.init_servo(self.id_right, 320, 2140, 177) self.pca.init_servo(self.id_stift, 320, 2140, 177)
def __init__(self, config, lsm): self.pwm = PCA9685(0x40) self.pwm.set_pwm_freq(60) self.lsm = lsm self.high_accuracy = False self.pan_channel = 0 self.tilt_channel = 1 self.target_degrees = 90 self.elevation = 90 self.pan_servo_max = 568 self.pan_servo_min = 258 self.tilt_servo_max = 415 # 0 degrees self.tilt_servo_min = 343 # 90 degrees self.tracking = False self.log = logging.getLogger('boresight') self._running = True self._worker = threading.Thread(target=self._servo_worker) self._worker.setDaemon(True) self._worker.start()
def __init__(self): self.i2c = I2C(id=self.id, sda=self.sda, scl=self.scl) self.pca9685 = PCA9685(self.i2c) left = Leg(self.i2c, pca9685=self.pca9685, name='LEFT', shoulder_pin=Limbs.LEFT_LEG, foot_pin=Limbs.LEFT_FOOT) right = Leg(self.i2c, pca9685=self.pca9685, name='RIGHT', shoulder_pin=Limbs.RIGHT_LEG, foot_pin=Limbs.RIGHT_FOOT) self.legs.append(left) self.legs.append(right) print("***", self.name, "is Online ***")
def main(): driver = PCA9685() driver.set_pwm_freq(60) time.sleep(3) for i in range(300, 500, 5): print(i) driver.set_pwm(0, 0, i) time.sleep(0.1) for i in range(500, 299, -5): print(i) driver.set_pwm(0, 0, i) time.sleep(0.1)
def __init__(self, L2=8, L3=8): # 创建一个I2C对象 i2c = I2C(scl=Pin(config['I2C_SCL']), sda=Pin(config['I2C_SDA']), freq=config['I2C_FREQUENCY']) # 初始化PCA9685对象 pca9685 = PCA9685(i2c, config['PCA9685_ADDRESS']) pca9685.freq(config['PCA9685_PWM_FREQUENCY']) self.L2 = L2 # MK1 Link2的长度 self.L3 = L3 # MK2 Link3的长度 self.joint1 = Joint1(pca9685) # 定义Joint1 self.joint2 = Joint2(pca9685) # 定义Joint2 self.joint3 = Joint3(pca9685) # 定义Joint3 self.gripper = Gripper(pca9685) # 机械爪 self.x = 0 self.y = 0 self.z = 0 self.init_joints()
""" Demonstrate how to use PWM output on PCA9685 """ from machine import I2C from pca9685 import PCA9685 from time import sleep i2c = I2C( 2 ) pca = PCA9685( i2c, freq=1200 ) # PWM frequency # Set the output 8 @ 50% duty cycle (half of 4095) pca.duty( 8, 2048 ) sleep( 1 ) # Set the output 8 @ 25% duty cycle (quarter of 4095) pca.duty( 8, 1024 ) sleep( 1 ) # duty sycle can also be controled with duty_percent() # set duty cycle @ 33% on output 8 pca.duty_percent( 8, 33 )
def __init__( self, inner_arm=8, # the lengths of the arms outer_arm=8, servo_1_centre=1500, # shoulder motor centre pulse-width servo_2_centre=1500, # elbow motor centre pulse-width servo_1_angle_pws=[], # pulse-widths for various angles servo_2_angle_pws=[], servo_1_degree_ms=-10, # milliseconds pulse-width per degree servo_2_degree_ms=10, # reversed for the mounting of the elbow servo arm_1_centre=-60, arm_2_centre=90, hysteresis_correction_1=0, # hardware error compensation hysteresis_correction_2=0, bounds=[-8, 4, 6, 13], # the maximum rectangular drawing area wait=None, virtual_mode = False, pw_up=1500, # pulse-widths for pen up/downx` pw_down=500, servo1_pin=14, servo2_pin=15 ): self.servo1_pin = servo1_pin self.servo2_pin = servo2_pin # set the pantograph geometry self.INNER_ARM = inner_arm self.OUTER_ARM = outer_arm self.virtual_mode = virtual_mode or force_virtual_mode # the box bounds describe a rectangle that we can safely draw in self.bounds = bounds # if pulse-widths to angles are supplied for each servo, we will feed them to # numpy.polyfit(), to produce a function for each one. Otherwise, we will use a simple # approximation based on a centre of travel of 1500µS and 10µS per degree self.servo_1_centre = servo_1_centre self.servo_1_degree_ms = servo_1_degree_ms self.arm_1_centre = arm_1_centre self.hysteresis_correction_1 = hysteresis_correction_1 self.servo_2_centre = servo_2_centre self.servo_2_degree_ms = servo_2_degree_ms self.arm_2_centre = arm_2_centre self.hysteresis_correction_2 = hysteresis_correction_2 # TODO : 解決偏移的問題 if servo_1_angle_pws: servo_1_shift = self.arm_1_centre # 修正角度 new = [] for angle, pwf in servo_1_angle_pws: new.append((angle + servo_1_shift, pwf)) servo_1_angle_pws = new servo_1_array = numpy.array(servo_1_angle_pws) self.angles_to_pw_1 = numpy.poly1d( numpy.polyfit( servo_1_array[:,0], servo_1_array[:,1], 3 ) ) else: self.angles_to_pw_1 = self.naive_angles_to_pulse_widths_1 servo_2_pwf_shift = 10 if servo_2_angle_pws: servo_2_shift = self.arm_2_centre # 修正角度 new = [] for angle, pwf in servo_2_angle_pws: new.append(((angle + servo_2_shift-180)*-1 , pwf+servo_2_pwf_shift)) servo_2_angle_pws = new servo_2_array = numpy.array(servo_2_angle_pws) self.angles_to_pw_2 = numpy.poly1d( numpy.polyfit( servo_2_array[:,0], servo_2_array[:,1], 3 ) ) else: self.angles_to_pw_2 = self.naive_angles_to_pulse_widths_2 self.dumpAngle2PWF() if self.virtual_mode: print("Initialising virtual BrachioGraph") self.virtual_pw_1 = self.angles_to_pw_1(-90) self.virtual_pw_2 = self.angles_to_pw_2(90) # by default in virtual mode, we use a wait factor of 0 for speed self.wait = wait or 0 print(" Pen is up") print(" Pulse-width 1", self.virtual_pw_1) print(" Pulse-width 2", self.virtual_pw_2) else: # instantiate this Raspberry Pi as a pigpio.pi() instance or use PCA9685 to control servo if USE_HAT: self.rpi = PCA9685(0x40, debug=False) self.rpi.setPWMFreq(50) else: self.rpi = pigpio.pi() # the pulse frequency should be no higher than 100Hz - higher values could (supposedly) damage the servos if USE_HAT: pass # PCA9685 Frequency is set up as 50HZ else: self.rpi.set_PWM_frequency(14, 50) self.rpi.set_PWM_frequency(15, 50) # reate the pen object, and make sure the pen is up if USE_HAT: pin = [15, 14] else: pin = 18 self.pen = Pen(pin=pin, bg=self, pw_up=pw_up, pw_down=pw_down, virtual_mode=self.virtual_mode) # Initialise the pantograph with the motors in the centre of their travel self.rpi.set_servo_pulsewidth(self.servo1_pin, self.angles_to_pw_1(self.arm_1_centre)) sleep(0.3) self.rpi.set_servo_pulsewidth(self.servo2_pin, self.angles_to_pw_2(self.arm_2_centre)) sleep(0.3) # by default we use a wait factor of 0.1 for accuracy self.wait = wait or .1 # Now the plotter is in a safe physical state. # Set the x and y position state, so it knows its current x/y position. self.current_x = -self.INNER_ARM self.current_y = self.OUTER_ARM self.reset_report() self.previous_pw_1 = self.previous_pw_2 = 0 self.active_hysteresis_correction_1 = self.active_hysteresis_correction_2 = 0
#!/usr/bin/python3 import argparse from pca9685 import PCA9685 parser = argparse.ArgumentParser() group1 = parser.add_mutually_exclusive_group(required=True) group1.add_argument('--get', action='store', nargs='+', type=int) group1.add_argument('--set', action='store', nargs='+', type=int) args = parser.parse_args() pca = PCA9685() pca.output_enable() if args.get: for channel in args.get: print(f'channel {channel} pwm: {pca.pwm[channel]}') else: for channel in args.set[:-1]: pca.pwm[channel] = args.set[-1]
s = usocket.socket() #初始化socket服务对象 s_addr = ('192.168.137.1', 10000) #服务器IP和端口 ############################################## # I2C外设初始化 ############################################## #OLED初始化 i2c1 = I2C(sda=Pin(13), scl=Pin(14)) # sda-->13, scl-->14 oled = SSD1306_I2C(128, 64, i2c1, addr=0x3c) #OLED显示屏初始化:128*64分辨率,OLED的I2C地址是0x3c oled.text("OLED OK", 0, 0) #写入第1行内容 oled.show() #OLED执行显示 #电机控制模块初始化I2C,pca9685芯片 i2c2 = I2C(sda=Pin(16, Pin.OUT, Pin.PULL_UP), scl=Pin(17, Pin.OUT, Pin.PULL_UP), freq=40000) pca = PCA9685(i2c2) #初始化PCA对象 pca.reset() #复位pca pca.freq(freq=1000) # set freq = 1000Hz oled.text("PWM OK", 0, 20) #写入第2行内容 oled.show() #OLED执行显示 #陀螺仪初始化 i2c3 = I2C(scl=Pin(15), sda=Pin(27)) #陀螺仪 scl->21 sda->33 accelerometer = mpu6050.accel(i2c3) #初始化陀螺仪对象 #电子罗盘初始化 qmc = QMC5883L() #初始化电子罗盘对象 ############################################## # 服务函数 ##############################################
#!/usr/bin/env python from pca9685 import PCA9685 pca9685 = PCA9685() pca9685.set_pwm_value(1, pulse=430)
from pca9685 import PCA9685 from machine import I2C i2c = I2C(2, freq=100000) pca = PCA9685(i2c, 0x40, 50) pca.setAllPWM(0, 160) pca.setPWM(0, 0, 160) pca.setPWM(4, 0, 160) pca.setServoMin(0, 120) pca.setServoMax(0, 240) pca.Servo(0, -90) #通道4测试 pca.setPWM(4, 0, 140) #min pca.setPWM(4, 0, 345) #cen pca.setPWM(4, 0, 550) #max pca.calibration(4, 140, 550, 345) pca.Servo(4, 45) pca.Servo(4, 0) pca.Servo(4, -45) #通道5测试 pca.setPWM(5, 0, 140) #min pca.setPWM(5, 0, 345) #cen pca.setPWM(5, 0, 550) #max pca.calibration(5, 140, 550, 345)
from machine import Pin, I2C from pca9685 import PCA9685 from servos import PCASG90 from configs import config # 创建一个I2C对象 i2c = I2C(scl=Pin(config['I2C_SCL']), sda=Pin(config['I2C_SDA']), freq=config['I2C_FREQUENCY']) # 初始化PCA9685对象 pca9685 = PCA9685(i2c, config['PCA9685_ADDRESS']) pca9685.freq(config['PCA9685_PWM_FREQUENCY']) # 底部舵机 servo_btm = PCASG90(pca9685, 0, min_angle=0, max_angle=180) #servo_btm.set_angle(40) # 左侧舵机 servo_left = PCASG90(pca9685, 1, min_angle=30, max_angle=150) #servo_left.set_angle() # 右侧舵机 servo_right = PCASG90(pca9685, 2, min_angle=18, max_angle=120) #servo_right.set_angle(18) servo_right.pulse_width(2000) # 爪子舵机 servo_crew = PCASG90(pca9685, 3, min_angle=100, max_angle=150) #servo_crew.set_angle(100)
def __init__(self): self.pca9685 = PCA9685() self.const_throttle = rospy.get_param("/CONST_THROTTLE")
from pca9685 import PCA9685 from evdev import InputDevice from select import select import time from led import ledShark, ledLight import threading from run import go pwm = PCA9685(0x40, debug=True) pwm.setPWMFreq(50) def setPwmAngle(channel, angle): if channel == 0: angle = int(angle / 270 * 2000) + 500 else: angle = int(angle / 180 * 2000) + 500 pwm.setServoPulse(channel, angle) def control(): angle_0 = 100 angle_1 = 90 diff = 20 time_delay = 0.25 setPwmAngle(0, angle_0) setPwmAngle(1, angle_1) time.sleep(0.5) dev = InputDevice('/dev/input/event0') while True: select([dev], [], []) for event in dev.read(): if event.value == 1: