2014-06-09

Stepper motor control with EasyDriver and Python on Linux desktop

My Mechanical Engineering colleagues recently asked me to build them a software-controlled stepper motor system - they wanna apply high-power pulse laser on the surface of rotating objects. Of course, the software must have GUI.



Hardware

I decided to control the stepper motor using a Linux PC with parallel ports (yes, people are still building motherboards with parallel ports), because developing software for desktop Linux is much easier. Oh, for tablets and smart phone fans, how do your devices talk to physical world? 

In searching for a proper driver chip/board for the stepper motor, I came across the handy EasyDriver board designed by Brian Schmalzhaus. I just need to provide one rising edge to the STEP pin of it, the stepper motor will rotate one step. I can control the direction of rotation and step size by configuring certain pins. It saves me the hassle of computing waveforms for controlling stepper motors. Check the references below to see how EasyDriver can simply your life.

Lastly, very important, let the EasyDriver board and the PC share the common ground. Otherwise the control signal from parallel port may not work (e.g., a high from parallel port is not high to the ground level of EasyDriver), or the parallel port might get burned.

Oh, one more thing, the EasyDriver board only drives bipolar stepper motors.

My circuit is prototyped on a breadboard like below:

The circuit takes power from the power supply of the desktop computer on which the control software will run. I used Pin 2 and Pin 4 of the parallel port to control the STEP and DIR on Easy Driver. MS1 and MS2 are grounded because I only use full step mode. I used two LEDs to show the signals from parallel ports.

Here is circuit in reality. As you can see, the circuit takes power from the old floppy disk power jack.


I used Saleae's Logic as a USB logic analyzer to check the waveforms. As you can see, for every rising edge on channel 4, the state of the first 4 channels change.

Software

At first I planned to use parapin, a C-based library for parallel port I/O. I used it when I was in college. After googling, I found out a Python module called PyParallel and it greatly simplifies software development.

I wrote a small piece of Python code below to control it.
"""This software is free software licensed under GNU GPL 3.0
Copyleft 2014 Forrest Sheng Bao http://fsbao.net

Dependencies: 
1. pyparallel: Can be installed on Ubuntu via
            sudo apt-get install python-parallel

Notes for Linux: 
1. make sure you execute
      sudo rmmod lp
           and
      sudo modprobe ppdev
  on your Unix shell first. 
2. Add yourself into lp user group on your system if you don't wanna use sudo all the time. 

Help the people under Communism tyranny in China, North Korea, Viet Nam and Cuba. 

"""

def rotate(Step, Dir, Step_Pin=5, Dir_Pin=2, Delay=0.25):
    """Rotate the motor by _Step_ steps in _Dir_ direction

    Input
    =======
    Step: integer
    Dir: string, 0 for clockwise and 1 for counterclockwise
    Step_Pin: int, the pin used to control stepping signal
    Dir_Pin: int, the pin used to control rotation direction
    Delay: float, the delay between each step. Do not set it 
           too small to exceed your motor's speed limit. 

    Notes
    =======
    The data pins in parallel ports are from 2 to 8. 

    """

    import parallel
    import time
    p = parallel.Parallel()

    p.setData(0)

    for i in range(int(Step)):
        p.setData(2 ** (Dir_Pin - 2) * Dir) # set or keep direction
        time.sleep(Delay)
        p.setData(2 ** (Dir_Pin - 2) * Dir + 2 ** (Step_Pin - 2)) # keep direction and rotate
        time.sleep(Delay)
    
#    p.setData(0)
    
if __name__ == "__main__" :
    import sys
    Step = int(sys.argv[1])
    Dir  = int(sys.argv[2])
    Delay = float(sys.argv[3])
    rotate(Step, Dir, Delay=Delay)


Note

1. The chip on EasyDriver can get really hot - up to 136 Celsius! Do not use it with a motor that requires lots of current and/or rotate the motor too frequently. Mine got burned out pretty soon. So I am ordering a motor with less torque but smaller current.
2. Do not send pulse to STEP pin of EasyDriver faster than the maximum speed of your motor. It will cause damage.

Questions and comments are all welcome!

References:

1. Easy Driver Example by Brain Schmalzhaus himself, http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html
2. Easy Driver Example by Sparkfun, https://www.sparkfun.com/tutorials/400








No comments: