ManualControl
Dependencies: TPixy-Interface
Fork of MbedOS_Robot by
ui.cpp@2:ca2a7430739b, 2018-02-03 (annotated)
- Committer:
- asobhy
- Date:
- Sat Feb 03 00:37:57 2018 +0000
- Revision:
- 2:ca2a7430739b
- Parent:
- 1:3e9684e81312
- Child:
- 4:417e475239c7
before trying a different control algorithm
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:a355e511bc5d | 63 | void consoleUI(void) |
asobhy | 0:a355e511bc5d | 64 | { |
asobhy | 1:3e9684e81312 | 65 | if (bluetooth.readable()) { |
asobhy | 1:3e9684e81312 | 66 | x = bluetooth.getc(); |
asobhy | 1:3e9684e81312 | 67 | |
asobhy | 0:a355e511bc5d | 68 | // if input from console is the letter 'r' |
asobhy | 0:a355e511bc5d | 69 | if(x == 'r') { |
asobhy | 0:a355e511bc5d | 70 | // reset watchdog timer |
asobhy | 0:a355e511bc5d | 71 | WatchdogReset(); |
asobhy | 1:3e9684e81312 | 72 | bluetooth.printf("\r\nWatchdog has been reset"); |
asobhy | 0:a355e511bc5d | 73 | } |
asobhy | 0:a355e511bc5d | 74 | |
asobhy | 0:a355e511bc5d | 75 | // if w is pressed increase the speed |
asobhy | 0:a355e511bc5d | 76 | // by incrementing u |
asobhy | 0:a355e511bc5d | 77 | else if(x == 'w') { |
asobhy | 1:3e9684e81312 | 78 | setpoint_mutex.lock(); |
asobhy | 1:3e9684e81312 | 79 | if ( setpoint < 560 ) |
asobhy | 1:3e9684e81312 | 80 | { |
asobhy | 1:3e9684e81312 | 81 | //setpoint = setpoint + SPEED_STEP; |
asobhy | 1:3e9684e81312 | 82 | setpoint = 100; |
asobhy | 0:a355e511bc5d | 83 | } |
asobhy | 1:3e9684e81312 | 84 | setpoint_mutex.unlock(); |
asobhy | 1:3e9684e81312 | 85 | |
asobhy | 0:a355e511bc5d | 86 | // display speed |
asobhy | 1:3e9684e81312 | 87 | bluetooth.printf("\r\n %5d", setpoint); |
asobhy | 0:a355e511bc5d | 88 | } |
asobhy | 0:a355e511bc5d | 89 | |
asobhy | 0:a355e511bc5d | 90 | // if s is pressed decrease the speed |
asobhy | 0:a355e511bc5d | 91 | // by decrementing u |
asobhy | 0:a355e511bc5d | 92 | else if(x == 's') { |
asobhy | 1:3e9684e81312 | 93 | |
asobhy | 1:3e9684e81312 | 94 | setpoint_mutex.lock(); |
asobhy | 1:3e9684e81312 | 95 | if (setpoint > -560) |
asobhy | 1:3e9684e81312 | 96 | { |
asobhy | 1:3e9684e81312 | 97 | setpoint = -100; |
asobhy | 1:3e9684e81312 | 98 | //setpoint = setpoint - SPEED_STEP; |
asobhy | 0:a355e511bc5d | 99 | } |
asobhy | 1:3e9684e81312 | 100 | |
asobhy | 1:3e9684e81312 | 101 | setpoint_mutex.unlock(); |
asobhy | 1:3e9684e81312 | 102 | |
asobhy | 0:a355e511bc5d | 103 | // display speed |
asobhy | 1:3e9684e81312 | 104 | bluetooth.printf("\r\n %5d", setpoint); |
asobhy | 0:a355e511bc5d | 105 | } |
asobhy | 0:a355e511bc5d | 106 | |
asobhy | 0:a355e511bc5d | 107 | // error wrong input |
asobhy | 0:a355e511bc5d | 108 | else { |
asobhy | 1:3e9684e81312 | 109 | 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 | 110 | } |
asobhy | 0:a355e511bc5d | 111 | } |
asobhy | 0:a355e511bc5d | 112 | position += dPosition; |
asobhy | 2:ca2a7430739b | 113 | 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 | 114 | |
asobhy | 0:a355e511bc5d | 115 | } |
asobhy | 0:a355e511bc5d | 116 | |
asobhy | 1:3e9684e81312 | 117 | /****************************************************************************** |
asobhy | 1:3e9684e81312 | 118 | User interface 2 |
asobhy | 1:3e9684e81312 | 119 | ******************************************************************************/ |
asobhy | 1:3e9684e81312 | 120 | |
asobhy | 1:3e9684e81312 | 121 | void consoleUI2(void) |
asobhy | 1:3e9684e81312 | 122 | { |
asobhy | 1:3e9684e81312 | 123 | |
asobhy | 1:3e9684e81312 | 124 | if (bluetooth.readable()) { |
asobhy | 1:3e9684e81312 | 125 | x = bluetooth.getc(); |
asobhy | 1:3e9684e81312 | 126 | |
asobhy | 1:3e9684e81312 | 127 | // if input from console is the letter 'r' |
asobhy | 1:3e9684e81312 | 128 | if(x == 'r') { |
asobhy | 1:3e9684e81312 | 129 | // reset watchdog timer |
asobhy | 1:3e9684e81312 | 130 | WatchdogReset(); |
asobhy | 1:3e9684e81312 | 131 | setpoint = 0; |
asobhy | 1:3e9684e81312 | 132 | bluetooth.printf("\r\nWatchdog has been reset"); |
asobhy | 1:3e9684e81312 | 133 | } |
asobhy | 1:3e9684e81312 | 134 | |
asobhy | 1:3e9684e81312 | 135 | // if w is pressed increase the speed |
asobhy | 1:3e9684e81312 | 136 | // by incrementing u |
asobhy | 1:3e9684e81312 | 137 | else if(x == 'w') { |
asobhy | 1:3e9684e81312 | 138 | setpoint_mutex.lock(); |
asobhy | 2:ca2a7430739b | 139 | if ( setpoint < 506 ) |
asobhy | 1:3e9684e81312 | 140 | { |
asobhy | 1:3e9684e81312 | 141 | //setpoint = setpoint + SPEED_STEP; |
asobhy | 1:3e9684e81312 | 142 | setpoint = 200; |
asobhy | 1:3e9684e81312 | 143 | } |
asobhy | 1:3e9684e81312 | 144 | setpoint_mutex.unlock(); |
asobhy | 1:3e9684e81312 | 145 | |
asobhy | 1:3e9684e81312 | 146 | // display speed |
asobhy | 1:3e9684e81312 | 147 | bluetooth.printf("\r\n %5d", setpoint); |
asobhy | 1:3e9684e81312 | 148 | } |
asobhy | 1:3e9684e81312 | 149 | |
asobhy | 1:3e9684e81312 | 150 | // if s is pressed decrease the speed |
asobhy | 1:3e9684e81312 | 151 | // by decrementing u |
asobhy | 1:3e9684e81312 | 152 | else if(x == 's') { |
asobhy | 1:3e9684e81312 | 153 | |
asobhy | 1:3e9684e81312 | 154 | setpoint_mutex.lock(); |
asobhy | 2:ca2a7430739b | 155 | if (setpoint > -560) |
asobhy | 1:3e9684e81312 | 156 | { |
asobhy | 1:3e9684e81312 | 157 | setpoint = -200; |
asobhy | 1:3e9684e81312 | 158 | //setpoint = setpoint - SPEED_STEP; |
asobhy | 1:3e9684e81312 | 159 | } |
asobhy | 1:3e9684e81312 | 160 | |
asobhy | 1:3e9684e81312 | 161 | setpoint_mutex.unlock(); |
asobhy | 1:3e9684e81312 | 162 | |
asobhy | 1:3e9684e81312 | 163 | // display speed |
asobhy | 1:3e9684e81312 | 164 | bluetooth.printf("\r\n %5d", setpoint); |
asobhy | 1:3e9684e81312 | 165 | } |
asobhy | 1:3e9684e81312 | 166 | else if (x=='e') |
asobhy | 1:3e9684e81312 | 167 | { |
asobhy | 1:3e9684e81312 | 168 | Ki = Ki + 0.005; |
asobhy | 1:3e9684e81312 | 169 | } |
asobhy | 1:3e9684e81312 | 170 | else if (x=='d') |
asobhy | 1:3e9684e81312 | 171 | { |
asobhy | 1:3e9684e81312 | 172 | Ki = Ki - 0.005; |
asobhy | 1:3e9684e81312 | 173 | } |
asobhy | 1:3e9684e81312 | 174 | else if (x=='t') |
asobhy | 1:3e9684e81312 | 175 | { |
asobhy | 1:3e9684e81312 | 176 | Kp = Kp + 0.05; |
asobhy | 1:3e9684e81312 | 177 | } |
asobhy | 1:3e9684e81312 | 178 | else if (x=='g') |
asobhy | 1:3e9684e81312 | 179 | { |
asobhy | 1:3e9684e81312 | 180 | Kp = Kp - 0.05; |
asobhy | 1:3e9684e81312 | 181 | } |
asobhy | 1:3e9684e81312 | 182 | |
asobhy | 1:3e9684e81312 | 183 | // error wrong input |
asobhy | 1:3e9684e81312 | 184 | else { |
asobhy | 1:3e9684e81312 | 185 | 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 | 186 | } |
asobhy | 1:3e9684e81312 | 187 | } |
asobhy | 1:3e9684e81312 | 188 | |
asobhy | 1:3e9684e81312 | 189 | position += dPosition; |
asobhy | 2:ca2a7430739b | 190 | |
asobhy | 1:3e9684e81312 | 191 | //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 | 192 | |
asobhy | 2:ca2a7430739b | 193 | bluetooth.printf("\r\ndP: %d, vel: %d, Kp: %f, Ki: %f, e: %d, xState: %d", dPosition, vel, Kp, Ki, e, xState); |
asobhy | 2:ca2a7430739b | 194 | |
asobhy | 2:ca2a7430739b | 195 | //bluetooth.printf("\r\ne: %d, Pos: %d, dP: %d, xState: %d, u: %d, dT: %d", e, position, dPosition, xState, u, dTime); |
asobhy | 1:3e9684e81312 | 196 | |
asobhy | 1:3e9684e81312 | 197 | } |