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:
Fri Mar 14 14:14:55 2014 +0000
Revision:
3:7aaf0072cc22
Parent:
2:b3c668ec43ac
CNC Machine driver, emulates PC LPT port, docs are wip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonFreeman 3:7aaf0072cc22 1 /**
JonFreeman 3:7aaf0072cc22 2 Programme "cnc__machine_driver_3"
JonFreeman 3:7aaf0072cc22 3 Author Jon Freeman
JonFreeman 3:7aaf0072cc22 4
JonFreeman 3:7aaf0072cc22 5 Accepts subset of 'G Code' CNC machine instructions as input from USB serial
JonFreeman 3:7aaf0072cc22 6 Generates 'Step' and 'Direction' signals for X, Y and Z axes, signals suit
JonFreeman 3:7aaf0072cc22 7 standard stepper / servo drive systems.
JonFreeman 3:7aaf0072cc22 8 Also generates similar Step and Dir for spindle
JonFreeman 3:7aaf0072cc22 9 Intended to connect to 25 pin 'D' connector to emulate old PC 'LPT' port
JonFreeman 3:7aaf0072cc22 10 for easy swap with PC/Mach driven CNC systems.
JonFreeman 3:7aaf0072cc22 11 Tested driving Sieg KX3 CNC mill.
JonFreeman 3:7aaf0072cc22 12
JonFreeman 3:7aaf0072cc22 13 Designed to compile and run on:
JonFreeman 3:7aaf0072cc22 14 Mbed LPC1768
JonFreeman 3:7aaf0072cc22 15 Freescale KL25Z
JonFreeman 3:7aaf0072cc22 16 Freescale KL46Z
JonFreeman 3:7aaf0072cc22 17 */
JonFreeman 3:7aaf0072cc22 18 using namespace std;
JonFreeman 3:7aaf0072cc22 19
JonFreeman 3:7aaf0072cc22 20 typedef float fl_typ; //
JonFreeman 3:7aaf0072cc22 21 const signed long
JonFreeman 3:7aaf0072cc22 22 motor_step_per_rev = 200, // KX3 has 200 step per rev steppers
JonFreeman 3:7aaf0072cc22 23 micro_steps = 10, // Arc Eurotrade choice 2,4,5,8,10,16,20,25,32,40,50,64,100,125,128
JonFreeman 3:7aaf0072cc22 24 interrupt_period_us = 25, //32, //24.0, //16 is possible with Mbed LPC1768
JonFreeman 3:7aaf0072cc22 25 interrupt_freq_Hz = 1000000 / interrupt_period_us, // Serious limit when doing all in software, no real limit with FPGA
JonFreeman 3:7aaf0072cc22 26 spindle_factor = interrupt_period_us * 4280, // Derived empirically
JonFreeman 3:7aaf0072cc22 27 spindle_max = 5000; // Stated in KX3 spec
JonFreeman 3:7aaf0072cc22 28
JonFreeman 3:7aaf0072cc22 29 const fl_typ ball_screw_pitch_mm = 4.0, // KX3 has 4mm pitch ball screws
JonFreeman 3:7aaf0072cc22 30 pulses_per_mm = micro_steps * motor_step_per_rev / ball_screw_pitch_mm,
JonFreeman 3:7aaf0072cc22 31 n_for_onemmpermin = pulses_per_mm * interrupt_period_us * pow(2.0,31) / 60000000.0; // n pir to produce 1mm/min travel
JonFreeman 3:7aaf0072cc22 32
JonFreeman 3:7aaf0072cc22 33 const signed long
JonFreeman 3:7aaf0072cc22 34 max_pulse_freq_Hz = interrupt_freq_Hz * 4 / 13, // / 3.25, // strictly 2.0, but allow a good margin
JonFreeman 3:7aaf0072cc22 35 feed_rate_max = 60 * max_pulse_freq_Hz / pulses_per_mm; // units mm per minute
JonFreeman 3:7aaf0072cc22 36 //The output frequency F<sub>out</sub> = 'Kernel Speed (Hz)' * n / (2 to the power of 32)
JonFreeman 3:7aaf0072cc22 37
JonFreeman 3:7aaf0072cc22 38 //#define Fourth_Axis
JonFreeman 3:7aaf0072cc22 39 //#define SPI_Enable
JonFreeman 3:7aaf0072cc22 40 #define I2C_Enable
JonFreeman 3:7aaf0072cc22 41 #define ESTOP 0x100 // bits used in input reading KX3 limit and EStop switches
JonFreeman 2:b3c668ec43ac 42 #define XLIM 1
JonFreeman 2:b3c668ec43ac 43 #define YLIM 2
JonFreeman 2:b3c668ec43ac 44 #define ZLIM 4
JonFreeman 2:b3c668ec43ac 45 #define UNKN 8
JonFreeman 3:7aaf0072cc22 46
JonFreeman 3:7aaf0072cc22 47 const fl_typ TWO_PI = 8.0 * atan(1.0);
JonFreeman 3:7aaf0072cc22 48 const fl_typ epsilon = 1e-5;
JonFreeman 1:66ee619f206b 49 struct pirbufgrain {
JonFreeman 3:7aaf0072cc22 50 fl_typ x,
JonFreeman 1:66ee619f206b 51 y,
JonFreeman 1:66ee619f206b 52 z,
JonFreeman 3:7aaf0072cc22 53 distance_code,
JonFreeman 1:66ee619f206b 54 f_rate;
JonFreeman 1:66ee619f206b 55 } ;
JonFreeman 0:5d0f270bfc87 56
JonFreeman 0:5d0f270bfc87 57 struct singleGparam { // Place to put all we know about 'x' or 'j' etc parameter from G Code line
JonFreeman 3:7aaf0072cc22 58 fl_typ flt;
JonFreeman 0:5d0f270bfc87 59 unsigned long ul;
JonFreeman 0:5d0f270bfc87 60 int i, c;
JonFreeman 0:5d0f270bfc87 61 bool changed; // Flagged true when new value for this axis found in Gcode line, false otherwise
JonFreeman 0:5d0f270bfc87 62 } ;
JonFreeman 0:5d0f270bfc87 63
JonFreeman 0:5d0f270bfc87 64 struct Gparams { // Where possibly messy G code line gets ordered and sorted into
JonFreeman 0:5d0f270bfc87 65 struct singleGparam x, y, z, i, j, r, a, b, c, d; // After sorting, know where to find any X, Y etc values !
JonFreeman 0:5d0f270bfc87 66 } ;
JonFreeman 0:5d0f270bfc87 67