Generating Stepped Motion

by Ivan Hamilton 7/28/2012 6:13:00 PM

The standard parallel port "pulsing" setup in Mach3 uses a set "kernel speed" (25-100kHz) and this is the rate at which it decides to output a step pulse (or not). It is therefore also the maximum pulsing rate. The implementation details are specific to Mach3, but how would you determine an "even" pulsing pattern?

Let's rephrase our problem: How do you evenly take Y steps over X periods (or, determine which points in a 2 dimensional raster should be plotted in order to form a close approximation of a straight line between two given points). Bresenham's line algorithm is a way to achieve this.

At the start, we're already 1/2 way to crossing into the next Y position. For each increment in X, we move Y/X closer. Once we cross into the next Y position, we're another 1 away from the next crossing.

This could be expressed as:

var accumulator = 0.5;
loop (X times)
	accumulator = accumulator + Y/X;
	if (accumulator >= 1)
		accumulator = accumulator - 1;

We can eliminate the fractional 1/X & 0.5 and use only integer math by multiplying everything by 2X.

var accumulator = X;
loop (X times)
	accumulator = accumulator + 2Y;
	if (accumulator >= 2X)
		accumulator = accumulator - 2X;

We can also reduce calculation load by precalculating and storing 2Y & 2X.

This algorithm gives nice even pulse patterns. An example of 0 to a maximum of 10 pulses is shown below:

 0  ..........
 1  ....X.....
 2  ..X....X..
 3  .X..X...X.
 4  .X.X..X.X.
 5  X.X.X.X.X.
 6  X.X.XX.X.X
 7  X.XXX.XX.X

The integer maths also makes this suitable for microcontrollers.

Below is an example of 3 & 7 (yellow top & blue bottom) pulses per 10 periods being generated by an AVR micro.

When we produce a continuous stream of these pulses, the pattern repeats (prior & next pattern shaded below)


So, how much processing power is consumed by this? I'm using a 256 cycle ring-buffer to precalulate 4 output channels, and inserting a 10ms delay when the buffer fills. To help measure calculation duration, I toggled a line (yellow) high during buffer re-fill. One of the output channels is display below (it's 25kHz signal is not decernable at this resolution).

Of a 16.80ms cycle, 10.40ms is "available" for other tasks. Pattern generation currently consumes ~40% of "available" CPU time. I say "available", since the CPU is constantly interrupted for short periods to update the output.

Currently rated 1.5 by 641 people

  • Currently 1.48362/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


CNC | Electronics

Related posts


Add comment



Powered by BlogEngine.NET
Original theme by Mads Kristensen

About the author

Name of author Ivan Hamilton
"My inner nerd can beat up your inner nerd."

E-mail me Send mail



<<  July 2017  >>

View posts in large calendar

Recent comments





    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2017

    Sign in