EmbeddedArtists AB / Mbed 2 deprecated lpc812_exp_solution_stepper-motor

Dependencies:   lpc812_exp_lib_PCF8591 mbed

Revision:
0:9a590fe3a1a1
diff -r 000000000000 -r 9a590fe3a1a1 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 25 14:47:27 2013 +0000
@@ -0,0 +1,299 @@
+#include "mbed.h"
+#include "PCF8591.h"
+
+DigitalOut ph1(D10);
+DigitalOut ph2(D11);
+DigitalOut ph3(D12);
+DigitalOut ph4(D13);
+
+PCF8591 adc;
+
+Serial pc(USBTX, USBRX);
+
+static void experiment1()
+{
+    // Delay between steps
+    float number = 0.5;
+
+    // Initialize outputs
+    ph1=1;
+    ph2=1;
+    ph3=1;
+    ph4=1;
+    pc.printf("\nStarting program...\n");
+
+    while(1) {
+        ph1 = 1;
+        ph2 = 0;
+        ph3 = 1;
+        ph4 = 0;
+        wait(number);
+        ph1 = 1;
+        ph2 = 0;
+        ph3 = 0;
+        ph4 = 1;
+        wait(number);
+        ph1 = 0;
+        ph2 = 1;
+        ph3 = 0;
+        ph4 = 1;
+        wait(number);
+        ph1 = 0;
+        ph2 = 1;
+        ph3 = 1;
+        ph4 = 0;
+        wait(number);
+    }
+}
+
+static void experiment2_alt1()
+{
+    // Delay between steps
+    float number = 0.5;
+
+    // Initialize outputs
+    ph1=1;
+    ph2=1;
+    ph3=1;
+    ph4=1;
+    pc.printf("\nStarting program...\n");
+
+    while(1) {
+        //read trimming potentiometer and convert to value in the 0-0.5 range
+        number = adc.read(PCF8591::A0);
+        number = number/512; //
+        pc.printf("%f\n", number);
+
+        ph1 = 1;
+        ph2 = 0;
+        ph3 = 1;
+        ph4 = 0;
+        wait(number);
+        ph1 = 1;
+        ph2 = 0;
+        ph3 = 0;
+        ph4 = 1;
+        wait(number);
+        ph1 = 0;
+        ph2 = 1;
+        ph3 = 0;
+        ph4 = 1;
+        wait(number);
+        ph1 = 0;
+        ph2 = 1;
+        ph3 = 1;
+        ph4 = 0;
+        wait(number);
+    }
+}
+
+#define ALPHA 0.6
+float getDelayAndDirection()
+{
+    static float lastValue = 0;
+
+    // read a value
+    int v = adc.read(PCF8591::A0);
+
+    // convert from 0..255 to -0.5..0.5
+    float f = v - 128;
+    f = f/256;
+
+    // apply filter
+    lastValue = (ALPHA * lastValue) + (1-ALPHA) * f;
+
+    return f;
+}
+
+static void experiment2_alt2()
+{
+    // Delay between steps
+    float delay = 0.5;
+
+    // Direction
+    bool clockwise = true;
+
+    // Initialize outputs
+    ph1=1;
+    ph2=1;
+    ph3=1;
+    ph4=1;
+    pc.printf("\nStarting program...\n");
+
+    while(1) {
+        delay = getDelayAndDirection();
+        pc.printf("%f\n", delay);
+
+        // determine direction and make sure speed is lowest around 0
+        clockwise = (delay < 0);
+        delay = 0.5 - abs(delay);
+
+        if (clockwise) {
+            ph1 = 1;
+            ph2 = 0;
+            ph3 = 1;
+            ph4 = 0;
+            wait(delay);
+            ph1 = 1;
+            ph2 = 0;
+            ph3 = 0;
+            ph4 = 1;
+            wait(delay);
+            ph1 = 0;
+            ph2 = 1;
+            ph3 = 0;
+            ph4 = 1;
+            wait(delay);
+            ph1 = 0;
+            ph2 = 1;
+            ph3 = 1;
+            ph4 = 0;
+            wait(delay);
+        } else {
+            ph1 = 0;
+            ph2 = 1;
+            ph3 = 1;
+            ph4 = 0;
+            wait(delay);
+            ph1 = 0;
+            ph2 = 1;
+            ph3 = 0;
+            ph4 = 1;
+            wait(delay);
+            ph1 = 1;
+            ph2 = 0;
+            ph3 = 0;
+            ph4 = 1;
+            wait(delay);
+            ph1 = 1;
+            ph2 = 0;
+            ph3 = 1;
+            ph4 = 0;
+            wait(delay);
+        }
+    }
+}
+
+int getTargetStep(bool filter)
+{
+    static int lastValue = -1;
+
+    // read a value
+    int v = adc.read(PCF8591::A0);
+
+    if (lastValue == -1) {
+        lastValue = v;
+    }
+
+    // apply filter
+    if (filter) {
+        lastValue = ((3*lastValue) + v) >> 2;
+    } else {
+        lastValue = v;
+    }
+
+    // convert from 0..255 to 0..19
+    return lastValue/13;
+}
+
+static void experiment3()
+{
+    // Delay between steps
+    float delay = 0.05;
+
+    // Current step
+    int current = 0;
+
+    // Initialize outputs
+    ph1=1;
+    ph2=1;
+    ph3=1;
+    ph4=1;
+    pc.printf("\nStarting program...\n");
+    
+
+    while(1) {
+        int target = getTargetStep(true);
+
+        if (target == current) {
+            wait(0.02);
+            continue;
+        }
+        //pc.printf("%d -> %d\n", current, target);
+
+        int clockwise = (target + 20 - current) % 20;
+        int anticlockwise = (current + 20 - target) % 20;
+        if (clockwise < anticlockwise) {
+            for (int i = 0; i < clockwise; i++) {
+                switch (current%4) {
+                    case 0:
+                        ph1 = 0;
+                        ph2 = 1;
+                        ph3 = 1;
+                        ph4 = 0;
+                        break;
+                    case 1:
+                        ph1 = 0;
+                        ph2 = 1;
+                        ph3 = 0;
+                        ph4 = 1;
+                        break;
+                    case 2:
+                        ph1 = 1;
+                        ph2 = 0;
+                        ph3 = 0;
+                        ph4 = 1;
+                        break;
+                    case 3:
+                        ph1 = 1;
+                        ph2 = 0;
+                        ph3 = 1;
+                        ph4 = 0;
+                        break;
+                }
+                wait(delay);
+                current++;
+            }
+        } else {
+            for (int i = 0; i < anticlockwise; i++) {
+                switch (current%4) {
+                    case 0:
+                        ph1 = 0;
+                        ph2 = 1;
+                        ph3 = 1;
+                        ph4 = 0;
+                        break;
+                    case 1:
+                        ph1 = 0;
+                        ph2 = 1;
+                        ph3 = 0;
+                        ph4 = 1;
+                        break;
+                    case 2:
+                        ph1 = 1;
+                        ph2 = 0;
+                        ph3 = 0;
+                        ph4 = 1;
+                        break;
+                    case 3:
+                        ph1 = 1;
+                        ph2 = 0;
+                        ph3 = 1;
+                        ph4 = 0;
+                        break;
+                }
+                wait(delay);
+                current+=19;
+            }
+        }
+        current = current % 20;
+    }
+}
+
+int main()
+{
+    //experiment1();  // Fixed delay
+    //experiment2_alt1(); // Trimming pot controls delay
+    experiment2_alt2(); // Trimming pot controls delay and direction
+    //experiment3(); // Absolute positioning
+}
\ No newline at end of file