Jitter-Free Servo Control on the Raspberry Pi

in python •  7 years ago 

2018-04-06 15.39.00.jpg

Previously we looked at motor control but for our robot there is another type of actuator that we need to drive, and those are Servos.

Servos

Servos are another kind of motor, but they are actually easier to use than regular motors with the Pi, because they have built-in controllers. There is one problem, however, which we will get to in a moment.

We can choose between the regular kind of servo, where the servo can be told to move to a position or angle, and then there are the continuous kind where instead of angle, the same instruction controls speed and direction.

Problem?

Rather than tell you about it, let me show you :)

Wire up a servo with the power connected to 5v, the ground connected to a GND, and the signal connected to Pin 18.

We will be using the chips pin numbers so refer to the Pinout.xyz guide here:

from gpiozero import AngularServo
from time import sleep

servo = AngularServo(18, min_angle=-42, max_angle=44)
while True:
    servo.min()
    sleep(1)
    servo.mid()
    sleep(1)
    servo.max()
    sleep(1)
    servo.angle = 40
    sleep(1)
    servo.angle = 20
    sleep(1)
    servo.angle = 0
    sleep(1)
    servo.angle = -20
    sleep(1)
    servo.angle = -40

This code will work but ill likely find the motion is stuttery. This is because of the Raspberry Pi being a fully-fledged computer rather than a microcontroller. Instead of just doing one thing at a time, the Pi has a multi-tasking operating system that has to do many things all at once.

Raspberry Pi Jitter Solution

Fortunately for us, a generous soul from the UK has ported a low-level library for us to use.

sudo pip3 pigpio

and run sudo pigpiod

Add the following code then check out the difference:

import pigpio
from time import sleep

# connect to the 
pi = pigpio.pi()

# loop forever
while True:

    pi.set_servo_pulsewidth(18, 0)    # off
    sleep(1)
    pi.set_servo_pulsewidth(18, 1000) # position anti-clockwise
    sleep(1)
    pi.set_servo_pulsewidth(18, 1500) # middle
    sleep(1)
    pi.set_servo_pulsewidth(18, 2000) # position clockwise
    sleep(1)

Much more stable, right?

What now?

As we have seen, other than the power limitations, the Pi can be a capable physical computing device, but for anything more in-depth I like to split the responsibilities of hardware control versus processing.

Check out my article where I combine the Raspberry Pi and Arduino.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Congratulations This post has been upvoted by SteemMakers. We are a community-based project that aims to support makers and DIYers on the blockchain in every way possible.

Join our Discord Channel to connect with us and nominate your own or somebody else's posts in our review channel.

Help us to reward you for making it ! Join our voting trail or delegate steem power to the community account.

Your post is also presented on the community website www.steemmakers.com where you can find other selected content.

If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.

IMG_20160202_163556.jpgIMG_20160202_163537.jpg

Very important I also make a machine

You got a 2.78% upvote from @postpromoter courtesy of @makerhacks!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!