![](/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@7:1cd1e239acf0, 2014-02-04 (annotated)
- Committer:
- mohammmo
- Date:
- Tue Feb 04 02:23:48 2014 +0000
- Revision:
- 7:1cd1e239acf0
- Parent:
- 5:4ccda4b4f345
change sample from serial input (float with a trailing #)
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" |
mohammmo | 7:1cd1e239acf0 | 6 | #include <string> |
oliverfang | 0:648dde0c4ef8 | 7 | |
oliverfang | 0:648dde0c4ef8 | 8 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
oliverfang | 0:648dde0c4ef8 | 9 | |
oliverfang | 0:648dde0c4ef8 | 10 | // Declare output LEDs |
oliverfang | 0:648dde0c4ef8 | 11 | DigitalOut ledgreen(PTD5); |
oliverfang | 0:648dde0c4ef8 | 12 | DigitalOut ledred(PTE29); |
oliverfang | 0:648dde0c4ef8 | 13 | |
oliverfang | 0:648dde0c4ef8 | 14 | // Declare USB serial connection |
oliverfang | 0:648dde0c4ef8 | 15 | Serial pc(USBTX,USBRX); |
oliverfang | 0:648dde0c4ef8 | 16 | |
oliverfang | 0:648dde0c4ef8 | 17 | // Declare timer interrupt |
oliverfang | 0:648dde0c4ef8 | 18 | Ticker timerAcc; |
oliverfang | 0:648dde0c4ef8 | 19 | Ticker timerMag; |
mohammmo | 2:bb31f097af0f | 20 | Ticker timerLight; |
mohammmo | 2:bb31f097af0f | 21 | Ticker timerTouch; |
oliverfang | 0:648dde0c4ef8 | 22 | |
mohammmo | 7:1cd1e239acf0 | 23 | //Sampling rate |
mohammmo | 7:1cd1e239acf0 | 24 | float sample = 0.5; |
mohammmo | 7:1cd1e239acf0 | 25 | |
oliverfang | 0:648dde0c4ef8 | 26 | // Declare pointer variables |
oliverfang | 0:648dde0c4ef8 | 27 | float xAcc; |
oliverfang | 0:648dde0c4ef8 | 28 | float yAcc; |
oliverfang | 0:648dde0c4ef8 | 29 | float zAcc; |
oliverfang | 0:648dde0c4ef8 | 30 | int xMag; |
oliverfang | 0:648dde0c4ef8 | 31 | int yMag; |
oliverfang | 0:648dde0c4ef8 | 32 | int zMag; |
mohammmo | 2:bb31f097af0f | 33 | float xLight; |
mohammmo | 2:bb31f097af0f | 34 | float xTouch; |
oliverfang | 0:648dde0c4ef8 | 35 | |
oliverfang | 0:648dde0c4ef8 | 36 | // Declare Accelerometer pins and I2C address |
oliverfang | 0:648dde0c4ef8 | 37 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0); |
oliverfang | 0:648dde0c4ef8 | 38 | // Declare Magnetometer pins |
oliverfang | 0:648dde0c4ef8 | 39 | MAG3110 mag(PTE25, PTE24); |
mohammmo | 2:bb31f097af0f | 40 | // Declare touch sensor pin |
mohammmo | 2:bb31f097af0f | 41 | TSISensor touch; |
mohammmo | 2:bb31f097af0f | 42 | // Declare light sensor pin |
mohammmo | 2:bb31f097af0f | 43 | AnalogIn light(PTE22); |
mohammmo | 7:1cd1e239acf0 | 44 | // variables for receive interrupt |
mohammmo | 7:1cd1e239acf0 | 45 | const int buffer_size = 255; |
mohammmo | 7:1cd1e239acf0 | 46 | char rx_buffer[buffer_size]; |
mohammmo | 7:1cd1e239acf0 | 47 | int track_rx = 0; |
mohammmo | 7:1cd1e239acf0 | 48 | bool received = false; |
oliverfang | 0:648dde0c4ef8 | 49 | |
oliverfang | 0:648dde0c4ef8 | 50 | // Functions |
oliverfang | 0:648dde0c4ef8 | 51 | void init(); |
oliverfang | 0:648dde0c4ef8 | 52 | void accTime(); |
oliverfang | 0:648dde0c4ef8 | 53 | void magTime(); |
mohammmo | 2:bb31f097af0f | 54 | void lightTime(); |
mohammmo | 2:bb31f097af0f | 55 | void touchTime(); |
mohammmo | 7:1cd1e239acf0 | 56 | void receive_handler(); |
mohammmo | 7:1cd1e239acf0 | 57 | void change_interval(float sample); |
oliverfang | 0:648dde0c4ef8 | 58 | |
oliverfang | 0:648dde0c4ef8 | 59 | void init() |
oliverfang | 0:648dde0c4ef8 | 60 | { |
oliverfang | 0:648dde0c4ef8 | 61 | // Attach timerAcc |
mohammmo | 7:1cd1e239acf0 | 62 | timerAcc.attach(&accTime, sample); |
mohammmo | 7:1cd1e239acf0 | 63 | timerMag.attach(&magTime, sample); |
mohammmo | 7:1cd1e239acf0 | 64 | timerLight.attach(&lightTime, sample); |
mohammmo | 7:1cd1e239acf0 | 65 | timerTouch.attach(&touchTime, sample); |
mohammmo | 7:1cd1e239acf0 | 66 | pc.attach(&receive_handler,Serial::RxIrq); |
oliverfang | 0:648dde0c4ef8 | 67 | ledred = 0; |
oliverfang | 0:648dde0c4ef8 | 68 | ledgreen = 0; |
oliverfang | 0:648dde0c4ef8 | 69 | } |
oliverfang | 0:648dde0c4ef8 | 70 | |
oliverfang | 0:648dde0c4ef8 | 71 | int main() |
oliverfang | 0:648dde0c4ef8 | 72 | { |
oliverfang | 0:648dde0c4ef8 | 73 | // Initialize |
oliverfang | 0:648dde0c4ef8 | 74 | init(); |
oliverfang | 0:648dde0c4ef8 | 75 | |
oliverfang | 0:648dde0c4ef8 | 76 | while(1) |
oliverfang | 0:648dde0c4ef8 | 77 | { |
mohammmo | 5:4ccda4b4f345 | 78 | // read all sensor data |
mohammmo | 7:1cd1e239acf0 | 79 | //pc.printf("%f %f %f %d %d %d %f %f\n", xAcc, yAcc, zAcc, xMag, yMag, zMag, xLight, xTouch); |
mohammmo | 7:1cd1e239acf0 | 80 | //wait(0.05); |
mohammmo | 7:1cd1e239acf0 | 81 | if (received){ |
mohammmo | 7:1cd1e239acf0 | 82 | __disable_irq(); |
mohammmo | 7:1cd1e239acf0 | 83 | sscanf(rx_buffer,"%f",&sample); |
mohammmo | 7:1cd1e239acf0 | 84 | pc.printf("%f\r\n", sample); |
mohammmo | 7:1cd1e239acf0 | 85 | change_interval(sample); |
mohammmo | 7:1cd1e239acf0 | 86 | received = false; |
mohammmo | 7:1cd1e239acf0 | 87 | __enable_irq(); |
mohammmo | 7:1cd1e239acf0 | 88 | } |
oliverfang | 0:648dde0c4ef8 | 89 | } |
oliverfang | 0:648dde0c4ef8 | 90 | } |
oliverfang | 0:648dde0c4ef8 | 91 | |
oliverfang | 0:648dde0c4ef8 | 92 | void accTime() |
oliverfang | 0:648dde0c4ef8 | 93 | { |
oliverfang | 0:648dde0c4ef8 | 94 | xAcc = abs(acc.getAccX()); |
oliverfang | 0:648dde0c4ef8 | 95 | yAcc = abs(acc.getAccY()); |
oliverfang | 0:648dde0c4ef8 | 96 | zAcc = abs(acc.getAccZ()); |
oliverfang | 0:648dde0c4ef8 | 97 | ledgreen = !ledgreen; |
oliverfang | 0:648dde0c4ef8 | 98 | } |
oliverfang | 0:648dde0c4ef8 | 99 | |
oliverfang | 0:648dde0c4ef8 | 100 | void magTime() |
oliverfang | 0:648dde0c4ef8 | 101 | { |
oliverfang | 0:648dde0c4ef8 | 102 | xMag = mag.getXVal(); |
oliverfang | 0:648dde0c4ef8 | 103 | yMag = mag.getYVal(); |
oliverfang | 0:648dde0c4ef8 | 104 | zMag = mag.getZVal(); |
oliverfang | 0:648dde0c4ef8 | 105 | ledred = !ledred; |
mohammmo | 2:bb31f097af0f | 106 | } |
mohammmo | 2:bb31f097af0f | 107 | |
mohammmo | 2:bb31f097af0f | 108 | void lightTime() |
mohammmo | 2:bb31f097af0f | 109 | { |
mohammmo | 2:bb31f097af0f | 110 | xLight = 1 - light.read(); |
mohammmo | 2:bb31f097af0f | 111 | } |
mohammmo | 2:bb31f097af0f | 112 | |
mohammmo | 2:bb31f097af0f | 113 | void touchTime() |
mohammmo | 2:bb31f097af0f | 114 | { |
mohammmo | 2:bb31f097af0f | 115 | xTouch = 1 - touch.readPercentage(); |
mohammmo | 7:1cd1e239acf0 | 116 | } |
mohammmo | 7:1cd1e239acf0 | 117 | |
mohammmo | 7:1cd1e239acf0 | 118 | void receive_handler(){ |
mohammmo | 7:1cd1e239acf0 | 119 | while (pc.readable() && track_rx < buffer_size){ |
mohammmo | 7:1cd1e239acf0 | 120 | rx_buffer[track_rx] = pc.getc(); |
mohammmo | 7:1cd1e239acf0 | 121 | if (rx_buffer[track_rx] == '#'){ |
mohammmo | 7:1cd1e239acf0 | 122 | rx_buffer[track_rx] = '\0'; |
mohammmo | 7:1cd1e239acf0 | 123 | track_rx=0; |
mohammmo | 7:1cd1e239acf0 | 124 | received = true; |
mohammmo | 7:1cd1e239acf0 | 125 | break; |
mohammmo | 7:1cd1e239acf0 | 126 | } |
mohammmo | 7:1cd1e239acf0 | 127 | track_rx++; |
mohammmo | 7:1cd1e239acf0 | 128 | } |
mohammmo | 7:1cd1e239acf0 | 129 | return; |
mohammmo | 7:1cd1e239acf0 | 130 | } |
mohammmo | 7:1cd1e239acf0 | 131 | |
mohammmo | 7:1cd1e239acf0 | 132 | void change_interval(float sample) |
mohammmo | 7:1cd1e239acf0 | 133 | { |
mohammmo | 7:1cd1e239acf0 | 134 | timerAcc.detach(); |
mohammmo | 7:1cd1e239acf0 | 135 | timerMag.detach(); |
mohammmo | 7:1cd1e239acf0 | 136 | timerLight.detach(); |
mohammmo | 7:1cd1e239acf0 | 137 | timerTouch.detach(); |
mohammmo | 7:1cd1e239acf0 | 138 | timerAcc.attach(&accTime, sample); |
mohammmo | 7:1cd1e239acf0 | 139 | timerMag.attach(&magTime, sample); |
mohammmo | 7:1cd1e239acf0 | 140 | timerLight.attach(&lightTime, sample); |
mohammmo | 7:1cd1e239acf0 | 141 | timerTouch.attach(&touchTime, sample); |
mohammmo | 7:1cd1e239acf0 | 142 | } |