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

Revision:
1:66ee619f206b
Parent:
0:5d0f270bfc87
Child:
2:b3c668ec43ac
--- a/lissajous.cpp	Fri Jan 31 11:16:21 2014 +0000
+++ b/lissajous.cpp	Thu Feb 06 08:45:02 2014 +0000
@@ -1,5 +1,8 @@
 #include "mbed.h"
-//extern  Serial pc;
+#include "rtos.h"
+#include "cnc.h"
+extern  Serial pc;
+extern  void    mover   (struct pirbufgrain & ins)   ;
 /*
 This file contains one function:
 void    lissajous   ()  ;
@@ -9,36 +12,55 @@
 
 Thus far we have proved only that both finish at the same point (give or take a few microns)
 */
+bool    liss_active = false;    //  global flag used to prevent running more than once at a time
 
-const double    Deg2Rad     = atan(1.0) / 45.0,
-                HALFPI      = 2.0 * atan(1.0),
-                TWOPI       = 8.0 * atan(1.0),
-                PI          = 4.0 * atan(1.0),
-                MaxX            = 6.40,
-                MaxY            = 3.20,
-                StartAngDegX    = 0.0,
-                StartAngDegY    = 10.0,
-                FreqRatio       = 0.254;
-const int       StepsPerRevX    = 100,
-                NumofXCycles    = 16;
+void    lissajous   (void const * arg_string)  {
+    const double    PI          = 4.0 * atan(1.0),  //3.142ish but more accurate
+                    Deg2Rad     = PI / 180.0, //  degrees to radian conversion factor
+                    MaxX            = 6.40,     // Plot size X to move +/- MaxX
+                    MaxY            = 6.4,   //3.20,     // Plot size Y to move +/- MaxY
+                    StartAngDegX    = 0.0,
+                    StartAngDegY    = 0.0,  //10.0,
+                    FreqRatio       = 1.0,  //0.254,
+                    FeedRate        = 500.0;    // In mm per minute
+    const int       StepsPerRevX    = 100,
+                    NumofXCycles    = 16;
+    const double    AngleStepX  = (2.0 * PI / StepsPerRevX),
+                    AngleStepY  = AngleStepX * FreqRatio;
 
-void    lissajous   ()  {
+//void    lissajous   (void const * arg_string)  {
+    struct  pirbufgrain Coords;
     double  AngleX = StartAngDegX * Deg2Rad,
-            AngleY = StartAngDegY * Deg2Rad,
-            AngleStepX  = (TWOPI / StepsPerRevX),
-            AngleStepY  = AngleStepX * FreqRatio,
-            X_Coord = MaxX * cos(AngleX),
-            Y_Coord = MaxY * sin(AngleY);
-
-    for (int i = 0; i < NumofXCycles; i++)   {
-        for (int j = 0; j < StepsPerRevX; j++)   {
-            AngleX += AngleStepX;
-            AngleY += AngleStepY;
-            X_Coord = MaxX * cos(AngleX);
-            Y_Coord = MaxY * sin(AngleY);
-        }    
-    }
-//    pc.printf("Lissajous finish point X%f, Y%f\r\n", X_Coord, Y_Coord);
+            AngleY = StartAngDegY * Deg2Rad;
+            
+    pc.printf("In lissajous func, Loading Lissajous\r\n");
+    while   (1) {
+        while   (!liss_active)  //  Other code to activate
+            osThreadYield();
+    //    liss_active = true;   //  this hapens in activating code
+        pc.printf("In lissajous func, Starting Lissajous, has been activated\r\n");
+        Coords.x = MaxX * cos(AngleX);  //  Coordinates of start position
+        Coords.y = MaxY * sin(AngleY);  //  assembled into packet for motion controller
+        Coords.z = 0.0;
+        Coords.f_rate = FeedRate;
+        mover(Coords);
+    
+        for (int i = 0; i < NumofXCycles; i++)   {      // Outer loop 'NumofXCycles' times
+            for (int j = 0; j < StepsPerRevX; j++)   {  // Inner loop 'StepsPerRevX' times
+                AngleX += AngleStepX;
+                AngleY += AngleStepY;
+                Coords.x = MaxX * cos(AngleX);
+                Coords.y = MaxY * sin(AngleY);
+                mover(Coords);
+                osThreadYield();
+            }
+        }
+        
+        pc.printf("Lissajous finish point X%f, Y%f\r\n", Coords.x, Coords.y);
+        liss_active = false;
+        pc.printf("Leaving liss\r\n");
+        osThreadYield();    //  terminate would be better here
+    }   //  end of while(1)
 }
 
 /*