code d'envoie de trame

Dependencies:   mbed ConfigFile

Fork of app4-router by APP Team

main.cpp

Committer:
passelin
Date:
2014-02-24
Revision:
3:ef0e12857e30
Parent:
2:bbe1253008e6
Child:
4:21b8959e8b71

File content as of revision 3:ef0e12857e30:

#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

DigitalOut myled(LED1);
DigitalOut myled2(LED2);
DigitalOut reset(p8);
Serial pc(USBTX, USBRX);
Serial xbee(p13, p14);
I2C i2c(p9, p10);            // I2C port (sda, scl)

//LocalFileSystem local("local");
ConfigFile cfg;

string AccelData;

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();                         
 
    // 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;
    char size = data.length() + 0x0E; //0x0E + data type + nombre de data = 0x0F + nombre de data
    xbee.putc(0x7E); // start delimeter
    xbee.putc(0x00); // length
    xbee.putc(size); // length
    
    xbee.putc(0x10); // frame type
    xbee.putc(0x01); // frame ID
    xbee.putc(0x00); // adress MAC debut
    xbee.putc(0x13); // |
    xbee.putc(0xA2); // |
    xbee.putc(0x00); // |
    xbee.putc(0x40); // |
    xbee.putc(0x8B); // |
    xbee.putc(0x41); // |
    xbee.putc(0x98); // adress MAC fin
    xbee.putc(0x00); // dest. adress network
    xbee.putc(0x00); // dest. adress network
    xbee.putc(0x00); // broadcast radius
    xbee.putc(0x00); // option
    checkSum = checkSum + 0x10 + 0x01 + 0x00 + 0x13+ 0xA2 + 0x00 + 0x40 + 0x8B +0x41 + 0x98 + 0x00 + 0x00 + 0x00 + 0x00;
    //data type
    for (int i=0;i<data.length();i++)  //data
    {
        xbee.putc(data[i]);
        checkSum += data[i];
    }
    checkSum = 0xff - checkSum;
    pc.printf(" check sum = %x" , checkSum);
    xbee.putc(checkSum); // checksum
    
}

int main() 
{
    unsigned short z; 
    
    xbee_init();
    initAccel();
    
    myled = 1;
    myled2 = 1;
    
    wait(4);
    
    while(1) 
    {
        // 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()); 
            myled2 = ! myled2;
        }
        if(pc.readable())
        {
            xbee.putc(pc.getc()); 
            myled = ! myled;     
        }*/
    }
}