Page 1 of 1

Adafruit PWM PCA9685 Frequency Help

Posted: Tue May 01, 2018 12:24 pm
by Innyve89
Using this as a base:
https://learn.adafruit.com/adafruit-16- ... -reference

I made code to control different servos using the PCA9685 board. However two boards need a frequency of 1000 hz and two boards need it to be 50 hz. When I try to change it to 50 hz later on in the code to handle the 50hz motors, it changes it for the all of them.The jist of my code is that it reads a text file that was generated by a user input and then moves the motors according to what button was pressed. Is there a way to go about this?

Code: Select all

#!/usr/bin/python
from evdev import InputDevice, categorize, ecodes
import Adafruit_PCA9685

pwm = Adafruit_PCA9685.PCA9685()
# Set frequency to 60hz, good for servos.

pwm.set_pwm_freq(1000);


#Accessing a text file - www.101computing.net/mp3-playlist/
file = open("held_btns.txt","r+")
text = file.read().strip()
btn = text.split(',')
file.truncate(0)
#
if((btn[1] == 'true') & (btn[16] == 'true')):
  btn[16] = 'false';
  print('option2')
elif((btn[1] == 'true') & (btn[16] == 'false')):
  btn[16] = 'true';
  print('option1')



seq = ',';
btn_merged = seq.join(btn)
file.seek(0,0)
file.write(btn_merged)
file.close()
print(btn)



armL = 0;
armR = 0;
linA = 0;
driveL = 0;
driveR = 0;
light = 0;


#make code for light

###########     Left Arm Code
if ((btn[4] == 'true') & (btn[6] == 'false')):
    armL = 1;
    #Left btn is pressed left arm clockwise
if ((btn[4] == 'false') & (btn[6] == 'true')):
    armL = 2;
    #Left trig is pressed left arm counter-clockwise
if ((btn[4] == 'false') & (btn[6] == 'false')):
    armL = 0;
    #Left trig and left btn not pressed, reset


###########     Right Arm Code
if ((btn[5] == 'true') & (btn[7] == 'false')):
    armR = 1;
    #Right btn is pressed left arm clockwise
if ((btn[5] == 'false') & (btn[7] == 'true')):
    armR = 2;
    #Right trig is pressed left arm counter-clockwise
if ((btn[5] == 'false') & (btn[7] == 'false')):
    armR = 0;
    #Right trig and right btn not pressed, reset


###########     Linear actuator Code
if ((btn[3] == 'true') & (btn[2] == 'false')):
    linA = 1;
    #Y button pressed Linear acutator up
if ((btn[3] == 'false') & (btn[2] == 'true')):
    linA = 2;
    #X button pressed Linear acutator down
if ((btn[3] == 'false') & (btn[2] == 'false')):
    linA = 0;
    #nothing pressed reset


###########     Drive  Code
if ((btn[12] == 'true') & (btn[13] == 'false')):
    driveL = 1;
    driveR = 1;
    #up pressed, forward
if ((btn[12] == 'false') & (btn[13] == 'true')):
    driveL = 2;
    driveR = 2;
    #down pressed, backward
if ((btn[14] == 'true') & (btn[15] == 'false')):
    driveL = 2;
    driveR = 1;
    #Left pressed, left side backward, right side forward
if ((btn[14] == 'false') & (btn[15] == 'true')):
    driveL = 1;
    driveR = 2;
    #Right pressed, right side backward, left side forward
if ((btn[12] == 'false') & (btn[13] == 'false') & (btn[14] == 'false') & (btn[15] == 'false') ):
    driveL = 0;
    driveR = 0;
    #Right pressed, right side backward, left side forward

########### Semi-light code
if (btn[16] == 'true'):
    light = 1;
if (btn[16] == 'false'):
    light = 0;




print(linA)
print(armR)
print(armL)
print(driveL)
print(driveR)
print(light)










if armR == 0:
    pwm.set_pwm(0,0,0)
    pwm.set_pwm(1,0,0)
    pwm.set_pwm(2,0,0)
    #print("reset")
elif armR == 1:
    pwm.set_pwm(0,0,4095)
    pwm.set_pwm(1,0,4095)
    pwm.set_pwm(2,0,0)
    #print("Clockwise Right")
elif armR == 2:
    pwm.set_pwm(0,0,4095)
    pwm.set_pwm(1,0,4095)
    pwm.set_pwm(2,0,4095)
    #print("Counter-Clockwise Right")

if armL == 0:

    pwm.set_pwm(4,0,0)
    pwm.set_pwm(5,0,0)
    pwm.set_pwm(6,0,0)
    #print("reset")
elif armL == 1:

    pwm.set_pwm(4,0,4095)
    pwm.set_pwm(5,0,4095)
    pwm.set_pwm(6,0,0)
    #print("Clockwise Left")
elif armL == 2:

    pwm.set_pwm(4,0,4095)
    pwm.set_pwm(5,0,4095)
    pwm.set_pwm(6,0,4095)
    #print("Counter-Clockwise Left")

if linA == 0:

    pwm.set_pwm(8,0,0)
    pwm.set_pwm(9,0,0)
    #print("reset")
elif linA == 1:

    pwm.set_pwm(8,0,4095)
    pwm.set_pwm(9,0,0)
    #print("linUp")
elif linA == 2:

    pwm.set_pwm(8,0,4095)
    pwm.set_pwm(9,0,4095)
    #print("linDown")

if light == 0:

    pwm.set_pwm(15,0,0)
    #off
elif light == 1:

    pwm.set_pwm(15,0,4095)
    #on

#50hz for drive motors
######### change to 205,308,410 respectively

pwm.set_pwm_freq(50);

if driveR == 0:

    pwm.set_pwm(12,0,308)#308
    #print("reset")
elif driveR == 1:

    pwm.set_pwm(12,0,410)#410
    #forward
elif driveR == 2:

    pwm.set_pwm(12,0,205)#205
    #backward

if driveL == 0:

    pwm.set_pwm(13,0,308)#308
    #print("reset")
elif driveL == 1:

    pwm.set_pwm(13,0,410)#410
    #forward
elif driveL == 2:

    pwm.set_pwm(13,0,205)#205
    #backward




Re: Adafruit PWM PCA9685 Frequency Help

Posted: Tue May 01, 2018 12:32 pm
by joan
That's a feature of the chip. One frequency is used for all channels.

The Pi does not need that board to generate PWM.

E.g. my pigpio will supply the pulses you need.

http://abyz.me.uk/rpi/pigpio/index.html