raymondlee306
Posts: 2
Joined: Tue May 16, 2017 1:26 am

Software PWM for beginner

Tue May 16, 2017 1:53 am

Hi Everyone. First time playing with Python and 1st time posting. I'm trying to use software PWM to control two servos independently. I'm okay with a little "jitter" for this project and plan to add an PCA9685 driver later down the road but I'm trying to do this for the time being. I have the left servo working great following some tutorials I found. It behaves exactly like I want in the for loop. The problem comes when I try to split the loop to control the left and right servos either together or on delays from each other. So two question I guess...can the GPIO run two PWM at the same time? and what's the best way to modify the code below to control another servo within the same loop. Or a separate loop but at the same time as the first loop (that would work too)

Thanks In Advance,

Code: Select all

import RPi.GPIO as GPIO

import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

leftpin = 11
rightpin = 13
GPIO.setup(leftpin, GPIO.OUT)
GPIO.setup(rightpin, GPIO.OUT)

freq = 50
pwmleft = GPIO.PWM(leftpin, freq)
pwmright = GPIO.PWM(leftpin, freq)

left_up = 0.75
left_rev = 2.5
left_down = (left_rev- left_up) / 2 + left_up

right_up = 2.5                      #servo is 180 degrees from left, hence DC reversed
right_rev = 0.75                   #servo is 180 degrees from left, hence DC reversed
right_down = (right_up - right_rev) / 2 + right_rev                 #servo is 180 degrees from left, hence DC reversed


positionlistleft = [left_up, left_down]
positionlistright = [right_up, right_down]

mpc_left = 1000 / freq
mpc_right = 1000 / freq

for i in range(3):
    for positionleft in positionlistleft:                       #I have tried adding a new line here with "right" in place of "left"
        dcpleft = positionleft * 100 / mpc_left          #I have tried adding a new line here with "right" in place of "left"
        pwmleft.start(dcpleft)                                     #I have tried adding a new line here with "right" in place of "left"
        time.sleep(1)

pwmleft.stop()

GPIO.cleanup()

ghp
Posts: 1145
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Software PWM for beginner

Tue May 16, 2017 4:41 am

Hello,
the rpi.gpio module is able to have multiple pwm active, each for a separate gpio-pin.
Your code does not fully follow this definition:
pwmleft = GPIO.PWM(leftpin, freq)
pwmright = GPIO.PWM(leftpin, freq)
the second line should be
pwmright = GPIO.PWM(rightpin, freq)

Your approach to use arrays to store the target positions is a nice idea.
You could solve your problem by

Code: Select all

# define servo pos 0..100 range (easy to read)

leftList  = [   0,  10,  20,  30,  40,  60, 100 ]
rightList = [ 100,  60,  40,  30,  20,  10,   0 ]

# start with 50%
pwmRight = GPIO.PWM(rightPin, 50)
pwmLeft = GPIO.PWM(leftPin, 50)
pwmRight.start(7.5)
pwmLeft.start(7.5)

for i in range ( len(leftList)):
    print ( leftList[i])
    print ( rightList[i])
    #
    # pwm pulse width is 1 to 2 ms in 20 ms cycle, 
    # which is 5% to 10%
    # 
    pLeftValue = float(100.0 + leftList[i]) / 20.0
    pRightValue = float(100.0 + rightList[i]) / 20.0
    
    print ( pLeftValue )
    print ( pRightValue )
    
    pwmLeft.ChangeDutyCycle(pLeftValue)
    pwmRight.ChangeDutyCycle(pRightValue)
    #
    # sleep a while
    #
The trick is to use an index variable (here 'i') to grab values from both arrays.
Hope this helps,
Gerhard
Last edited by ghp on Tue May 16, 2017 4:50 am, edited 4 times in total.

User avatar
joan
Posts: 12738
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Software PWM for beginner

Tue May 16, 2017 4:42 am

RPi.GPIO has a software PWM class which is implemented using threads. You could use that.

Alternatively use something like servoblaster or (my) pigpio which provide hardware timed PWM pulses suitable for servos on GPIO 0-31.

See http://abyz.co.uk/rpi/pigpio/python.htm ... pulsewidth

raymondlee306
Posts: 2
Joined: Tue May 16, 2017 1:26 am

Re: Software PWM for beginner

Tue May 16, 2017 10:34 am

Thank you both very much. I'll read through this and try some of it after work tonight.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 15 guests