The Servo Motor - Part I
bentejuy Sun Nov 22, 2015 Tags MotorServo InterfacePWM

The servo motor is one of the fundamental elements of robotics. Thanks to its functionality, it is widely used in a multitude of applications. Internally, the servo motor is made up of a reduction gear, a DC motor and an electronic control unity that controls the position or angle of the servo motor.

Servos have three wires: two for power and a third control wire. The control unit of a servo receives a square wave signal at a certain frequency through the control wire, and the servo moves at a certain angle according to the pulse width of the signal. The variation in pulse width is called the “work cycle” and the signal is known as pulse width modulation (PWM).

The maximum turning angle of the servo motor depends on the manufacturer. You can find servos with maximum turning angles from 60° to 360° on the market. You can also find continuous rotation servo motors which can turn 360° with no limitations. The manufacturer is the one who specifies the pulse times in microseconds that the servo must receive in order to position itself at the minimum and maximum angles. In this table, you can see some of the most important characteristics and technical specifications of some servos:

Details of some Servo Motors
Futaba S3003 TowerPro SG90 TowerPro MG996R
Torque 4.8V : 3.17 kg-cm
6.0V : 4.10 kg-cm
4.8V : 0.708 kg-cm 4.8V : 9.40 kg-cm
6.0V : 11.00 kg-cm
Speed 4.8V : 0.23 sec/60°
6.0V : 0.19 sec/60°
4.8V : 0.12 sec/60° 4.8V : 0.19 sec/60°
6.0V : 0.15 sec/60°
Weight 37.0 g 9.0 g 55.0 g
Gear Type Plastic Plastic Metal
Rotation 0º - 60º 0º - 180º 0º - 360º(1*)
Pulse Width 500 µs - 3000 µs 500 µs - 2400 µs 200 µs - 2400 µs
1.- In the TowerPro Online Store you can find two servo motors with the same reference code (MG996R) but with different maximum angles??. In fact I have four "TowerPro MG996R" servos and the maximum angle I achieved with them does not exceed 270º. I don’t know why and I’m actually pretty confused by this.

The goal of the example at the bottom on this page is to test various servos with their respective “InterfacePWM”, showing how they work. As with everything in Raspybot, when designing the “MotorServo” object, I tried to make control and management as simple and exact as possible, and for that reason it’s important to correctly state the relationship between the maximum and minimum pulse times with their respective angles. If the configuration is correct, the servo motor will move to the predicted angle. Otherwise, the movement of the servo will be unpredictable.

To test the functions of the “MotorServo” object, I created a simple Python script we you can see how you can control servos. If you look at the code you will see that the first parameter that is given to the “MotorServo” object is an “InterfacePWM” object; the rest of the parameters are factory values necessary for the correct functioning of the “MotorServo” object.

‘Til next time.

Download Code
										
#!/usr/bin/env python
# -+- coding: utf-8 -+-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
# Name:         tests/motorservo1
# Purpose:
#
# Created:      03/27/2015
# Modified:     12/17/2015
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

import time

from raspybot.devices.motor import MotorServo
from raspybot.io.interface import InterfaceManager, InterfacePWM

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

# Angles for Servo 9G
SG90_MIN_ANGLE = 0
SG90_MAX_ANGLE = 180
SG90_HALF_ANGLE = (abs(SG90_MAX_ANGLE) - abs(SG90_MIN_ANGLE)) / 4

# Angles for Servo MG 996R
MG996R_MIN_ANGLE = 0
MG996R_MAX_ANGLE = 270
MG996R_HALF_ANGLE = (abs(MG996R_MAX_ANGLE) - abs(MG996R_MIN_ANGLE)) / 4

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

def startinfo(motor):
    print '\tStarting Motor => %s' % motor.get_name()


def stopinfo(motor):
    print '\tStopping Motor => %s' % motor.get_name()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

manager = InterfaceManager()

iface1 = InterfacePWM(manager, pinout=5)
iface2 = InterfacePWM(manager, pinout=13)

servo1 = MotorServo(iface1, (0.0005, 0.0024), (SG90_MIN_ANGLE, SG90_MAX_ANGLE), 50, 0.16/60, 'Servo SG90', start=startinfo, stop=stopinfo)      # Servo SG90
servo2 = MotorServo(iface2, (0.0002, 0.0024), (MG996R_MIN_ANGLE, MG996R_MAX_ANGLE), 50, 0.22/60, 'Servo MG996R', start=startinfo, stop=stopinfo)    # Servo MG996R

try:
    count = 5

    while count:
        if not servo1.alive() and not servo2.alive():
            print 'Running...', count

            if count % 2:
                servo1.forward()
                servo2.forward()
            else:
                servo1.backward()
                servo2.backward()

            count -= 1

        time.sleep(3)

    for x in xrange(20):
        print 'Moving Servo 1 to', (x % 5) * SG90_HALF_ANGLE, 'degrees'
        print 'Moving Servo 2 to', (x % 5) * MG996R_HALF_ANGLE, 'degrees'

        servo1.angle_to((x % 5) * SG90_HALF_ANGLE)
        servo2.angle_to((x % 5) * MG996R_HALF_ANGLE)

        time.sleep(3)

except KeyboardInterrupt:
    servo1.stop()
    servo2.stop()

finally:
    manager.delete(iface1)
    manager.delete(iface2)

    manager.cleanup()
										
									
										
root@raspberrypi:/home/pi/Projects/python/raspybot# python tests/motorservo1.py 
Running... 5
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Running... 4
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Running... 3
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Running... 2
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Running... 1
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 0 degrees
Moving Servo 2 to 0 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 45 degrees
Moving Servo 2 to 67 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 90 degrees
Moving Servo 2 to 134 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 135 degrees
Moving Servo 2 to 201 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 180 degrees
Moving Servo 2 to 268 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 0 degrees
Moving Servo 2 to 0 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 45 degrees
Moving Servo 2 to 67 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 90 degrees
Moving Servo 2 to 134 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 135 degrees
Moving Servo 2 to 201 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 180 degrees
Moving Servo 2 to 268 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 0 degrees
Moving Servo 2 to 0 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 45 degrees
Moving Servo 2 to 67 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 90 degrees
Moving Servo 2 to 134 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 135 degrees
Moving Servo 2 to 201 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 180 degrees
Moving Servo 2 to 268 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 0 degrees
Moving Servo 2 to to0 degrees
	Starting Motor => Servo SG90
tarting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 45 degrees
Moving Servo 2 to 67 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 90 degrees
Moving Servo 2 to 134 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 135 degrees
Moving Servo 2 to 201 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
Moving Servo 1 to 180 degrees
Moving Servo 2 to 268 degrees
	Starting Motor => Servo SG90
	Starting Motor => Servo MG996R
	Stopping Motor => Servo SG90
	Stopping Motor => Servo MG996R
root@raspberrypi:/home/pi/Projects/python/raspybot#