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
arith.cpp@1:66ee619f206b, 2014-02-06 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonFreeman | 0:5d0f270bfc87 | 1 | #include "mbed.h" |
JonFreeman | 0:5d0f270bfc87 | 2 | #include "cnc.h" |
JonFreeman | 0:5d0f270bfc87 | 3 | using namespace std; |
JonFreeman | 0:5d0f270bfc87 | 4 | |
JonFreeman | 1:66ee619f206b | 5 | const double ball_screw_pitch_mm = 4.0, // KX3 has 4mm ball screws |
JonFreeman | 1:66ee619f206b | 6 | motor_step_per_rev = 200.0, // KX3 has 200 step per rev steppers |
JonFreeman | 1:66ee619f206b | 7 | micro_steps = 20.0, // Arc Eurotrade choice 2,4,5,8,10,16,20,25,32,40,50,64,100,125,128 |
JonFreeman | 1:66ee619f206b | 8 | pulses_per_mm = micro_steps * motor_step_per_rev / ball_screw_pitch_mm, |
JonFreeman | 0:5d0f270bfc87 | 9 | |
JonFreeman | 1:66ee619f206b | 10 | interrupt_period_us = 24.0, //16 is possible with Mbed LPC1768 |
JonFreeman | 1:66ee619f206b | 11 | interrupt_freq_Hz = 1000000.0 / interrupt_period_us, // Serious limit when doing all in software, no real limit with FPGA |
JonFreeman | 1:66ee619f206b | 12 | max_pulse_freq_Hz = interrupt_freq_Hz / 4.5, // strictly 4, but allow a good margin |
JonFreeman | 1:66ee619f206b | 13 | max_mm_per_min = 60.0 * max_pulse_freq_Hz / pulses_per_mm, |
JonFreeman | 1:66ee619f206b | 14 | n_for_onemmpermin = pulses_per_mm * interrupt_period_us * pow(2.0,32) / 60000000.0, // n pir to produce 1mm/min travel |
JonFreeman | 1:66ee619f206b | 15 | feed_rate_max = max_mm_per_min, |
JonFreeman | 1:66ee619f206b | 16 | spindle_max = 5000.0; |
JonFreeman | 0:5d0f270bfc87 | 17 | //The output frequency F<sub>out</sub> = 'Kernel Speed (Hz)' * n / (2 to the power of 32) |
JonFreeman | 0:5d0f270bfc87 | 18 | |
JonFreeman | 0:5d0f270bfc87 | 19 | struct Gparams last_position; |
JonFreeman | 0:5d0f270bfc87 | 20 | void grain_clr (struct singleGparam & g) { |
JonFreeman | 0:5d0f270bfc87 | 21 | g.dbl = 0.0; |
JonFreeman | 0:5d0f270bfc87 | 22 | g.ul = 0L; |
JonFreeman | 0:5d0f270bfc87 | 23 | g.i = g.c = 0; |
JonFreeman | 0:5d0f270bfc87 | 24 | g.changed = false; |
JonFreeman | 0:5d0f270bfc87 | 25 | } |
JonFreeman | 0:5d0f270bfc87 | 26 | void pirs_clr2 (struct Gparams & p) { |
JonFreeman | 0:5d0f270bfc87 | 27 | grain_clr (p.x); grain_clr (p.y); grain_clr (p.z); grain_clr (p.i); grain_clr (p.j); |
JonFreeman | 0:5d0f270bfc87 | 28 | grain_clr (p.r); grain_clr (p.a); grain_clr (p.b); grain_clr (p.c); grain_clr (p.d); |
JonFreeman | 0:5d0f270bfc87 | 29 | } |
JonFreeman | 1:66ee619f206b | 30 | void more_setup () { |
JonFreeman | 0:5d0f270bfc87 | 31 | pirs_clr2 (last_position); |
JonFreeman | 0:5d0f270bfc87 | 32 | } |
JonFreeman | 0:5d0f270bfc87 | 33 | |
JonFreeman | 0:5d0f270bfc87 | 34 |