ECE 4180 Final Project - Bluetooth Controlled Robot that aims/fires a Digital Output Device, in this case, a laser or buzzer
main.cpp@0:b5ac3f893191, 2020-04-29 (annotated)
- Committer:
- dpatel344
- Date:
- Wed Apr 29 18:29:34 2020 +0000
- Revision:
- 0:b5ac3f893191
Final ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dpatel344 | 0:b5ac3f893191 | 1 | #include "mbed.h" |
dpatel344 | 0:b5ac3f893191 | 2 | #include "Servo.h" |
dpatel344 | 0:b5ac3f893191 | 3 | |
dpatel344 | 0:b5ac3f893191 | 4 | Serial bt(p28,p27); //tx,rx |
dpatel344 | 0:b5ac3f893191 | 5 | |
dpatel344 | 0:b5ac3f893191 | 6 | Servo pan(p24); |
dpatel344 | 0:b5ac3f893191 | 7 | Servo tilt(p23); |
dpatel344 | 0:b5ac3f893191 | 8 | |
dpatel344 | 0:b5ac3f893191 | 9 | PwmOut left(p21); |
dpatel344 | 0:b5ac3f893191 | 10 | PwmOut right(p22); |
dpatel344 | 0:b5ac3f893191 | 11 | |
dpatel344 | 0:b5ac3f893191 | 12 | DigitalOut fwdl(p5); |
dpatel344 | 0:b5ac3f893191 | 13 | DigitalOut revl(p6); |
dpatel344 | 0:b5ac3f893191 | 14 | |
dpatel344 | 0:b5ac3f893191 | 15 | DigitalOut laser(p7); |
dpatel344 | 0:b5ac3f893191 | 16 | |
dpatel344 | 0:b5ac3f893191 | 17 | DigitalOut fwdr(p11); |
dpatel344 | 0:b5ac3f893191 | 18 | DigitalOut revr(p12); |
dpatel344 | 0:b5ac3f893191 | 19 | |
dpatel344 | 0:b5ac3f893191 | 20 | union packet { |
dpatel344 | 0:b5ac3f893191 | 21 | |
dpatel344 | 0:b5ac3f893191 | 22 | float f; |
dpatel344 | 0:b5ac3f893191 | 23 | char c[4]; |
dpatel344 | 0:b5ac3f893191 | 24 | |
dpatel344 | 0:b5ac3f893191 | 25 | }; |
dpatel344 | 0:b5ac3f893191 | 26 | |
dpatel344 | 0:b5ac3f893191 | 27 | int main() { |
dpatel344 | 0:b5ac3f893191 | 28 | |
dpatel344 | 0:b5ac3f893191 | 29 | char button, temp = 0; |
dpatel344 | 0:b5ac3f893191 | 30 | union packet x, y; // range is -10 to 10 |
dpatel344 | 0:b5ac3f893191 | 31 | |
dpatel344 | 0:b5ac3f893191 | 32 | // initialize servo to middle, motors off, and laser off |
dpatel344 | 0:b5ac3f893191 | 33 | fwdl = revl = fwdr = revr = 0; |
dpatel344 | 0:b5ac3f893191 | 34 | left = right = 0.0; |
dpatel344 | 0:b5ac3f893191 | 35 | pan = 0.5; |
dpatel344 | 0:b5ac3f893191 | 36 | tilt = 0.225; |
dpatel344 | 0:b5ac3f893191 | 37 | laser = 0; |
dpatel344 | 0:b5ac3f893191 | 38 | |
dpatel344 | 0:b5ac3f893191 | 39 | while (1) { |
dpatel344 | 0:b5ac3f893191 | 40 | |
dpatel344 | 0:b5ac3f893191 | 41 | if (bt.getc() == '!') { |
dpatel344 | 0:b5ac3f893191 | 42 | |
dpatel344 | 0:b5ac3f893191 | 43 | button = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 44 | switch (button) { |
dpatel344 | 0:b5ac3f893191 | 45 | |
dpatel344 | 0:b5ac3f893191 | 46 | case 'B': // if button |
dpatel344 | 0:b5ac3f893191 | 47 | |
dpatel344 | 0:b5ac3f893191 | 48 | button = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 49 | //state = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 50 | |
dpatel344 | 0:b5ac3f893191 | 51 | switch (button) { |
dpatel344 | 0:b5ac3f893191 | 52 | |
dpatel344 | 0:b5ac3f893191 | 53 | case '1': // toggle the laser/digital output device on pin 7 |
dpatel344 | 0:b5ac3f893191 | 54 | |
dpatel344 | 0:b5ac3f893191 | 55 | //if (state == '1') { |
dpatel344 | 0:b5ac3f893191 | 56 | |
dpatel344 | 0:b5ac3f893191 | 57 | laser = !laser; |
dpatel344 | 0:b5ac3f893191 | 58 | break; |
dpatel344 | 0:b5ac3f893191 | 59 | |
dpatel344 | 0:b5ac3f893191 | 60 | //} |
dpatel344 | 0:b5ac3f893191 | 61 | |
dpatel344 | 0:b5ac3f893191 | 62 | case '2': // recenter the servos |
dpatel344 | 0:b5ac3f893191 | 63 | |
dpatel344 | 0:b5ac3f893191 | 64 | //if (state == '1') { |
dpatel344 | 0:b5ac3f893191 | 65 | |
dpatel344 | 0:b5ac3f893191 | 66 | tilt = 0.225; |
dpatel344 | 0:b5ac3f893191 | 67 | pan = 0.5; |
dpatel344 | 0:b5ac3f893191 | 68 | wait(0.2); |
dpatel344 | 0:b5ac3f893191 | 69 | break; |
dpatel344 | 0:b5ac3f893191 | 70 | |
dpatel344 | 0:b5ac3f893191 | 71 | //} |
dpatel344 | 0:b5ac3f893191 | 72 | |
dpatel344 | 0:b5ac3f893191 | 73 | case '5': // forward |
dpatel344 | 0:b5ac3f893191 | 74 | |
dpatel344 | 0:b5ac3f893191 | 75 | //if (state == '1') { |
dpatel344 | 0:b5ac3f893191 | 76 | |
dpatel344 | 0:b5ac3f893191 | 77 | if (tilt <= 0.05) {tilt = 0.0;} |
dpatel344 | 0:b5ac3f893191 | 78 | else {tilt = tilt - 0.05;} |
dpatel344 | 0:b5ac3f893191 | 79 | wait(0.1); |
dpatel344 | 0:b5ac3f893191 | 80 | break; |
dpatel344 | 0:b5ac3f893191 | 81 | |
dpatel344 | 0:b5ac3f893191 | 82 | //} |
dpatel344 | 0:b5ac3f893191 | 83 | |
dpatel344 | 0:b5ac3f893191 | 84 | case '6': // backward |
dpatel344 | 0:b5ac3f893191 | 85 | |
dpatel344 | 0:b5ac3f893191 | 86 | //if (state == '1') { |
dpatel344 | 0:b5ac3f893191 | 87 | |
dpatel344 | 0:b5ac3f893191 | 88 | if (tilt >= 0.40) {tilt = 0.45;} |
dpatel344 | 0:b5ac3f893191 | 89 | else {tilt = tilt + 0.05;} |
dpatel344 | 0:b5ac3f893191 | 90 | wait(0.1); |
dpatel344 | 0:b5ac3f893191 | 91 | break; |
dpatel344 | 0:b5ac3f893191 | 92 | |
dpatel344 | 0:b5ac3f893191 | 93 | //} |
dpatel344 | 0:b5ac3f893191 | 94 | |
dpatel344 | 0:b5ac3f893191 | 95 | case '7': // right |
dpatel344 | 0:b5ac3f893191 | 96 | |
dpatel344 | 0:b5ac3f893191 | 97 | //if (state == '1') { |
dpatel344 | 0:b5ac3f893191 | 98 | |
dpatel344 | 0:b5ac3f893191 | 99 | if (pan <= 0.05) {pan = 0.0;} |
dpatel344 | 0:b5ac3f893191 | 100 | else {pan = pan - 0.05;} |
dpatel344 | 0:b5ac3f893191 | 101 | wait(0.1); |
dpatel344 | 0:b5ac3f893191 | 102 | break; |
dpatel344 | 0:b5ac3f893191 | 103 | |
dpatel344 | 0:b5ac3f893191 | 104 | //} |
dpatel344 | 0:b5ac3f893191 | 105 | |
dpatel344 | 0:b5ac3f893191 | 106 | |
dpatel344 | 0:b5ac3f893191 | 107 | case '8': // left |
dpatel344 | 0:b5ac3f893191 | 108 | |
dpatel344 | 0:b5ac3f893191 | 109 | //if (state == '1') { |
dpatel344 | 0:b5ac3f893191 | 110 | |
dpatel344 | 0:b5ac3f893191 | 111 | if (pan >= 0.95) {pan = 1.0;} |
dpatel344 | 0:b5ac3f893191 | 112 | else {pan = pan + 0.05;} |
dpatel344 | 0:b5ac3f893191 | 113 | wait(0.1); |
dpatel344 | 0:b5ac3f893191 | 114 | break; |
dpatel344 | 0:b5ac3f893191 | 115 | |
dpatel344 | 0:b5ac3f893191 | 116 | // } |
dpatel344 | 0:b5ac3f893191 | 117 | |
dpatel344 | 0:b5ac3f893191 | 118 | } |
dpatel344 | 0:b5ac3f893191 | 119 | break; |
dpatel344 | 0:b5ac3f893191 | 120 | |
dpatel344 | 0:b5ac3f893191 | 121 | case 'A': // otherwise accelerometer |
dpatel344 | 0:b5ac3f893191 | 122 | |
dpatel344 | 0:b5ac3f893191 | 123 | // fetch and store accelerometer data |
dpatel344 | 0:b5ac3f893191 | 124 | |
dpatel344 | 0:b5ac3f893191 | 125 | for (int i = 0; i < 4; i++) { |
dpatel344 | 0:b5ac3f893191 | 126 | |
dpatel344 | 0:b5ac3f893191 | 127 | temp = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 128 | x.c[i] = temp; |
dpatel344 | 0:b5ac3f893191 | 129 | |
dpatel344 | 0:b5ac3f893191 | 130 | } |
dpatel344 | 0:b5ac3f893191 | 131 | |
dpatel344 | 0:b5ac3f893191 | 132 | for (int i = 0; i < 4; i++) { |
dpatel344 | 0:b5ac3f893191 | 133 | |
dpatel344 | 0:b5ac3f893191 | 134 | temp = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 135 | y.c[i] = temp; |
dpatel344 | 0:b5ac3f893191 | 136 | |
dpatel344 | 0:b5ac3f893191 | 137 | } |
dpatel344 | 0:b5ac3f893191 | 138 | |
dpatel344 | 0:b5ac3f893191 | 139 | // final loop to capture Z accelerometer data (NOT USED) |
dpatel344 | 0:b5ac3f893191 | 140 | for (int i = 0; i < 4; i++) { |
dpatel344 | 0:b5ac3f893191 | 141 | |
dpatel344 | 0:b5ac3f893191 | 142 | temp = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 143 | |
dpatel344 | 0:b5ac3f893191 | 144 | } |
dpatel344 | 0:b5ac3f893191 | 145 | |
dpatel344 | 0:b5ac3f893191 | 146 | temp = bt.getc(); |
dpatel344 | 0:b5ac3f893191 | 147 | |
dpatel344 | 0:b5ac3f893191 | 148 | // read accelerometer data and control motors |
dpatel344 | 0:b5ac3f893191 | 149 | |
dpatel344 | 0:b5ac3f893191 | 150 | if (x.f <= -2.0) { // left |
dpatel344 | 0:b5ac3f893191 | 151 | |
dpatel344 | 0:b5ac3f893191 | 152 | fwdl = fwdr = 1; |
dpatel344 | 0:b5ac3f893191 | 153 | revl = revr = 0; |
dpatel344 | 0:b5ac3f893191 | 154 | left = 0.1; |
dpatel344 | 0:b5ac3f893191 | 155 | right = 1.0; |
dpatel344 | 0:b5ac3f893191 | 156 | |
dpatel344 | 0:b5ac3f893191 | 157 | } |
dpatel344 | 0:b5ac3f893191 | 158 | |
dpatel344 | 0:b5ac3f893191 | 159 | else if (x.f >= 2.0) { // right |
dpatel344 | 0:b5ac3f893191 | 160 | |
dpatel344 | 0:b5ac3f893191 | 161 | fwdl = fwdr = 1; |
dpatel344 | 0:b5ac3f893191 | 162 | revl = revr = 0; |
dpatel344 | 0:b5ac3f893191 | 163 | left = 1.0; |
dpatel344 | 0:b5ac3f893191 | 164 | right = 0.1; |
dpatel344 | 0:b5ac3f893191 | 165 | |
dpatel344 | 0:b5ac3f893191 | 166 | } |
dpatel344 | 0:b5ac3f893191 | 167 | |
dpatel344 | 0:b5ac3f893191 | 168 | else { // if no tilt, make sure that motors are disengaged |
dpatel344 | 0:b5ac3f893191 | 169 | |
dpatel344 | 0:b5ac3f893191 | 170 | fwdl = fwdr = revl = revr = 0; |
dpatel344 | 0:b5ac3f893191 | 171 | left = right = 0.0; |
dpatel344 | 0:b5ac3f893191 | 172 | } |
dpatel344 | 0:b5ac3f893191 | 173 | |
dpatel344 | 0:b5ac3f893191 | 174 | if (y.f <= -2.0) { // reverse |
dpatel344 | 0:b5ac3f893191 | 175 | |
dpatel344 | 0:b5ac3f893191 | 176 | fwdl = fwdr = 0; |
dpatel344 | 0:b5ac3f893191 | 177 | revl = revr = 1; |
dpatel344 | 0:b5ac3f893191 | 178 | right = left = 1.0; |
dpatel344 | 0:b5ac3f893191 | 179 | |
dpatel344 | 0:b5ac3f893191 | 180 | } |
dpatel344 | 0:b5ac3f893191 | 181 | |
dpatel344 | 0:b5ac3f893191 | 182 | else if (y.f >= 2.0) { // forward |
dpatel344 | 0:b5ac3f893191 | 183 | |
dpatel344 | 0:b5ac3f893191 | 184 | fwdl = fwdr = 1; |
dpatel344 | 0:b5ac3f893191 | 185 | revl = revr = 0; |
dpatel344 | 0:b5ac3f893191 | 186 | right = left = 1.0; |
dpatel344 | 0:b5ac3f893191 | 187 | |
dpatel344 | 0:b5ac3f893191 | 188 | } |
dpatel344 | 0:b5ac3f893191 | 189 | |
dpatel344 | 0:b5ac3f893191 | 190 | break; |
dpatel344 | 0:b5ac3f893191 | 191 | |
dpatel344 | 0:b5ac3f893191 | 192 | } |
dpatel344 | 0:b5ac3f893191 | 193 | } |
dpatel344 | 0:b5ac3f893191 | 194 | } |
dpatel344 | 0:b5ac3f893191 | 195 | } |