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@12:2bc6047d219b, 2021-12-15 (annotated)
- Committer:
- magiwarriorx
- Date:
- Wed Dec 15 00:02:35 2021 +0000
- Revision:
- 12:2bc6047d219b
- Parent:
- 11:0309bef74ba8
- Child:
- 13:8231d6cce099
post-zeroing;
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 | 12:2bc6047d219b | 38 | /* |
| magiwarriorx | 12:2bc6047d219b | 39 | volatile int currX; |
| magiwarriorx | 12:2bc6047d219b | 40 | volatile int currY; |
| magiwarriorx | 12:2bc6047d219b | 41 | volatile int currZ; |
| magiwarriorx | 12:2bc6047d219b | 42 | volatile int toX; |
| magiwarriorx | 12:2bc6047d219b | 43 | volatile int toY; |
| magiwarriorx | 12:2bc6047d219b | 44 | volatile int toZ; |
| magiwarriorx | 12:2bc6047d219b | 45 | */ |
| geky | 7:8d9919175929 | 46 | Thread thread; |
| magiwarriorx | 12:2bc6047d219b | 47 | Thread thread2; |
| magiwarriorx | 12:2bc6047d219b | 48 | |
| magiwarriorx | 12:2bc6047d219b | 49 | |
| magiwarriorx | 12:2bc6047d219b | 50 | void parseCoords(std::string coords){ |
| magiwarriorx | 12:2bc6047d219b | 51 | |
| magiwarriorx | 12:2bc6047d219b | 52 | int start = 0; |
| magiwarriorx | 12:2bc6047d219b | 53 | int end = coords.find(','); |
| magiwarriorx | 12:2bc6047d219b | 54 | |
| magiwarriorx | 12:2bc6047d219b | 55 | float tempX = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 12:2bc6047d219b | 56 | |
| magiwarriorx | 12:2bc6047d219b | 57 | start = end + 1; |
| magiwarriorx | 12:2bc6047d219b | 58 | end = coords.find(',', start); |
| magiwarriorx | 12:2bc6047d219b | 59 | |
| magiwarriorx | 12:2bc6047d219b | 60 | float tempY = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 12:2bc6047d219b | 61 | |
| magiwarriorx | 12:2bc6047d219b | 62 | start = end + 1; |
| magiwarriorx | 12:2bc6047d219b | 63 | end = coords.length(); |
| magiwarriorx | 12:2bc6047d219b | 64 | float tempZ = atof(coords.substr(start, end-start).c_str()); |
| magiwarriorx | 12:2bc6047d219b | 65 | |
| magiwarriorx | 12:2bc6047d219b | 66 | table.goTo(tempX, tempY, tempZ); |
| magiwarriorx | 12:2bc6047d219b | 67 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 68 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 69 | uLCD.printf("%d, %d, %d", tempX,tempY,tempZ); |
| magiwarriorx | 12:2bc6047d219b | 70 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 71 | |
| magiwarriorx | 12:2bc6047d219b | 72 | } |
| magiwarriorx | 12:2bc6047d219b | 73 | |
| magiwarriorx | 12:2bc6047d219b | 74 | void bluetoothFlush(){ |
| magiwarriorx | 12:2bc6047d219b | 75 | char temp; |
| magiwarriorx | 12:2bc6047d219b | 76 | while(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 77 | temp = bluetooth.getc(); |
| emilmont | 1:491820ee784d | 78 | } |
| emilmont | 1:491820ee784d | 79 | } |
| magiwarriorx | 12:2bc6047d219b | 80 | |
| magiwarriorx | 12:2bc6047d219b | 81 | void bluetooth_thread(){ |
| magiwarriorx | 12:2bc6047d219b | 82 | |
| magiwarriorx | 12:2bc6047d219b | 83 | //char bnum=0; |
| magiwarriorx | 12:2bc6047d219b | 84 | while(1) { |
| magiwarriorx | 12:2bc6047d219b | 85 | std::string coords = ""; |
| magiwarriorx | 12:2bc6047d219b | 86 | Thread::wait(100); |
| magiwarriorx | 12:2bc6047d219b | 87 | |
| magiwarriorx | 12:2bc6047d219b | 88 | if (bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 89 | while (bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 90 | char temp = bluetooth.getc(); |
| magiwarriorx | 12:2bc6047d219b | 91 | if (temp == 'z'){ |
| magiwarriorx | 12:2bc6047d219b | 92 | table.setZero(); |
| magiwarriorx | 12:2bc6047d219b | 93 | } |
| magiwarriorx | 12:2bc6047d219b | 94 | |
| magiwarriorx | 12:2bc6047d219b | 95 | else{ |
| magiwarriorx | 12:2bc6047d219b | 96 | coords += temp; |
| magiwarriorx | 12:2bc6047d219b | 97 | } |
| magiwarriorx | 12:2bc6047d219b | 98 | } |
| magiwarriorx | 12:2bc6047d219b | 99 | //Thread::wait(1000); |
| magiwarriorx | 12:2bc6047d219b | 100 | if (coords.find('z') == -1){ |
| magiwarriorx | 12:2bc6047d219b | 101 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 102 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 103 | uLCD.printf(coords.c_str()); |
| magiwarriorx | 12:2bc6047d219b | 104 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 105 | parseCoords(coords); |
| magiwarriorx | 12:2bc6047d219b | 106 | |
| magiwarriorx | 12:2bc6047d219b | 107 | } |
| magiwarriorx | 12:2bc6047d219b | 108 | else{ |
| magiwarriorx | 12:2bc6047d219b | 109 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 110 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 111 | uLCD.printf("Zeroed!"); |
| magiwarriorx | 12:2bc6047d219b | 112 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 113 | } |
| magiwarriorx | 12:2bc6047d219b | 114 | } |
| magiwarriorx | 12:2bc6047d219b | 115 | |
| magiwarriorx | 12:2bc6047d219b | 116 | |
| emilmont | 1:491820ee784d | 117 | } |
| emilmont | 1:491820ee784d | 118 | } |
| magiwarriorx | 12:2bc6047d219b | 119 | |
| magiwarriorx | 12:2bc6047d219b | 120 | int main() { |
| magiwarriorx | 12:2bc6047d219b | 121 | //Y axis direction inverted from others to get behavior that makes me happy (rewards = negative, forward = positive) |
| magiwarriorx | 12:2bc6047d219b | 122 | table.invertY(); |
| magiwarriorx | 12:2bc6047d219b | 123 | table.invertZ(); |
| magiwarriorx | 12:2bc6047d219b | 124 | uLCD.baudrate(300000); |
| magiwarriorx | 12:2bc6047d219b | 125 | wait(0.5); |
| magiwarriorx | 12:2bc6047d219b | 126 | |
| magiwarriorx | 12:2bc6047d219b | 127 | |
| magiwarriorx | 12:2bc6047d219b | 128 | //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 | 129 | //As such its technically a bug that it skips zeroing a given axis if something is entered |
| magiwarriorx | 12:2bc6047d219b | 130 | //However, I like this functionality more than what I intended |
| magiwarriorx | 12:2bc6047d219b | 131 | //So I'm labeling the "bug" as a feature |
| magiwarriorx | 12:2bc6047d219b | 132 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 133 | uLCD.printf("Enter null value to begin X zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 134 | //LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 135 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 136 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 137 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 138 | break; |
| magiwarriorx | 12:2bc6047d219b | 139 | } |
| magiwarriorx | 12:2bc6047d219b | 140 | } |
| magiwarriorx | 12:2bc6047d219b | 141 | //LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 142 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 143 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 144 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 145 | |
| magiwarriorx | 12:2bc6047d219b | 146 | table.zeroX(); |
| magiwarriorx | 12:2bc6047d219b | 147 | uLCD.printf("Running!"); |
| magiwarriorx | 12:2bc6047d219b | 148 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 149 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 150 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 151 | xLimit = 1; |
| magiwarriorx | 12:2bc6047d219b | 152 | break; |
| magiwarriorx | 12:2bc6047d219b | 153 | } |
| magiwarriorx | 12:2bc6047d219b | 154 | } |
| magiwarriorx | 12:2bc6047d219b | 155 | |
| magiwarriorx | 12:2bc6047d219b | 156 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 157 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 158 | uLCD.printf("Enter null value to begin Y zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 159 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 160 | |
| magiwarriorx | 12:2bc6047d219b | 161 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 162 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 163 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 164 | break; |
| magiwarriorx | 12:2bc6047d219b | 165 | } |
| magiwarriorx | 12:2bc6047d219b | 166 | } |
| magiwarriorx | 12:2bc6047d219b | 167 | |
| magiwarriorx | 12:2bc6047d219b | 168 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 169 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 170 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 171 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 172 | |
| magiwarriorx | 12:2bc6047d219b | 173 | table.zeroY(); |
| magiwarriorx | 12:2bc6047d219b | 174 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 175 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 176 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 177 | yLimit = 1; |
| magiwarriorx | 12:2bc6047d219b | 178 | break; |
| magiwarriorx | 12:2bc6047d219b | 179 | } |
| magiwarriorx | 12:2bc6047d219b | 180 | } |
| magiwarriorx | 12:2bc6047d219b | 181 | |
| magiwarriorx | 12:2bc6047d219b | 182 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 183 | uLCD.cls(); |
| magiwarriorx | 12:2bc6047d219b | 184 | uLCD.printf("Enter null value to begin Z zero, anything else to skip"); |
| magiwarriorx | 12:2bc6047d219b | 185 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 186 | |
| magiwarriorx | 12:2bc6047d219b | 187 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 188 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 189 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 190 | break; |
| magiwarriorx | 12:2bc6047d219b | 191 | } |
| magiwarriorx | 12:2bc6047d219b | 192 | } |
| magiwarriorx | 12:2bc6047d219b | 193 | |
| magiwarriorx | 12:2bc6047d219b | 194 | LCD.lock(); |
| magiwarriorx | 12:2bc6047d219b | 195 | uLCD.printf("\n"); |
| magiwarriorx | 12:2bc6047d219b | 196 | uLCD.printf("Enter anything to stop"); |
| magiwarriorx | 12:2bc6047d219b | 197 | LCD.unlock(); |
| magiwarriorx | 12:2bc6047d219b | 198 | |
| magiwarriorx | 12:2bc6047d219b | 199 | table.zeroZ(); |
| magiwarriorx | 12:2bc6047d219b | 200 | while(1){ |
| magiwarriorx | 12:2bc6047d219b | 201 | if(bluetooth.readable()){ |
| magiwarriorx | 12:2bc6047d219b | 202 | bluetoothFlush(); |
| magiwarriorx | 12:2bc6047d219b | 203 | zLimit = 1; |
| magiwarriorx | 12:2bc6047d219b | 204 | break; |
| magiwarriorx | 12:2bc6047d219b | 205 | } |
| magiwarriorx | 12:2bc6047d219b | 206 | } |
| magiwarriorx | 12:2bc6047d219b | 207 | |
| 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 | 12:2bc6047d219b | 219 | thread.start(bluetooth_thread); |
| magiwarriorx | 12:2bc6047d219b | 220 | |
| magiwarriorx | 12:2bc6047d219b | 221 | while(1); |
| magiwarriorx | 12:2bc6047d219b | 222 | } |