ManualControl

Dependencies:   TPixy-Interface

Fork of MbedOS_Robot by ECE4333 - 2018 - Ahmed & Brandon

ui.cpp

Committer:
asobhy
Date:
2018-03-29
Revision:
23:1839085ffdcf
Parent:
22:c09acff62e6a

File content as of revision 23:1839085ffdcf:

/******************************************************************************/
// ECE4333
// LAB Partner 1:   Ahmed Sobhy - ID: 3594449
// LAB Partner 2:   Brandon Kingman - ID: 3470444
// Project:         Autonomous Robot Design
// Instructor:      Prof. Chris Diduch
/******************************************************************************/
// filename: ui.cpp
// file content description:
//      * Functions to display and manage the user interface on the PC terminal.
/******************************************************************************/

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

bool startRecording = false;
bool MC = true;

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

bool killRobot = false;

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


void displayStartupMsg()
{
    bluetooth.printf("\r\n************************************");
    bluetooth.printf("\r\n**** AUTONOMOUS FOLLOWING ROBOT ****");
    bluetooth.printf("\r\n************************************");
    bluetooth.printf("\r\n-PRESS 'r' TO reset");
}

/******************************************************************************
                           User interface 3 - Manual Control
******************************************************************************/

void consoleUI(void)
{
    if (bluetooth.readable()) {
        x = bluetooth.getc();

        // enter manual control and exit patrol mode
        if(x == 'm') {
            MC = true;
        }

        // exit manual control and enter patrol mode
        if(x == 'p') {
            MC = false;
            startRecording = false;
        }
        
        if(x == 'l'){
            startRecording = true;
        }

        // if input from console is the letter 'r'
        if(x == 'r' && (MC)) {
            
            // reset watchdog timer
            WatchdogReset();
            setpointR = 0;
            setpointL = 0;
            MC = true;
            startRecording = false;
            memoryFull = false;
            dpArray.i = 0;
            
            // reset array
            for(int i = 0; i < dpArray.size; i++){
                dpArray.dpR[i] = 0;
                dpArray.dpL[i] = 0;
            }
            
            bluetooth.printf("\r\nSystem has been Reset");
            
        }

        /******************************ROBOT FWD RVS***********************************/
        // if w is pressed increase the speed
        // by incrementing u
        else if(x == 'w'&& (MC)) {
            mutexSetpoint.lock();
            Setpoint = -40;
            mutexSetpoint.unlock();

        }

        // if s is pressed decrease the speed
        // by decrementing u
        else if(x == 's' && (MC)) {
            mutexSetpoint.lock();
            Setpoint = 40;
            mutexSetpoint.unlock();

        }

        /******************************ROBOT STEERING**********************************/
        else if (x=='a'&& (MC)) {
            mutexSetpoint.lock();
            SteeringError = 80;
            mutexSetpoint.unlock();
        } else if (x=='d'&& (MC)) {
            mutexSetpoint.lock();
            SteeringError = -80;
            mutexSetpoint.unlock();
        }
        // 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");
        }
    } else {
        // If no key is pressed stop the robot.
        Setpoint = 0;
        SteeringError = 0;
    }
    
    if(memoryFull){
        bluetooth.printf("\r\nmemory is full");
    }

}