PlayBack

Dependencies:   TPixy-Interface

Fork of ManualControlFinal by ECE4333 - 2018 - Ahmed & Brandon

Committer:
asobhy
Date:
Mon Feb 19 17:42:33 2018 +0000
Revision:
8:a0890fa79084
Parent:
6:e7ce340fe91e
Child:
9:fe56b888985c
added more comments

Who changed what in which revision?

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