Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed threeAxis mbed-rtos 4DGL-uLCD-SE
main.cpp@14:d6c8724a69e8, 2021-12-15 (annotated)
- Committer:
- magiwarriorx
- Date:
- Wed Dec 15 02:10:57 2021 +0000
- Revision:
- 14:d6c8724a69e8
- Parent:
- 13:8231d6cce099
- Child:
- 15:dee1c2f0e82f
IT WORKS REALLY GOOD
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
| mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
| magiwarriorx | 12:2bc6047d219b | 3 | #include "uLCD_4DGL.h" |
| magiwarriorx | 12:2bc6047d219b | 4 | #include "string" |
| magiwarriorx | 12:2bc6047d219b | 5 | #include "threeAxis.h" |
| magiwarriorx | 12:2bc6047d219b | 6 | //#define stepToInch 0.0006723474 |
| magiwarriorx | 12:2bc6047d219b | 7 | //#define stepToInch 0.0007925725 |
| magiwarriorx | 12:2bc6047d219b | 8 | //#define maxX 11.5 |
| magiwarriorx | 12:2bc6047d219b | 9 | //#define maxY 6.75 |
| magiwarriorx | 12:2bc6047d219b | 10 | //#define maxZ 1.75 |
| magiwarriorx | 12:2bc6047d219b | 11 | |
| magiwarriorx | 12:2bc6047d219b | 12 | //Enable is active low. |
| magiwarriorx | 12:2bc6047d219b | 13 | |
| magiwarriorx | 12:2bc6047d219b | 14 | //xStep, yStep, zStep, xDir, yDir, zDir, xEnable, yEnable, zEnable, stepRatio, xBound, yBound, zBound, xLimit, yLimit, zLimit |
| magiwarriorx | 12:2bc6047d219b | 15 | threeAxis table(p23, p25, p21, p24, p26, p22, p19, p18, p20, 0.0007925725, 11.5, 6.75, 1.75, p12, p14, p16); |
| magiwarriorx | 12:2bc6047d219b | 16 | |
| magiwarriorx | 12:2bc6047d219b | 17 | DigitalOut xLimit(p13); |
| magiwarriorx | 12:2bc6047d219b | 18 | DigitalOut yLimit(p15); |
| magiwarriorx | 12:2bc6047d219b | 19 | DigitalOut zLimit(p17); |
| magiwarriorx | 12:2bc6047d219b | 20 | |
| magiwarriorx | 12:2bc6047d219b | 21 | //DigitalOut zStep(p21); |
| magiwarriorx | 12:2bc6047d219b | 22 | //DigitalOut zEnable(p20); |
| magiwarriorx | 12:2bc6047d219b | 23 | //DigitalOut zDir(p22); |
| magiwarriorx | 12:2bc6047d219b | 24 | |
| magiwarriorx | 12:2bc6047d219b | 25 | //DigitalOut xEnable(p19); |
| magiwarriorx | 12:2bc6047d219b | 26 | //DigitalOut xStep(p23); |
| magiwarriorx | 12:2bc6047d219b | 27 | //DigitalOut xDir(p24); |
| magiwarriorx | 12:2bc6047d219b | 28 | |
| magiwarriorx | 12:2bc6047d219b | 29 | //DigitalOut yEnable(p18); |
| magiwarriorx | 12:2bc6047d219b | 30 | //DigitalOut yStep(p25); |
| magiwarriorx | 12:2bc6047d219b | 31 | //DigitalOut yDir(p26); |
| magiwarriorx | 12:2bc6047d219b | 32 | |
| magiwarriorx | 12:2bc6047d219b | 33 | |
| magiwarriorx | 12:2bc6047d219b | 34 | Serial bluetooth(p28,p27); |
| magiwarriorx | 12:2bc6047d219b | 35 | uLCD_4DGL uLCD(p9,p10,p11); |
| magiwarriorx | 12:2bc6047d219b | 36 | DigitalOut actualLED(LED1); |
| magiwarriorx | 12:2bc6047d219b | 37 | Mutex LCD; |
| magiwarriorx | 14:d6c8724a69e8 | 38 | bool buffering; |
| magiwarriorx | 12:2bc6047d219b | 39 | /* |
| magiwarriorx | 12:2bc6047d219b | 40 | volatile int currX; |
| magiwarriorx | 12:2bc6047d219b | 41 | volatile int currY; |
| magiwarriorx | 12:2bc6047d219b | 42 | volatile int currZ; |
| magiwarriorx | 12:2bc6047d219b | 43 | volatile int toX; |
| magiwarriorx | 12:2bc6047d219b | 44 | volatile int toY; |
| magiwarriorx | 12:2bc6047d219b | 45 | volatile int toZ; |
| magiwarriorx | 12:2bc6047d219b | 46 | */ |
| geky | 7:8d9919175929 | 47 | Thread thread; |
| magiwarriorx | 12:2bc6047d219b | 48 | Thread thread2; |
| magiwarriorx | 12:2bc6047d219b | 49 | |
| magiwarriorx | 13:8231d6cce099 | 50 | void raiseLimit(DigitalOut* arg){ |
| magiwarriorx | 13:8231d6cce099 | 51 | while(1){ |
| magiwarriorx | 13:8231d6cce099 | 52 | if(bluetooth.readable()){ |
| magiwarriorx | 13:8231d6cce099 | 53 | char temp; |
| magiwarriorx | 13:8231d6cce099 | 54 | while(bluetooth.readable()){ |
| magiwarriorx | 13:8231d6cce099 | 55 | temp = bluetooth.getc(); |
| magiwarriorx | 13:8231d6cce099 | 56 | } |
| magiwarriorx | 13:8231d6cce099 | 57 | //bluetoothFlush(); |
| magiwarriorx | 13:8231d6cce099 | 58 | *arg = 1; |
| magiwarriorx | 13:8231d6cce099 | 59 | break; |
| magiwarriorx | 13:8231d6cce099 | 60 | } |
| magiwarriorx | 13:8231d6cce099 | 61 | } |
| magiwarriorx | 12:2bc6047d219b | 62 | } |
| magiwarriorx | 12:2bc6047d219b | 63 | |
| magiwarriorx | 12:2bc6047d219b | 64 | void bluetoothFlush(){ |
| magiwarriorx | 12:2bc6047d219b | 65 | char temp; |
| magiwarriorx | 12:2bc6047d219b | 66 | while(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 67 | temp = bluetooth.getc(); |
| emilmont | 1:491820ee784d | 68 | } |
| emilmont | 1:491820ee784d | 69 | } |
| magiwarriorx | 12:2bc6047d219b | 70 | |
| magiwarriorx | 14:d6c8724a69e8 | 71 | void parseCoords(std::string coords){ |
| magiwarriorx | 13:8231d6cce099 | 72 | |
| magiwarriorx | 13:8231d6cce099 | 73 | int start = 0; |
| magiwarriorx | 13:8231d6cce099 | 74 | int end = coords.find(','); |
| magiwarriorx | 13:8231d6cce099 | 75 | |
| magiwarriorx | 13:8231d6cce099 | 76 | |
| magiwarriorx | 13:8231d6cce099 | 77 | float tempX = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 13:8231d6cce099 | 78 | |
| magiwarriorx | 13:8231d6cce099 | 79 | |
| magiwarriorx | 13:8231d6cce099 | 80 | start = end + 1; |
| magiwarriorx | 13:8231d6cce099 | 81 | end = coords.find(',', start); |
| magiwarriorx | 13:8231d6cce099 | 82 | |
| magiwarriorx | 13:8231d6cce099 | 83 | float tempY = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 13:8231d6cce099 | 84 | |
| magiwarriorx | 13:8231d6cce099 | 85 | start = end + 1; |
| magiwarriorx | 13:8231d6cce099 | 86 | end = coords.length(); |
| magiwarriorx | 13:8231d6cce099 | 87 | float tempZ = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 14:d6c8724a69e8 | 88 | |
| magiwarriorx | 14:d6c8724a69e8 | 89 | if(!buffering){ |
| magiwarriorx | 14:d6c8724a69e8 | 90 | table.goTo(tempX, tempY, tempZ); |
| magiwarriorx | 14:d6c8724a69e8 | 91 | } |
| magiwarriorx | 14:d6c8724a69e8 | 92 | else{ |
| magiwarriorx | 14:d6c8724a69e8 | 93 | table.addToBuffer(tempX, tempY, tempZ); |
| magiwarriorx | 14:d6c8724a69e8 | 94 | } |
| magiwarriorx | 13:8231d6cce099 | 95 | |
| magiwarriorx | 13:8231d6cce099 | 96 | } |
| magiwarriorx | 13:8231d6cce099 | 97 | |
| magiwarriorx | 12:2bc6047d219b | 98 | void bluetooth_thread(){ |
| magiwarriorx | 14:d6c8724a69e8 | 99 | |
| magiwarriorx | 14:d6c8724a69e8 | 100 | |
| magiwarriorx | 12:2bc6047d219b | 101 | //char bnum=0; |
| magiwarriorx | 12:2bc6047d219b | 102 | while(1) { |
| magiwarriorx | 12:2bc6047d219b | 103 | std::string coords = ""; |
| magiwarriorx | 12:2bc6047d219b | 104 | Thread::wait(100); |
| magiwarriorx | 12:2bc6047d219b | 105 | |
| magiwarriorx | 12:2bc6047d219b | 106 | if (bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 107 | while (bluetooth.readable()){ |
| magiwarriorx | 14:d6c8724a69e8 | 108 | |
| magiwarriorx | 12:2bc6047d219b | 109 | char temp = bluetooth.getc(); |
| magiwarriorx | 14:d6c8724a69e8 | 110 | |
| magiwarriorx | 12:2bc6047d219b | 111 | if (temp == 'z'){ |
| magiwarriorx | 12:2bc6047d219b | 112 | table.setZero(); |
| magiwarriorx | 12:2bc6047d219b | 113 | } |
| magiwarriorx | 14:d6c8724a69e8 | 114 | else if(temp == 'b'){ |
| magiwarriorx | 14:d6c8724a69e8 | 115 | buffering = !buffering; |
| magiwarriorx | 14:d6c8724a69e8 | 116 | } |
| magiwarriorx | 12:2bc6047d219b | 117 | |
| magiwarriorx | 12:2bc6047d219b | 118 | coords += temp; |
| magiwarriorx | 12:2bc6047d219b | 119 | } |
| magiwarriorx | 14:d6c8724a69e8 | 120 | |
| magiwarriorx | 12:2bc6047d219b | 121 | //Thread::wait(1000); |
| magiwarriorx | 14:d6c8724a69e8 | 122 | LCD.lock(); |
| magiwarriorx | 14:d6c8724a69e8 | 123 | uLCD.printf("\n"); |
| magiwarriorx | 14:d6c8724a69e8 | 124 | if ((coords.find('z') == -1) && (coords.find('b') == -1)){ |
| magiwarriorx | 14:d6c8724a69e8 | 125 | uLCD.printf(coords.c_str()); |
| magiwarriorx | 14:d6c8724a69e8 | 126 | LCD.unlock(); |
| magiwarriorx | 14:d6c8724a69e8 | 127 | parseCoords(coords); |
| magiwarriorx | 14:d6c8724a69e8 | 128 | |
| magiwarriorx | 14:d6c8724a69e8 | 129 | } |
| magiwarriorx | 14:d6c8724a69e8 | 130 | else{ |
| magiwarriorx | 12:2bc6047d219b | 131 | |
| magiwarriorx | 14:d6c8724a69e8 | 132 | if (coords.find('z') != -1){ |
| magiwarriorx | 14:d6c8724a69e8 | 133 | uLCD.printf("Zeroed!"); |
| magiwarriorx | 14:d6c8724a69e8 | 134 | LCD.unlock(); |
| magiwarriorx | 14:d6c8724a69e8 | 135 | } |
| magiwarriorx | 14:d6c8724a69e8 | 136 | else if (coords.find('b') != -1){ |
| magiwarriorx | 14:d6c8724a69e8 | 137 | if (buffering){ |
| magiwarriorx | 14:d6c8724a69e8 | 138 | uLCD.printf("Now buffering!"); |
| magiwarriorx | 14:d6c8724a69e8 | 139 | LCD.unlock(); |
| magiwarriorx | 14:d6c8724a69e8 | 140 | } |
| magiwarriorx | 14:d6c8724a69e8 | 141 | else if (!buffering){ |
| magiwarriorx | 14:d6c8724a69e8 | 142 | uLCD.printf("Time to run buffer!"); |
| magiwarriorx | 14:d6c8724a69e8 | 143 | LCD.unlock(); |
| magiwarriorx | 14:d6c8724a69e8 | 144 | table.runBuffer(); |
| magiwarriorx | 14:d6c8724a69e8 | 145 | } |
| magiwarriorx | 14:d6c8724a69e8 | 146 | } |
| magiwarriorx | 12:2bc6047d219b | 147 | } |
| magiwarriorx | 14:d6c8724a69e8 | 148 | } |
| magiwarriorx | 12:2bc6047d219b | 149 | |
| magiwarriorx | 12:2bc6047d219b | 150 | |
| emilmont | 1:491820ee784d | 151 | } |
| magiwarriorx | 12:2bc6047d219b | 152 | |
| magiwarriorx | 14:d6c8724a69e8 | 153 | } |
| magiwarriorx | 14:d6c8724a69e8 | 154 | |
| magiwarriorx | 14:d6c8724a69e8 | 155 | |
| magiwarriorx | 12:2bc6047d219b | 156 | int main() { |
| magiwarriorx | 12:2bc6047d219b | 157 | //Y axis direction inverted from others to get behavior that makes me happy (rewards = negative, forward = positive) |
| magiwarriorx | 12:2bc6047d219b | 158 | table.invertY(); |
| magiwarriorx | 12:2bc6047d219b | 159 | table.invertZ(); |
| magiwarriorx | 13:8231d6cce099 | 160 | table.setWait(3); |
| magiwarriorx | 12:2bc6047d219b | 161 | uLCD.baudrate(300000); |
| magiwarriorx | 12:2bc6047d219b | 162 | wait(0.5); |
| magiwarriorx | 12:2bc6047d219b | 163 | |
| magiwarriorx | 12:2bc6047d219b | 164 | |
| magiwarriorx | 12:2bc6047d219b | 165 | //I'm going to be honest, my understanding of my own code means that ANY character should start the zero'ing process, and that nothing should skip it |
| magiwarriorx | 12:2bc6047d219b | 166 | //As such its technically a bug that it skips zeroing a given axis if something is entered |
| magiwarriorx | 12:2bc6047d219b | 167 | //However, I like this functionality more than what I intended |
| magiwarriorx | 12:2bc6047d219b | 168 | //So I'm labeling the "bug" as a feature |
| magiwarriorx | 12:2bc6047d219b | 169 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 170 | uLCD.printf("Enter null value to begin X zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 171 | //LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 172 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 173 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 174 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 175 | break; |
| magiwarriorx | 12:2bc6047d219b | 176 | } |
| magiwarriorx | 12:2bc6047d219b | 177 | } |
| magiwarriorx | 12:2bc6047d219b | 178 | //LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 179 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 180 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 181 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 182 | |
| magiwarriorx | 13:8231d6cce099 | 183 | thread2.start(raiseLimit,&xLimit); |
| magiwarriorx | 13:8231d6cce099 | 184 | Thread::wait(50); |
| magiwarriorx | 12:2bc6047d219b | 185 | table.zeroX(); |
| magiwarriorx | 13:8231d6cce099 | 186 | |
| magiwarriorx | 12:2bc6047d219b | 187 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 188 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 189 | uLCD.printf("Enter null value to begin Y zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 190 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 191 | |
| magiwarriorx | 12:2bc6047d219b | 192 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 193 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 194 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 195 | break; |
| magiwarriorx | 12:2bc6047d219b | 196 | } |
| magiwarriorx | 12:2bc6047d219b | 197 | } |
| magiwarriorx | 12:2bc6047d219b | 198 | |
| magiwarriorx | 12:2bc6047d219b | 199 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 200 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 201 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 202 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 203 | |
| magiwarriorx | 13:8231d6cce099 | 204 | thread2.start(raiseLimit,&yLimit); |
| magiwarriorx | 13:8231d6cce099 | 205 | Thread::wait(50); |
| magiwarriorx | 12:2bc6047d219b | 206 | table.zeroY(); |
| magiwarriorx | 12:2bc6047d219b | 207 | |
| magiwarriorx | 12:2bc6047d219b | 208 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 209 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 210 | uLCD.printf("Enter null value to begin Z zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 211 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 212 | |
| magiwarriorx | 12:2bc6047d219b | 213 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 214 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 215 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 216 | break; |
| magiwarriorx | 12:2bc6047d219b | 217 | } |
| magiwarriorx | 12:2bc6047d219b | 218 | } |
| magiwarriorx | 12:2bc6047d219b | 219 | |
| magiwarriorx | 12:2bc6047d219b | 220 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 221 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 222 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 223 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 224 | |
| magiwarriorx | 13:8231d6cce099 | 225 | |
| magiwarriorx | 13:8231d6cce099 | 226 | thread2.start(raiseLimit,&zLimit); |
| magiwarriorx | 13:8231d6cce099 | 227 | Thread::wait(50); |
| magiwarriorx | 12:2bc6047d219b | 228 | table.zeroZ(); |
| magiwarriorx | 12:2bc6047d219b | 229 | |
| magiwarriorx | 13:8231d6cce099 | 230 | thread2.terminate(); |
| magiwarriorx | 12:2bc6047d219b | 231 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 232 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 233 | |
| magiwarriorx | 12:2bc6047d219b | 234 | table.setLimits(true); |
| magiwarriorx | 12:2bc6047d219b | 235 | xLimit = 0; |
| magiwarriorx | 12:2bc6047d219b | 236 | yLimit = 0; |
| magiwarriorx | 12:2bc6047d219b | 237 | zLimit = 0; |
| magiwarriorx | 12:2bc6047d219b | 238 | |
| magiwarriorx | 12:2bc6047d219b | 239 | uLCD.printf("Waiting..."); |
| magiwarriorx | 12:2bc6047d219b | 240 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 241 | |
| magiwarriorx | 13:8231d6cce099 | 242 | bluetoothFlush(); |
| magiwarriorx | 13:8231d6cce099 | 243 | |
| magiwarriorx | 12:2bc6047d219b | 244 | thread.start(bluetooth_thread); |
| magiwarriorx | 12:2bc6047d219b | 245 | |
| magiwarriorx | 12:2bc6047d219b | 246 | while(1); |
| magiwarriorx | 12:2bc6047d219b | 247 | } |