PlayBack

Dependencies:   TPixy-Interface

Fork of ManualControlFinal by ECE4333 - 2018 - Ahmed & Brandon

Committer:
asobhy
Date:
Sat Feb 10 19:35:21 2018 +0000
Revision:
6:e7ce340fe91e
Parent:
4:417e475239c7
Child:
8:a0890fa79084
02/10/2018

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