Hello
I have been trying to understand the code used in GcodeCNCDemo4AxisRAMPS, especially the algorithm used to draw a line. I have been reading several articles on Bresenham's line algorithm.
derivation
code example
I understand above code and derivation
But they only explain about slope less than 1 and code used is a lot different than the one implemented here.
Can anyone help me understand it? Here's the code:
Code:
/**
* Uses bresenham's line algorithm to move both motors
* @input newx the destination x position
* @input newy the destination y position
**/
void line(float newx,float newy,float newz,float newe) {
a[0].delta = newx-px;
a[1].delta = newy-py;
a[2].delta = newz-pz;
a[3].delta = newe-pe;
long i,j,maxsteps=0;
for(i=0; i<NUM_AXIES; ++i) {
a[i].absdelta = abs(a[i].delta);
a[i].dir = a[i].delta > 0 ? 1:-1;
if( maxsteps < a[i].absdelta ) maxsteps = a[i].absdelta;
}
for(i=0; i<NUM_AXIES; ++i) {
a[i].over=maxsteps/2;
}
#ifdef VERBOSE
Serial.println(F("Start >"));
#endif
for(i=0; i<maxsteps; ++i) {
for(j=0; j<NUM_AXIES; ++j) {
a[j].over += a[j].absdelta;
if(a[j].over >= maxsteps) {
a[j].over -= maxsteps;
onestep(j,a[j].dir);
}
}
pause(step_delay);
}
#ifdef VERBOSE
Serial.println(F("< Done."));
#endif
position(newx,newy,newz,newe);
}
Above code is for moving stepper motors in CNC machine using arduino..
Thanks