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

main.cpp

Committer:
Stathisn
Date:
2014-08-26
Revision:
1:7b420a2ea7db
Parent:
0:01fd80c0a524
Child:
2:a73037a7d85d

File content as of revision 1:7b420a2ea7db:

#include "mbed.h"

enum calib_state_t {
    UNCALIBRATED,
    INITIALISING,
    CALIBRATING,
    CALIBRATED};

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();


ConsoleSerial consoleSerial(SERIAL_TX, SERIAL_RX);


int main() {
    
    
    consoleSerial.setBaud(CONSOLE_BAUD);
    
    my_led = 0;
    
    // Step 1: Attach interrupt signals to appropriate functions
    
    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 MCU to the turn table
    calibrate();
    // Step 3: Control turntable using hard coded commands
    // Step 4: Include WiConnect Library
    // Step 5: Interpret commands sent over TCP
    
    
    // Configure sanity LED to blink
    while(1) {
        //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;
}