Code to drive a CNC machine via a PC LPT port lookalike 25 pin 'D', experiment in 'PC/Mach3' replacement. Designed to compile and run on mbed LPC1768, Freescale KL25Z and Freescale KL46Z. Proved on LPC1768 and KL25Z, problem with serial port on KL46Z. Reads subset of 'G Codes' through usb/serial port and drives 3 stepper/servo drives for X, Y and Z, also similar Step/Dir outputs for spindle motor control. Emulates PC LPT, outputs 'charge pump', proved driving Seig KX3 CNC mill

Dependencies:   MODSERIAL mbed

lissajous.cpp

Committer:
JonFreeman
Date:
2014-01-31
Revision:
0:5d0f270bfc87
Child:
1:66ee619f206b

File content as of revision 0:5d0f270bfc87:

#include "mbed.h"
//extern  Serial pc;
/*
This file contains one function:
void    lissajous   ()  ;
The purpose is to replicate a list of XY coordinates produced by a G Code programme written to run on Mach3 software
to be used to set NCOs in sequence to prove this simple code running on a 'mbed' or 'Freescale KL25Z'
does correctly drive the Sieg KX3 CNC mill, just the same as using the pc / Mach3 setup.

Thus far we have proved only that both finish at the same point (give or take a few microns)
*/

const double    Deg2Rad     = atan(1.0) / 45.0,
                HALFPI      = 2.0 * atan(1.0),
                TWOPI       = 8.0 * atan(1.0),
                PI          = 4.0 * atan(1.0),
                MaxX            = 6.40,
                MaxY            = 3.20,
                StartAngDegX    = 0.0,
                StartAngDegY    = 10.0,
                FreqRatio       = 0.254;
const int       StepsPerRevX    = 100,
                NumofXCycles    = 16;

void    lissajous   ()  {
    double  AngleX = StartAngDegX * Deg2Rad,
            AngleY = StartAngDegY * Deg2Rad,
            AngleStepX  = (TWOPI / StepsPerRevX),
            AngleStepY  = AngleStepX * FreqRatio,
            X_Coord = MaxX * cos(AngleX),
            Y_Coord = MaxY * sin(AngleY);

    for (int i = 0; i < NumofXCycles; i++)   {
        for (int j = 0; j < StepsPerRevX; j++)   {
            AngleX += AngleStepX;
            AngleY += AngleStepY;
            X_Coord = MaxX * cos(AngleX);
            Y_Coord = MaxY * sin(AngleY);
        }    
    }
//    pc.printf("Lissajous finish point X%f, Y%f\r\n", X_Coord, Y_Coord);
}

/*
The complete Mach3 G Code programme listing "lissajous.txt" follows :-
*/

/*
; This Section to put machine into known, safe state
M5  ;   Stop spindle
G17 ;   Select XY plane
G21 ;   Units are mm
G40 ;   Cancel cutter radius compensation
G49 ;   Cancel tool length offset
G61 ;   Exact stop
G50 ;   Reset all scale factors to 1.0
G90 ;   Absolute distance mode
G94 ;   Feed mm per minute mode - as mm selected above by G21
;   Title:      Lissajous Pattern Generator 2014
;   Programme Name  "lissajous.txt"
;   Author:     Jon Freeman
;   Date:       Feb 2014

;   Demo code used to demonstrate Freescale FRDM-KL25Z computer board
;   driving a Sieg KX3 CNC mill without PC, and without Mach3 !!

; _____________________________________________
; Put user alterable parameter values in this section
; User is invited to alter the 6 parameters in this section.

#10 = 6.40  ;   Max 'X' excursion
#11 = 3.20  ;   Max 'Y' excursion
#12 = 0.0   ;   Start angle of 'X'
#13 = 10.0  ;   Start angle of 'Y'
#14 = 0.254 ;   Frequency ratio of X and Y signals
#15 = 100   ;   Int Steps per 2PI of X
#16 = 16    ;   Int Number of whole cycles of 'X'
;
;   Programme starts here

#50 = [#10 * cos[#12]]  ;Start X coord
#51 = [#11 * sin[#13]]  ;Start Y coord
#52 = [360.0 / #15]     ;Angle step X
#53 = [#52 * #14]       ;Angle step Y
G0 X#50 Y#51
M98 P   1000    L #16   ;Execute subroutine 'Numof X Cycles' times
M5 M30  ;   Stop, end and rewind

O 1000  ;   Subroutine executed once per complete turn of 'X'
M98 P   2000    L #15   ;Execute the subroutine and repeat 'Steps per Rev' times
M99 ;   Return

O 2000  ;   Subroutine executed 'Numof X Cycles' * 'Steps per Rev' times
#12 = [#12 + #52]   ;   Update X angle
#13 = [#13 + #53]   ;   Update X angle
#50 = [#10 * cos[#12]]  ;Update X coord
#51 = [#11 * sin[#13]]  ;Update Y coord
G1 X#50 Y#51
M99 ;   Return
*/