code d'envoie de trame
Dependencies: mbed ConfigFile
Fork of app4-router by
Revision 3:ef0e12857e30, committed 2014-02-24
- Comitter:
- passelin
- Date:
- Mon Feb 24 15:44:51 2014 +0000
- Parent:
- 2:bbe1253008e6
- Child:
- 4:21b8959e8b71
- Commit message:
- accelero ajout?
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Feb 22 23:46:06 2014 +0000 +++ b/main.cpp Mon Feb 24 15:44:51 2014 +0000 @@ -1,6 +1,23 @@ #include "mbed.h" +#include <sstream> #include <string> - +#include "ConfigFile.h" + +//#define HIGH_RES 10 // Uncomment to operate the accelerometer in HIGH RESOLUTION(12 bits) otherwise, it would operates in LOW RESOLUTION(8 bits) + +#define DELAY 0.25 + +#define MMA8452_WRITE_ADDRESS 0x3A +#define MMA8452_READ_ADDRESS 0x3B +#define WHO_AM_I 0x0D +#define OUT_X_MSB 0x01 +#define OUT_X_LSB 0x02 +#define OUT_Y_MSB 0x03 +#define OUT_Y_LSB 0x04 +#define OUT_Z_MSB 0x05 +#define OUT_Z_LSB 0x06 +#define CTRL_REG1 0x2A +#define CTRL_REG2 0x2B #define ACCEL 0x01 #define PUSHBOUTON 0x02 @@ -10,15 +27,97 @@ DigitalOut reset(p8); Serial pc(USBTX, USBRX); Serial xbee(p13, p14); -string data = "allo bonjour"; +I2C i2c(p9, p10); // I2C port (sda, scl) + +//LocalFileSystem local("local"); +ConfigFile cfg; + +string AccelData; - // 7E 00 0F 10 01 00 13 A2 00 40 8B 41 98 00 00 00 00 33 62 - // 7E 00 10 10 01 00 13 A2 00 40 8B 41 98 00 00 00 00 33 33 2F +void xbee_init() +{ + reset = 0; + wait_ms(400); + reset = 1; +} + +char i2c_read_reg(char address) +{ + // Read from selected register adress + i2c.start(); + i2c.write(MMA8452_WRITE_ADDRESS); + i2c.write(address); + i2c.start(); + i2c.write(MMA8452_READ_ADDRESS); + char data = i2c.read(0); + i2c.stop(); - void checksum() + // return the data readed + return data; +} + +bool initAccel() +{ + // Start I2C communication + char data = i2c_read_reg(WHO_AM_I); + if(data != 0x2A) + { + return false; + } + + // Set accelerometer in Low Noise Low Power mode + i2c.start(); + i2c.write(MMA8452_WRITE_ADDRESS); + i2c.write(CTRL_REG2); + i2c.write(0x01); + i2c.stop(); + + // Set accelerometer in active mode + i2c.start(); + i2c.write(MMA8452_WRITE_ADDRESS); + i2c.write(CTRL_REG1); + i2c.write(0x01); + i2c.stop(); + + return true; +} + + +unsigned short getAccelValue(char MSB_addr, char LSB_addr) { + unsigned short retValue; + unsigned char msb; + + msb = i2c_read_reg(MSB_addr); // Read MSB data from MSB register + +#ifdef HIGH_RES // Use LSB only in HIGH RESOLUTION mode. + unsigned char lsb; + lsb = i2c_read_reg(LSB_addr); // Read LSB data from MSB register + retValue = (msb << 4); // Shift the data by 4 to the left in order to rebuild the 12 bits data ( MSB MSB MSB MSB MSB MSB MSB MSB 0 0 0 0 ) + retValue = ((lsb >> 4) | retValue); // Shift the data by 4 to the right in order to keep only the 4 first bits and rebuild the 12 bits data ( MSB MSB MSB MSB MSB MSB MSB MSB LSB LSB LSB LSB) + retValue = retValue*36000/4096; + +#endif +#ifndef HIGH_RES // In LOW RESOLUTION mode, we use only the MSB + retValue = msb; + retValue = retValue*36000/256; +#endif + + if(retValue > 19000) // value cannot be between 90deg and 270deg. + { + retValue = 36000 - retValue; + } + else if(retValue > 9000) + { + retValue = 9000 - (retValue - 9000); + } + // Invert the angle value to use the honrizontal axis as reference instead of z axis + retValue = 9000 - retValue; + + return retValue; } + void xbee_transmit(char type, string data) { char checkSum = 0; @@ -56,19 +155,29 @@ int main() { + unsigned short z; + + xbee_init(); + initAccel(); + myled = 1; myled2 = 1; - reset = 0; - wait_ms(400); - reset = 1; - myled = 0; - myled2 = 0; + + wait(4); + while(1) { - wait(4); - xbee_transmit(ACCEL, data); + // Get z angle from the accelerometer + z= getAccelValue(OUT_Z_MSB, OUT_Z_LSB); + std::stringstream out; + out << z; + AccelData = out.str(); + + //Transmit to coordonitator + xbee_transmit(ACCEL, AccelData); - + + /* if(xbee.readable()) { pc.putc(xbee.getc());