eLab Team / Mbed 2 deprecated LaLaBox

Dependencies:   mbed CREALIB

Revision:
1:ab4c9a0a5374
Parent:
0:30cb0f6dad87
Child:
2:050f12806bc5
--- a/main.cpp	Fri Jun 17 10:41:31 2016 +0000
+++ b/main.cpp	Fri Jun 17 10:48:18 2016 +0000
@@ -1,17 +1,242 @@
-#include "mbed.h"
+#include "LaLaBox.h"
+
+// ---------------- Local global variables --------------
+DigitalOut myled(LED1);     // Blinking LED
+// -------------------- Motor ---------------------------
+Ticker  MotorSystemTick;    // System Callback for Motor
+timestamp_t MotorStepTime;  // Time in µs for one motor step
+uint32_t    MotorFullTurn;  // Number of step for a complete turn
+uint32_t    NumWires;       // Number of Wires
+uint32_t    NumSteps;       // Number of Steps = NumWire * MotorFullTurn
+
+enum MotorStateList {   // Define Motor States for the State Machine
+    Motor_IDLE = 0,
+    Motor_RUN,
+    Motor_PAUSE,
+    Motor_ZERO,
+    Motor_CALIB
+    } MotorState = Motor_IDLE;
+
+enum MotorCommandList { // Define Motor State Machine Commands
+    k_nop = 0,
+    k_wire,
+    k_pause,
+    k_restart,
+    k_stop,
+    k_zero
+    } MotorCommand;
+    
+enum MotorDirectionList { // Define Motor Clockwise or Anticlockwise
+    d_clock = 0,
+    d_anti
+    } MotorDir;
+    
+// --- Sound ---
+char *song_happy_birthday = "Happy Birthday Song:d=4,o=5,b=125:16c,32p,32c,32p,8d,32p,8c,32p,8f,32p,e,16p,16c,32p,32c,32p,8d,32p,8c,32p,8g,32p,f,8p,16c,32p,32c,32p,8c6,32p,8a,32p,8f,32p,8e,32p,8d,32p,16a#,32p,32a#,32p,8a,32p,8f,32p,8g,32p,f";
+char *song_greensleaves = "Greensleaves:d=4,o=5,b=140:g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,2a#,g,g.,8f,g,2a,f,2d,g,2a#,c6,d.6,8e6,d6,2c6,a,f.,8g,a,a#.,8a,g,f#.,8e,f#,2g";
+char *song_lightmyfire = "LightMyFire:d=4,o=5,b=140:8b,16g,16a,8b,8d6,8c6,8b,8a,8g,8a,16f,16a,8c6,8f6,16d6,16c6,16a#,16g,8g#,8g,8g#,16g,16a,8b,8c#6,16b,16a,16g,16f,8e,8f,1a,a";
+char *song_xfile = "Xfiles:d=4,o=5,b=140:e,b,a,b,d6,2b.";
+char *song_christmas ="Christmas:d=4,o=5,b=100:f#,g#,2a#,2a#,d#.,8f,f,f,2f#,2d#,2f#.6,f#,#g,8g#,g#,8a#,b,8c#,c#,2c#,8d#,8f.,8f#.,8f.,d#,f,2f#.,a#,8b.,8b.,8b.,d#,f,2c#.,a#,8b.,8b.,8b.,d#,f,f#,p,g#,g#,g#,8g#,8a#,8g#,f#.,g#,a#,p,c,c,c,8c,8c#,8c,a#.,c,c#,a#,b,a#,a,a#,b,c,";
+
+// --- Define PC_8 as the output of PWM use for Tones -----
+Buzzer buzzer(PC_8);
 
-Ticker toggle_led_ticker;
+Music* pMusic=0;    //the song
+Note la("A#4",50);  //the sound
+
+// --- Define the Foor PINs used for Motor drive -----
+DigitalOut MPh3(PA_7);
+DigitalOut MPh2(PB_6);
+DigitalOut MPh1(PC_7);
+DigitalOut MPh0(PA_9);
+// --- Motor Variable
+int MotorIndex = 0;
+// --- Motor Routine
+void    StopMotor() // --- Stop Motor
+{
+        MPh0 = 0;  MPh1 = 0;  MPh2 = 0;  MPh3 = 0;
+        MotorIndex = 0;
+}
+void    StartMotor()
+{
+        MotorIndex = 0;
+        MPh0 = 1;  MPh1 = 0;  MPh2 = 0;   MPh3 = 0;
+}
+void    RightMotor()    // Move the Motor one step Right
+{
+    const   int RPh0[4] = {0, 1, 0, 0};
+    const   int RPh1[4] = {0, 0, 1, 0};
+    const   int RPh2[4] = {0, 0, 0, 1};
+    const   int RPh3[4] = {1, 0, 0, 0};
+    MPh0 = RPh0[MotorIndex];  MPh1 = RPh1[MotorIndex];  MPh2 = RPh2[MotorIndex];  MPh3 = RPh3[MotorIndex];
+    if (MotorIndex<3) MotorIndex++;
+    else    MotorIndex = 0;
+}
+void    LeftMotor()    // Move the Motor one step Right
+{
+    const   int LPh0[4] = {0, 0, 1, 0};
+    const   int LPh1[4] = {0, 1, 0, 0};
+    const   int LPh2[4] = {1, 0, 0, 0};
+    const   int LPh3[4] = {0, 0, 0, 1};
+    MPh0 = LPh0[MotorIndex];  MPh1 = LPh1[MotorIndex];  MPh2 = LPh2[MotorIndex];  MPh3 = LPh3[MotorIndex];
+    if (MotorIndex<3) MotorIndex++;
+    else    MotorIndex = 0;
+}
+
+void    RunMotor()     // Move the Motor in the user direction
+{
+        if (MotorDir==d_clock) RightMotor();
+        else LeftMotor();
+}
 
-DigitalOut led1(LED1);
+void ProcessMotorStateMachine()
+{
+        if (MotorState==Motor_IDLE) {
+            uint32_t    led = 0;
+            if (MotorCommand == k_wire) {
+                // Start the Wiring
+                StartMotor();
+                led = 1;
+                MotorState = Motor_RUN;
+                }
+            else if (MotorCommand == k_zero) {
+                // Start zeroing the Motor
+                StartMotor();
+                led = 1;
+                MotorState = Motor_ZERO;
+                }
+            MotorCommand = k_nop;
+            myled = led;        // LED is on when motor in use
+            }
+        else if (MotorState==Motor_RUN) {
+            // Action always performed in that state
+             if (NumSteps>0) {
+                RunMotor();
+                NumSteps--;
+                }
+            // Check next state
+            if (MotorCommand == k_pause) {
+                MotorState = Motor_PAUSE;
+                }
+            else if ((MotorCommand == k_stop)||(NumSteps<=0)) {
+                StopMotor();
+                MotorState = Motor_IDLE;
+                }
+            MotorCommand = k_nop;
+            }
+        else if (MotorState==Motor_PAUSE) {
+            if (MotorCommand == k_stop) {
+                StopMotor();
+                NumSteps=0;
+                MotorState = Motor_IDLE;
+                }
+            else if (MotorCommand == k_restart) {
+                MotorState = Motor_RUN;
+                }
+            MotorCommand = k_nop;
+            }
+        else if (MotorState==Motor_ZERO) {
+            MotorCommand = k_nop;
+            }
+}
 
-void toggle_led() {
-    led1 = !led1;
+void TestMotor()    // Just to check that it make a full taurn back and forth
+{
+    int i;
+    StartMotor();
+    for (i=0; i<MotorFullTurn; i++) {
+        wait(0.005);
+        RightMotor();
+        }   
+    wait(0.5);
+    for (i=0; i<MotorFullTurn; i++) {
+        wait(0.005);
+        LeftMotor();
+        }   
+    StopMotor();
+}
+void help() // Display list of Commands
+{
+    DEBUG("List of commands:\n\r");
+    DEBUG(" h --> Help, display list of cammands\n\r");
+    DEBUG(" z --> Go to motor zero\n\r");
+    DEBUG(" k --> Calibrate motor\n\r");
+    DEBUG(" n dddd --> define Number of coils (default=100)\n\r");
+    DEBUG(" c --> define Clockwise (default)\n\r");
+    DEBUG(" a --> define Anti-clockwise\n\r");
+    DEBUG(" m --> Memorise configuration\n\r");
+    DEBUG(" l --> List current configuration\n\r");
+    DEBUG(" w --> start Wiring the coils\n\r");
+    DEBUG(" p --> Pause wiring\n\r");
+    DEBUG(" r --> Resume wiring\n\r");
+    DEBUG(" s --> Stop (abort) wiring\n\r");
+    DEBUG(" [space] --> print remaining Motor steps\n\r");
+}
+
+void end_wiring()   // Display message and Play a song when wirring is completed
+{
+    DEBUG("--- SUCCESS: Coils wiring completed ---\n\r");
+    // Play a single Song
+    pMusic= new Music(song_lightmyfire);
+    pMusic->play(&buzzer);
+    buzzer.tone(&la);
+    delete(pMusic);
 }
 
 int main() {
-    // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
-    toggle_led_ticker.attach(&toggle_led, 0.1);
-    while (true) {
-        // Do other things...
+    myled = 1;      // To see something is alive
+    DEBUG("\n\n\r");
+    DEBUG("------------------------------------------\n\r"); 
+    DEBUG("----- Wire WINDER (fbd38) version 1.0 ----\n\r");
+    DEBUG("------------------------------------------\n\r"); 
+    help();
+    DEBUG("------------------------------------------\n\r"); 
+    wait(5);        // Some delay
+    myled = 0;      // Real stuff starts here
+    //
+    // Connect Interrupt routine in which the motor and all the state machine is performed
+    //
+    MotorDir = d_clock;         // Default direction is clockwise
+    MotorState = Motor_IDLE;    // Default state is IDLE
+    MotorCommand = k_nop;       // Default command is NOP
+    MotorStepTime = 10000;      // value in micro second for one step
+    MotorFullTurn = 2140;       // Initial Calibration
+    NumSteps = 0;               // Default
+    MotorSystemTick.attach_us(&ProcessMotorStateMachine, MotorStepTime);
+    //
+    while(1) {
+        char command;   // Command to execute
+        uint32_t TimeToRunSec, TimeToRunMin;
+        DEBUG(">> ");
+        command = pc_uart.getc();
+        DEBUG("%c", command);
+        switch (command) {
+        case 'h': help(); break;
+        case 'z': MotorCommand = k_zero; break;
+        case 'k': TestMotor(); break;
+        case 'n':
+            pc_uart.scanf("%d", &NumWires);
+            NumSteps = NumWires*MotorFullTurn;
+            TimeToRunSec = (NumSteps * (MotorStepTime / 1000))/1000;
+            TimeToRunMin = TimeToRunSec / 60;
+            TimeToRunSec %= 60;
+            DEBUG("%d  --> Step = %d; Time = %d,%2d (min, sec)\n\r", NumWires, NumSteps, TimeToRunMin, TimeToRunSec);
+            break;
+        case 'c': MotorDir = d_clock; break;
+        case 'a': MotorDir = d_anti;  break;
+        case 'm': DEBUG(" [ not ready yet ]\n\r"); break;
+        case 'l': DEBUG(" [ not ready yet ]\n\r"); break;
+        case 'w': DEBUG(" -- Starting --\n\r"); MotorCommand = k_wire;    break;
+        case 'p': DEBUG(" -- Pause    --\n\r"); MotorCommand = k_pause;   break;
+        case 'r': DEBUG(" -- Re-Start --\n\r"); MotorCommand = k_restart; break;
+        case 's': DEBUG(" -- Stop     --\n\r"); MotorCommand = k_stop;    break;
+        case ' ': 
+            TimeToRunSec = (NumSteps * (MotorStepTime / 1000))/1000;
+            TimeToRunMin = TimeToRunSec / 60;
+            TimeToRunSec %= 60;
+            DEBUG(" Step to run: %d; Time %d,%2d (min, sec)\n\r", NumSteps, TimeToRunMin, TimeToRunSec); 
+            break;
+        default : DEBUG("invalid command; use: 'h' for help()");
+        }
     }
-}
\ No newline at end of file
+}