ee202hw1
Dependencies: Useless SB TSI mbed
Fork of Final_Homework1 by
main.cpp@1:a00bb47c1028, 2014-02-15 (annotated)
- Committer:
- bmdlh
- Date:
- Sat Feb 15 06:10:17 2014 +0000
- Revision:
- 1:a00bb47c1028
- Parent:
- 0:c8a7071d8692
ee202hw1(main function)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bmdlh | 0:c8a7071d8692 | 1 | #include "mbed.h" |
bmdlh | 0:c8a7071d8692 | 2 | #include "TSISensor.h" |
bmdlh | 0:c8a7071d8692 | 3 | #include "MMA8451Q.h" |
bmdlh | 0:c8a7071d8692 | 4 | #include "MAG3110.h" |
bmdlh | 1:a00bb47c1028 | 5 | |
bmdlh | 1:a00bb47c1028 | 6 | #define OFF 0 |
bmdlh | 1:a00bb47c1028 | 7 | #define ON 1 |
bmdlh | 0:c8a7071d8692 | 8 | |
bmdlh | 0:c8a7071d8692 | 9 | Serial pc(USBTX, USBRX); |
bmdlh | 0:c8a7071d8692 | 10 | |
bmdlh | 0:c8a7071d8692 | 11 | PwmOut rled(PTE29); |
bmdlh | 0:c8a7071d8692 | 12 | PwmOut gled(PTD5); |
bmdlh | 1:a00bb47c1028 | 13 | |
bmdlh | 1:a00bb47c1028 | 14 | MMA8451Q acc(PTE25, PTE24,0x1d<<1); |
bmdlh | 1:a00bb47c1028 | 15 | MAG3110 mag(PTE25, PTE24); |
bmdlh | 0:c8a7071d8692 | 16 | AnalogIn lightSensor(PTE22); |
bmdlh | 1:a00bb47c1028 | 17 | TSISensor tsi; |
bmdlh | 1:a00bb47c1028 | 18 | AnalogIn sinwave(PTB1); |
bmdlh | 1:a00bb47c1028 | 19 | |
bmdlh | 1:a00bb47c1028 | 20 | struct |
bmdlh | 1:a00bb47c1028 | 21 | { |
bmdlh | 1:a00bb47c1028 | 22 | int mag : 1; |
bmdlh | 1:a00bb47c1028 | 23 | int acc : 1; |
bmdlh | 1:a00bb47c1028 | 24 | int light : 1; |
bmdlh | 1:a00bb47c1028 | 25 | int touch : 1; |
bmdlh | 1:a00bb47c1028 | 26 | int sine : 1; |
bmdlh | 1:a00bb47c1028 | 27 | } status; |
bmdlh | 1:a00bb47c1028 | 28 | |
bmdlh | 1:a00bb47c1028 | 29 | Timer timer_mag; |
bmdlh | 1:a00bb47c1028 | 30 | Timer timer_acc; |
bmdlh | 1:a00bb47c1028 | 31 | Timer timer_light; |
bmdlh | 1:a00bb47c1028 | 32 | Timer timer_touch; |
bmdlh | 1:a00bb47c1028 | 33 | Timer timer_sin; |
bmdlh | 1:a00bb47c1028 | 34 | |
bmdlh | 1:a00bb47c1028 | 35 | int threshold1 = 32767; |
bmdlh | 1:a00bb47c1028 | 36 | int threshold2 = 32767; |
bmdlh | 1:a00bb47c1028 | 37 | int threshold3 = 32767; |
bmdlh | 1:a00bb47c1028 | 38 | int threshold4 = 32767; |
bmdlh | 1:a00bb47c1028 | 39 | int threshold5 = 32767; |
bmdlh | 1:a00bb47c1028 | 40 | |
bmdlh | 1:a00bb47c1028 | 41 | int START = OFF; |
bmdlh | 0:c8a7071d8692 | 42 | |
bmdlh | 0:c8a7071d8692 | 43 | void calXY() //magnetometer calibration: finding max and min of X, Y axis |
bmdlh | 0:c8a7071d8692 | 44 | { |
bmdlh | 0:c8a7071d8692 | 45 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
bmdlh | 0:c8a7071d8692 | 46 | |
bmdlh | 0:c8a7071d8692 | 47 | rled = ON; |
bmdlh | 0:c8a7071d8692 | 48 | |
bmdlh | 0:c8a7071d8692 | 49 | printf("Waiting for initial press\n"); |
bmdlh | 0:c8a7071d8692 | 50 | // Wait for slider to be pressed |
bmdlh | 0:c8a7071d8692 | 51 | while( tsi.readDistance() == 0 ) { |
bmdlh | 0:c8a7071d8692 | 52 | rled = ON; |
bmdlh | 0:c8a7071d8692 | 53 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 54 | rled = OFF; |
bmdlh | 0:c8a7071d8692 | 55 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 56 | } |
bmdlh | 0:c8a7071d8692 | 57 | |
bmdlh | 0:c8a7071d8692 | 58 | printf("Waiting for release\n"); |
bmdlh | 0:c8a7071d8692 | 59 | |
bmdlh | 0:c8a7071d8692 | 60 | // Wait for release |
bmdlh | 0:c8a7071d8692 | 61 | while( tsi.readDistance() != 0 ) { |
bmdlh | 0:c8a7071d8692 | 62 | rled = OFF; |
bmdlh | 0:c8a7071d8692 | 63 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 64 | rled = ON; |
bmdlh | 0:c8a7071d8692 | 65 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 66 | } |
bmdlh | 0:c8a7071d8692 | 67 | rled = OFF; |
bmdlh | 0:c8a7071d8692 | 68 | wait(0.5); |
bmdlh | 0:c8a7071d8692 | 69 | |
bmdlh | 0:c8a7071d8692 | 70 | printf("Rotate\n"); |
bmdlh | 0:c8a7071d8692 | 71 | |
bmdlh | 0:c8a7071d8692 | 72 | tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); |
bmdlh | 0:c8a7071d8692 | 73 | tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); |
bmdlh | 0:c8a7071d8692 | 74 | |
bmdlh | 0:c8a7071d8692 | 75 | while(tsi.readDistance() == 0) { |
bmdlh | 0:c8a7071d8692 | 76 | gled = ON; |
bmdlh | 0:c8a7071d8692 | 77 | wait(0.1); |
bmdlh | 0:c8a7071d8692 | 78 | gled = OFF; |
bmdlh | 0:c8a7071d8692 | 79 | wait(0.1); |
bmdlh | 0:c8a7071d8692 | 80 | newX = mag.readVal(MAG_OUT_X_MSB); |
bmdlh | 0:c8a7071d8692 | 81 | newY = mag.readVal(MAG_OUT_Y_MSB); |
bmdlh | 0:c8a7071d8692 | 82 | if (newX > tempXmax) tempXmax = newX; |
bmdlh | 0:c8a7071d8692 | 83 | if (newX < tempXmin) tempXmin = newX; |
bmdlh | 0:c8a7071d8692 | 84 | if (newY > tempYmax) tempYmax = newY; |
bmdlh | 0:c8a7071d8692 | 85 | if (newY < tempYmin) tempYmin = newY; |
bmdlh | 0:c8a7071d8692 | 86 | } |
bmdlh | 0:c8a7071d8692 | 87 | |
bmdlh | 0:c8a7071d8692 | 88 | mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
bmdlh | 0:c8a7071d8692 | 89 | |
bmdlh | 0:c8a7071d8692 | 90 | // Wait for release |
bmdlh | 0:c8a7071d8692 | 91 | while( tsi.readDistance() != 0 ) { |
bmdlh | 0:c8a7071d8692 | 92 | gled = OFF; |
bmdlh | 0:c8a7071d8692 | 93 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 94 | gled = ON; |
bmdlh | 0:c8a7071d8692 | 95 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 96 | } |
bmdlh | 0:c8a7071d8692 | 97 | gled = OFF; |
bmdlh | 0:c8a7071d8692 | 98 | wait(1.0); |
bmdlh | 0:c8a7071d8692 | 99 | } |
bmdlh | 1:a00bb47c1028 | 100 | void send4bytes(char label,void* x){ |
bmdlh | 1:a00bb47c1028 | 101 | char *p = (char *)x; |
bmdlh | 1:a00bb47c1028 | 102 | pc.putc(label); |
bmdlh | 1:a00bb47c1028 | 103 | pc.putc(*p); |
bmdlh | 1:a00bb47c1028 | 104 | pc.putc(*(p+1)); |
bmdlh | 1:a00bb47c1028 | 105 | pc.putc(*(p+2)); |
bmdlh | 1:a00bb47c1028 | 106 | pc.putc(*(p+3)); |
bmdlh | 1:a00bb47c1028 | 107 | } |
bmdlh | 1:a00bb47c1028 | 108 | void mag_send(){ |
bmdlh | 1:a00bb47c1028 | 109 | int x = 0, y = 0, z = 0; |
bmdlh | 1:a00bb47c1028 | 110 | float h = mag.getHeading(); |
bmdlh | 1:a00bb47c1028 | 111 | mag.getValues(&x, &y, &z); |
bmdlh | 1:a00bb47c1028 | 112 | __disable_irq(); |
bmdlh | 1:a00bb47c1028 | 113 | send4bytes(0x00,&x); |
bmdlh | 1:a00bb47c1028 | 114 | send4bytes(0x01,&y); |
bmdlh | 1:a00bb47c1028 | 115 | send4bytes(0x02,&z); |
bmdlh | 1:a00bb47c1028 | 116 | send4bytes(0x03,&h); |
bmdlh | 1:a00bb47c1028 | 117 | __enable_irq(); |
bmdlh | 1:a00bb47c1028 | 118 | } |
bmdlh | 1:a00bb47c1028 | 119 | void accl_send(){ |
bmdlh | 1:a00bb47c1028 | 120 | float x = acc.getAccX(); |
bmdlh | 1:a00bb47c1028 | 121 | float y = acc.getAccY(); |
bmdlh | 1:a00bb47c1028 | 122 | float z = acc.getAccZ(); |
bmdlh | 1:a00bb47c1028 | 123 | __disable_irq(); |
bmdlh | 1:a00bb47c1028 | 124 | send4bytes(0x04,&x); |
bmdlh | 1:a00bb47c1028 | 125 | send4bytes(0x05,&y); |
bmdlh | 1:a00bb47c1028 | 126 | send4bytes(0x06,&z); |
bmdlh | 1:a00bb47c1028 | 127 | __enable_irq(); |
bmdlh | 1:a00bb47c1028 | 128 | } |
bmdlh | 0:c8a7071d8692 | 129 | |
bmdlh | 1:a00bb47c1028 | 130 | void light_send(){ |
bmdlh | 1:a00bb47c1028 | 131 | float cur_value = lightSensor; |
bmdlh | 1:a00bb47c1028 | 132 | __disable_irq(); |
bmdlh | 1:a00bb47c1028 | 133 | send4bytes(0x07,&cur_value); |
bmdlh | 1:a00bb47c1028 | 134 | __enable_irq(); |
bmdlh | 1:a00bb47c1028 | 135 | } |
bmdlh | 1:a00bb47c1028 | 136 | void touch_send(){ |
bmdlh | 1:a00bb47c1028 | 137 | float cur_value = tsi.readPercentage();; |
bmdlh | 1:a00bb47c1028 | 138 | __disable_irq(); |
bmdlh | 1:a00bb47c1028 | 139 | send4bytes(0x08,&cur_value); |
bmdlh | 1:a00bb47c1028 | 140 | __enable_irq(); |
bmdlh | 1:a00bb47c1028 | 141 | } |
bmdlh | 1:a00bb47c1028 | 142 | void sin_send(){ |
bmdlh | 1:a00bb47c1028 | 143 | float cur_value = sinwave.read();; |
bmdlh | 1:a00bb47c1028 | 144 | __disable_irq(); |
bmdlh | 1:a00bb47c1028 | 145 | send4bytes(0x09,&cur_value); |
bmdlh | 1:a00bb47c1028 | 146 | __enable_irq(); |
bmdlh | 1:a00bb47c1028 | 147 | } |
bmdlh | 1:a00bb47c1028 | 148 | |
bmdlh | 1:a00bb47c1028 | 149 | void configuration(){ |
bmdlh | 1:a00bb47c1028 | 150 | rled = OFF; |
bmdlh | 1:a00bb47c1028 | 151 | char command = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 152 | command = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 153 | float sr; |
bmdlh | 1:a00bb47c1028 | 154 | char temp[4]; |
bmdlh | 1:a00bb47c1028 | 155 | if (command == 0x01){ // magnetometer |
bmdlh | 1:a00bb47c1028 | 156 | status.mag = ON; |
bmdlh | 1:a00bb47c1028 | 157 | temp[0] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 158 | temp[1] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 159 | temp[2] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 160 | temp[3] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 161 | sr = *(float*)temp; |
bmdlh | 1:a00bb47c1028 | 162 | threshold1 =(int) 1/sr *1000; |
bmdlh | 1:a00bb47c1028 | 163 | timer_mag.reset(); |
bmdlh | 1:a00bb47c1028 | 164 | } |
bmdlh | 1:a00bb47c1028 | 165 | else{ |
bmdlh | 1:a00bb47c1028 | 166 | status.mag = OFF; |
bmdlh | 1:a00bb47c1028 | 167 | timer_mag.stop(); |
bmdlh | 1:a00bb47c1028 | 168 | } |
bmdlh | 1:a00bb47c1028 | 169 | command = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 170 | if (command == 0x01){ // accelerometer |
bmdlh | 1:a00bb47c1028 | 171 | status.acc = ON; |
bmdlh | 1:a00bb47c1028 | 172 | temp[0] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 173 | temp[1] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 174 | temp[2] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 175 | temp[3] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 176 | sr = *(float*)temp; |
bmdlh | 1:a00bb47c1028 | 177 | threshold2 =(int) 1/sr *1000; |
bmdlh | 1:a00bb47c1028 | 178 | timer_acc.reset(); |
bmdlh | 1:a00bb47c1028 | 179 | } |
bmdlh | 1:a00bb47c1028 | 180 | else{ |
bmdlh | 1:a00bb47c1028 | 181 | status.acc = OFF; |
bmdlh | 1:a00bb47c1028 | 182 | timer_acc.stop(); |
bmdlh | 1:a00bb47c1028 | 183 | } |
bmdlh | 1:a00bb47c1028 | 184 | command = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 185 | if (command == 0x01){ // light sensor |
bmdlh | 1:a00bb47c1028 | 186 | status.light = ON; |
bmdlh | 1:a00bb47c1028 | 187 | temp[0] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 188 | temp[1] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 189 | temp[2] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 190 | temp[3] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 191 | sr = *(float*)temp; |
bmdlh | 1:a00bb47c1028 | 192 | threshold3 =(int) 1/sr *1000; |
bmdlh | 1:a00bb47c1028 | 193 | timer_light.reset(); |
bmdlh | 1:a00bb47c1028 | 194 | } |
bmdlh | 1:a00bb47c1028 | 195 | else{ |
bmdlh | 1:a00bb47c1028 | 196 | status.light = OFF; |
bmdlh | 1:a00bb47c1028 | 197 | timer_light.stop(); |
bmdlh | 1:a00bb47c1028 | 198 | } |
bmdlh | 1:a00bb47c1028 | 199 | command = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 200 | if (command == 0x01){ // touch sensor |
bmdlh | 1:a00bb47c1028 | 201 | status.touch = ON; |
bmdlh | 1:a00bb47c1028 | 202 | temp[0] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 203 | temp[1] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 204 | temp[2] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 205 | temp[3] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 206 | sr = *(float*)temp; |
bmdlh | 1:a00bb47c1028 | 207 | threshold4 =(int) 1/sr *1000; |
bmdlh | 1:a00bb47c1028 | 208 | timer_touch.reset(); |
bmdlh | 1:a00bb47c1028 | 209 | } |
bmdlh | 1:a00bb47c1028 | 210 | else{ |
bmdlh | 1:a00bb47c1028 | 211 | status.touch = OFF; |
bmdlh | 1:a00bb47c1028 | 212 | timer_touch.stop(); |
bmdlh | 1:a00bb47c1028 | 213 | } |
bmdlh | 1:a00bb47c1028 | 214 | command = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 215 | if (command == 0x01){ // sinewave receiver |
bmdlh | 1:a00bb47c1028 | 216 | status.sine = ON; |
bmdlh | 1:a00bb47c1028 | 217 | temp[0] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 218 | temp[1] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 219 | temp[2] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 220 | temp[3] = pc.getc(); |
bmdlh | 1:a00bb47c1028 | 221 | sr = *(float*)temp; |
bmdlh | 1:a00bb47c1028 | 222 | threshold5 =(int) 1/sr *1000; |
bmdlh | 1:a00bb47c1028 | 223 | timer_sin.reset(); |
bmdlh | 1:a00bb47c1028 | 224 | } |
bmdlh | 1:a00bb47c1028 | 225 | else{ |
bmdlh | 1:a00bb47c1028 | 226 | status.sine = OFF; |
bmdlh | 1:a00bb47c1028 | 227 | timer_sin.stop(); |
bmdlh | 1:a00bb47c1028 | 228 | } |
bmdlh | 1:a00bb47c1028 | 229 | rled = ON; |
bmdlh | 1:a00bb47c1028 | 230 | START = ON; |
bmdlh | 1:a00bb47c1028 | 231 | } |
bmdlh | 1:a00bb47c1028 | 232 | |
bmdlh | 1:a00bb47c1028 | 233 | int main(){ |
bmdlh | 1:a00bb47c1028 | 234 | status.mag = 0; |
bmdlh | 1:a00bb47c1028 | 235 | status.acc = 0; |
bmdlh | 1:a00bb47c1028 | 236 | status.light = 0; |
bmdlh | 1:a00bb47c1028 | 237 | status.touch = 0; |
bmdlh | 1:a00bb47c1028 | 238 | status.sine = 0; |
bmdlh | 0:c8a7071d8692 | 239 | calXY(); |
bmdlh | 1:a00bb47c1028 | 240 | pc.attach(&configuration); |
bmdlh | 1:a00bb47c1028 | 241 | while(START == OFF){ |
bmdlh | 1:a00bb47c1028 | 242 | pc.putc(0xFF); |
bmdlh | 1:a00bb47c1028 | 243 | } |
bmdlh | 1:a00bb47c1028 | 244 | pc.putc(0xFE); |
bmdlh | 1:a00bb47c1028 | 245 | |
bmdlh | 0:c8a7071d8692 | 246 | |
bmdlh | 1:a00bb47c1028 | 247 | timer_mag.start(); |
bmdlh | 1:a00bb47c1028 | 248 | timer_acc.start(); |
bmdlh | 1:a00bb47c1028 | 249 | timer_light.start(); |
bmdlh | 1:a00bb47c1028 | 250 | timer_touch.start(); |
bmdlh | 1:a00bb47c1028 | 251 | timer_sin.start(); |
bmdlh | 1:a00bb47c1028 | 252 | while(1) { |
bmdlh | 1:a00bb47c1028 | 253 | if (status.mag == ON){ |
bmdlh | 1:a00bb47c1028 | 254 | if (timer_mag.read_ms()>threshold1){ |
bmdlh | 1:a00bb47c1028 | 255 | mag_send(); |
bmdlh | 1:a00bb47c1028 | 256 | timer_mag.reset(); |
bmdlh | 1:a00bb47c1028 | 257 | } |
bmdlh | 1:a00bb47c1028 | 258 | } |
bmdlh | 1:a00bb47c1028 | 259 | if (status.acc == ON){ |
bmdlh | 1:a00bb47c1028 | 260 | if (timer_acc.read_ms()>threshold2){ |
bmdlh | 1:a00bb47c1028 | 261 | accl_send(); |
bmdlh | 1:a00bb47c1028 | 262 | timer_acc.reset(); |
bmdlh | 1:a00bb47c1028 | 263 | } |
bmdlh | 1:a00bb47c1028 | 264 | } |
bmdlh | 1:a00bb47c1028 | 265 | if (status.light == ON){ |
bmdlh | 1:a00bb47c1028 | 266 | if (timer_light.read_ms()>threshold3){ |
bmdlh | 1:a00bb47c1028 | 267 | light_send(); |
bmdlh | 1:a00bb47c1028 | 268 | timer_light.reset(); |
bmdlh | 1:a00bb47c1028 | 269 | } |
bmdlh | 1:a00bb47c1028 | 270 | } |
bmdlh | 1:a00bb47c1028 | 271 | if (status.touch == ON){ |
bmdlh | 1:a00bb47c1028 | 272 | if (timer_touch.read_ms()>threshold4){ |
bmdlh | 1:a00bb47c1028 | 273 | touch_send(); |
bmdlh | 1:a00bb47c1028 | 274 | timer_touch.reset(); |
bmdlh | 1:a00bb47c1028 | 275 | } |
bmdlh | 1:a00bb47c1028 | 276 | } |
bmdlh | 1:a00bb47c1028 | 277 | if (status.sine == ON){ |
bmdlh | 1:a00bb47c1028 | 278 | if (timer_sin.read_ms()>threshold5){ |
bmdlh | 1:a00bb47c1028 | 279 | sin_send(); |
bmdlh | 1:a00bb47c1028 | 280 | timer_sin.reset(); |
bmdlh | 1:a00bb47c1028 | 281 | } |
bmdlh | 1:a00bb47c1028 | 282 | } |
bmdlh | 0:c8a7071d8692 | 283 | } |
bmdlh | 1:a00bb47c1028 | 284 | } |