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 06 08:45:02 2014 +0000
Revision:
1:66ee619f206b
Parent:
0:5d0f270bfc87
Child:
2:b3c668ec43ac
Currently creates 3 sets of Step and Dir signals for stepper motor drivers.  Accepts via putty etc, as yet minimal set of 'G Codes' for CNC - G0, G1, G2 and G3.  Still wip.

Who changed what in which revision?

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