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@13:8231d6cce099, 2021-12-15 (annotated)
- Committer:
- magiwarriorx
- Date:
- Wed Dec 15 01:18:03 2021 +0000
- Revision:
- 13:8231d6cce099
- Parent:
- 12:2bc6047d219b
- Child:
- 14:d6c8724a69e8
THIS BUILD WORKS
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 | 13:8231d6cce099 | 38 | Mutex blue; |
| 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 | 13:8231d6cce099 | 71 | void parseCoords(std::string coords){ |
| magiwarriorx | 13:8231d6cce099 | 72 | |
| magiwarriorx | 13:8231d6cce099 | 73 | |
| magiwarriorx | 13:8231d6cce099 | 74 | int start = 0; |
| magiwarriorx | 13:8231d6cce099 | 75 | int end = coords.find(','); |
| magiwarriorx | 13:8231d6cce099 | 76 | |
| magiwarriorx | 13:8231d6cce099 | 77 | |
| magiwarriorx | 13:8231d6cce099 | 78 | float tempX = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 13:8231d6cce099 | 79 | |
| magiwarriorx | 13:8231d6cce099 | 80 | |
| magiwarriorx | 13:8231d6cce099 | 81 | start = end + 1; |
| magiwarriorx | 13:8231d6cce099 | 82 | end = coords.find(',', start); |
| magiwarriorx | 13:8231d6cce099 | 83 | |
| magiwarriorx | 13:8231d6cce099 | 84 | float tempY = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 13:8231d6cce099 | 85 | |
| magiwarriorx | 13:8231d6cce099 | 86 | start = end + 1; |
| magiwarriorx | 13:8231d6cce099 | 87 | end = coords.length(); |
| magiwarriorx | 13:8231d6cce099 | 88 | float tempZ = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 13:8231d6cce099 | 89 | |
| magiwarriorx | 13:8231d6cce099 | 90 | table.goTo(tempX, tempY, tempZ); |
| magiwarriorx | 13:8231d6cce099 | 91 | |
| magiwarriorx | 13:8231d6cce099 | 92 | } |
| magiwarriorx | 13:8231d6cce099 | 93 | |
| magiwarriorx | 12:2bc6047d219b | 94 | void bluetooth_thread(){ |
| magiwarriorx | 12:2bc6047d219b | 95 | |
| magiwarriorx | 12:2bc6047d219b | 96 | //char bnum=0; |
| magiwarriorx | 12:2bc6047d219b | 97 | while(1) { |
| magiwarriorx | 12:2bc6047d219b | 98 | std::string coords = ""; |
| magiwarriorx | 12:2bc6047d219b | 99 | Thread::wait(100); |
| magiwarriorx | 12:2bc6047d219b | 100 | |
| magiwarriorx | 12:2bc6047d219b | 101 | if (bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 102 | while (bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 103 | char temp = bluetooth.getc(); |
| magiwarriorx | 12:2bc6047d219b | 104 | if (temp == 'z'){ |
| magiwarriorx | 12:2bc6047d219b | 105 | table.setZero(); |
| magiwarriorx | 12:2bc6047d219b | 106 | } |
| magiwarriorx | 12:2bc6047d219b | 107 | |
| magiwarriorx | 12:2bc6047d219b | 108 | else{ |
| magiwarriorx | 12:2bc6047d219b | 109 | coords += temp; |
| magiwarriorx | 12:2bc6047d219b | 110 | } |
| magiwarriorx | 12:2bc6047d219b | 111 | } |
| magiwarriorx | 12:2bc6047d219b | 112 | //Thread::wait(1000); |
| magiwarriorx | 12:2bc6047d219b | 113 | if (coords.find('z') == -1){ |
| magiwarriorx | 12:2bc6047d219b | 114 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 115 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 116 | uLCD.printf(coords.c_str()); |
| magiwarriorx | 12:2bc6047d219b | 117 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 118 | parseCoords(coords); |
| magiwarriorx | 12:2bc6047d219b | 119 | |
| magiwarriorx | 12:2bc6047d219b | 120 | } |
| magiwarriorx | 12:2bc6047d219b | 121 | else{ |
| magiwarriorx | 12:2bc6047d219b | 122 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 123 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 124 | uLCD.printf("Zeroed!"); |
| magiwarriorx | 12:2bc6047d219b | 125 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 126 | } |
| magiwarriorx | 12:2bc6047d219b | 127 | } |
| magiwarriorx | 12:2bc6047d219b | 128 | |
| magiwarriorx | 12:2bc6047d219b | 129 | |
| emilmont | 1:491820ee784d | 130 | } |
| emilmont | 1:491820ee784d | 131 | } |
| magiwarriorx | 12:2bc6047d219b | 132 | |
| magiwarriorx | 12:2bc6047d219b | 133 | int main() { |
| magiwarriorx | 12:2bc6047d219b | 134 | //Y axis direction inverted from others to get behavior that makes me happy (rewards = negative, forward = positive) |
| magiwarriorx | 12:2bc6047d219b | 135 | table.invertY(); |
| magiwarriorx | 12:2bc6047d219b | 136 | table.invertZ(); |
| magiwarriorx | 13:8231d6cce099 | 137 | table.setWait(3); |
| magiwarriorx | 12:2bc6047d219b | 138 | uLCD.baudrate(300000); |
| magiwarriorx | 12:2bc6047d219b | 139 | wait(0.5); |
| magiwarriorx | 12:2bc6047d219b | 140 | |
| magiwarriorx | 12:2bc6047d219b | 141 | |
| magiwarriorx | 12:2bc6047d219b | 142 | //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 | 143 | //As such its technically a bug that it skips zeroing a given axis if something is entered |
| magiwarriorx | 12:2bc6047d219b | 144 | //However, I like this functionality more than what I intended |
| magiwarriorx | 12:2bc6047d219b | 145 | //So I'm labeling the "bug" as a feature |
| magiwarriorx | 12:2bc6047d219b | 146 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 147 | uLCD.printf("Enter null value to begin X zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 148 | //LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 149 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 150 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 151 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 152 | break; |
| magiwarriorx | 12:2bc6047d219b | 153 | } |
| magiwarriorx | 12:2bc6047d219b | 154 | } |
| magiwarriorx | 12:2bc6047d219b | 155 | //LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 156 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 157 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 158 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 159 | |
| magiwarriorx | 13:8231d6cce099 | 160 | thread2.start(raiseLimit,&xLimit); |
| magiwarriorx | 13:8231d6cce099 | 161 | Thread::wait(50); |
| magiwarriorx | 12:2bc6047d219b | 162 | table.zeroX(); |
| magiwarriorx | 13:8231d6cce099 | 163 | |
| magiwarriorx | 12:2bc6047d219b | 164 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 165 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 166 | uLCD.printf("Enter null value to begin Y zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 167 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 168 | |
| magiwarriorx | 12:2bc6047d219b | 169 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 170 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 171 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 172 | break; |
| magiwarriorx | 12:2bc6047d219b | 173 | } |
| magiwarriorx | 12:2bc6047d219b | 174 | } |
| magiwarriorx | 12:2bc6047d219b | 175 | |
| magiwarriorx | 12:2bc6047d219b | 176 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 177 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 178 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 179 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 180 | |
| magiwarriorx | 13:8231d6cce099 | 181 | thread2.start(raiseLimit,&yLimit); |
| magiwarriorx | 13:8231d6cce099 | 182 | Thread::wait(50); |
| magiwarriorx | 12:2bc6047d219b | 183 | table.zeroY(); |
| magiwarriorx | 12:2bc6047d219b | 184 | |
| magiwarriorx | 12:2bc6047d219b | 185 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 186 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 187 | uLCD.printf("Enter null value to begin Z zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 188 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 189 | |
| magiwarriorx | 12:2bc6047d219b | 190 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 191 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 192 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 193 | break; |
| magiwarriorx | 12:2bc6047d219b | 194 | } |
| magiwarriorx | 12:2bc6047d219b | 195 | } |
| magiwarriorx | 12:2bc6047d219b | 196 | |
| magiwarriorx | 12:2bc6047d219b | 197 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 198 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 199 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 200 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 201 | |
| magiwarriorx | 13:8231d6cce099 | 202 | |
| magiwarriorx | 13:8231d6cce099 | 203 | thread2.start(raiseLimit,&zLimit); |
| magiwarriorx | 13:8231d6cce099 | 204 | Thread::wait(50); |
| magiwarriorx | 12:2bc6047d219b | 205 | table.zeroZ(); |
| magiwarriorx | 12:2bc6047d219b | 206 | |
| magiwarriorx | 13:8231d6cce099 | 207 | thread2.terminate(); |
| magiwarriorx | 12:2bc6047d219b | 208 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 209 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 210 | |
| magiwarriorx | 12:2bc6047d219b | 211 | table.setLimits(true); |
| magiwarriorx | 12:2bc6047d219b | 212 | xLimit = 0; |
| magiwarriorx | 12:2bc6047d219b | 213 | yLimit = 0; |
| magiwarriorx | 12:2bc6047d219b | 214 | zLimit = 0; |
| magiwarriorx | 12:2bc6047d219b | 215 | |
| magiwarriorx | 12:2bc6047d219b | 216 | uLCD.printf("Waiting..."); |
| magiwarriorx | 12:2bc6047d219b | 217 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 218 | |
| magiwarriorx | 13:8231d6cce099 | 219 | bluetoothFlush(); |
| magiwarriorx | 13:8231d6cce099 | 220 | |
| magiwarriorx | 12:2bc6047d219b | 221 | thread.start(bluetooth_thread); |
| magiwarriorx | 12:2bc6047d219b | 222 | |
| magiwarriorx | 12:2bc6047d219b | 223 | while(1); |
| magiwarriorx | 12:2bc6047d219b | 224 | } |