Uncategorized

Servo GPIO PWM

Asked 
Active 2 months ago
Viewed 114 times
3

I’m working with face tracking and I want to activate some servomotors depending the motion of my face. For facetracking I’m using, an usb cam, opencv and dlib.

Now results the raspberry can’t run my code in real time, it’s to much for it. Probably is a very stupid question, but, can I run my program in my computer and pass the data to the raspberry in real time, and with the raspberry read this data and use the gpios to activate the servos?

Thank you very much

  • You surely could do this by several means. As stated it’s pretty broad. Also, if this is your approach, using Arduino to control the servos might be a better choice because it doesn’t sound like you need a general operating system on the controller. – Brick Jun 6 at 14:06
  • But I decided to use raspberry instead of arduino because I’m using python and specifically opencv and dlib libraries. – NEA Jun 6 at 14:11
  • Once you make this division, the two sides will be decoupled from those tools. You’ll in any case need to design some sort of architecture whereby limited amounts of data are passed as messages and the messages processed. Exactly what’s on each side would be application-specific, and we cannot help you with that given the current description. I’d strongly recommend though not to get yourself wrapped up in Python for Python’s sake. Arduino sounds – best I can tell from what you’ve given so far – like a better choice. Of course if there’s something not presented here, the answer might change. – Brick Jun 6 at 14:14

2 Answers

5

The pigpio library lets you control the GPIO of one or more networked Pis from a laptop. The laptop may be Windows, Mac, Android, or Linux based – in fact it can run any operating system as long as it can run Python. The pigpio Python module allows control of the remote GPIO.

pigpio will let you properly control servos. It provides hardware timed PWM (suitable for servos) on all the GPIO.

The Raspberry Pi foundation gpiozero supports pigpio as a back end and thus allows networked GPIO control.

-2

Question

  1. Face tracking using laptop usb cam, opencv and dlib
  2. Face motion signal from laptop to Rpi to use GPIO to move servo motors.
  3. Is this config OK?

Answer

I think it is a good use of laptop and Rpi. To know more about how to use Rpi’s PWM GPIO pins to move servo motors, you might like to read my answer in the following post.

Using Rpi PWM GPIO pin to move servo motors

Swing Servo Youtube – tlfong01 2019may1201

And since the more powerful Rpi4 is coming soon, it is time to plan doing both jobs in Rpi4, saving your laptop for other R&D work.

Update 2019jun07hkt1241

The toy servo I used above as an example is not a good choice, because its positioning is not that precise and stable, not to mention is it is slow, and span limited (about 180 degrees), and has dead bands, … For more advanced applications, I would recommend cheapy stepping motors and BLDC (BrushLess Direct Current) motors (with and without Hall effect quadrature position encoders) (references to add later). But for rapid prototyping, toy servos are very good。)

References

Pictures showing how Rpi PWM GPIO can move servo motors

rpi servo 1

rpi servo 2

rpi servo 3

rpi servo 4

Fully debugged python programs (1) to move motor and (2) display PWM signal, with sample outputs

# Servo_test32 tlfong01 2019may12hkt1506 ***
# Raspbian stretch 2019apr08, Python 3.5.3

import RPi.GPIO as GPIO
from time import sleep

# *** GPIO Housekeeping Functions ***

def setupGpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    return

def cleanupGpio():
    GPIO.cleanup()
    return

# *** GPIO Input/Output Mode Setup and High/Low Level Output ***

def setGpioPinLowLevel(gpioPinNum):
    lowLevel = 0
    GPIO.output(gpioPinNum, lowLevel)
    return

def setGpioPinHighLevel(gpioPinNum):
    highLevel = 1
    GPIO.output(gpioPinNum, highLevel)
    return

def setGpioPinOutputMode(gpioPinNum):
    GPIO.setup(gpioPinNum, GPIO.OUT)
    setGpioPinLowLevel(gpioPinNum)
    return

# *** GPIO PWM Mode Setup and PWM Output ***

def setGpioPinPwmMode(gpioPinNum, frequency):
    pwmPinObject = GPIO.PWM(gpioPinNum, frequency)
    return pwmPinObject

def pwmPinChangeFrequency(pwmPinObject, frequency):
    pwmPinObject.ChangeFrequency(frequency)
    return

def pwmPinChangeDutyCycle(pwmPinObject, dutyCycle):
    pwmPinObject.ChangeDutyCycle(dutyCycle)
    return

def pwmPinStart(pwmPinObject):
    initDutyCycle = 50
    pwmPinObject.start(initDutyCycle)
    return

def pwmPinStop(pwmPinObject):
    pwmPinObject.stop()
    return

# *** Test Functions ***

def setHighLevelGpioPin18():
    print('  Begin setHighLevelGpioPin18, ...')
    gpioPinNum   = 18
    sleepSeconds =  2    
    setupGpio()
    setGpioPinOutputMode(gpioPinNum)
    setGpioPinHighLevel(gpioPinNum)
    sleep(sleepSeconds)
    cleanupGpio()
    print('  End setHighLevelGpioPin18, ...\r\n')
    return

def setPwmModeGpioPin18():
    print('  Begin setPwmModeGpioPin18, ...')

    gpioPinNum   =   18
    sleepSeconds =   10
    frequency    = 1000
    dutyCycle    =   50

    setupGpio()
    setGpioPinOutputMode(gpioPinNum)

    pwmPinObject = setGpioPinPwmMode(gpioPinNum, frequency)
    pwmPinStart(pwmPinObject)
    pwmPinChangeFrequency(pwmPinObject, frequency)
    pwmPinChangeDutyCycle(pwmPinObject, dutyCycle)
    sleep(sleepSeconds)
    pwmPinObject.stop()
    cleanupGpio()   

    print('  End   setPwmModeGpioPin18, ...\r\n')

    return

# *** Main ***

print('Begin testing, ...\r\n')
setHighLevelGpioPin18()
setPwmModeGpioPin18()
print('End   testing.')

# *** End of program ***

'''
Sample Output - 2019may12hkt1319
>>> 
 RESTART: /home/pi/Python Programs/Python_Programs/test1198/servo_test31_2019may1201.py 
Begin testing, ...

  Begin setHighLevelGpioPin18, ...
  End setHighLevelGpioPin18, ...

  Begin setPwmModeGpioPin18, ...
  End   setPwmModeGpioPin18, ...

End   testing.
>>> 

>>> 


'''

To display PWM waveform

def servoPwmBasicTimingTestGpioPin18():
    print('  Begin servoPwmBasicTimingTestGpioPin18, ...')

    gpioPinNum         =   18
    sleepSeconds       =  120
    frequency          =   50
    dutyCycle          =    7

    setupGpio()
    setGpioPinOutputMode(gpioPinNum)

    pwmPinObject = setGpioPinPwmMode(gpioPinNum, frequency)
    pwmPinStart(pwmPinObject)
    pwmPinChangeFrequency(pwmPinObject, frequency)
    pwmPinChangeDutyCycle(pwmPinObject, dutyCycle)

    sleep(sleepSeconds)

    pwmPinObject.stop()
    cleanupGpio()   

    print('  End   servoPwmBasicTimingTestGpioPin18, ...\r\n')

    return

Raspberry Pi Servo Motor control – Rpi Tutorials

Servo MG996R Datasheet – TowerPro

Python (RPi.GPIO) API – SparkFun

Using PWM in RPi.GPIO – SourceForge

Raspberry Pi PWM Generation using Python and C – ElectronicWing

Servo Tutorial – Lady Ada

PWM Tutorial – Lady Ada

UniPolar/Bipolar Stepping Motor 28byj48 Using L297D

TowerPro SG5010 360 Degrees Servo

Servo Basics and Control Function Discussion

Not the answer you’re looking for? Browse other questions tagged   or ask your own question.

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: