![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Playing around with accelerometer and magnetometer on mbed KL46Z
Dependencies: MAG3110 MMA8451Q PinDetect mbed TSI
main.cpp@10:f9a1e1dd5de1, 2014-02-06 (annotated)
- Committer:
- oliverfang
- Date:
- Thu Feb 06 07:54:58 2014 +0000
- Revision:
- 10:f9a1e1dd5de1
- Parent:
- 8:b87b93a62a6a
- Child:
- 11:7af59a3d9ac5
reading in serial commands from pyserial GUI now works!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oliverfang | 0:648dde0c4ef8 | 1 | #include "mbed.h" |
oliverfang | 0:648dde0c4ef8 | 2 | #include "PinDetect.h" |
oliverfang | 0:648dde0c4ef8 | 3 | #include "MMA8451Q.h" |
oliverfang | 0:648dde0c4ef8 | 4 | #include "MAG3110.h" |
mohammmo | 2:bb31f097af0f | 5 | #include "TSISensor.h" |
oliverfang | 0:648dde0c4ef8 | 6 | |
oliverfang | 0:648dde0c4ef8 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
oliverfang | 0:648dde0c4ef8 | 8 | |
oliverfang | 0:648dde0c4ef8 | 9 | // Declare output LEDs |
oliverfang | 0:648dde0c4ef8 | 10 | DigitalOut ledgreen(PTD5); |
oliverfang | 0:648dde0c4ef8 | 11 | DigitalOut ledred(PTE29); |
oliverfang | 0:648dde0c4ef8 | 12 | |
oliverfang | 0:648dde0c4ef8 | 13 | // Declare USB serial connection |
oliverfang | 0:648dde0c4ef8 | 14 | Serial pc(USBTX,USBRX); |
oliverfang | 0:648dde0c4ef8 | 15 | |
oliverfang | 0:648dde0c4ef8 | 16 | // Declare timer interrupt |
oliverfang | 0:648dde0c4ef8 | 17 | Ticker timerAcc; |
oliverfang | 0:648dde0c4ef8 | 18 | Ticker timerMag; |
mohammmo | 2:bb31f097af0f | 19 | Ticker timerLight; |
mohammmo | 2:bb31f097af0f | 20 | Ticker timerTouch; |
oliverfang | 0:648dde0c4ef8 | 21 | |
oliverfang | 0:648dde0c4ef8 | 22 | // Declare pointer variables |
oliverfang | 0:648dde0c4ef8 | 23 | float xAcc; |
oliverfang | 0:648dde0c4ef8 | 24 | float yAcc; |
oliverfang | 0:648dde0c4ef8 | 25 | float zAcc; |
oliverfang | 0:648dde0c4ef8 | 26 | int xMag; |
oliverfang | 0:648dde0c4ef8 | 27 | int yMag; |
oliverfang | 0:648dde0c4ef8 | 28 | int zMag; |
mohammmo | 2:bb31f097af0f | 29 | float xLight; |
mohammmo | 2:bb31f097af0f | 30 | float xTouch; |
oliverfang | 0:648dde0c4ef8 | 31 | |
oliverfang | 3:552b7c450b2f | 32 | // Sampling rates |
oliverfang | 4:0d2eefc2be8e | 33 | float accRate = 0.1; |
oliverfang | 4:0d2eefc2be8e | 34 | float magRate = 0.1; |
oliverfang | 3:552b7c450b2f | 35 | float lightRate = 0.1; |
oliverfang | 3:552b7c450b2f | 36 | float touchRate = 0.1; |
oliverfang | 3:552b7c450b2f | 37 | |
oliverfang | 8:b87b93a62a6a | 38 | // Receiving Data |
oliverfang | 8:b87b93a62a6a | 39 | const int bufferSize = 255; |
oliverfang | 8:b87b93a62a6a | 40 | char buffer[bufferSize]; |
oliverfang | 8:b87b93a62a6a | 41 | int index = 0; |
oliverfang | 10:f9a1e1dd5de1 | 42 | int received = 0; |
oliverfang | 10:f9a1e1dd5de1 | 43 | int readEn = 0; |
oliverfang | 10:f9a1e1dd5de1 | 44 | float temp; |
oliverfang | 3:552b7c450b2f | 45 | |
oliverfang | 0:648dde0c4ef8 | 46 | // Declare Accelerometer pins and I2C address |
oliverfang | 0:648dde0c4ef8 | 47 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0); |
oliverfang | 0:648dde0c4ef8 | 48 | // Declare Magnetometer pins |
oliverfang | 0:648dde0c4ef8 | 49 | MAG3110 mag(PTE25, PTE24); |
mohammmo | 2:bb31f097af0f | 50 | // Declare touch sensor pin |
mohammmo | 2:bb31f097af0f | 51 | TSISensor touch; |
mohammmo | 2:bb31f097af0f | 52 | // Declare light sensor pin |
mohammmo | 2:bb31f097af0f | 53 | AnalogIn light(PTE22); |
oliverfang | 0:648dde0c4ef8 | 54 | |
oliverfang | 0:648dde0c4ef8 | 55 | // Functions |
oliverfang | 0:648dde0c4ef8 | 56 | void init(); |
oliverfang | 4:0d2eefc2be8e | 57 | void printData(); |
oliverfang | 8:b87b93a62a6a | 58 | void receiveHandler(); |
oliverfang | 8:b87b93a62a6a | 59 | void processCommand(); |
oliverfang | 0:648dde0c4ef8 | 60 | void accTime(); |
oliverfang | 0:648dde0c4ef8 | 61 | void magTime(); |
mohammmo | 2:bb31f097af0f | 62 | void lightTime(); |
mohammmo | 2:bb31f097af0f | 63 | void touchTime(); |
oliverfang | 0:648dde0c4ef8 | 64 | |
oliverfang | 0:648dde0c4ef8 | 65 | void init() |
oliverfang | 0:648dde0c4ef8 | 66 | { |
oliverfang | 0:648dde0c4ef8 | 67 | // Attach timerAcc |
oliverfang | 10:f9a1e1dd5de1 | 68 | pc.baud(9600); |
oliverfang | 3:552b7c450b2f | 69 | timerAcc.attach(&accTime, accRate); |
oliverfang | 3:552b7c450b2f | 70 | timerMag.attach(&magTime, magRate); |
oliverfang | 3:552b7c450b2f | 71 | timerLight.attach(&lightTime, lightRate); |
oliverfang | 3:552b7c450b2f | 72 | timerTouch.attach(&touchTime, touchRate); |
oliverfang | 8:b87b93a62a6a | 73 | pc.attach(&receiveHandler, Serial::RxIrq); |
oliverfang | 0:648dde0c4ef8 | 74 | ledred = 0; |
oliverfang | 0:648dde0c4ef8 | 75 | ledgreen = 0; |
oliverfang | 0:648dde0c4ef8 | 76 | } |
oliverfang | 0:648dde0c4ef8 | 77 | |
oliverfang | 0:648dde0c4ef8 | 78 | int main() |
oliverfang | 0:648dde0c4ef8 | 79 | { |
oliverfang | 0:648dde0c4ef8 | 80 | // Initialize |
oliverfang | 0:648dde0c4ef8 | 81 | init(); |
oliverfang | 0:648dde0c4ef8 | 82 | |
oliverfang | 0:648dde0c4ef8 | 83 | while(1) |
oliverfang | 0:648dde0c4ef8 | 84 | { |
oliverfang | 10:f9a1e1dd5de1 | 85 | __disable_irq(); |
oliverfang | 4:0d2eefc2be8e | 86 | printData(); |
oliverfang | 10:f9a1e1dd5de1 | 87 | __enable_irq(); |
oliverfang | 8:b87b93a62a6a | 88 | wait(0.05); |
oliverfang | 10:f9a1e1dd5de1 | 89 | if(received == 1){ |
oliverfang | 8:b87b93a62a6a | 90 | __disable_irq(); |
oliverfang | 8:b87b93a62a6a | 91 | processCommand(); |
oliverfang | 8:b87b93a62a6a | 92 | __enable_irq(); |
oliverfang | 8:b87b93a62a6a | 93 | } |
oliverfang | 8:b87b93a62a6a | 94 | //ledgreen = !ledgreen; |
oliverfang | 0:648dde0c4ef8 | 95 | } |
oliverfang | 0:648dde0c4ef8 | 96 | } |
oliverfang | 0:648dde0c4ef8 | 97 | |
oliverfang | 4:0d2eefc2be8e | 98 | void printData() |
oliverfang | 4:0d2eefc2be8e | 99 | { |
oliverfang | 10:f9a1e1dd5de1 | 100 | |
oliverfang | 10:f9a1e1dd5de1 | 101 | pc.printf("/%f/%f/%f/%d/%d/%d/%f/%f/%f/%f/%f/%f/%d/\r\n", |
oliverfang | 10:f9a1e1dd5de1 | 102 | xAcc, yAcc, zAcc, xMag, yMag, zMag, xLight, xTouch, |
oliverfang | 10:f9a1e1dd5de1 | 103 | accRate, magRate, lightRate, touchRate, received); |
oliverfang | 10:f9a1e1dd5de1 | 104 | |
oliverfang | 8:b87b93a62a6a | 105 | } |
oliverfang | 8:b87b93a62a6a | 106 | |
oliverfang | 8:b87b93a62a6a | 107 | void receiveHandler() |
oliverfang | 8:b87b93a62a6a | 108 | { |
oliverfang | 10:f9a1e1dd5de1 | 109 | while (pc.readable()){ |
oliverfang | 10:f9a1e1dd5de1 | 110 | temp = pc.getc(); |
oliverfang | 10:f9a1e1dd5de1 | 111 | //begin reading if char is @ |
oliverfang | 10:f9a1e1dd5de1 | 112 | if (temp == '@') |
oliverfang | 10:f9a1e1dd5de1 | 113 | { |
oliverfang | 10:f9a1e1dd5de1 | 114 | readEn = 1; |
oliverfang | 10:f9a1e1dd5de1 | 115 | } |
oliverfang | 10:f9a1e1dd5de1 | 116 | //stop reading if char is # |
oliverfang | 10:f9a1e1dd5de1 | 117 | else if (temp == '#' && readEn == 1) |
oliverfang | 10:f9a1e1dd5de1 | 118 | { |
oliverfang | 10:f9a1e1dd5de1 | 119 | readEn = 0; |
oliverfang | 10:f9a1e1dd5de1 | 120 | received = 1; |
oliverfang | 10:f9a1e1dd5de1 | 121 | //ledred = 1; |
oliverfang | 8:b87b93a62a6a | 122 | index = 0; |
oliverfang | 10:f9a1e1dd5de1 | 123 | return; |
oliverfang | 8:b87b93a62a6a | 124 | } |
oliverfang | 10:f9a1e1dd5de1 | 125 | // if read enable is on, then read in data |
oliverfang | 10:f9a1e1dd5de1 | 126 | else if (readEn == 1) |
oliverfang | 10:f9a1e1dd5de1 | 127 | { |
oliverfang | 10:f9a1e1dd5de1 | 128 | buffer[index] = temp; |
oliverfang | 10:f9a1e1dd5de1 | 129 | index++; |
oliverfang | 10:f9a1e1dd5de1 | 130 | } |
oliverfang | 8:b87b93a62a6a | 131 | } |
oliverfang | 8:b87b93a62a6a | 132 | return; |
oliverfang | 8:b87b93a62a6a | 133 | } |
oliverfang | 8:b87b93a62a6a | 134 | |
oliverfang | 8:b87b93a62a6a | 135 | void processCommand() |
oliverfang | 8:b87b93a62a6a | 136 | { |
oliverfang | 8:b87b93a62a6a | 137 | //pc.printf("%s\r\n", buffer); |
oliverfang | 8:b87b93a62a6a | 138 | char* commands; |
oliverfang | 8:b87b93a62a6a | 139 | commands = strtok(buffer, "x"); |
oliverfang | 8:b87b93a62a6a | 140 | //pc.printf("%s\r\n", commands); |
oliverfang | 8:b87b93a62a6a | 141 | switch(*commands) |
oliverfang | 8:b87b93a62a6a | 142 | { |
oliverfang | 8:b87b93a62a6a | 143 | case '0': |
oliverfang | 8:b87b93a62a6a | 144 | commands = strtok(NULL, "x"); |
oliverfang | 8:b87b93a62a6a | 145 | //pc.printf("%s\r\n", commands); |
oliverfang | 10:f9a1e1dd5de1 | 146 | temp = strtod(commands, NULL)/1000; |
oliverfang | 10:f9a1e1dd5de1 | 147 | if (temp > 0.05) |
oliverfang | 10:f9a1e1dd5de1 | 148 | { |
oliverfang | 10:f9a1e1dd5de1 | 149 | accRate = temp; |
oliverfang | 10:f9a1e1dd5de1 | 150 | timerAcc.detach(); |
oliverfang | 10:f9a1e1dd5de1 | 151 | timerAcc.attach(&accTime, accRate); |
oliverfang | 10:f9a1e1dd5de1 | 152 | } |
oliverfang | 8:b87b93a62a6a | 153 | break; |
oliverfang | 8:b87b93a62a6a | 154 | case '1': |
oliverfang | 8:b87b93a62a6a | 155 | commands = strtok(NULL, "x"); |
oliverfang | 8:b87b93a62a6a | 156 | //pc.printf("%s\r\n", commands); |
oliverfang | 10:f9a1e1dd5de1 | 157 | temp = strtod(commands, NULL)/1000; |
oliverfang | 10:f9a1e1dd5de1 | 158 | if (temp > 0.05) |
oliverfang | 10:f9a1e1dd5de1 | 159 | { |
oliverfang | 10:f9a1e1dd5de1 | 160 | magRate = temp; |
oliverfang | 10:f9a1e1dd5de1 | 161 | timerMag.detach(); |
oliverfang | 10:f9a1e1dd5de1 | 162 | timerMag.attach(&magTime, magRate); |
oliverfang | 10:f9a1e1dd5de1 | 163 | } |
oliverfang | 8:b87b93a62a6a | 164 | break; |
oliverfang | 8:b87b93a62a6a | 165 | case '2': |
oliverfang | 8:b87b93a62a6a | 166 | commands = strtok(NULL, "x"); |
oliverfang | 8:b87b93a62a6a | 167 | //pc.printf("%s\r\n", commands); |
oliverfang | 10:f9a1e1dd5de1 | 168 | temp = strtod(commands, NULL)/1000; |
oliverfang | 10:f9a1e1dd5de1 | 169 | if (temp > 0.05) |
oliverfang | 10:f9a1e1dd5de1 | 170 | { |
oliverfang | 10:f9a1e1dd5de1 | 171 | lightRate = temp; |
oliverfang | 10:f9a1e1dd5de1 | 172 | timerLight.detach(); |
oliverfang | 10:f9a1e1dd5de1 | 173 | timerLight.attach(&lightTime, lightRate); |
oliverfang | 10:f9a1e1dd5de1 | 174 | } |
oliverfang | 8:b87b93a62a6a | 175 | break; |
oliverfang | 8:b87b93a62a6a | 176 | case '3': |
oliverfang | 8:b87b93a62a6a | 177 | commands = strtok(NULL, "x"); |
oliverfang | 8:b87b93a62a6a | 178 | //pc.printf("%s\r\n", commands); |
oliverfang | 10:f9a1e1dd5de1 | 179 | temp = strtod(commands, NULL)/1000; |
oliverfang | 10:f9a1e1dd5de1 | 180 | if (temp > 0.05) |
oliverfang | 10:f9a1e1dd5de1 | 181 | { |
oliverfang | 10:f9a1e1dd5de1 | 182 | touchRate = temp; |
oliverfang | 10:f9a1e1dd5de1 | 183 | timerTouch.detach(); |
oliverfang | 10:f9a1e1dd5de1 | 184 | timerTouch.attach(&touchTime, touchRate); |
oliverfang | 10:f9a1e1dd5de1 | 185 | } |
oliverfang | 8:b87b93a62a6a | 186 | break; |
oliverfang | 8:b87b93a62a6a | 187 | default: |
oliverfang | 8:b87b93a62a6a | 188 | //pc.printf("incorrect input\r\n"); |
oliverfang | 8:b87b93a62a6a | 189 | break; |
oliverfang | 8:b87b93a62a6a | 190 | } |
oliverfang | 10:f9a1e1dd5de1 | 191 | received = 0; |
oliverfang | 8:b87b93a62a6a | 192 | memset(buffer, 0, bufferSize); |
oliverfang | 8:b87b93a62a6a | 193 | //pc.printf("%s\r\n", buffer); |
oliverfang | 8:b87b93a62a6a | 194 | } |
oliverfang | 8:b87b93a62a6a | 195 | |
oliverfang | 0:648dde0c4ef8 | 196 | void accTime() |
oliverfang | 0:648dde0c4ef8 | 197 | { |
oliverfang | 0:648dde0c4ef8 | 198 | xAcc = abs(acc.getAccX()); |
oliverfang | 0:648dde0c4ef8 | 199 | yAcc = abs(acc.getAccY()); |
oliverfang | 0:648dde0c4ef8 | 200 | zAcc = abs(acc.getAccZ()); |
oliverfang | 10:f9a1e1dd5de1 | 201 | ledred = !ledred; |
oliverfang | 0:648dde0c4ef8 | 202 | } |
oliverfang | 0:648dde0c4ef8 | 203 | |
oliverfang | 0:648dde0c4ef8 | 204 | void magTime() |
oliverfang | 0:648dde0c4ef8 | 205 | { |
oliverfang | 0:648dde0c4ef8 | 206 | xMag = mag.getXVal(); |
oliverfang | 0:648dde0c4ef8 | 207 | yMag = mag.getYVal(); |
oliverfang | 0:648dde0c4ef8 | 208 | zMag = mag.getZVal(); |
oliverfang | 8:b87b93a62a6a | 209 | //ledred = !ledred; |
mohammmo | 2:bb31f097af0f | 210 | } |
mohammmo | 2:bb31f097af0f | 211 | |
mohammmo | 2:bb31f097af0f | 212 | void lightTime() |
mohammmo | 2:bb31f097af0f | 213 | { |
mohammmo | 2:bb31f097af0f | 214 | xLight = 1 - light.read(); |
mohammmo | 2:bb31f097af0f | 215 | } |
mohammmo | 2:bb31f097af0f | 216 | |
mohammmo | 2:bb31f097af0f | 217 | void touchTime() |
mohammmo | 2:bb31f097af0f | 218 | { |
mohammmo | 2:bb31f097af0f | 219 | xTouch = 1 - touch.readPercentage(); |
oliverfang | 8:b87b93a62a6a | 220 | } |