PlayBack

Dependencies:   TPixy-Interface

Fork of ManualControlFinal by ECE4333 - 2018 - Ahmed & Brandon

Committer:
asobhy
Date:
Sat Mar 03 00:33:41 2018 +0000
Revision:
12:172c448a359e
Parent:
9:fe56b888985c
Child:
14:5777377537a2
robot vision two motors debug final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asobhy 8:a0890fa79084 1 /******************************************************************************/
asobhy 8:a0890fa79084 2 // ECE4333
asobhy 9:fe56b888985c 3 // LAB Partner 1: Ahmed Sobhy - ID: 3594449
asobhy 9:fe56b888985c 4 // LAB Partner 2: Brandon Kingman - ID: 3470444
asobhy 9:fe56b888985c 5 // Project: Autonomous Robot Design
asobhy 9:fe56b888985c 6 // Instructor: Prof. Chris Diduch
asobhy 8:a0890fa79084 7 /******************************************************************************/
asobhy 8:a0890fa79084 8 // filename: ui.cpp
asobhy 8:a0890fa79084 9 // file content description:
asobhy 8:a0890fa79084 10 // * Functions to display and manage the user interface on the PC terminal.
asobhy 8:a0890fa79084 11 /******************************************************************************/
asobhy 8:a0890fa79084 12
asobhy 0:a355e511bc5d 13 #include "mbed.h"
asobhy 0:a355e511bc5d 14 #include "WatchdogThread.h"
asobhy 0:a355e511bc5d 15 #include "ui.h"
asobhy 0:a355e511bc5d 16
asobhy 0:a355e511bc5d 17 Serial bluetooth(p9,p10); // Define the bluetooth channel and IO pins
asobhy 0:a355e511bc5d 18 Serial pc(USBTX, USBRX); // Pins (tx, rx) for PC serial channel
asobhy 0:a355e511bc5d 19
asobhy 9:fe56b888985c 20 Mutex setpointR_mutex;
asobhy 9:fe56b888985c 21 Mutex setpointL_mutex;
asobhy 0:a355e511bc5d 22
asobhy 0:a355e511bc5d 23 // speed
asobhy 9:fe56b888985c 24 int setpointR = 0;
asobhy 9:fe56b888985c 25 int setpointL = 0;
asobhy 9:fe56b888985c 26
asobhy 0:a355e511bc5d 27 // variable to store character recieved from terminal
asobhy 0:a355e511bc5d 28 char x;
asobhy 0:a355e511bc5d 29
asobhy 9:fe56b888985c 30 /*
asobhy 1:3e9684e81312 31 extern int16_t dPosition, dTime;
asobhy 1:3e9684e81312 32 extern float Ki;
asobhy 1:3e9684e81312 33 extern float Kp;
asobhy 1:3e9684e81312 34 extern int vel;
asobhy 2:ca2a7430739b 35 extern int32_t e;
asobhy 2:ca2a7430739b 36 extern int32_t xState;
asobhy 2:ca2a7430739b 37 extern int32_t u;
asobhy 12:172c448a359e 38 //extern int time_passed;
asobhy 9:fe56b888985c 39 */
asobhy 1:3e9684e81312 40
asobhy 1:3e9684e81312 41 int16_t position;
asobhy 1:3e9684e81312 42
asobhy 2:ca2a7430739b 43
asobhy 0:a355e511bc5d 44
asobhy 0:a355e511bc5d 45 /******************************************************************************
asobhy 0:a355e511bc5d 46 A function to test blutooth communication
asobhy 0:a355e511bc5d 47 ******************************************************************************/
asobhy 0:a355e511bc5d 48 void twoTerminalsTest()
asobhy 0:a355e511bc5d 49 {
asobhy 0:a355e511bc5d 50 if (pc.readable()) { // If a key is pressed on the pc channel
asobhy 0:a355e511bc5d 51 x=pc.getc(); // read the character and send it to both the
asobhy 0:a355e511bc5d 52 bluetooth.putc(x); // bluetooth channel and the pc channel for
asobhy 0:a355e511bc5d 53 pc.putc(x); // display.
asobhy 0:a355e511bc5d 54 }
asobhy 0:a355e511bc5d 55 if (bluetooth.readable()) {
asobhy 0:a355e511bc5d 56 x=bluetooth.getc(); // If there’s a keypress on the bluetooth
asobhy 0:a355e511bc5d 57 pc.putc(x); // channel, read the character and send it to
asobhy 0:a355e511bc5d 58 bluetooth.putc(x); // both the pc channel and the bluetooth
asobhy 0:a355e511bc5d 59 } // channel for display
asobhy 0:a355e511bc5d 60 }
asobhy 0:a355e511bc5d 61
asobhy 0:a355e511bc5d 62
asobhy 0:a355e511bc5d 63 /******************************************************************************
asobhy 0:a355e511bc5d 64 A function to Display startup Messsage
asobhy 0:a355e511bc5d 65 ******************************************************************************/
asobhy 0:a355e511bc5d 66 void displayStartupMsg()
asobhy 0:a355e511bc5d 67 {
asobhy 1:3e9684e81312 68 bluetooth.printf("\r\n************************************");
asobhy 1:3e9684e81312 69 bluetooth.printf("\r\n**** DC Motor Control using PWM ****");
asobhy 1:3e9684e81312 70 bluetooth.printf("\r\n************************************");
asobhy 1:3e9684e81312 71 bluetooth.printf("\r\n-Enter r to reset the watchdog timer");
asobhy 9:fe56b888985c 72 bluetooth.printf("\r\n-press w to increase motor speedR");
asobhy 9:fe56b888985c 73 bluetooth.printf("\r\n-press s to decrease motor speedR");
asobhy 9:fe56b888985c 74 bluetooth.printf("\r\n-press i to increase motor speedL");
asobhy 9:fe56b888985c 75 bluetooth.printf("\r\n-press k to decrease motor speedL");
asobhy 0:a355e511bc5d 76 }
asobhy 0:a355e511bc5d 77
asobhy 0:a355e511bc5d 78
asobhy 0:a355e511bc5d 79 /******************************************************************************
asobhy 8:a0890fa79084 80 User interface 1
asobhy 0:a355e511bc5d 81 ******************************************************************************/
asobhy 4:417e475239c7 82 /*
asobhy 0:a355e511bc5d 83 void consoleUI(void)
asobhy 0:a355e511bc5d 84 {
asobhy 1:3e9684e81312 85 if (bluetooth.readable()) {
asobhy 1:3e9684e81312 86 x = bluetooth.getc();
asobhy 1:3e9684e81312 87
asobhy 0:a355e511bc5d 88 // if input from console is the letter 'r'
asobhy 0:a355e511bc5d 89 if(x == 'r') {
asobhy 0:a355e511bc5d 90 // reset watchdog timer
asobhy 0:a355e511bc5d 91 WatchdogReset();
asobhy 1:3e9684e81312 92 bluetooth.printf("\r\nWatchdog has been reset");
asobhy 0:a355e511bc5d 93 }
asobhy 0:a355e511bc5d 94
asobhy 0:a355e511bc5d 95 // if w is pressed increase the speed
asobhy 0:a355e511bc5d 96 // by incrementing u
asobhy 0:a355e511bc5d 97 else if(x == 'w') {
asobhy 1:3e9684e81312 98 setpoint_mutex.lock();
asobhy 1:3e9684e81312 99 if ( setpoint < 560 )
asobhy 1:3e9684e81312 100 {
asobhy 1:3e9684e81312 101 //setpoint = setpoint + SPEED_STEP;
asobhy 1:3e9684e81312 102 setpoint = 100;
asobhy 0:a355e511bc5d 103 }
asobhy 1:3e9684e81312 104 setpoint_mutex.unlock();
asobhy 1:3e9684e81312 105
asobhy 0:a355e511bc5d 106 // display speed
asobhy 1:3e9684e81312 107 bluetooth.printf("\r\n %5d", setpoint);
asobhy 0:a355e511bc5d 108 }
asobhy 0:a355e511bc5d 109
asobhy 0:a355e511bc5d 110 // if s is pressed decrease the speed
asobhy 0:a355e511bc5d 111 // by decrementing u
asobhy 0:a355e511bc5d 112 else if(x == 's') {
asobhy 1:3e9684e81312 113
asobhy 1:3e9684e81312 114 setpoint_mutex.lock();
asobhy 1:3e9684e81312 115 if (setpoint > -560)
asobhy 1:3e9684e81312 116 {
asobhy 1:3e9684e81312 117 setpoint = -100;
asobhy 1:3e9684e81312 118 //setpoint = setpoint - SPEED_STEP;
asobhy 0:a355e511bc5d 119 }
asobhy 1:3e9684e81312 120
asobhy 1:3e9684e81312 121 setpoint_mutex.unlock();
asobhy 1:3e9684e81312 122
asobhy 0:a355e511bc5d 123 // display speed
asobhy 1:3e9684e81312 124 bluetooth.printf("\r\n %5d", setpoint);
asobhy 0:a355e511bc5d 125 }
asobhy 0:a355e511bc5d 126
asobhy 0:a355e511bc5d 127 // error wrong input
asobhy 0:a355e511bc5d 128 else {
asobhy 1:3e9684e81312 129 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");
asobhy 0:a355e511bc5d 130 }
asobhy 0:a355e511bc5d 131 }
asobhy 0:a355e511bc5d 132 position += dPosition;
asobhy 2:ca2a7430739b 133 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);
asobhy 0:a355e511bc5d 134
asobhy 0:a355e511bc5d 135 }
asobhy 4:417e475239c7 136 */
asobhy 1:3e9684e81312 137 /******************************************************************************
asobhy 1:3e9684e81312 138 User interface 2
asobhy 1:3e9684e81312 139 ******************************************************************************/
asobhy 1:3e9684e81312 140
asobhy 4:417e475239c7 141 void consoleUI(void)
asobhy 1:3e9684e81312 142 {
asobhy 1:3e9684e81312 143
asobhy 1:3e9684e81312 144 if (bluetooth.readable()) {
asobhy 1:3e9684e81312 145 x = bluetooth.getc();
asobhy 1:3e9684e81312 146
asobhy 1:3e9684e81312 147 // if input from console is the letter 'r'
asobhy 1:3e9684e81312 148 if(x == 'r') {
asobhy 1:3e9684e81312 149 // reset watchdog timer
asobhy 1:3e9684e81312 150 WatchdogReset();
asobhy 9:fe56b888985c 151 setpointR = 0;
asobhy 9:fe56b888985c 152 setpointL = 0;
asobhy 1:3e9684e81312 153 bluetooth.printf("\r\nWatchdog has been reset");
asobhy 1:3e9684e81312 154 }
asobhy 9:fe56b888985c 155
asobhy 9:fe56b888985c 156 /******************************RIGHT MOTOR*************************************/
asobhy 1:3e9684e81312 157 // if w is pressed increase the speed
asobhy 1:3e9684e81312 158 // by incrementing u
asobhy 1:3e9684e81312 159 else if(x == 'w') {
asobhy 9:fe56b888985c 160 setpointR_mutex.lock();
asobhy 9:fe56b888985c 161 if ( setpointR < 560 )
asobhy 1:3e9684e81312 162 {
asobhy 1:3e9684e81312 163 //setpoint = setpoint + SPEED_STEP;
asobhy 9:fe56b888985c 164 setpointR = 280;
asobhy 1:3e9684e81312 165 }
asobhy 9:fe56b888985c 166 setpointR_mutex.unlock();
asobhy 12:172c448a359e 167 bluetooth.printf("\r\n %5d", setpointR);
asobhy 1:3e9684e81312 168 }
asobhy 1:3e9684e81312 169
asobhy 1:3e9684e81312 170 // if s is pressed decrease the speed
asobhy 1:3e9684e81312 171 // by decrementing u
asobhy 1:3e9684e81312 172 else if(x == 's') {
asobhy 1:3e9684e81312 173
asobhy 9:fe56b888985c 174 setpointR_mutex.lock();
asobhy 9:fe56b888985c 175 if (setpointR > -560)
asobhy 1:3e9684e81312 176 {
asobhy 9:fe56b888985c 177 setpointR = -280;
asobhy 1:3e9684e81312 178 //setpoint = setpoint - SPEED_STEP;
asobhy 1:3e9684e81312 179 }
asobhy 1:3e9684e81312 180
asobhy 9:fe56b888985c 181 setpointR_mutex.unlock();
asobhy 1:3e9684e81312 182
asobhy 1:3e9684e81312 183 // display speed
asobhy 9:fe56b888985c 184 bluetooth.printf("\r\n %5d", setpointR);
asobhy 1:3e9684e81312 185 }
asobhy 9:fe56b888985c 186
asobhy 9:fe56b888985c 187 /******************************LEFT MOTOR**************************************/
asobhy 4:417e475239c7 188 else if (x=='i')
asobhy 1:3e9684e81312 189 {
asobhy 9:fe56b888985c 190 setpointL_mutex.lock();
asobhy 9:fe56b888985c 191 if ( setpointL < 560 )
asobhy 9:fe56b888985c 192 {
asobhy 9:fe56b888985c 193 //setpoint = setpoint + SPEED_STEP;
asobhy 9:fe56b888985c 194 setpointL = 280;
asobhy 9:fe56b888985c 195 }
asobhy 9:fe56b888985c 196 setpointL_mutex.unlock();
asobhy 12:172c448a359e 197 bluetooth.printf("\r\n %5d", setpointL);
asobhy 1:3e9684e81312 198 }
asobhy 4:417e475239c7 199 else if (x=='k')
asobhy 1:3e9684e81312 200 {
asobhy 9:fe56b888985c 201 setpointL_mutex.lock();
asobhy 9:fe56b888985c 202 if (setpointL > -560)
asobhy 9:fe56b888985c 203 {
asobhy 9:fe56b888985c 204 setpointL = -280;
asobhy 9:fe56b888985c 205 //setpoint = setpoint - SPEED_STEP;
asobhy 9:fe56b888985c 206 }
asobhy 9:fe56b888985c 207
asobhy 9:fe56b888985c 208 setpointL_mutex.unlock();
asobhy 9:fe56b888985c 209
asobhy 9:fe56b888985c 210 // display speed
asobhy 9:fe56b888985c 211 bluetooth.printf("\r\n %5d", setpointL);
asobhy 1:3e9684e81312 212 }
asobhy 9:fe56b888985c 213 /******************************END MOTOR SETPOINT******************************/
asobhy 1:3e9684e81312 214
asobhy 1:3e9684e81312 215 // error wrong input
asobhy 1:3e9684e81312 216 else {
asobhy 1:3e9684e81312 217 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");
asobhy 1:3e9684e81312 218 }
asobhy 1:3e9684e81312 219 }
asobhy 1:3e9684e81312 220
asobhy 8:a0890fa79084 221
asobhy 8:a0890fa79084 222
asobhy 1:3e9684e81312 223 //bluetooth.printf("\r\nPos: %d, dP: %d, dT: %d, Kp: %f, Ki: %f, vel: %d, e: %d", position, dPosition, dTime, Kp, Ki, vel, e);
asobhy 2:ca2a7430739b 224
asobhy 8:a0890fa79084 225 //bluetooth.printf("\r\ndP: %d, vel: %d, (Kp*e) Kp: %f, (Ki*xState) Ki: %f, e: %d, xState: %d, u: %d", dPosition, vel, Kp, Ki, e, xState , u);
asobhy 8:a0890fa79084 226
asobhy 9:fe56b888985c 227 /*
asobhy 8:a0890fa79084 228 bluetooth.printf("\r\n %d, ", e);
asobhy 8:a0890fa79084 229 bluetooth.printf("%d, ", dPosition);
asobhy 8:a0890fa79084 230 bluetooth.printf("%d, ", xState);
asobhy 8:a0890fa79084 231 bluetooth.printf("%d, ", u);
asobhy 9:fe56b888985c 232 */
asobhy 2:ca2a7430739b 233
asobhy 2:ca2a7430739b 234 //bluetooth.printf("\r\ne: %d, Pos: %d, dP: %d, xState: %d, u: %d, dT: %d", e, position, dPosition, xState, u, dTime);
asobhy 1:3e9684e81312 235
asobhy 1:3e9684e81312 236 }