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

Committer:
JonFreeman
Date:
Thu Feb 20 09:27:18 2014 +0000
Revision:
2:b3c668ec43ac
Parent:
1:66ee619f206b
Child:
3:7aaf0072cc22
As used to produce Lissajous patterns for EiM articles.  XYZ axis good, Axis A not implemented, spindle 'S' working.  Using MODSERIAL, therefore no longer compatible with KL46Z, good for KL25Z and Mbed LPC1768

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonFreeman 0:5d0f270bfc87 1 #include "mbed.h"
JonFreeman 2:b3c668ec43ac 2 //#include "rtos.h"
JonFreeman 2:b3c668ec43ac 3 #include "MODSERIAL.h"
JonFreeman 1:66ee619f206b 4 #include "cnc.h"
JonFreeman 2:b3c668ec43ac 5 extern MODSERIAL pc;
JonFreeman 2:b3c668ec43ac 6 //extern Serial pc;
JonFreeman 2:b3c668ec43ac 7 extern void move_to_XYZ (struct pirbufgrain & ins) ;
JonFreeman 0:5d0f270bfc87 8 /*
JonFreeman 0:5d0f270bfc87 9 This file contains one function:
JonFreeman 0:5d0f270bfc87 10 void lissajous () ;
JonFreeman 0:5d0f270bfc87 11 The purpose is to replicate a list of XY coordinates produced by a G Code programme written to run on Mach3 software
JonFreeman 0:5d0f270bfc87 12 to be used to set NCOs in sequence to prove this simple code running on a 'mbed' or 'Freescale KL25Z'
JonFreeman 0:5d0f270bfc87 13 does correctly drive the Sieg KX3 CNC mill, just the same as using the pc / Mach3 setup.
JonFreeman 0:5d0f270bfc87 14
JonFreeman 0:5d0f270bfc87 15 Thus far we have proved only that both finish at the same point (give or take a few microns)
JonFreeman 0:5d0f270bfc87 16 */
JonFreeman 2:b3c668ec43ac 17 //bool liss_active = false; // global flag used to prevent running more than once at a time
JonFreeman 0:5d0f270bfc87 18
JonFreeman 2:b3c668ec43ac 19 void lissajous (double feed_rate) {
JonFreeman 1:66ee619f206b 20 const double PI = 4.0 * atan(1.0), //3.142ish but more accurate
JonFreeman 1:66ee619f206b 21 Deg2Rad = PI / 180.0, // degrees to radian conversion factor
JonFreeman 2:b3c668ec43ac 22 MaxX = 40.0, // Plot size X to move +/- MaxX
JonFreeman 2:b3c668ec43ac 23 MaxY = 25.0, // Plot size Y to move +/- MaxY
JonFreeman 1:66ee619f206b 24 StartAngDegX = 0.0,
JonFreeman 2:b3c668ec43ac 25 StartAngDegY = 10.0,
JonFreeman 2:b3c668ec43ac 26 FreqRatio = 0.255;
JonFreeman 1:66ee619f206b 27 const int StepsPerRevX = 100,
JonFreeman 1:66ee619f206b 28 NumofXCycles = 16;
JonFreeman 1:66ee619f206b 29 const double AngleStepX = (2.0 * PI / StepsPerRevX),
JonFreeman 1:66ee619f206b 30 AngleStepY = AngleStepX * FreqRatio;
JonFreeman 0:5d0f270bfc87 31
JonFreeman 1:66ee619f206b 32 //void lissajous (void const * arg_string) {
JonFreeman 1:66ee619f206b 33 struct pirbufgrain Coords;
JonFreeman 0:5d0f270bfc87 34 double AngleX = StartAngDegX * Deg2Rad,
JonFreeman 1:66ee619f206b 35 AngleY = StartAngDegY * Deg2Rad;
JonFreeman 1:66ee619f206b 36
JonFreeman 2:b3c668ec43ac 37 // pc.printf("In lissajous func, Loading Lissajous\r\n");
JonFreeman 2:b3c668ec43ac 38 // while (1) {
JonFreeman 2:b3c668ec43ac 39 // while (!liss_active) // Other code to activate
JonFreeman 2:b3c668ec43ac 40 // osThreadYield();
JonFreeman 1:66ee619f206b 41 // liss_active = true; // this hapens in activating code
JonFreeman 1:66ee619f206b 42 pc.printf("In lissajous func, Starting Lissajous, has been activated\r\n");
JonFreeman 2:b3c668ec43ac 43 Coords.x = 0.0;
JonFreeman 2:b3c668ec43ac 44 Coords.y = 0.0;
JonFreeman 2:b3c668ec43ac 45 Coords.z = 12.7;
JonFreeman 2:b3c668ec43ac 46 Coords.f_rate = feed_rate;
JonFreeman 2:b3c668ec43ac 47 move_to_XYZ (Coords);
JonFreeman 1:66ee619f206b 48 Coords.x = MaxX * cos(AngleX); // Coordinates of start position
JonFreeman 1:66ee619f206b 49 Coords.y = MaxY * sin(AngleY); // assembled into packet for motion controller
JonFreeman 2:b3c668ec43ac 50 move_to_XYZ (Coords);
JonFreeman 1:66ee619f206b 51 Coords.z = 0.0;
JonFreeman 2:b3c668ec43ac 52 move_to_XYZ (Coords);
JonFreeman 1:66ee619f206b 53
JonFreeman 1:66ee619f206b 54 for (int i = 0; i < NumofXCycles; i++) { // Outer loop 'NumofXCycles' times
JonFreeman 1:66ee619f206b 55 for (int j = 0; j < StepsPerRevX; j++) { // Inner loop 'StepsPerRevX' times
JonFreeman 1:66ee619f206b 56 AngleX += AngleStepX;
JonFreeman 1:66ee619f206b 57 AngleY += AngleStepY;
JonFreeman 1:66ee619f206b 58 Coords.x = MaxX * cos(AngleX);
JonFreeman 1:66ee619f206b 59 Coords.y = MaxY * sin(AngleY);
JonFreeman 2:b3c668ec43ac 60 move_to_XYZ (Coords);
JonFreeman 2:b3c668ec43ac 61 // osThreadYield();
JonFreeman 1:66ee619f206b 62 }
JonFreeman 2:b3c668ec43ac 63 pc.printf("liss cyc %d\r\n", i);
JonFreeman 1:66ee619f206b 64 }
JonFreeman 2:b3c668ec43ac 65 Coords.z = 100.0;
JonFreeman 2:b3c668ec43ac 66 move_to_XYZ (Coords);
JonFreeman 2:b3c668ec43ac 67 Coords.x = 0.0;
JonFreeman 2:b3c668ec43ac 68 Coords.y = 0.0;
JonFreeman 2:b3c668ec43ac 69 move_to_XYZ (Coords);
JonFreeman 1:66ee619f206b 70
JonFreeman 1:66ee619f206b 71 pc.printf("Lissajous finish point X%f, Y%f\r\n", Coords.x, Coords.y);
JonFreeman 2:b3c668ec43ac 72 // liss_active = false;
JonFreeman 1:66ee619f206b 73 pc.printf("Leaving liss\r\n");
JonFreeman 2:b3c668ec43ac 74 // osThreadYield(); // terminate would be better here
JonFreeman 2:b3c668ec43ac 75 // } // end of while(1)
JonFreeman 0:5d0f270bfc87 76 }
JonFreeman 0:5d0f270bfc87 77
JonFreeman 0:5d0f270bfc87 78 /*
JonFreeman 0:5d0f270bfc87 79 The complete Mach3 G Code programme listing "lissajous.txt" follows :-
JonFreeman 0:5d0f270bfc87 80 */
JonFreeman 0:5d0f270bfc87 81
JonFreeman 0:5d0f270bfc87 82 /*
JonFreeman 0:5d0f270bfc87 83 ; This Section to put machine into known, safe state
JonFreeman 0:5d0f270bfc87 84 M5 ; Stop spindle
JonFreeman 0:5d0f270bfc87 85 G17 ; Select XY plane
JonFreeman 0:5d0f270bfc87 86 G21 ; Units are mm
JonFreeman 0:5d0f270bfc87 87 G40 ; Cancel cutter radius compensation
JonFreeman 0:5d0f270bfc87 88 G49 ; Cancel tool length offset
JonFreeman 0:5d0f270bfc87 89 G61 ; Exact stop
JonFreeman 0:5d0f270bfc87 90 G50 ; Reset all scale factors to 1.0
JonFreeman 0:5d0f270bfc87 91 G90 ; Absolute distance mode
JonFreeman 0:5d0f270bfc87 92 G94 ; Feed mm per minute mode - as mm selected above by G21
JonFreeman 0:5d0f270bfc87 93 ; Title: Lissajous Pattern Generator 2014
JonFreeman 0:5d0f270bfc87 94 ; Programme Name "lissajous.txt"
JonFreeman 0:5d0f270bfc87 95 ; Author: Jon Freeman
JonFreeman 0:5d0f270bfc87 96 ; Date: Feb 2014
JonFreeman 0:5d0f270bfc87 97
JonFreeman 0:5d0f270bfc87 98 ; Demo code used to demonstrate Freescale FRDM-KL25Z computer board
JonFreeman 0:5d0f270bfc87 99 ; driving a Sieg KX3 CNC mill without PC, and without Mach3 !!
JonFreeman 0:5d0f270bfc87 100
JonFreeman 0:5d0f270bfc87 101 ; _____________________________________________
JonFreeman 0:5d0f270bfc87 102 ; Put user alterable parameter values in this section
JonFreeman 0:5d0f270bfc87 103 ; User is invited to alter the 6 parameters in this section.
JonFreeman 0:5d0f270bfc87 104
JonFreeman 0:5d0f270bfc87 105 #10 = 6.40 ; Max 'X' excursion
JonFreeman 0:5d0f270bfc87 106 #11 = 3.20 ; Max 'Y' excursion
JonFreeman 0:5d0f270bfc87 107 #12 = 0.0 ; Start angle of 'X'
JonFreeman 0:5d0f270bfc87 108 #13 = 10.0 ; Start angle of 'Y'
JonFreeman 0:5d0f270bfc87 109 #14 = 0.254 ; Frequency ratio of X and Y signals
JonFreeman 0:5d0f270bfc87 110 #15 = 100 ; Int Steps per 2PI of X
JonFreeman 0:5d0f270bfc87 111 #16 = 16 ; Int Number of whole cycles of 'X'
JonFreeman 0:5d0f270bfc87 112 ;
JonFreeman 0:5d0f270bfc87 113 ; Programme starts here
JonFreeman 0:5d0f270bfc87 114
JonFreeman 0:5d0f270bfc87 115 #50 = [#10 * cos[#12]] ;Start X coord
JonFreeman 0:5d0f270bfc87 116 #51 = [#11 * sin[#13]] ;Start Y coord
JonFreeman 0:5d0f270bfc87 117 #52 = [360.0 / #15] ;Angle step X
JonFreeman 0:5d0f270bfc87 118 #53 = [#52 * #14] ;Angle step Y
JonFreeman 0:5d0f270bfc87 119 G0 X#50 Y#51
JonFreeman 0:5d0f270bfc87 120 M98 P 1000 L #16 ;Execute subroutine 'Numof X Cycles' times
JonFreeman 0:5d0f270bfc87 121 M5 M30 ; Stop, end and rewind
JonFreeman 0:5d0f270bfc87 122
JonFreeman 0:5d0f270bfc87 123 O 1000 ; Subroutine executed once per complete turn of 'X'
JonFreeman 0:5d0f270bfc87 124 M98 P 2000 L #15 ;Execute the subroutine and repeat 'Steps per Rev' times
JonFreeman 0:5d0f270bfc87 125 M99 ; Return
JonFreeman 0:5d0f270bfc87 126
JonFreeman 0:5d0f270bfc87 127 O 2000 ; Subroutine executed 'Numof X Cycles' * 'Steps per Rev' times
JonFreeman 0:5d0f270bfc87 128 #12 = [#12 + #52] ; Update X angle
JonFreeman 0:5d0f270bfc87 129 #13 = [#13 + #53] ; Update X angle
JonFreeman 0:5d0f270bfc87 130 #50 = [#10 * cos[#12]] ;Update X coord
JonFreeman 0:5d0f270bfc87 131 #51 = [#11 * sin[#13]] ;Update Y coord
JonFreeman 0:5d0f270bfc87 132 G1 X#50 Y#51
JonFreeman 0:5d0f270bfc87 133 M99 ; Return
JonFreeman 0:5d0f270bfc87 134 */
JonFreeman 0:5d0f270bfc87 135