Shop OBEX P1 Docs P2 Docs Learn Events
Need some help understanding a few things. — Parallax Forums

Need some help understanding a few things.

TCTC Posts: 1,019
edited 2013-05-29 17:55 in Propeller 1
Hello all, I have hit a roadblock with my 3D printer project. I am having trouble understanding how the prop keep track of time, how long it takes to do something, how I can calculate a pulse. Most of all, understanding how timing works in general.

And for my 3D printer, it just has to move in a straight line. I found “Bresenham’s line algorithm” but I cant understand how it works, or how to make it work with the prop. I found countless examples in C, C++, and in Java, but I have never used or worked with them.

I was wondering if someone would please help me out, and help me understand these problems?

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-05-28 19:10
    This is a line method for a graphic display that I translated from C -- it might be helpful (and, yes, it works in the display).
    pub line(x1, y1, x2, y2, mode) | dx, dy, stepx, stepy, fraction
    
    '' Draw line between x1/y1 and x2/y2
    
      dy := y2 - y1
      dx := x2 - x1
    
      if (dx < 0)
        dx := -dx 
        stepx := -1 
      else
        stepx := 1 
    
      if (dy < 0)
        dy := -dy 
        stepy := -1 
      else
        stepy := 1
    
      dx <<= 1 
      dy <<= 1   
    
      pixel(x1, y1, mode)
    
      if (dx > dy)
        fraction := dy - (dx >> 1)
        repeat while (x1 <> x2)
          if (fraction => 0)
            y1 += stepy
            fraction -= dx
          x1 += stepx
          fraction += dy
          pixel(x1, y1, mode)
    
      else
        fraction := dx - (dy >> 1)
        repeat while (y1 <> y2)
          if (fraction >= 0)
            x1 += stepx
            fraction -= dy
          else
            y1 += stepy
            fraction += dx  
            pixel(x1, y1, mode)
    
  • JasonDorieJasonDorie Posts: 1,930
    edited 2013-05-29 01:15
    When plotting a 2D line, you move along two axis (X & Y). The Bresenham algorithm picks the longer of the two axis you're going to move along, and then figures out for each step along the long axis, how big of a step to make on the shorter one. If your line was moving from 0,0 to 10, 5, Bresenham would choose the X axis as the long one, because it's moving 10 steps. The short one would then move 1/2 a step for each step along the long axis.

    Bresenham is just a special case of an algorithm called a digital differential analyzer: http://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)

    The cool part of Bresenham is that is does all the math with integers. There are lots of explanations of how it works online. The one on Wikipedia is pretty good - Once you read the page I linked above, follow the link on it to the Bresenham description and work through it slowly.

    To move a pair of steppers in time, you'll have to figure out how fast you want them to move, then figure out which one has the farthest to go. That's your long axis. Send a single motor pulse to that motor for every Bresenham loop iteration. Every time the other axis (the short one) would increment and reset the error term, take a step on the smaller axis.
  • Mark_TMark_T Posts: 1,981
    edited 2013-05-29 17:01
    Time is kept track of in the CNT register, which increments every clock cycle (usually 80MHz).
  • TCTC Posts: 1,019
    edited 2013-05-29 17:48
    JonnyMac wrote: »
    This is a line method for a graphic display that I translated from C -- it might be helpful (and, yes, it works in the display).
    pub line(x1, y1, x2, y2, mode) | dx, dy, stepx, stepy, fraction
    
    '' Draw line between x1/y1 and x2/y2
    
      dy := y2 - y1
      dx := x2 - x1
    
      if (dx < 0)
        dx := -dx 
        stepx := -1 
      else
        stepx := 1 
    
      if (dy < 0)
        dy := -dy 
        stepy := -1 
      else
        stepy := 1
    
      dx <<= 1 
      dy <<= 1   
    
      pixel(x1, y1, mode)
    
      if (dx > dy)
        fraction := dy - (dx >> 1)
        repeat while (x1 <> x2)
          if (fraction => 0)
            y1 += stepy
            fraction -= dx
          x1 += stepx
          fraction += dy
          pixel(x1, y1, mode)
    
      else
        fraction := dx - (dy >> 1)
        repeat while (y1 <> y2)
          if (fraction >= 0)
            x1 += stepx
            fraction -= dy
          else
            y1 += stepy
            fraction += dx  
            pixel(x1, y1, mode)
    

    Looking at your code, I am starting to understand it better. I do have one question, What is "mode" for?
  • TCTC Posts: 1,019
    edited 2013-05-29 17:51
    JasonDorie wrote: »

    Bresenham is just a special case of an algorithm called a digital differential analyzer: http://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)

    The cool part of Bresenham is that is does all the math with integers. There are lots of explanations of how it works online. The one on Wikipedia is pretty good - Once you read the page I linked above, follow the link on it to the Bresenham description and work through it slowly.

    I tried that, but I have a major problem understanding math problems. that is why I need the help, I want to understand them better.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-05-29 17:53
    Looking at your code, I am starting to understand it better. I do have one question, What is "mode" for?


    In that program a pixel has three possible modes: 0 (off), 1 (on), or 2 (xor) -- that's not applicable to your motor movement requirements.
  • TCTC Posts: 1,019
    edited 2013-05-29 17:54
    Mark_T wrote: »
    Time is kept track of in the CNT register, which increments every clock cycle (usually 80MHz).

    So would that be every time the hub comes back to start?
    For example;
    CNT = 0
    cog1
    cog2
    cog3
    cog4
    cog5
    cog6
    cog7
    cog8
    CNT=1
  • TCTC Posts: 1,019
    edited 2013-05-29 17:55
    JonnyMac wrote: »
    In that program a pixel has three possible modes: 0 (off), 1 (on), or 2 (xor) -- that's not applicable to your motor movement requirements.

    Oh, Ok thank you.
Sign In or Register to comment.