This project will enable remote control of a motorised turntable via a WiFi enabled TCP link using ACKme's (http://ack.me/) Wi-Fi enablement platform

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Stathisn
Date:
Wed Aug 27 12:28:48 2014 +0000
Parent:
1:7b420a2ea7db
Commit message:
Cleaned up control code and created an object to handle its operation; Integrated feedback over serial for debugging purposes

Changed in this revision

TurntableControl.cpp Show annotated file Show diff for this revision Revisions of this file
TurntableControl.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 7b420a2ea7db -r a73037a7d85d TurntableControl.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TurntableControl.cpp	Wed Aug 27 12:28:48 2014 +0000
@@ -0,0 +1,97 @@
+#include "TurntableControl.h"
+
+TurntableControl::TurntableControl()
+    : encoder(PA_13, PullDown), limitSW(PA_15, PullUp), ttdriver(PA_14, PullDown)
+{
+    encoderMax = 0;
+    encoderCurrent = 0;
+}
+
+TurntableControl::TurntableControl(PinName en, PinName lSW, PinName ttd)
+    : encoder(en, PullDown), limitSW(lSW, PullUp), ttdriver(ttd, PullDown)
+{
+    encoderMax = 0;
+    encoderCurrent = 0;
+}
+
+void TurntableControl::initialise()
+{
+    ttdriver = 1; // Start motor
+    while (limitSW.read()) // Continue until limit switch reached
+    {}
+    while (!limitSW.read()) // Wait for limit switch to reset
+    {}
+    ttdriver = 0; // Stop motor
+}
+
+int TurntableControl::calibrate()
+{
+    int lastVal = encoder.read(); // Start at a known encoder sample
+    ttdriver = 1; // Start motor
+    
+    while (limitSW.read()) // Continue until limit switch reached
+    {
+        if (lastVal != encoder.read()) // Check for edge
+        {
+            encoderMax++; // Increment on edge
+        }
+        lastVal = encoder.read(); // Update current sample
+    }
+    while (!limitSW.read()) // Continue until limit switch reached
+    {
+        if (lastVal != encoder.read()) // Check for edge
+        {
+            encoderMax++; // Increment on edge
+        }
+        lastVal = encoder.read(); // Update current sample
+    }
+    ttdriver = 0; // Stop motor
+    return encoderMax;
+}
+
+void TurntableControl::incrementTurntable(int edges)
+{
+    ttdriver = 1;
+    int lastVal = encoder.read();
+    int target = encoderCurrent + edges;
+    while(target >= encoderCurrent) // Cycle through number of edges 
+    {
+        if(lastVal != encoder.read()) // Check and wait for edge
+        {
+            encoderCurrent++;
+        }
+        lastVal = encoder.read();
+    }
+    ttdriver = 0;
+}
+
+void TurntableControl::reset()
+{
+    encoderMax = 0;
+    encoderCurrent = 0;
+    initialise();
+}
+
+int TurntableControl::getEncoderCurrent()
+{
+    return encoderCurrent;
+}
+
+int TurntableControl::getEncoderMax()
+{
+    return encoderMax;
+}
+
+int TurntableControl::getEncoder()
+{
+    return encoder.read();
+}
+
+void TurntableControl::quarterTurns(int n) // Turn the turntable a quarter rotation, n times
+{
+    int quaterEdges = getEncoderMax()/4;
+    for(int i = 0; i < n; i++)
+    {
+        incrementTurntable(quaterEdges);
+    }
+}
\ No newline at end of file
diff -r 7b420a2ea7db -r a73037a7d85d TurntableControl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TurntableControl.h	Wed Aug 27 12:28:48 2014 +0000
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "mbed.h"
+
+class TurntableControl
+{
+private:
+    int encoderMax; // Calibrated maximum value for the encoder
+    int encoderCurrent; // Keeps track of the current position of the turn table
+    DigitalIn encoder;
+    DigitalIn limitSW;
+    DigitalOut ttdriver;
+public:
+    TurntableControl();
+    TurntableControl(PinName, PinName, PinName);
+    void initialise();
+    int calibrate(); // synchronises and calibrates MCU to turntable
+    void incrementTurntable(int); // increments value i, used as an interrupt target
+    void reset();
+    int getEncoderCurrent();
+    int getEncoderMax();
+    int getEncoder();
+    void quarterTurns(int);
+};
\ No newline at end of file
diff -r 7b420a2ea7db -r a73037a7d85d main.cpp
--- a/main.cpp	Tue Aug 26 12:33:27 2014 +0000
+++ b/main.cpp	Wed Aug 27 12:28:48 2014 +0000
@@ -1,116 +1,45 @@
 #include "mbed.h"
+#include "TurntableControl.h"
+#include "ConsoleSerial.h"
 
-enum calib_state_t {
-    UNCALIBRATED,
-    INITIALISING,
-    CALIBRATING,
-    CALIBRATED};
+#define CONSOLE_BAUD 115200
+
+
 
 DigitalOut my_led(LED1); // Sanity LED
-DigitalOut ttDrive(PA_14); // the drive signal for the turntable
-InterruptIn encoder(PA_13); // Signal from encoder
-InterruptIn limitSwitch(PA_15); // Signal from limit switch
-
-calib_state_t calib_state = UNCALIBRATED; // Flag for calibration state
-int encoderMax = 0; // Calibrated maximum value for the encoder
-int encoderCurrent = 0; // Keeps track of the current position of the turn table
-bool limitFlag = 0;
-
-void calibrate(); // synchronises and calibrates MCU to turntable
-void incrementEncoder(); // increments value i, used as an interrupt target
-void resetEncoder();
+TurntableControl tc; // Turntable controller
 
 
-ConsoleSerial consoleSerial(SERIAL_TX, SERIAL_RX);
+ConsoleSerial cs(SERIAL_TX, SERIAL_RX);
 
 
 int main() {
     
     
-    consoleSerial.setBaud(CONSOLE_BAUD);
+    cs.setBaud(CONSOLE_BAUD);
+    
+    cs.printf("resetting\n\r");
     
-    my_led = 0;
-    
-    // Step 1: Attach interrupt signals to appropriate functions
+    // Step 1: reset the turntable
+    tc.reset();
+    cs.printf("reset\n\rcalibrating\n\r");
     
-    limitSwitch.mode(PullUp);
-    limitSwitch.fall(&resetEncoder);  //****Ask someone about correct handling of interrupts, including; naming convention (resetEncoder also handles calib_state), potential coupling realted to having a function perform two tasks
-    encoder.mode(PullDown);
-    encoder.fall(&incrementEncoder);
-    encoder.rise(&incrementEncoder);
+    // Step 2: calibrate the turntable
+    cs.printf("Max encoder = %d\n\r", tc.calibrate());
     
-    // Step 2: Calibrate MCU to the turn table
-    calibrate();
-    // Step 3: Control turntable using hard coded commands
+    // Step 3: rotate to a specified quarter
+    tc.quarterTurns(4);
+    
+    cs.printf("Done incrementing\r\n");
+    cs.printf("Encoder Current = %d\r\n", tc.getEncoderCurrent());
+    
     // Step 4: Include WiConnect Library
     // Step 5: Interpret commands sent over TCP
     
     
     // Configure sanity LED to blink
     while(1) {
-        //my_led = !my_led;
+        my_led = !my_led;
         wait(0.5);
     }
 }
-
-void calibrate()
-{
-    // Step 1:  Set status to uncalibrated
-    calib_state = UNCALIBRATED;
-    // Step 2:  Get the motor turning
-    ttDrive = 1;
-    calib_state = INITIALISING;
-    encoderMax = 0; // reset the maximum encoder value
-    
-    // Step 2:  Continue turning until calibration state is "CALIBRATED"
-    //          the actual calibration will be handled by interrupts
-    
-    while (calib_state != CALIBRATED)
-    {
-        // run time reaches here....my_led = 1;
-        my_led = limitFlag;
-        if(limitFlag == 1)
-        {
-            my_led = 1;
-            // run time does not reach here
-            // Step 2: Process the calibration state
-            limitFlag = 0;
-            switch(calib_state)
-            {
-                case (UNCALIBRATED):
-                    calib_state = INITIALISING;
-                    break;
-                case (INITIALISING):
-                    calib_state = CALIBRATING;
-                    break;
-                case (CALIBRATING):
-                    calib_state = CALIBRATED;
-                    break;
-                case (CALIBRATED):
-                    break;
-                default:
-                    calib_state = UNCALIBRATED;
-            }
-        }
-    }
-}
-
-void incrementEncoder()
-{
-    // Step 1: De-refference pointer and increment the value
-    if (calib_state == CALIBRATING)
-        ++encoderMax;
-    else
-        ++encoderCurrent;
-}
-
-void resetEncoder()
-{
-    // code reaches here...my_led = 1;
-    // Step 1: Set encoderCurrent value to zero
-    encoderCurrent = 0;
-    
-    // Show that the limit switch has been pressed
-    limitFlag = 1;
-    my_led = limitFlag;
-}
\ No newline at end of file