PlayBack

Dependencies:   TPixy-Interface

Fork of ManualControlFinal by ECE4333 - 2018 - Ahmed & Brandon

Committer:
asobhy
Date:
Thu Feb 01 03:58:00 2018 +0000
Revision:
0:a355e511bc5d
Child:
1:3e9684e81312
before testing the QEI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asobhy 0:a355e511bc5d 1 #include "mbed.h"
asobhy 0:a355e511bc5d 2 #include "WatchdogThread.h"
asobhy 0:a355e511bc5d 3 #include "ui.h"
asobhy 0:a355e511bc5d 4
asobhy 0:a355e511bc5d 5 Serial bluetooth(p9,p10); // Define the bluetooth channel and IO pins
asobhy 0:a355e511bc5d 6 Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel
asobhy 0:a355e511bc5d 7
asobhy 0:a355e511bc5d 8 Mutex setpoint_mutex;
asobhy 0:a355e511bc5d 9
asobhy 0:a355e511bc5d 10 // speed
asobhy 0:a355e511bc5d 11 int setpoint;
asobhy 0:a355e511bc5d 12
asobhy 0:a355e511bc5d 13 // variable to store character recieved from terminal
asobhy 0:a355e511bc5d 14 char x;
asobhy 0:a355e511bc5d 15
asobhy 0:a355e511bc5d 16 extern uint16_t dPosition, dTime;
asobhy 0:a355e511bc5d 17
asobhy 0:a355e511bc5d 18 uint16_t position;
asobhy 0:a355e511bc5d 19
asobhy 0:a355e511bc5d 20 /******************************************************************************
asobhy 0:a355e511bc5d 21 A function to test blutooth communication
asobhy 0:a355e511bc5d 22 ******************************************************************************/
asobhy 0:a355e511bc5d 23 void twoTerminalsTest()
asobhy 0:a355e511bc5d 24 {
asobhy 0:a355e511bc5d 25 if (pc.readable()) { // If a key is pressed on the pc channel
asobhy 0:a355e511bc5d 26 x=pc.getc(); // read the character and send it to both the
asobhy 0:a355e511bc5d 27 bluetooth.putc(x); // bluetooth channel and the pc channel for
asobhy 0:a355e511bc5d 28 pc.putc(x); // display.
asobhy 0:a355e511bc5d 29 }
asobhy 0:a355e511bc5d 30 if (bluetooth.readable()) {
asobhy 0:a355e511bc5d 31 x=bluetooth.getc(); // If there’s a keypress on the bluetooth
asobhy 0:a355e511bc5d 32 pc.putc(x); // channel, read the character and send it to
asobhy 0:a355e511bc5d 33 bluetooth.putc(x); // both the pc channel and the bluetooth
asobhy 0:a355e511bc5d 34 } // channel for display
asobhy 0:a355e511bc5d 35 }
asobhy 0:a355e511bc5d 36
asobhy 0:a355e511bc5d 37
asobhy 0:a355e511bc5d 38 /******************************************************************************
asobhy 0:a355e511bc5d 39 A function to Display startup Messsage
asobhy 0:a355e511bc5d 40 ******************************************************************************/
asobhy 0:a355e511bc5d 41 void displayStartupMsg()
asobhy 0:a355e511bc5d 42 {
asobhy 0:a355e511bc5d 43 pc.printf("\r\n************************************");
asobhy 0:a355e511bc5d 44 pc.printf("\r\n**** DC Motor Control using PWM ****");
asobhy 0:a355e511bc5d 45 pc.printf("\r\n************************************");
asobhy 0:a355e511bc5d 46 pc.printf("\r\n-Enter r to reset the watchdog timer");
asobhy 0:a355e511bc5d 47 pc.printf("\r\n-press w to increase motor speed");
asobhy 0:a355e511bc5d 48 pc.printf("\r\n-press s to decrease motor speed");
asobhy 0:a355e511bc5d 49 }
asobhy 0:a355e511bc5d 50
asobhy 0:a355e511bc5d 51
asobhy 0:a355e511bc5d 52 /******************************************************************************
asobhy 0:a355e511bc5d 53 Console interface
asobhy 0:a355e511bc5d 54 ******************************************************************************/
asobhy 0:a355e511bc5d 55 void consoleUI(void)
asobhy 0:a355e511bc5d 56 {
asobhy 0:a355e511bc5d 57 if (pc.readable()) {
asobhy 0:a355e511bc5d 58 x = pc.getc();
asobhy 0:a355e511bc5d 59
asobhy 0:a355e511bc5d 60 // if input from console is the letter 'r'
asobhy 0:a355e511bc5d 61 if(x == 'r') {
asobhy 0:a355e511bc5d 62 // reset watchdog timer
asobhy 0:a355e511bc5d 63 WatchdogReset();
asobhy 0:a355e511bc5d 64 pc.printf("\r\nWatchdog has been reset");
asobhy 0:a355e511bc5d 65 }
asobhy 0:a355e511bc5d 66
asobhy 0:a355e511bc5d 67 // if w is pressed increase the speed
asobhy 0:a355e511bc5d 68 // by incrementing u
asobhy 0:a355e511bc5d 69 else if(x == 'w') {
asobhy 0:a355e511bc5d 70 // convert to integer
asobhy 0:a355e511bc5d 71 if(setpoint<=10000) {
asobhy 0:a355e511bc5d 72 setpoint_mutex.lock();
asobhy 0:a355e511bc5d 73 setpoint = setpoint + SPEED_STEP;
asobhy 0:a355e511bc5d 74 setpoint_mutex.unlock();
asobhy 0:a355e511bc5d 75 }
asobhy 0:a355e511bc5d 76 // display speed
asobhy 0:a355e511bc5d 77 pc.printf("\r\n %5d", setpoint);
asobhy 0:a355e511bc5d 78 }
asobhy 0:a355e511bc5d 79
asobhy 0:a355e511bc5d 80 // if s is pressed decrease the speed
asobhy 0:a355e511bc5d 81 // by decrementing u
asobhy 0:a355e511bc5d 82 else if(x == 's') {
asobhy 0:a355e511bc5d 83 // convert to integer
asobhy 0:a355e511bc5d 84 if(setpoint>-10000) {
asobhy 0:a355e511bc5d 85 setpoint_mutex.lock();
asobhy 0:a355e511bc5d 86 setpoint = setpoint - SPEED_STEP;
asobhy 0:a355e511bc5d 87 setpoint_mutex.unlock();
asobhy 0:a355e511bc5d 88 }
asobhy 0:a355e511bc5d 89 // display speed
asobhy 0:a355e511bc5d 90 pc.printf("\r\n %5d", setpoint);
asobhy 0:a355e511bc5d 91 }
asobhy 0:a355e511bc5d 92
asobhy 0:a355e511bc5d 93 // error wrong input
asobhy 0:a355e511bc5d 94 else {
asobhy 0:a355e511bc5d 95 pc.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");
asobhy 0:a355e511bc5d 96 }
asobhy 0:a355e511bc5d 97 }
asobhy 0:a355e511bc5d 98 position += dPosition;
asobhy 0:a355e511bc5d 99 pc.printf("\r\nposition: %d, dPosition: %d, dTime: %d", position, dPosition, dTime);
asobhy 0:a355e511bc5d 100
asobhy 0:a355e511bc5d 101 }
asobhy 0:a355e511bc5d 102