ObjectFollower

Dependencies:   TPixy-Interface

Fork of PlayBack by ECE4333 - 2018 - Ahmed & Brandon

ui.cpp

Committer:
asobhy
Date:
2018-02-09
Revision:
5:b29220d29022
Parent:
4:417e475239c7

File content as of revision 5:b29220d29022:

#include "mbed.h"
#include "WatchdogThread.h"
#include "ui.h"

Serial bluetooth(p9,p10); // Define the bluetooth channel and IO pins
Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel

Mutex setpoint_mutex;

// speed
int setpoint = 0;

// variable to store character recieved from terminal
char x;

extern int16_t dPosition, dTime;
extern float Ki; 
extern float Kp;
extern int vel;
extern int32_t e;
extern int32_t xState;
extern int32_t u;

int16_t position;

bool start_counting = false;


/******************************************************************************
            A function to test blutooth communication
******************************************************************************/
void twoTerminalsTest()
{
    if (pc.readable()) { // If a key is pressed on the pc channel
        x=pc.getc(); // read the character and send it to both the
        bluetooth.putc(x); // bluetooth channel and the pc channel for
        pc.putc(x); // display.
    }
    if (bluetooth.readable()) {
        x=bluetooth.getc(); // If there’s a keypress on the bluetooth
        pc.putc(x); // channel, read the character and send it to
        bluetooth.putc(x); // both the pc channel and the bluetooth
    } // channel for display
}


/******************************************************************************
            A function to Display startup Messsage
******************************************************************************/
void displayStartupMsg()
{
    bluetooth.printf("\r\n************************************");
    bluetooth.printf("\r\n**** DC Motor Control using PWM ****");
    bluetooth.printf("\r\n************************************");
    bluetooth.printf("\r\n-Enter r to reset the watchdog timer");
    bluetooth.printf("\r\n-press w to increase motor speed");
    bluetooth.printf("\r\n-press s to decrease motor speed");
}


/******************************************************************************
                           User interface
******************************************************************************/
/*
void consoleUI(void)
{
    if (bluetooth.readable()) {
        x = bluetooth.getc();
        
        // if input from console is the letter 'r'
        if(x == 'r') {
            // reset watchdog timer
            WatchdogReset();
            bluetooth.printf("\r\nWatchdog has been reset");
        }

        // if w is pressed increase the speed
        // by incrementing u
        else if(x == 'w') {
            setpoint_mutex.lock();
            if ( setpoint < 560 ) 
            {
                //setpoint = setpoint + SPEED_STEP;
                setpoint = 100;
            }
            setpoint_mutex.unlock();

            // display speed
            bluetooth.printf("\r\n %5d", setpoint);
        }

        // if s is pressed decrease the speed
        // by decrementing u
        else if(x == 's') {

            setpoint_mutex.lock();
            if (setpoint > -560) 
            {
                setpoint = -100;
                //setpoint = setpoint - SPEED_STEP;
            }
                
            setpoint_mutex.unlock();

            // display speed
            bluetooth.printf("\r\n %5d", setpoint);
        }

        // error wrong input
        else {
            bluetooth.printf("\r\nwrong input please enter \'w\' to increase the speed, \'s\' to reduce it or move in the opposite direction and \'r\' to reset the watchdog");
        }
    }
    position += dPosition;
    bluetooth.printf("\r\nPos: %d, dP: %d, dT: %d, Kp: %f, Ki: %f, vel: %d, e: %d", position, dPosition, xState, dTime, Kp, Ki, vel, e);
    
}
*/
/******************************************************************************
                           User interface 2
******************************************************************************/

void consoleUI(void)
{
     
    if (bluetooth.readable()) {
        x = bluetooth.getc();
        
        // if input from console is the letter 'r'
        if(x == 'r') {
            // reset watchdog timer
            WatchdogReset();
            setpoint = 0;
            bluetooth.printf("\r\nWatchdog has been reset");
        }

        // if w is pressed increase the speed
        // by incrementing u
        else if(x == 'w') {
            setpoint_mutex.lock();
            if ( setpoint < 506 ) 
            {
                //setpoint = setpoint + SPEED_STEP;
                setpoint = 420;
                start_counting = true;
            }
            setpoint_mutex.unlock();

            // display speed
            bluetooth.printf("\r\n %5d", setpoint);
        }

        // if s is pressed decrease the speed
        // by decrementing u
        else if(x == 's') {

            setpoint_mutex.lock();
            if (setpoint > -560) 
            {
                setpoint = -400;
                //setpoint = setpoint - SPEED_STEP;
            }
                
            setpoint_mutex.unlock();

            // display speed
            bluetooth.printf("\r\n %5d", setpoint);
        }
        else if (x=='i')
        {
            Ki = Ki + 0.001;
        }
        else if (x=='k')
        {
            Ki = Ki - 0.001;
        }
        else if (x=='o')
        {
            Kp = Kp + 0.01;
        }
        else if (x=='l')
        {
            Kp = Kp - 0.01;    
        }
        
        // error wrong input
        else {
            bluetooth.printf("\r\nwrong input please enter \'w\' to increase the speed, \'s\' to reduce it or move in the opposite direction and \'r\' to reset the watchdog");
        }
    }   
    
    position += dPosition;
    
    //bluetooth.printf("\r\nPos: %d, dP: %d, dT: %d, Kp: %f, Ki: %f, vel: %d, e: %d", position, dPosition, dTime, Kp, Ki, vel, e);
    
    //bluetooth.printf("\r\ndP: %d, vel: %d, Kp: %f, Ki: %f, e: %d, xState: %d", dPosition, vel, Kp, Ki, e, xState);
    
    //bluetooth.printf("\r\ne: %d, Pos: %d, dP: %d, xState: %d, u: %d, dT: %d", e, position, dPosition, xState, u, dTime);
    
     bluetooth.printf("\r\n%d", dPosition);
     
     
    
}