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

## lissajous.cpp@0:5d0f270bfc87, 2014-01-31 (annotated)

- Committer:
- JonFreeman
- Date:
- Fri Jan 31 11:16:21 2014 +0000
- Revision:
- 0:5d0f270bfc87
- Child:
- 1:66ee619f206b

First wip, tested on KL25 and KL46

### Who changed what in which revision?

User | Revision | Line number | New contents of line |
---|---|---|---|

JonFreeman | 0:5d0f270bfc87 | 1 | #include "mbed.h" |

JonFreeman | 0:5d0f270bfc87 | 2 | //extern Serial pc; |

JonFreeman | 0:5d0f270bfc87 | 3 | /* |

JonFreeman | 0:5d0f270bfc87 | 4 | This file contains one function: |

JonFreeman | 0:5d0f270bfc87 | 5 | void lissajous () ; |

JonFreeman | 0:5d0f270bfc87 | 6 | 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 | 7 | to be used to set NCOs in sequence to prove this simple code running on a 'mbed' or 'Freescale KL25Z' |

JonFreeman | 0:5d0f270bfc87 | 8 | does correctly drive the Sieg KX3 CNC mill, just the same as using the pc / Mach3 setup. |

JonFreeman | 0:5d0f270bfc87 | 9 | |

JonFreeman | 0:5d0f270bfc87 | 10 | Thus far we have proved only that both finish at the same point (give or take a few microns) |

JonFreeman | 0:5d0f270bfc87 | 11 | */ |

JonFreeman | 0:5d0f270bfc87 | 12 | |

JonFreeman | 0:5d0f270bfc87 | 13 | const double Deg2Rad = atan(1.0) / 45.0, |

JonFreeman | 0:5d0f270bfc87 | 14 | HALFPI = 2.0 * atan(1.0), |

JonFreeman | 0:5d0f270bfc87 | 15 | TWOPI = 8.0 * atan(1.0), |

JonFreeman | 0:5d0f270bfc87 | 16 | PI = 4.0 * atan(1.0), |

JonFreeman | 0:5d0f270bfc87 | 17 | MaxX = 6.40, |

JonFreeman | 0:5d0f270bfc87 | 18 | MaxY = 3.20, |

JonFreeman | 0:5d0f270bfc87 | 19 | StartAngDegX = 0.0, |

JonFreeman | 0:5d0f270bfc87 | 20 | StartAngDegY = 10.0, |

JonFreeman | 0:5d0f270bfc87 | 21 | FreqRatio = 0.254; |

JonFreeman | 0:5d0f270bfc87 | 22 | const int StepsPerRevX = 100, |

JonFreeman | 0:5d0f270bfc87 | 23 | NumofXCycles = 16; |

JonFreeman | 0:5d0f270bfc87 | 24 | |

JonFreeman | 0:5d0f270bfc87 | 25 | void lissajous () { |

JonFreeman | 0:5d0f270bfc87 | 26 | double AngleX = StartAngDegX * Deg2Rad, |

JonFreeman | 0:5d0f270bfc87 | 27 | AngleY = StartAngDegY * Deg2Rad, |

JonFreeman | 0:5d0f270bfc87 | 28 | AngleStepX = (TWOPI / StepsPerRevX), |

JonFreeman | 0:5d0f270bfc87 | 29 | AngleStepY = AngleStepX * FreqRatio, |

JonFreeman | 0:5d0f270bfc87 | 30 | X_Coord = MaxX * cos(AngleX), |

JonFreeman | 0:5d0f270bfc87 | 31 | Y_Coord = MaxY * sin(AngleY); |

JonFreeman | 0:5d0f270bfc87 | 32 | |

JonFreeman | 0:5d0f270bfc87 | 33 | for (int i = 0; i < NumofXCycles; i++) { |

JonFreeman | 0:5d0f270bfc87 | 34 | for (int j = 0; j < StepsPerRevX; j++) { |

JonFreeman | 0:5d0f270bfc87 | 35 | AngleX += AngleStepX; |

JonFreeman | 0:5d0f270bfc87 | 36 | AngleY += AngleStepY; |

JonFreeman | 0:5d0f270bfc87 | 37 | X_Coord = MaxX * cos(AngleX); |

JonFreeman | 0:5d0f270bfc87 | 38 | Y_Coord = MaxY * sin(AngleY); |

JonFreeman | 0:5d0f270bfc87 | 39 | } |

JonFreeman | 0:5d0f270bfc87 | 40 | } |

JonFreeman | 0:5d0f270bfc87 | 41 | // pc.printf("Lissajous finish point X%f, Y%f\r\n", X_Coord, Y_Coord); |

JonFreeman | 0:5d0f270bfc87 | 42 | } |

JonFreeman | 0:5d0f270bfc87 | 43 | |

JonFreeman | 0:5d0f270bfc87 | 44 | /* |

JonFreeman | 0:5d0f270bfc87 | 45 | The complete Mach3 G Code programme listing "lissajous.txt" follows :- |

JonFreeman | 0:5d0f270bfc87 | 46 | */ |

JonFreeman | 0:5d0f270bfc87 | 47 | |

JonFreeman | 0:5d0f270bfc87 | 48 | /* |

JonFreeman | 0:5d0f270bfc87 | 49 | ; This Section to put machine into known, safe state |

JonFreeman | 0:5d0f270bfc87 | 50 | M5 ; Stop spindle |

JonFreeman | 0:5d0f270bfc87 | 51 | G17 ; Select XY plane |

JonFreeman | 0:5d0f270bfc87 | 52 | G21 ; Units are mm |

JonFreeman | 0:5d0f270bfc87 | 53 | G40 ; Cancel cutter radius compensation |

JonFreeman | 0:5d0f270bfc87 | 54 | G49 ; Cancel tool length offset |

JonFreeman | 0:5d0f270bfc87 | 55 | G61 ; Exact stop |

JonFreeman | 0:5d0f270bfc87 | 56 | G50 ; Reset all scale factors to 1.0 |

JonFreeman | 0:5d0f270bfc87 | 57 | G90 ; Absolute distance mode |

JonFreeman | 0:5d0f270bfc87 | 58 | G94 ; Feed mm per minute mode - as mm selected above by G21 |

JonFreeman | 0:5d0f270bfc87 | 59 | ; Title: Lissajous Pattern Generator 2014 |

JonFreeman | 0:5d0f270bfc87 | 60 | ; Programme Name "lissajous.txt" |

JonFreeman | 0:5d0f270bfc87 | 61 | ; Author: Jon Freeman |

JonFreeman | 0:5d0f270bfc87 | 62 | ; Date: Feb 2014 |

JonFreeman | 0:5d0f270bfc87 | 63 | |

JonFreeman | 0:5d0f270bfc87 | 64 | ; Demo code used to demonstrate Freescale FRDM-KL25Z computer board |

JonFreeman | 0:5d0f270bfc87 | 65 | ; driving a Sieg KX3 CNC mill without PC, and without Mach3 !! |

JonFreeman | 0:5d0f270bfc87 | 66 | |

JonFreeman | 0:5d0f270bfc87 | 67 | ; _____________________________________________ |

JonFreeman | 0:5d0f270bfc87 | 68 | ; Put user alterable parameter values in this section |

JonFreeman | 0:5d0f270bfc87 | 69 | ; User is invited to alter the 6 parameters in this section. |

JonFreeman | 0:5d0f270bfc87 | 70 | |

JonFreeman | 0:5d0f270bfc87 | 71 | #10 = 6.40 ; Max 'X' excursion |

JonFreeman | 0:5d0f270bfc87 | 72 | #11 = 3.20 ; Max 'Y' excursion |

JonFreeman | 0:5d0f270bfc87 | 73 | #12 = 0.0 ; Start angle of 'X' |

JonFreeman | 0:5d0f270bfc87 | 74 | #13 = 10.0 ; Start angle of 'Y' |

JonFreeman | 0:5d0f270bfc87 | 75 | #14 = 0.254 ; Frequency ratio of X and Y signals |

JonFreeman | 0:5d0f270bfc87 | 76 | #15 = 100 ; Int Steps per 2PI of X |

JonFreeman | 0:5d0f270bfc87 | 77 | #16 = 16 ; Int Number of whole cycles of 'X' |

JonFreeman | 0:5d0f270bfc87 | 78 | ; |

JonFreeman | 0:5d0f270bfc87 | 79 | ; Programme starts here |

JonFreeman | 0:5d0f270bfc87 | 80 | |

JonFreeman | 0:5d0f270bfc87 | 81 | #50 = [#10 * cos[#12]] ;Start X coord |

JonFreeman | 0:5d0f270bfc87 | 82 | #51 = [#11 * sin[#13]] ;Start Y coord |

JonFreeman | 0:5d0f270bfc87 | 83 | #52 = [360.0 / #15] ;Angle step X |

JonFreeman | 0:5d0f270bfc87 | 84 | #53 = [#52 * #14] ;Angle step Y |

JonFreeman | 0:5d0f270bfc87 | 85 | G0 X#50 Y#51 |

JonFreeman | 0:5d0f270bfc87 | 86 | M98 P 1000 L #16 ;Execute subroutine 'Numof X Cycles' times |

JonFreeman | 0:5d0f270bfc87 | 87 | M5 M30 ; Stop, end and rewind |

JonFreeman | 0:5d0f270bfc87 | 88 | |

JonFreeman | 0:5d0f270bfc87 | 89 | O 1000 ; Subroutine executed once per complete turn of 'X' |

JonFreeman | 0:5d0f270bfc87 | 90 | M98 P 2000 L #15 ;Execute the subroutine and repeat 'Steps per Rev' times |

JonFreeman | 0:5d0f270bfc87 | 91 | M99 ; Return |

JonFreeman | 0:5d0f270bfc87 | 92 | |

JonFreeman | 0:5d0f270bfc87 | 93 | O 2000 ; Subroutine executed 'Numof X Cycles' * 'Steps per Rev' times |

JonFreeman | 0:5d0f270bfc87 | 94 | #12 = [#12 + #52] ; Update X angle |

JonFreeman | 0:5d0f270bfc87 | 95 | #13 = [#13 + #53] ; Update X angle |

JonFreeman | 0:5d0f270bfc87 | 96 | #50 = [#10 * cos[#12]] ;Update X coord |

JonFreeman | 0:5d0f270bfc87 | 97 | #51 = [#11 * sin[#13]] ;Update Y coord |

JonFreeman | 0:5d0f270bfc87 | 98 | G1 X#50 Y#51 |

JonFreeman | 0:5d0f270bfc87 | 99 | M99 ; Return |

JonFreeman | 0:5d0f270bfc87 | 100 | */ |

JonFreeman | 0:5d0f270bfc87 | 101 |