# Thread: How does it work?

1. ## How does it work?

Question for programming oriented people:

How does CNC software such as TurboCNC calculate the timing for the step /direction outputs for multiple axes- i.e. what sort of math is involved in synchronizing the axes so a circle comes out a circle and a diagonal line comes out a diagonal line with the tiniest possible stair-stepping?

Thanks,

TD

2. Very good question. I want the answer too so I'll subscribe to this thread

I would guess you could just use the equation of a circle, (x - h)^2 + (y - k)^2 = r^2, h and k being the x and y coords of the center of the circle. With that you can solve for X or Y. From there you can determine the domain and range of the function to figure out which values to substitute.

Just a guess... I enjoy solving those sorts of problems, and I'm sure it'd be an interesting program to write. Hopefully someone who has done it before can give us a concise answer.

3. If you pay the \$60 to register it, you'll get all the code for TurboCNC. The math can get very complicated when you factor in the acceleration as well.

You can also get the source code for EMC for free. Check out http://www.linuxcnc.org

4. Originally Posted by Lionclaw
Very good question. I want the answer too so I'll subscribe to this thread

I would guess you could just use the equation of a circle, (x - h)^2 + (y - k)^2 = r^2, h and k being the x and y coords of the center of the circle. With that you can solve for X or Y. From there you can determine the domain and range of the function to figure out which values to substitute.

Just a guess... I enjoy solving those sorts of problems, and I'm sure it'd be an interesting program to write. Hopefully someone who has done it before can give us a concise answer.
I believe that TurboCNC and similar programs include this sort of math.

The more you think about it, the more complex it seems. You have to make sure that all axes reach their respective end points at the same time. Say you want to make a diagonal line in a 2 axis system. If the angle were 45 degrees, you'd simply alternate one step in x direction then one step in y direction until you got to the end point.

Now think about an arbitrary angle. Let's say you needed to go a total of 321 steps in the x driection and 751 steps in the y direction to make the line. Presumably you'd step in the y direction a couple steps, then step once in x then go back to stepping in y. You need to figure out where to insert the steps in the x direction so that in the end you land on the desired end point, and so that your line is approximately straight.

321/751=.427430093. So for 1000 steps in y direction, you need to make 427 steps in x direction, with some error (430x10-6). You get even less error if you step 10000 in Y and 4274 in X (30x10-6). Assume that amount of error is low enough. You need to figure out when to insert x steps as you put out y steps so that both will end at the same time.

Now think about making a circle of arbitrary radius... Do you turn it into a polygon then perform the same type of calculations for each side? How many sides? Or is there a way to ensure getting a circle using some other technique?

TD

5. Originally Posted by tyler_derden
If the angle were 45 degrees, you'd simply alternate one step in x direction then one step in y direction until you got to the end point.
If you did that, you'd get a stair step effect, although it would possibly be too small to see. The axis need to move simultaneously, not alternating. And as I mentioned before, throw in acceleration and it gets a LOT harder.

6. ## I have it partially figured out, I think.

You have to convert it from a distance to a time problem. Essentially, you need to output step instructions independently on the axes so that they reach their final counts at the same time.

For example, lets say we want to move the tool/table along a diagonal line 3000 steps in the x direction and 4321 steps in the y direction. The total distance to travel will be 5260.032708 steps. If you want to complete the trip at a rate of 1000 steps per second (the maximum is a function of motor construction), it will take 5.260032708 seconds. In 5.260032708 seconds, you need to complete 3000 steps on the x axis- i.e. you need 5.260032708 sec/3000 steps=1753.44236 us/step. The y axis requires 1217.31838 us/step.

If you set up a clock that runs at 1 MHz, and two counters, one for each axis, when the x axis counter which is counting the clock pulses reaches 1753, you send out a step instruction to the x axis motor, reset the counter, then resume counting. You do the same at the y axis- only output the step instruction to the y axis motor every time the y axis count reaches 1217. When you have sent 3000 steps on the x axis and 4321 steps on the y axis, you stop.

You can get greater precision by using a higher frequency clock (or lower stepping rate on the motors). If the clock runs at 10 MHz, then the x axis gets a step instruction every time the count reaches 17534 and the y axis gets a step instruction at 12173.

Time to start thinking about circles...

TD

7. Wow this brings back memories. I wrote a machine control program in basic about 10 years ago. I wanted to control a small 3 axis machine with stepping motors. I started thinking about using 2 different numbers for each axis - similar to how servos work. One would be where the steppers should be and one would be where the stepper was. if the error between the "should be" and the "where" where off by a step the stepper would be incramented to the next step to match the "should be" - if that made sense. sort of an error differance between the calculated position and the actual position.

It was a pain to get the feed rates to work - exp using basic as there was no time base. I ended up doing a loop and timing it at the start of the program to calculate how big my delay loops would be to calcuate feed rates. \

now circles - the only easy way I could think of doing them at the time was using degrees to wipe around the arc. this made it so the feed worked for me - I would calculat how many degrees per resolution I wanted around the circle and then could calculate how long the delay loop would be between each degree (or fraction of degree depending). I ran a calculation for accelleration and deaccelleration for the arcs and lines - worked pretty well.

nothing ever came of it - when I got back into cnc equipment turbocnc was out and turned circles around what I was trying to do. I was to the point of trying to do some sort of constant velosity calculations so I wouldn't slow down / speed up at each end point. this is where I couldn't spend enough time on it. I figured I would have to read ahead so many end point and figure out how much I would have to slow down for each change in direction.

I will have to see if I can find that program - it would be neet to look though it again.

sam