Shop OBEX P1 Docs P2 Docs Learn Events
Driving a stepper motor driver — Parallax Forums

Driving a stepper motor driver

4x5n4x5n Posts: 745
edited 2011-10-07 02:38 in Robotics
I'm working on a program that does among other things drives a stepper motor at 75rpm (more or less with consistency being more important that actual speed as long as the speed is close) two revolutions one way, stop quickly and then run two revolutions the other way and repeat until told to stop.

I've gone out and bought a bipolar stepper and driver from probotix.and have gotten it to work more or less correctly. The problem I'm having is that the stepper motor is supposed to have 200 steps for a complete revolution but I find that I need to provide 410 steps to go a complete two revolutions. At first I thought the problem was that I was going from a standstill to a full 75rpm instantly, going two revolutions and then slamming it to a stop and then reversing direction. I then added a bit of code to slow the acceleration and deceleration a bit and it has helped a bit. I also found that ramping the start and stop does no good at all so I removed that from the code.

In theory the driver is very nice. A pulse per step on the step input and a logical high or low on a direction pin controls the driver sending the appropriate signals to the motor. I know that most robot motion is done with a servo (would love to use a high torque continuous rotation one but can't find one with enough torque that spins at 75rpm) or dc motors. The dc motor is starting to become attractive but adds the major complication of controlling speed and number of revolutions. As it is I'm getting the right speed and number of revolutions but am wondering why I need 205 steps per rev instead of 200.

Here is the spin code I'm using. Please be nice. I'm new to spin and the original code has been HEAVILY and quickly hacked. I know that it needs a rewrite. :smile:
CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  rot1 = 410
  rot2 = 410

VAR
  long plscnt, sdelay,dcount, cntnum
  
  
PUB Stepper

  dira[14..16]~~
  dira[8]~~

  repeat
'' set initial state for startup
    plscnt := 1
    cntnum := rot1
    outa[14..8]~
  
'' wait for input to start
    repeat until ina[21]

''pin 16 is tied to the enable pin on the stepper driver

    outa[16]~~
    dcount := cnt

''spin motor until input to stop
  
    repeat until ina[22]

''Check to see if number of steps for current motor direction has been set and toggle if needed.
''Also set the cntnum variable to number of steps for that direction.
        if plscnt == cntnum
          plscnt := 1
          !outa[14]
          outa[8] := outa[14]
          
          if outa[14] == 0
             cntnum := rot1
          else
             cntnum := rot2
             
''Slow step rate for the first and last 20 steps when changing direction.          
        if (plscnt < 20)
          sdelay := clkfreq/(100)
        else
          sdelay := clkfreq/250

        if (plscnt > (cntnum-20))
          sdelay := clkfreq/(100)
        else
          sdelay := clkfreq/250
          
        plscnt++
              
'' Send step pulse to motor
        outa[15]~
        waitcnt(clkfreq/100000+ cnt)
        outa[15]~~
        waitcnt(dcount += sdelay)
    outa[16]~
.

Comments

Sign In or Register to comment.