Shop OBEX P1 Docs P2 Docs Learn Events
Code for standalone stepper motor controller. — Parallax Forums

Code for standalone stepper motor controller.

Richard905Richard905 Posts: 4
edited 2013-02-14 09:03 in General Discussion
I am very new to PARALLAX Propeller.

I'd like make a simple Standalone stepper motor controller by using P8X32A QuickStart and Easydriver(DIR & STEP),

What I need do is: when I press momentary push button switch, The stepper motor turn 30 rev. (assume 200STEPS/REV.), then
stop.

Can somebody write a code for me?

Thanks a lot in advance!

Comments

  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-02-12 20:56
    No one is going to write your code for you. You will need to do a little foot work. Once you have something then people will be more than happy to help you with any problems you might have, For the Prop this is a good place to start: http://www.parallax.com/portals/0/help/P8X32A/QnaWeb/
  • ercoerco Posts: 20,256
    edited 2013-02-13 08:39
    Richard905 wrote: »

    Can somebody write a code for me?

    Thanks a lot in advance!

    How much does this job pay? :)
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-02-13 09:24
    As you don't seem to need smaller resolution, connect ms1 and ms2 to gnd.
    and then create a loop in Spin that change a pin state 200*30 times and stop.
    Quote:
    Step Input (STEP). A low-to-high transition on the STEP input sequences the translator and advances the motor one increment.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-02-14 06:27
    Try the Parallax OBEX for some good code. Actually stepper motors are not that hard to code, but you need to be specific as to what kind of stepper motor - 4 wire, 5 wire, 6 wire, or 8 wire. One of them is consider a universal stepper motor as it can be reconfigured to others (I believe that is the 6 wire).


    If you want to use a driver device such as the Easy Driver, you really have to read all the material and figure out what does what. Nobody wants to read documents for somebody else. That Easy Driver has some sort of 'control language' that should have examples included with its documentation.

    Frankly, depending on the size and voltage of the stepper motor, the wiring of it is actually a much bigger hurdle than the coding. You can Google for both coding and wiring samples, but it is just of loop of 4 or 8 steps in the usual setups. Rotation rate is determined by inserting a delay.

    I do have an example of doing this in PropForth with a UNL2803 and a tiny 5 volt stepper and it was extremely easy.


    Here is just one code example from OBEX. Sorry, but no code for Easy Driver.. that's a SparkFun product.
    http://obex.parallax.com/objects/682/
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-14 07:25
    I have an old EasyStepper board I purchased soon after I started learning about microcontrollers. I never got it to work. It's likely I just didn't know enough at the time to code a program for it correctly.

    After purchasing a Propeller Professional Development Board, I wanted to try writing a program that would use the buttons (normally pulled high) to control a stepper with the on board L293D chip. The attached code was written in June 2010 (expect for the modifications I made this morning).

    I've attached that program modified to only turn 30 times one way or the other based on which of two buttons if pressed.

    The code should work with a lot of different motor control chip that use a "half" H-bridge circuit.

    There's no ramping or speed control. I'll likely be adding these as soon as OBC sends me a set of cheap steppers and wheels for another robot.

    Here's the part of the code that looks for the button presses and sends the motor driving object instructions to turn 30 times.
    if ina[12] == 0    ' pin 12 normally held high if button brings pin low it is "on"
          repeat
            intructionAcceptedFlag := Stepper.DriveRevolutions(30, 0)
          while intructionAcceptedFlag == 0
          Debug.str(string("30 full revolution cw")) 
          Debug.tx(13)
        elseif ina[13] == 0
          repeat
            intructionAcceptedFlag := Stepper.DriveRevolutions(30, 1)
          while intructionAcceptedFlag == 0
          Debug.str(string("30 full revolution ccw"))
          Debug.tx(13)
    

    I'm sure there are many better stepper objects around (including Loopy's).

    As Loopy warned, there are lots of different kinds of steppers. The code I attached was used with a bipolar (four wire) stepper motor.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-02-14 08:02
    @Duane
    The original poster got a rather cold reception for being a new user. Your help is more in keeping with what Parallax Forums do best.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2013-02-14 08:37
    Welcome to the forums!

    Your question is a little broad, being there are many stepper motor options out there, but here's some simple code which I'm using to run those little 5v stepper motors you find almost everywhere.
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
       StepperA = 18                       ' Stepper Motor Coil 1
       StepperB = 19                       ' Stepper Motor Coil 2
       StepperC = 20                       ' Stepper Motor Coil 3
       StepperD = 21                       ' Stepper Motor Coil 4
    
       forward = 1
       backward = 0
    
    VAR
    
       byte BitPattern[ 5 ] 
       long count
         
    
    PUB Demo | cycles, direction
    
      cycles := 512
      direction := forward
    
      OneStepper(StepperA,StepperB,StepperC,StepperD,direction,cycles) 
    
    
    PUB OneStepper (in1,in2,in3,in4,direction,cycles) | pos0,pos1
    
      if direction == 1
         pos0:=0
         pos1:=3
      if direction == 0
         pos0:=3
         pos1:=0
      
      BitPattern[ 0 ] := %1100 '%1000
      BitPattern[ 1 ] := %0110 '%1100
      BitPattern[ 2 ] := %0011 '%0100
      BitPattern[ 3 ] := %1001 '%0110
     
      DirA[ in1..in4 ] := %1111
    
      repeat cycles
        repeat count from pos0 to pos1 
          OutA[ in1..in4 ] := BitPattern[count]
     
          waitcnt(cnt+(clkfreq>>8))
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-14 09:03
    Jeff,

    I'm sure I'll be working on my stepper code more once I start working with your version of a cheap an inexpensive robot.

    When I first started working with a stepper, I noticed how the bit pattern just seemed to rotate so I decided to use bit rotation of a single bit mask instead of having multiple bit masks.

    I define the intial pattern as:
    _InitialPattern = %11001100_11001100_11001100_11001100
    
    

    And then rotate the pattern one way or the other depending on the direction.
    PRI ClockWise
      repeat until (stepCounter-- =< 0)
        stepPattern <-= 1
        outa[pinD..pinA] := stepPattern
        long[globalStepsLocation] += 1
        Pause(_MinimumStepDelay + delay)
      ready := 1  
      
    PRI CounterClockWise
    
      repeat until (stepCounter-- =< 0)
        stepPattern ->= 1
        outa[pinD..pinA] := stepPattern
        long[globalStepsLocation] -= 1
        Pause(_MinimumStepDelay + delay)
      ready := 1    
        
     
    

    Since bit ratotation is a "long" operation, I repeat the byte sized bit pattern four times. Only the lowest byte of the long is used to set the output pins.

    Anyway, I thought it was kind of a cool way to do it. (I'm sure I'm not the first to do it this way.)

    I'll have to see if I can get my OBC CheapBot to do a figure 8 with this method.
Sign In or Register to comment.