Shop OBEX P1 Docs P2 Docs Learn Events
Basic Program for Stepper Motors — Parallax Forums

Basic Program for Stepper Motors

NWUpgradesNWUpgrades Posts: 292
edited 2005-07-04 17:28 in BASIC Stamp
Jon, a while back ago you posted a reply to me with code for a PBasic program to help test my stepper motors.· I unfortunately did not have that saved and lost my hard drive. Could you maybe point me back to that code? Thanks
«1

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-14 14:52
    I don't know which BASIC Stamp module you're using, so I've attached BS1 and BS2 code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-06-14 17:24
    Jon, I don't understand the above BS2 program.

    StpIdx is declared a nibble.· You wrote:

    Step_Fwd:
    · stpIdx = stpIdx + 1 // 4····················· ' point to next step
    · GOTO Do_Step

    What is the starting value of StpIdx.· If 0, then it looks like StpIdx will remain at 0 since I don't see where it is incremented.

    Sorry to be so dumb.

    Sid
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-06-14 19:36
    The stamp does left to right evaluations of expressions (Did I see somewhere that some newer stamps (or is it the IDE?) supports expression nesting?).

    stpIdx = stpIdx + 1 // 4

    when executed first adds 1 to stpIdx then performs the modulus 4 of the result meaning the progression is 0,1,2,3,0,1,2,...

    Did I understand your question correctly? Did this answer it?
  • NewzedNewzed Posts: 2,503
    edited 2005-06-14 19:45
    suppose StpIdx = 0

    then StpIdx = 0 + 1//4 which = 0

    It looks like StpIdx will stay at 0 until the dividend is·=> than 4.·

    If I understand //4 correctly, it says "show the remainder after dividing by 4".

    ???????

    Sid
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-06-14 19:50
    You are correct in everything except you are applying order of precedence to a system which does not use it (you are assuming the modulus function is done before the increment). PBASIC does a strict left to right evaluation of the expression, so:

    A) StpIdx is directed to receive the result.
    B) The temporary variable is initialized to StpIdx
    C) The temporary variable is incremented by 1
    D) The temporary variable is mod 4'ed
    E) The temporary variable is written to StpIdx

    Oh and 1//4 is 1 not 0, the remainder of 1/4 is 1.

    So for your example:

    StpIdx = 0 + 1//4 = 1


    Post Edited (Paul Baker) : 6/14/2005 7:56:04 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2005-06-14 20:03
    I just wrote a little program:

    stp· VAR byte

    Stp = 0
    Stp = stp + 1//4
    DEBUG dec ? stp, cr

    The debug said stp = 1.· I don't understand it, but I know it works.· Of course, I guess you don't have to know how everything works, just how to use it.

    Maybe some day.........................

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-06-14 20:52
    I just figured it out.

    1//4 really means (0), 1 or 1/4 = 0 with 1 left over.

    Now, wasn't that easy?

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-14 21:12
    Two things to remember...

    1) The code I post is usually tested and working· tongue.gif
    2) The BASIC Stamp evaluates from left to right unless parenthesis are used, so....

    ··· stpIdx = stpIdx + 1 // 4

    ··· ... causes stpIdx to be incremented by 1 before divided by 4.· Since the modulus operator is used, the final result of the equation is the remainder of the division.

    Do this as a test:

    Main:
    · DO
    ··· stpIdx = stpIdx + 1 // 4
    ·· ·DEBUG DEC stpIdx, CR
    ··· PAUSE 500
    · LOOP

    And here's a trick for making the values loop and wrap-around the other direction:

    Main:
    · DO
    ··· stpIdx = stpIdx +·3 // 4
    ·· ·DEBUG DEC stpIdx, CR
    ··· PAUSE 500
    · LOOP
    ···
    I'll leave it to you to figure out why it works (this is one of my favorite "tricks").

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-06-16 21:07
    Jon, if I increase the delay in your program from 15 to 30 the shaft will turn at half speed.· Will this make it jerky?· I don't know how fast the motor shaft turns and I might want to slow it down.· Just looking ahead.

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-16 21:21
    I don't know, Sid, why don't you give the stuff a try?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2005-06-16 21:35
    Too bad PBasic doesn't use the % for modulus [noparse];)[/noparse] *teasing*

    At least it's better than having to type MOD all the time -

    Ryan
  • NewzedNewzed Posts: 2,503
    edited 2005-06-17 13:46
    I have been studying the stepper program above and it looks like power is applies to each winding for 15ms plus overhead.· Decreasing 15 would make it step faster, increasing 15 would make it step slower up to the point where travel became jerky.

    Does anyone have an idea how to convert this program from full step to half step, or can it even be done with a Stamp.· I think I remember reading that windings must be pulsed for a minimum of 5us - there was no reference to a max pulse.

    Sid
  • BeanBean Posts: 8,129
    edited 2005-06-17 13:58
    The pulse time depends on how fast you want the motor to turn.
    Stepper motors can turn as SLOW as you want (including standing still), so there is no maximum pulse time.

    BUT...
    Because of inertia you should start moving at a slower speed then ramp the speed up. Same with stopping, you should ramp the speed down before you stop. Remember steppers have the more torque at·slower speeds.

    Some stepper control systems change the current to the stepper too, at startup and stop current is increased. When stopped or moving slowly current is decreased.

    With the stamp alone, you probably cannot pulse the stepper too fast.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    Product web site: www.sxvm.com

    "What's the difference between ignorance and apathy ?"
    "I don't know, and I don't care."
    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-06-17 14:20
    Thanks, Bean.· I'll add a ramping sequence to my program - will double pause for first 100 steps be adequate?

    Can you help on the half-step thingie?

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-17 14:29
    Half stepping is indeed possible -- but the stepper loses torque when you do that.· What you need to do is change the table to half steps, and update the step routine (the modulus value will be eight instead of four).·

    This is what a half step table looks like:

    Step1·········· DATA··· %1000
    Step2·········· DATA··· %1100

    Step3·········· DATA··· %0100
    Step4·········· DATA··· %0110
    Step5·········· DATA··· %0010
    Step6·········· DATA··· %0011
    Step7·········· DATA··· %0001
    Step8·········· DATA··· %1001



    And the code to use it:

    Step_Fwd:
    · sAddr = sAddr + 1 // 8
    · READ (Step1 + sAddr), Coils
    · PAUSE speed
    · RETURN


    Step_Rev:
    · sAddr = sAddr +·7 // 8
    · READ (Step1 + sAddr), Coils
    · PAUSE speed
    · RETURN


    (This happens to be from StampWorks 1.2)

    As a reminder ... Google is your friend.· I have found a lot of interesting stepper projects and resources with a just·a few minutes of web searching.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 6/17/2005 2:33:15 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2005-06-26 17:04
    Jon's stepper program, posted way up above, is for a unipolar.· I have another stepper with four wires so it must be bipolar.· Could I use Jon's program to run a bipolar?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-26 17:41
    Yes, you can use the same code -- with a different driver.· Using the L293D (on the PDB and I know you have one) can be used to drive unipolar or bipolar steppers (since the L293D outputs are driven in both directions).·

    This N&V article should help: http://www.parallax.com/dl/docs/cols/nv/vol6/col/nv121.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-06-26 18:34
    Thank you very much, Jon.· Just what I needed.· The wire switch trick for the bipolar makes life easy.

    I see the L293D is rated at 36 VDC.· Can I apply up to this voltage to the V+ screw terminal?

    If I increase the delay the stepper runs slower but has more torque.· Is this correct?

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-06-29 13:25
    I got my little stepper wired up and running with Jon's sample code.· The problem is that 48 steps turns it 1/2 revolution.· The phases were identified according to the info here:

    http://www.doc.ic.ac.uk/~ih/doc/stepper/others/

    The stepper appears to run quite smoothly -· is it possible that one of the phases is incorrectly identified?

    Thanks

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-06-30 12:22
    Jon's program uses

    Phase··· VAR· OUTB

    and

    DIRB = %1111

    which makes things very neat.

    I have a old BS2 I would like to dedicate to running one of my biploar steppers.· Unfortunately some of the pins are damaged and I do not have DIRA or DIRB available.· I do have Pins 0, 1, 6 and 7.· Can I write:

    Phase··· VAR OUTL

    and then

    DIRL = %110000011

    Thanks

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-30 12:29
    Of course you can, but using OUTL like that affects eight pins at a time (some that may be needed for other things).· I would be more inclined to write a subroutine where the coil output pins are named and grab the bit values from the step table.· Something like this:

    Do_Step:
    · READ (Steps + stpIdx), coilData
    · Coil1 = coilData.BIT0
    · Coil2 = coilData.BIT1
    · Coil3 = coilData.BIT2
    · Coil4 = coilData.BIT3
    · PAUSE stpDelay
    · RETURN


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-06-30 12:41
    Like this:

    Coil1· ·PIN 0
    Coil2·· PIN 1
    Coil3·· PIN 6
    Coil4·· PIN 7

    then

    Output 0
    Output 1
    Output 6
    Output 7

    and to rest of program using the modified Do_Step.

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-30 12:57
    Yep. Or you can set the outputs with DIRS since that usually needs to happen just once.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-06-30 16:14
    Can anyone ghive me an estimate of how much power I would lose if I ran a bipolar at half-step instead of full step?

    Thanks

    Sid
  • ChipCircuitChipCircuit Posts: 23
    edited 2005-06-30 19:22
    Exactly the same or double, depending on the driver circuit.

    Each phase is energized for a half step.

    There are different types of driver circuits, and some will actually use full power on both phases at the same time, others will use half power on each phase.

    But, if you size your power supply to the total watts of the two phases, you are set.

    Dave
  • NewzedNewzed Posts: 2,503
    edited 2005-06-30 20:48
    I am driving with a BS2 and an L293D, running the motor at 12VDC for the moment'

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-06-30 21:12
    I modified Jon's N&V Col.121 BS1 program to run on a BS2, driving an L293D.· Both enables are pulled high and I have 12VDC on Pin 8 of the L293.· I debugged StpIdx and Phase and am getting all the right numbers.

    However, the motor doesn't run.· I hooked up my scope and am getting input signals on the four inputs but no signal of any kind on the four outputs.

    Would you say I had a bad L293?

    I'm sure the program is OK, but I have attached it anyway.

    Sid
  • cyberbiotacyberbiota Posts: 79
    edited 2005-07-01 01:40
    Sid-

    Are the Stamp and motor grounds shared and connected to the '293?

    peter

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Peter C. Charles

    Director, Research and Technology
    CyberBiota, Incorporated
    Peter.charles@cyberbiota.com
    http://www.cyberbiota.com
  • NewzedNewzed Posts: 2,503
    edited 2005-07-01 11:14
    Yes - I'm running off of Vin right now.

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-07-02 22:31
    I am trying to come up with a circuit to drive a bipolar but I'm not having much luck.· It is too big for the L293 on my PDB.· I'm not looking for a Gecko type controller - I just want to make the thing run.· The biggest chips I have are TIP120s.· Resistance of each phase pair is about 7.7 ohm, so at 18VDC it is going to draw about 2.3 amps.· Can anyone help - nothing fancy - all I want to do is run the thing.

    Sid
Sign In or Register to comment.