Monday, 8 September 2014

Servo control

Introduction

A robot to be able to participate in the interaction with the environment should use the servos. We can control the servo using PWM outputs.

We need:

  • Arduino with firmata library (Firmata - Arduino)
  • Raspberry Pi with python and firmata package (Firmata - Raspberry Pi)
  • servo, can be very cheap (eg. TG9)
  • additional DC supply or battery, voltage should be adjusted to the servo

Servo connection

In scheme we need additional supply DC. It's very important! Servo gets high current, which can not provide the power arduino (in our case the usb Raspebrry).
DC supply voltage must be matched to the voltage servo. Most servos working with voltages 4.8 - 6.0V.

Schema


Breadboard

and photo from my real connections:



I replaced the batteries DC power supply.

Software

Access to the servo with python:
from pyfirmata import ArduinoMega
board = ArduinoMega('/dev/ttyUSB0')#usb port
servo = board.get_pin('d:2:s')     #pin PWM no 2 (depending
servo.write(50)                    #set servo position 0-255



Below I put the program to command line control the servo. Should focus on two lines, one:
board = ArduinoMega('/dev/ttyUSB0') # usb port
If necessary, change the port that is connected to the Arduino.
second:
servo = board.get_pin('d:2:s') # pin PWN no 2
where:
d - digital output (not change)
2 - number PWM pin to connect the servo
s - servo control. This is very important! If you set the "p" (power) can damage the servo!

Sample app to command line control the servo:

from pyfirmata import ArduinoMega, time
board = ArduinoMega('/dev/ttyUSB0') # usb port
servo = board.get_pin('d:2:s') # pin PWM no 2

__copyright__ = "Copyright 2014, http://letsmakearobot.blogspot.com/"
__version__ = "0.1.0"
__license__ = "GPL"
__email__ = "sebastian.dziak@gmail.com"

def info():
    print "------------------------------------------------"
    print "Command parameters:"
    print "  0-255 - sevo position"
    print "  ...and exit from app: exit"
    print
    print "Examples:"
    print "  25"
    print "  120"
    print "  exit"
    print "------------------------------------------------"

def validate(position):
   if not position is None and (float(position)<0 or float(position)>255):
      return "Invalid parameter!"

info()
while True:
   value = raw_input('Position (0-255):')
   if value == 'exit': break  
   resp = validate(value)
   if resp is None:
      servo.write(float(value))
   else:
      print resp
 
print "goodbye"

links:
http://arduino.cc/en/Tutorial/sweep
http://www.robotoid.com/appnotes/arduino-operating-two-servos.html

No comments:

Post a Comment

Note: only a member of this blog may post a comment.