Saturday 24 May 2014

DC motor controller

Introduciton

A robot must have a possibility to move. One of the simplest solutions is to use a tracked chassis propelled by two engines. Two DC motors working allows for forward movement, one motor or two in differnet directions - turn left/right.

We need

  • L298N coontroller
  • Arduino with firmata library
  • Raspberry Pi with python and firmata package
  • two DC motors for test (probably we will use other engines to drive the robot)




Motor drive controller with L298N module

L298N DC motors controller specification:
  • operating voltage <48V
  • 2A per channel
  • the maximum 20W power at 75 °C
  • supply current 36mA circuit  
  • operating temperature:-20C +135 C
  • dimensions 44mm x 44mm x 27mm
I took the diagram from  http://www.funnyrobotics.com/2011/03/arduino-with-l298n-based-dual-motor.html

and my test connections:



my application to control motors (motor_test.py):
from pyfirmata import ArduinoMega
board = ArduinoMega('/dev/ttyUSB0')

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

pin_motor_A_L = board.get_pin('d:4:p')
pin_motor_A_R = board.get_pin('d:5:p')
pin_motor_B_R = board.get_pin('d:6:p')
pin_motor_B_L = board.get_pin('d:7:p')

cmd_motor = ('A','B')
cmd_direction = ('L','R','S')
invalid_number_parameters = "Invalid number of parameters!"
invalid_parameter = "Invalid parameter: "

def set_motor(motor, cmd, speed):
    if args[0]=='A':
        if cmd=='S':
        pin_motor_A_L.write(0)  
        pin_motor_A_R.write(0)  
        elif cmd=='L':
        pin_motor_A_R.write(0)             
        pin_motor_A_L.write(speed)  
        elif cmd=='R':
        pin_motor_A_L.write(0)  
        pin_motor_A_R.write(speed)  

    if args[0]=='B':
        if cmd=='S':
        pin_motor_B_L.write(0)  
        pin_motor_B_R.write(0)  
        elif cmd=='L':
        pin_motor_B_R.write(0)             
        pin_motor_B_L.write(speed)  
        elif cmd=='R':
        pin_motor_B_L.write(0)  
        pin_motor_B_R.write(speed)  

   
def info():
    print "------------------------------------------------"
    print "Command parameters:"
    print "  A,B - motor id"
    print "  L,R,S - L - left rotation, R - right rotation, S - motor stop"
    print "  0.0-1.0 - velocity (should be given only with the option of L or R)"
    print "  ...and exit from app: exit"
    print   
    print "Examples:"
    print "  A R 0.4"
    print "  A S"
    print "------------------------------------------------"

def validate(engine, cmd, speed):
    if engine not in cmd_motor:
        return invalid_parameter+engine
    elif cmd not in cmd_direction:
        return invalid_parameter+cmd
    elif cmd=='S' and not speed is None:
        return invalid_number_parameters
    elif (cmd=='L' or cmd=='R') and speed is None:
        return invalid_number_parameters
    elif not speed is None and (float(speed)<0.0 or float(speed)>1.0):
         return invalid_parameter+speed

info()
while True:
    cmdLine = raw_input('CMD (A/B L/R/S 0.0-1.0):')
    if cmdLine == 'exit': break    

    args = cmdLine.split() 

    if len(args)>0:    
        engine = args[0]
    else:
        engine = None

    if len(args)>1:    
        cmd = args[1]
    else:
        cmd = None
    
    if len(args)>2:    
    speed = float(args[2])
    else:
    speed = None       

    resp = validate(engine, cmd, speed)

    if resp is None:
    set_motor(engine, cmd, speed)
    else:
    print resp

print "goodbye"


links:
http://arduino-info.wikispaces.com/MotorDrivers
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00000240.pdf
http://www.funnyrobotics.com/2011/03/arduino-with-l298n-based-dual-motor.html 
http://en.wikipedia.org/wiki/Phase-locked_loop




No comments:

Post a Comment

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