Shop OBEX P1 Docs P2 Docs Learn Events
Linear interpolation — Parallax Forums

Linear interpolation

curious1curious1 Posts: 104
edited 2007-04-06 14:25 in BASIC Stamp
Hey Brains,
··
Question is : Can code be written to the BS2 to have··2 motors do linear interpolation· similar to the way a CNC controller runs a mill to cut a straight line at an angle to the X,Y axis,·or a lathe to cut threads ?
I am considering building a small machine specific to 1 part that would not require a PC with CNC software.
I have made 2 such 2 axis machines·and they work very well even though they are open loop steppers·but now I·would like·to interpolate with the 2 motors.
Any sample code would be appreciated, I did the math, ratio from 1 motor to the other was 42 to 1 but how to pulsout motor 1 one pulse for· every 42 pulses on motor 2 has me baffled.
Thanks

Comments

  • curious1curious1 Posts: 104
    edited 2007-04-04 13:51
    Here's a little more info.

    Pulsout to motor 1 - 4000 pulses
    " 2 - 1 pulse for every 42 pulses of motor 1

    There has to be a simple way, I just can't see it. Thanks
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-04 14:19
    Curious,

    Do you want motor·1 to move 42 times faster than motor·2 ?

    Ed
  • curious1curious1 Posts: 104
    edited 2007-04-04 14:21
    Yes, but how ? PWM or just by counting ?
  • stamptrolstamptrol Posts: 1,731
    edited 2007-04-04 14:24
    I think the idea would be this, assuming stepper motors:

    1. bring both motors to a known starting point.

    2. start sending pulses to motor 1. Keep track of how many pulses. I just use a word variable within the loop that sends the pulsout.

    3. every time the pulse variable totals 42, goto a loop to send a pulse to motor 2. Reset the pulse counter variable to zero.

    4. go back to step 2. Repeat until all of motor 1's pulses have been counted.

    A BS2sx will let you get to about 700 pulse/sec, in my experience.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • metron9metron9 Posts: 1,100
    edited 2007-04-04 14:24
    Syntax may not be correct but the logic flow should work for you.

    MAX=4000
    COUNT=0
    COUNTa=0

    loop:
    counta=counta+1
    count=count+1
    gosub motor1 'drive motor1
    if count=42 then
    gosub motor2 'drive motor2
    count=0
    endif
    if counta<MAX then goto loop

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-04 14:27
    Don't mix up pulse count and pulse width. The count defines how long the motor will run.
    Say 100 pulses and in the loop is a PAUSE 20 so it will run 2000 msec.

    To define the speed, you need to select the proper pulse width. See the robotics Version 2.2
    manual on page 115 .. do you have that ?

    Ed
  • curious1curious1 Posts: 104
    edited 2007-04-04 18:08
    Ed,
    no, I dont have it.
    If I did this :
    for counter 1 to 1000
    Pulsout motor 1
    Pause 10
    and at he same time :
    for counter 1 to 100
    Pulsout motor 2
    Pause 100
    Would the motors start and stop at the same time ?
    Wouldnt motor 2 run exactly 50% revolutions as motor 1 ?
    ?????
  • T ChapT Chap Posts: 4,223
    edited 2007-04-04 18:48
    As a simpler example. assume the X and Y motors are at X0Y0. You want to move both motors to X2Y2(just think 2 inches), so that the coordinate is now both up and over to the right 2,2 to create a diagonal tool path. This is just a straight line tool path, from 0,0 to 2,2. In this example, the motors move the same number of steps at the same speed. For the example, lets say 1000 pulses/steps per rev, and each rev moves the tool .1". So, 10000 revs = 1" of travel. This means we have to step each motor 20,000 times to get it to X2Y2.

    Here is the essence of the code

    
    repeat 
       outputX = 1
       outputY = 1
       pause 10   'fixed pause time here sets a fixed pulse width for the Step
       outputX = 0
       outputY = 0
       pause 100   'this is a variable length pause, that wil be used to change Speed on the steppers
    
    




    This has a slight flaw, which is that it moves X motor first and then Y based on doing the Xpin instruction first. A better way would be to move both motors at the same time, so some method to output both pins states on the same line would be preferred, just not shown in the example.

    Now change the example from X2Y2 to X1Y2, which means we still need to get X and Y motors to the same place at the same time, but now the distance to move is not the same anymore. That means two pieces of code have to be running to allow for two speeds. How to manipulate this on the Stamp I have no idea, the Propeller is a breeze to simultaneously and synchonously move multiple steppers, and I would really recommend the change of processor for your project.

    But, here is the general idea, if anyone knows a "Stamp" way please show a way to accomplish this

    
    'launch these at the same time by some mysterious way
    
    repeat 10000     '1 inch of travel, X needs to move at 1/2 the speed of Y to arrive at same time
       outputX = 1
       pause 10   'fixed pulse On width
       outputX = 0
       Pause 200   'sets speed
    
    
    repeat 20000
       outputY = 1
       pause 10
       outputY = 0
       Pause 100
    
    
    

    Post Edited (TChapman) : 4/4/2007 11:29:05 PM GMT
  • curious1curious1 Posts: 104
    edited 2007-04-04 18:59
    Thanks TC,
    Is there a Propeller tutorial for this sort of thing ?
    Online ?
    Thanks,
    Russ
  • T ChapT Chap Posts: 4,223
    edited 2007-04-04 19:08
    I think there is a simple stepper object in the objects library, I never looked at it though. This is as simple as you can get on the Propeller, just one notch above blinking some LEDs. The speeds of the Propeller will be needed for driving multiple steppers in sync, especially if they are 1/2 stepped or higher resolution. If you get a Prop board I'll help you with a simple test code that you can adapt.

    There is some chance someone here can see a way to Stamp this idea, but it will certainly be slower than I would want to run it on a machine.
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-04 19:33
    TC, I'm very puzzled by your solution of the problem. Servo speed is not impacted by the
    length of the pause e.g. the servo's delivered with the Parallax package operate well as long as the pause
    is in the range of 10 - 40 ms. Outside that range operation is "adverserly" impacted.
    That is what Parallax states in their Robotics manual.

    What I understand from the same Robotics manual is that the only proper way to control
    the servo's speed is by changing the pulse width.

    If not, what did I overlook ?

    thanks,
    Ed
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-04 19:36
    Curious1,

    Link to the Robotics version 2.2 manual

    http://www.parallax.com/dl/docs/books/edu/Roboticsv2_2.pdf
  • T ChapT Chap Posts: 4,223
    edited 2007-04-04 19:42
    Since he is duscussing steppers, I think my solution is correct. For servos, you can forget about a Stamp doing the motion and counting encoder steps, which would be mandatory for CNC use. The pulseout is not the way to drive steppers syncronously in a manner to interpolate. The Step outputs for a stepper are "controlled" pulses hi and low, the time between on/off does in fact determine the speed of a stepper.

    Post Edited (TChapman) : 4/4/2007 7:56:21 PM GMT
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-04 19:53
    TC, yep .. steppers.
    Overlooked that "word" and I was focused on servo's because that is the only thing I have used so far.

    Curious, sorry for the misleading info.

    Ed
  • curious1curious1 Posts: 104
    edited 2007-04-04 20:11
    No prob Ed,
    Looks like overall speed is going to be the problem though.
    Is there NO way to run 2 pulse trains simultaneously with the BS2 ?
    These stamps are so amazing, it would be a wonder that it wouldn't.
    Thanks for the help.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-04-04 22:30
    Say to get from point A to point B you need 420 pulses to stepper A and 230 to stepper B. Then you could do something like this:

    FOR n = 1 to 10  ' outer loop, commen factor of 10
    For idx=0 to 23 * 42   ' least common multiple
      x.bit0 = phase // 23   ' bit 0 = 0 once every 23
      x.bit1 = phase // 42   ' bit 1 = 0 once every 42
      OUTA = ~x      ' transfer pattern to output, high pulse when step needed
      OUTA = 0
    NEXT
    NEXT     ' pin p0 has pulsed 420 times, pin p1 230 times
    
    



    That would assume you just have to feed pulses to the stepper controller. If the Stamp has to sequence the stepper, the OUTA = ~x would be replaced by the state machine code for sequencing.

    If the ratio is 1 in 42, say 42000 steps on one axis and 1000 on the other, the outside common factor loop would execute n = 1000 times, and the inner loop would execute 42 times with a least common multiple of 1*42. The speed of motion from the program side is deterimined by the least common multiiple, so a pause may be included so as to adjust the speed if it turns out to be too fast when the lcm is low. Whether it is fast enough depends on the the machine, I guess.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-04-04 23:11
    Tracy,

    Shouldn't idx and phase be the same variable?

    -Phil
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-04-04 23:28
    Right, Phil. Good eye. And it should go from zero to LCM-1to get the count right. In this the phase accumulator is advancing by one in each time step. Another try at it...


    FOR n = 1 to 10  ' outer loop, common factor of 10
    For phase=0 to 23 * 42-1   ' least common multiple
      x.bit0 = phase // 23   ' bit 0 = 0 once every 23
      x.bit1 = phase // 42   ' bit 1 = 0 once every 42
      OUTA = ~x      ' transfer pattern to output, high pulse when step needed
      OUTA = 0
    NEXT
    NEXT     ' pin p0 has pulsed 420 times, pin p1 230 times
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-04-04 23:57
    If your stepper motors can handle a little phase jitter (and most can) there's another way to do this that doesn't require the multiplication — or the overflow issues it presents. It also guarantees that the motor that moves the farthest does so at top speed. The sample code shown below draws a waveform on the debug screen instead of changing any port outputs. It also keeps track of the actual number of steps moved to verify that it's doing what it's supposed to.

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    xsteps    VAR Word
    ysteps    VAR Word
    i         VAR Word
    k         VAR Word        'Optional: Replace "k" in the program with  "xsteps MAX ysteps" if variable space is limited.
    xph       VAR Word
    yph       VAR Word
    x         VAR Bit
    y         VAR Bit
    
    movx      VAR Word        'Optional: Used for logging purposes only.
    movy      VAR Word        'Optional: Used for logging purposes only.
    
    xsteps = 31
    ysteps = 50
    
    xph = ysteps / 2
    yph = xsteps / 2
    k = xsteps MAX ysteps
    
    FOR i = 1 TO xsteps MIN ysteps
      IF xph <= k THEN   'Step X
        x = ~x
        xph = xph + ysteps
        movx = movx + 1
      ENDIF
      xph = xph - k
      IF yph <= k THEN   'Step Y
        y = ~y
        yph = yph + xsteps
        movy = movy + 1
      ENDIF
      yph = yph - k
      IF x THEN
        DEBUG " |    "
      ELSE
        DEBUG "|     "
      ENDIF
      IF y THEN DEBUG " "
      DEBUG "|", CR
    NEXT
    DEBUG "X: ", DEC movx, "  Y: ", DEC movy   'Double check the number of steps actually moved.
    
    
    


    This same algorithm can be used to draw straight lines in a bitmapped graphics array, BTW.

    -Phil
  • T ChapT Chap Posts: 4,223
    edited 2007-04-05 03:30
    As is, the pulses will be slow, even still, there should be some method to adjust the speed from the Stamps max speed capability to a lower speed to allow compensation for material, router speed, depth of cut, and tool being used. The code Phil and Tracy very cleverly devised would need a simple mod to slow it down by some rate IF needed, which could just be a simple pause X on every cycle.

    Assume .25 acme screws and 16 TPI typical on a small machine at 1/2 step, 200 step motor:

    16(threads) * 400(1/2 steps) = 6400 pulses to move the tool/table 1 inch.

    Assume a nominal cutting speed of 10 inches per minute, what kind of perfomance can be obtained with the amount of code to execute for two motors? How is the Z(router) supposed to be getting into position without a 3rd stepper?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-04-05 03:54
    I come up with 1066pps at 10ipm. That might be asking a lot from PBASIC — especially since we haven't even thrown in the ramping computations yet. (And, yes, ramping will be necessary at that pulse rate.) How are you at SX assembly programming?

    -Phil
  • T ChapT Chap Posts: 4,223
    edited 2007-04-05 04:44
    Assembly is not in my vocabulary. Spin would be very reasonanble performance at 1/2 or 1/4 steps and very simple code. Adding the Z is no load on the Propeller. I think the Stamp idea is DOA for real world application for 2 motors, and once the OP gets into this project, I predict the 3rd motor will be right around the corner. The last resort on the Stamp would be 4 or 8 TPI acme, but then you are looking at .5" screws, but would get closer to some usable speed that wouldn't drive you crazy. That means a trade off for torque and resolution though, on a hobby machine maybe who cares.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-04-05 16:44
    Curious1,

    If you haven't already, do check out Sid Weaver's super mill. H worked first in PBASIC on the Stamp and more recently in Spin on the Propeller. I think a lot of the PBASIC discussion about it is in the Sandbox forum in the immense community milling machine thread. There are also lot of deep threads about CNC on the propeller forum.

    Phil,

    I'm staring at your incremental algorithm, and I'm going to have to try it out to see it in operation. Is that a standard bitmap algorithm?

    I saw the problem about overflow of the multiplication on the Stamp with the 16 bit word. That is why I broke it down into nested loops using the least common multiple, but of course it would have to be massaged further if the X and Y step numbers are do not have a common factor. As an aside, the X and Y numbers can be broken down as a continued fraction, and the convergents can always be used to guide a motion pattern in shorter cycles. To put that in relevant terms, approximating Y/X is always being done on an integer grid by making a series of shorter excursions that have integer coordinates, Y1/X1...Y2/X2... so that in the end all the Yn and Xn add up to Y and X. The continued fraction is essentially a description of the optiimal path, that is, the path through the grid that stays closest to the slop Y/X. The final convergent is alway the least common multiple of Y and X that could be used in the algorithm I posted.

    I'll attached a little program that demonstrates how to extract the convergents of a continued fraction in PBASIC.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-04-05 17:31
    Tracy,
    Tracy Allen said...
    Is that a standard bitmap algorithm?
    I don't know, frankly. It's something I came up with when I was working on the drawing algorithm for the PropCAM firmware. It's fast, since at least one axis gets moved at every step. (In a way, it's just a compression of your algorithm, with the non-motion steps removed.) But the phase jitter might be objectionable in certain apps. In that case, a multiple of xsteps or ysteps can be added when a step is taken to smooth things out, with the time interval between iterations reduced accordingly. Of course, overflow becomes a possibility again, if the multple times the maximum number of steps on an axis exceeds the word size.

    The continued fractions stuff is quite intriguing. It's amazing how much can be accomplished with integer operations. I fear that a lot of interesting math is going to get lost when floating point processors find their way into "low level" microcontrollers!

    -Phil
  • curious1curious1 Posts: 104
    edited 2007-04-06 14:25
    Thanks for everyones input.
    I'll read the CNC topics on the other forums.

    When GW talks weird, they call it a BUSHISM.
    When Gore talks weird do they call it an AlGorism ?· rolleyes.gif
Sign In or Register to comment.