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: C12832_lcd FatFileSystemCpp MMA7660 mbed
Fork of app-board-Bubble-Level by
main.cpp@2:2fb847807890, 2015-08-04 (annotated)
- Committer:
- ecleland
- Date:
- Tue Aug 04 15:01:11 2015 +0000
- Revision:
- 2:2fb847807890
- Parent:
- 1:876f52a697c1
- Child:
- 3:6dae4f871cdc
Work please
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 1:876f52a697c1 | 1 | //Uses x & y acceleration to simulate a bubble level |
4180_1 | 1:876f52a697c1 | 2 | //on the application board LCD display |
Sissors | 0:bd0546063b0a | 3 | #include "mbed.h" |
Sissors | 0:bd0546063b0a | 4 | #include "MMA7660.h" |
4180_1 | 1:876f52a697c1 | 5 | #include "C12832_lcd.h" |
ecleland | 2:2fb847807890 | 6 | #include "USBHostMSD.h" |
ecleland | 2:2fb847807890 | 7 | |
ecleland | 2:2fb847807890 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
Sissors | 0:bd0546063b0a | 9 | |
4180_1 | 1:876f52a697c1 | 10 | C12832_LCD lcd; //On board LCD display |
4180_1 | 1:876f52a697c1 | 11 | MMA7660 MMA(p28, p27); //I2C Accelerometer |
4180_1 | 1:876f52a697c1 | 12 | DigitalOut connectionLed(LED1);//Accel OK LED |
Sissors | 0:bd0546063b0a | 13 | |
ecleland | 2:2fb847807890 | 14 | #include "mbed.h" |
ecleland | 2:2fb847807890 | 15 | #include <string> |
ecleland | 2:2fb847807890 | 16 | |
ecleland | 2:2fb847807890 | 17 | //opens mbed memory for writing |
ecleland | 2:2fb847807890 | 18 | LocalFileSystem local("local"); |
ecleland | 2:2fb847807890 | 19 | FILE *fp = fopen("/local/MovementData.txt", "w"); |
ecleland | 2:2fb847807890 | 20 | |
ecleland | 2:2fb847807890 | 21 | //PwmOut strafe(p22); |
ecleland | 2:2fb847807890 | 22 | DigitalOut strafe(p21); |
ecleland | 2:2fb847807890 | 23 | DigitalOut forback(p22); |
ecleland | 2:2fb847807890 | 24 | DigitalOut updown(p23); |
ecleland | 2:2fb847807890 | 25 | DigitalOut rlturn(p24); |
ecleland | 2:2fb847807890 | 26 | AnalogOut output(p18); |
ecleland | 2:2fb847807890 | 27 | |
ecleland | 2:2fb847807890 | 28 | float StrafeVM = 0.48638; |
ecleland | 2:2fb847807890 | 29 | float ForBackVM = .508172; |
ecleland | 2:2fb847807890 | 30 | float UpDownVM = .50424; |
ecleland | 2:2fb847807890 | 31 | float RLTurnVM = .508475; |
ecleland | 2:2fb847807890 | 32 | |
ecleland | 2:2fb847807890 | 33 | float step = .001; |
ecleland | 2:2fb847807890 | 34 | |
ecleland | 2:2fb847807890 | 35 | void NoOut() |
ecleland | 2:2fb847807890 | 36 | { |
ecleland | 2:2fb847807890 | 37 | strafe = 1; |
ecleland | 2:2fb847807890 | 38 | forback = 1; |
ecleland | 2:2fb847807890 | 39 | updown = 1; |
ecleland | 2:2fb847807890 | 40 | rlturn = 1; |
ecleland | 2:2fb847807890 | 41 | } |
ecleland | 2:2fb847807890 | 42 | |
ecleland | 2:2fb847807890 | 43 | void SET(string movement, float Out) |
ecleland | 2:2fb847807890 | 44 | { |
ecleland | 2:2fb847807890 | 45 | |
ecleland | 2:2fb847807890 | 46 | NoOut(); |
ecleland | 2:2fb847807890 | 47 | |
ecleland | 2:2fb847807890 | 48 | output = Out; |
ecleland | 2:2fb847807890 | 49 | |
ecleland | 2:2fb847807890 | 50 | if (movement == "s") { |
ecleland | 2:2fb847807890 | 51 | strafe = 0; |
ecleland | 2:2fb847807890 | 52 | } |
ecleland | 2:2fb847807890 | 53 | |
ecleland | 2:2fb847807890 | 54 | if (movement == "fb") { |
ecleland | 2:2fb847807890 | 55 | forback = 0; |
ecleland | 2:2fb847807890 | 56 | } |
ecleland | 2:2fb847807890 | 57 | |
ecleland | 2:2fb847807890 | 58 | if (movement == "ud") { |
ecleland | 2:2fb847807890 | 59 | updown = 0; |
ecleland | 2:2fb847807890 | 60 | } |
ecleland | 2:2fb847807890 | 61 | |
ecleland | 2:2fb847807890 | 62 | if (movement == "rl") { |
ecleland | 2:2fb847807890 | 63 | rlturn = 0; |
ecleland | 2:2fb847807890 | 64 | } |
ecleland | 2:2fb847807890 | 65 | |
ecleland | 2:2fb847807890 | 66 | wait(.005); |
ecleland | 2:2fb847807890 | 67 | |
ecleland | 2:2fb847807890 | 68 | fprintf(fp,"%s ", (string)movement); |
ecleland | 2:2fb847807890 | 69 | fprintf(fp,"%f ", (float)Out); |
ecleland | 2:2fb847807890 | 70 | |
ecleland | 2:2fb847807890 | 71 | NoOut(); |
ecleland | 2:2fb847807890 | 72 | |
ecleland | 2:2fb847807890 | 73 | } |
ecleland | 2:2fb847807890 | 74 | |
ecleland | 2:2fb847807890 | 75 | //latch chip drifts voltage up pretty quickly beyond the limits of the phantom if we reset the values though all is good. |
ecleland | 2:2fb847807890 | 76 | void REFRESH() |
ecleland | 2:2fb847807890 | 77 | { |
ecleland | 2:2fb847807890 | 78 | SET("s", StrafeVM); |
ecleland | 2:2fb847807890 | 79 | SET("fb", ForBackVM); |
ecleland | 2:2fb847807890 | 80 | SET("ud", UpDownVM); |
ecleland | 2:2fb847807890 | 81 | SET("rl", RLTurnVM); |
ecleland | 2:2fb847807890 | 82 | } |
ecleland | 2:2fb847807890 | 83 | |
ecleland | 2:2fb847807890 | 84 | void IDLE() |
ecleland | 2:2fb847807890 | 85 | { |
ecleland | 2:2fb847807890 | 86 | SET("s", 0.48638); |
ecleland | 2:2fb847807890 | 87 | SET("fb", 0.508172); |
ecleland | 2:2fb847807890 | 88 | SET("ud", 0.50424); |
ecleland | 2:2fb847807890 | 89 | SET("rl", 0.508475); |
ecleland | 2:2fb847807890 | 90 | } |
ecleland | 2:2fb847807890 | 91 | |
ecleland | 2:2fb847807890 | 92 | void START() |
ecleland | 2:2fb847807890 | 93 | { |
ecleland | 2:2fb847807890 | 94 | SET("s", 0.575379); |
ecleland | 2:2fb847807890 | 95 | SET("fb", 0.592171); |
ecleland | 2:2fb847807890 | 96 | SET("ud", 0.421241); |
ecleland | 2:2fb847807890 | 97 | SET("rl", 0.589474); |
ecleland | 2:2fb847807890 | 98 | wait(3); |
ecleland | 2:2fb847807890 | 99 | } |
ecleland | 2:2fb847807890 | 100 | |
4180_1 | 1:876f52a697c1 | 101 | int main() |
4180_1 | 1:876f52a697c1 | 102 | { |
ecleland | 2:2fb847807890 | 103 | bool on = false; |
ecleland | 2:2fb847807890 | 104 | bool YN = true; |
ecleland | 2:2fb847807890 | 105 | |
ecleland | 2:2fb847807890 | 106 | pc.printf("\n"); |
ecleland | 2:2fb847807890 | 107 | |
ecleland | 2:2fb847807890 | 108 | lcd.cls(); |
ecleland | 2:2fb847807890 | 109 | lcd.locate(0,3); |
ecleland | 2:2fb847807890 | 110 | lcd.printf("Turn Phantom 2 Drone On? (Y/N) "); |
ecleland | 2:2fb847807890 | 111 | |
ecleland | 2:2fb847807890 | 112 | while(YN) { |
ecleland | 2:2fb847807890 | 113 | char a = pc.getc(); |
ecleland | 2:2fb847807890 | 114 | if (a == 'y' || a == 'Y') { |
ecleland | 2:2fb847807890 | 115 | on = true; |
ecleland | 2:2fb847807890 | 116 | YN = false; |
ecleland | 2:2fb847807890 | 117 | } |
ecleland | 2:2fb847807890 | 118 | if (a == 'n' || a == 'N') { |
ecleland | 2:2fb847807890 | 119 | YN = false; |
ecleland | 2:2fb847807890 | 120 | } |
ecleland | 2:2fb847807890 | 121 | |
Sissors | 0:bd0546063b0a | 122 | } |
Sissors | 0:bd0546063b0a | 123 | |
ecleland | 2:2fb847807890 | 124 | if(on) { |
ecleland | 2:2fb847807890 | 125 | |
ecleland | 2:2fb847807890 | 126 | //all limits need to be investigatged as vcc is not actually +3.304V also phantom 2 voltages vary based on battery charge in controller. |
ecleland | 2:2fb847807890 | 127 | while(on) { |
ecleland | 2:2fb847807890 | 128 | |
ecleland | 2:2fb847807890 | 129 | if(MMA.z() < 0) { |
ecleland | 2:2fb847807890 | 130 | IDLE(); |
ecleland | 2:2fb847807890 | 131 | START(); |
ecleland | 2:2fb847807890 | 132 | IDLE(); |
ecleland | 2:2fb847807890 | 133 | } |
ecleland | 2:2fb847807890 | 134 | |
ecleland | 2:2fb847807890 | 135 | //set c to be arbitrary |
ecleland | 2:2fb847807890 | 136 | char c = '?'; |
ecleland | 2:2fb847807890 | 137 | |
ecleland | 2:2fb847807890 | 138 | REFRESH(); |
ecleland | 2:2fb847807890 | 139 | |
ecleland | 2:2fb847807890 | 140 | //pc.getc() hangs (stops) the loop until a caracter is read. due to the need to |
ecleland | 2:2fb847807890 | 141 | if (pc.readable()) { |
ecleland | 2:2fb847807890 | 142 | c = pc.getc(); |
ecleland | 2:2fb847807890 | 143 | } |
ecleland | 2:2fb847807890 | 144 | |
ecleland | 2:2fb847807890 | 145 | //strafing |
ecleland | 2:2fb847807890 | 146 | if((c == 'a') && (StrafeVM < /*0.57234*/ .6)) { |
ecleland | 2:2fb847807890 | 147 | StrafeVM += step; |
ecleland | 2:2fb847807890 | 148 | SET("s", StrafeVM); |
ecleland | 2:2fb847807890 | 149 | pc.printf("%f%", StrafeVM); |
ecleland | 2:2fb847807890 | 150 | } |
ecleland | 2:2fb847807890 | 151 | if((c == 'd') && (StrafeVM > 0.410714)) { |
ecleland | 2:2fb847807890 | 152 | StrafeVM -= step; |
ecleland | 2:2fb847807890 | 153 | SET("s", StrafeVM); |
ecleland | 2:2fb847807890 | 154 | pc.printf("%f%", StrafeVM); |
ecleland | 2:2fb847807890 | 155 | } |
ecleland | 2:2fb847807890 | 156 | |
ecleland | 2:2fb847807890 | 157 | //Forward and Backwards |
ecleland | 2:2fb847807890 | 158 | if((c == 'w') && (ForBackVM > 0.424031)) { |
ecleland | 2:2fb847807890 | 159 | ForBackVM -= step; |
ecleland | 2:2fb847807890 | 160 | SET("fb", ForBackVM); |
ecleland | 2:2fb847807890 | 161 | pc.printf("%f%", ForBackVM); |
ecleland | 2:2fb847807890 | 162 | } |
ecleland | 2:2fb847807890 | 163 | if((c == 's') && (ForBackVM < /*0.58535*/ .6)) { |
ecleland | 2:2fb847807890 | 164 | ForBackVM += step; |
ecleland | 2:2fb847807890 | 165 | SET("fb", ForBackVM); |
ecleland | 2:2fb847807890 | 166 | pc.printf("%f%", ForBackVM); |
ecleland | 2:2fb847807890 | 167 | } |
ecleland | 2:2fb847807890 | 168 | |
ecleland | 2:2fb847807890 | 169 | //Up and Down Issues with being off by .08v ish |
ecleland | 2:2fb847807890 | 170 | if((c == 'i') && (UpDownVM < 0.58323)) { |
ecleland | 2:2fb847807890 | 171 | UpDownVM += step; |
ecleland | 2:2fb847807890 | 172 | SET("ud", UpDownVM); |
ecleland | 2:2fb847807890 | 173 | pc.printf("%f%", UpDownVM); |
ecleland | 2:2fb847807890 | 174 | } |
ecleland | 2:2fb847807890 | 175 | if((c == 'k') && (UpDownVM > 0.42161)) { |
ecleland | 2:2fb847807890 | 176 | UpDownVM -= step; |
ecleland | 2:2fb847807890 | 177 | SET("ud", UpDownVM); |
ecleland | 2:2fb847807890 | 178 | pc.printf("%f%", UpDownVM); |
ecleland | 2:2fb847807890 | 179 | } |
ecleland | 2:2fb847807890 | 180 | |
ecleland | 2:2fb847807890 | 181 | //Turning |
ecleland | 2:2fb847807890 | 182 | if((c == 'j') && (RLTurnVM < 0.5905)) { |
ecleland | 2:2fb847807890 | 183 | RLTurnVM += step; |
ecleland | 2:2fb847807890 | 184 | SET("rl", RLTurnVM); |
ecleland | 2:2fb847807890 | 185 | pc.printf("%f%", RLTurnVM); |
ecleland | 2:2fb847807890 | 186 | } |
ecleland | 2:2fb847807890 | 187 | if((c == 'l') && (RLTurnVM > 0.42615)) { |
ecleland | 2:2fb847807890 | 188 | RLTurnVM -= step; |
ecleland | 2:2fb847807890 | 189 | SET("rl", RLTurnVM); |
ecleland | 2:2fb847807890 | 190 | pc.printf("%f%", RLTurnVM); |
ecleland | 2:2fb847807890 | 191 | } |
ecleland | 2:2fb847807890 | 192 | |
ecleland | 2:2fb847807890 | 193 | //emergency idle |
ecleland | 2:2fb847807890 | 194 | if(c == ' ') { |
ecleland | 2:2fb847807890 | 195 | IDLE(); |
ecleland | 2:2fb847807890 | 196 | } |
ecleland | 2:2fb847807890 | 197 | |
ecleland | 2:2fb847807890 | 198 | //ground/turn off the drone |
ecleland | 2:2fb847807890 | 199 | if(c == 'g') { |
ecleland | 2:2fb847807890 | 200 | IDLE(); |
ecleland | 2:2fb847807890 | 201 | |
ecleland | 2:2fb847807890 | 202 | while(UpDownVM > 0.48) { |
ecleland | 2:2fb847807890 | 203 | UpDownVM -= .01; |
ecleland | 2:2fb847807890 | 204 | SET("ud", UpDownVM); |
ecleland | 2:2fb847807890 | 205 | wait(0.1); |
ecleland | 2:2fb847807890 | 206 | } |
ecleland | 2:2fb847807890 | 207 | wait(2); |
ecleland | 2:2fb847807890 | 208 | |
ecleland | 2:2fb847807890 | 209 | on = false; |
ecleland | 2:2fb847807890 | 210 | |
ecleland | 2:2fb847807890 | 211 | } |
ecleland | 2:2fb847807890 | 212 | |
ecleland | 2:2fb847807890 | 213 | } |
ecleland | 2:2fb847807890 | 214 | |
ecleland | 2:2fb847807890 | 215 | } |
ecleland | 2:2fb847807890 | 216 | |
ecleland | 2:2fb847807890 | 217 | fclose(fp); |
ecleland | 2:2fb847807890 | 218 | pc.printf("Drone off"); |
ecleland | 2:2fb847807890 | 219 | while(1) { |
ecleland | 2:2fb847807890 | 220 | SET("s", 0.48638); |
ecleland | 2:2fb847807890 | 221 | SET("fb", 0.508172); |
ecleland | 2:2fb847807890 | 222 | SET("ud", .421); |
ecleland | 2:2fb847807890 | 223 | SET("rl", 0.508475); |
ecleland | 2:2fb847807890 | 224 | } |
ecleland | 2:2fb847807890 | 225 | return 0; |
Sissors | 0:bd0546063b0a | 226 | } |