posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

main.cpp

Committer:
jkaderka
Date:
2015-02-13
Revision:
0:16fd37cf4a7c
Child:
1:3ec5f7df2410

File content as of revision 0:16fd37cf4a7c:

#include "mbed.h"
#include "L3GD20_init.h"
#include "math.h"
#include "stdint.h"
#include "DirHandle.h"
#include "SDCard.h"
#include "L3GD20.h"
#include "adc.h"
#include "RF.h"
#include <stdio.h>

#define SAMPLE_RATE         750000                          // S mple rate for ADC conversion
#define PERIOD              0.01                            // Setting the period of measuring, 0.01 menas 100 times per second
#define SIZE                1000                            // Setting number of instance in array - for saveing to SD card


class Watchdog {
public:
    void kick(float s) {
        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4 
        LPC_WDT->WDTC = s * (float)clk;         
        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset        
        kick();
    }
    
    void kick() {
        LPC_WDT->WDFEED = 0xAA;
        LPC_WDT->WDFEED = 0x55;
    }
};

Watchdog w;

SDCard Logger(p5, p6, p7, p8, "SDCard");                // Initialise the SD Card to SPI interface
ADC adc(SAMPLE_RATE, 1);                                // Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1   originally MMA7361L
RF nRF(p11, p12, p13, p14, p15);                        // Connecting of wireless module nRF24L01P to SPI
Ticker Saving_Ticker;                                   // Tickers for interrupts on measuring
I2C i2c(p28, p27);                                      // NEW function GYRO connection                                     originally L3G4200D
     
DigitalOut led1(LED1); 
DigitalOut led2(LED2); 
DigitalOut led3(LED3); 
DigitalOut led4(LED4); 

static volatile uint64_t i = 0;                         // Variable for numbering saved data
volatile int j = 0;

char value[] = "data";                                  // Variable for name of Text file saved on SDCard
char fname[20];                                         
char ch;
unsigned char data;

int acc_x[SIZE];
int acc_y[SIZE];
int acc_z[SIZE];

float gyro_x[SIZE];
float gyro_y[SIZE];
float gyro_z[SIZE];

char readByte(char address, char reg)
{
    char result;    
    i2c.start();
    i2c.write(address);         // Slave address with direction=write
    i2c.write(reg);             // Subaddress (register)
    i2c.start();                // Break transmission to change bus direction
    i2c.write(address + 1);     // Slave address with direction=read [bit0=1]
    result = i2c.read(0);
    i2c.stop();
    return (result);
}
    
void writeByte(char address, char reg,char value)       // Sends 1 byte to an I2C address
{    
    i2c.start();
    i2c.write(address);         // Slave address
    i2c.write(reg);             // Subaddress (register)
    i2c.write(value);
    i2c.stop();    
}
    
void initSensors (void)
{
    //pc.printf("\n L3GD20 init \n");
    //pc.printf("Ping L3GD20 (should reply 0xD4): %x \n",readByte(L3GD20_ADDR,gWHO_AM_I));
    writeByte(L3GD20_ADDR,gCTRL_REG1,0x0F); // Set ODR to 95Hz, BW to 12.5Hz, enable axes and turn on device
    writeByte(L3GD20_ADDR,gCTRL_REG4,0x10); // Full scale selected at 500dps (degrees per second)
    //pc.printf("L3GD20 gyro Temp: %x degrees C \n",readByte(L3GD20_ADDR,gOUT_TEMP));
    
    wait(1); // Wait for settings to stabilize
}    


void measuring(void)              // funkce, ktera nameri hodnoty, nasklada je do poli a pole ulozi, az bude dost velke
{
    static unsigned int counter = 0;
    // At the beginning, global variable i is set to 0
    // Accelerometr
    // i is total order variable
    // j is relative order variable in one measuring step
        
    adc.start();                                        // Starting ADC conversion
    adc.select(p18);                                    // Measure pin 20
    while(!adc.done(p18));                              // Wait for it to complete
    acc_x[j] = adc.read(p18); 
       
    adc.start();                                        // Again .....
    adc.select(p19);                                    // Measure pin 20     
    while(!adc.done(p19));                              // Wait for it to complete
    acc_y[j] = adc.read(p19);
    
    adc.start(); 
    adc.select(p20);                                    // Measure pin 20
    while(!adc.done(p20));                              // Wait for it to complete
    acc_z[j] = adc.read(p20);  

    // Gyro
    char        xL, xH, yL, yH, zL, zH;
    int16_t     gX, gY, gZ;
    
    xL=readByte(L3GD20_ADDR,gOUT_X_L);
    xH=readByte(L3GD20_ADDR,gOUT_X_H);
    yL=readByte(L3GD20_ADDR,gOUT_Y_L);
    yH=readByte(L3GD20_ADDR,gOUT_Y_H);
    zL=readByte(L3GD20_ADDR,gOUT_Z_L);
    zH=readByte(L3GD20_ADDR,gOUT_Z_H);
    
    gX = (xH<<8) | (xL);                      // 16-bit 2's complement data
    gY = (yH<<8) | (yL);
    gZ = (zH<<8) | (zL);
    
    gyro_x[j] = (float) gX * (17.5/1000.0);        // At 500dps sensitivity, L3GD20 returns 17.5/1000 dps per digit
    gyro_y[j] = (float) gY * (17.5/1000.0);
    gyro_z[j] = (float) gZ * (17.5/1000.0);        //FIXME because of printing values
 
    nRF.packet(counter, acc_x[j], acc_y[j], acc_z[j], gyro_x[j], gyro_y[j], gyro_z[j]);
    
    
    i++;
    j++;  
    counter++;
    if ( i % (SIZE/20) == 0)
    {
        led3 = !led3;
    }

    if ( i % SIZE == 0)
    {
        led4 = !led4;
        sprintf(fname, "/SDCard/%s.txt", value);            // Openning SD-CARD
        FILE *fp = fopen(fname, "a");                       // otevreni souboru pro doplneni
        if(fp == NULL) {                                    // Controll for opened SDCard
            nRF.printf("\nConnecting ERROR in Saveing\n");            // Printing warning
        } 
        
        int k;
        for(k = 0; k < SIZE; k++)
        {
            // zapis prommennych
            fprintf(fp, "%d\t ",i-SIZE+k);    // Writing measured data
            fprintf(fp, "%d\t %d\t %d\t ",acc_x[k], acc_y[k], acc_z[k]);    // Writing measured data
            fprintf(fp, "%f\t %f\t %f\t \n",gyro_x[k],gyro_y[k],gyro_z[k]);        // Writing measured data    puvodne %d 
        }
        fclose(fp);  
        j = 0;                     
    }
}    
   
void Get_data(void)                                     // Function for getting data from SD_CARD and sending to PC                             
{   
    sprintf(fname, "/SDCard/%s.txt", value);            
    FILE *fp = fopen(fname, "r");                       // Openning SD card for reading
    if(fp == NULL) {                                    // Controlling if card is opened
        nRF.printf("\nConnecting ERROR in Getting data\n");
    } 
    while((ch = fgetc(fp)) != 81)
    {                                                   // Writeing everything until "Q" character appears
        nRF.printf("%c",ch);
    }
    fclose(fp);                                         // Closing file
}

int main()
{
    initSensors();                                      // INIT GYRO
    Logger.SelectCRCMode(1);                            // I dont know / it was in example code
    
    adc.setup(p18,1);                                   // Set up ADC on pin 18
    adc.setup(p19,1);                                   // Set up ADC on pin 19
    adc.setup(p20,1);                                   // Set up ADC on pin 20    
    sprintf(fname, "/SDCard/%s.txt", value);
  
  /*
  
    i = 0;
    j = 0;     
                                                                 // Setting global variable to 0
    Saving_Ticker.attach(&measuring, PERIOD);       // attach Function for saveing data
    */
                  
    int rem = remove(fname);
        if (rem == 0){
            nRF.printf("File was deleted -> OK \r\n");
        }else{
            nRF.printf("No file for deleting found. OK\n\r");
        }

    nRF.printf("Boot ... \n\r");
    /*
                    led1 = 1;
                led2 = led3 = led4 = 0;
                sprintf(fname, "/SDCard/%s.txt", value);
                FILE *fp = fopen(fname, "w");                   // Open for writing
                if(fp == NULL) 
                {                                    // Controll for opened SDCard
                        nRF.printf("\nConnecting ERROR before saveing\n");            // Printing warning
                }  
                fprintf(fp, "NEW Data from nRF\n ");      // Write Starting sintaxe
                fprintf(fp, "i\t Acc_x\t Acc_y\t Acc_z\t Gyro_x\t Gyro_y\t Gyro_z\t \n");    // Writing measured data
                fclose(fp);
                              
                i = 0;
                j = 0;     
                                                                 // Setting global variable to 0
                Saving_Ticker.attach(&measuring, PERIOD);       // attach Function for saveing data
                
               */ 
                
    while(1)
    {
        data = nRF.getc();                         // On receive, get the character.
        
        switch( data ){
        case 115 :                                      // s - save data
            {                                           // This block will starts measuring
                led1 = 1;
                led2 = led3 = led4 = 0;
                sprintf(fname, "/SDCard/%s.txt", value);
                FILE *fp = fopen(fname, "w");                   // Open for writing
                if(fp == NULL) 
                {                                    // Controll for opened SDCard
                        nRF.printf("\nConnecting ERROR before saveing\n");            // Printing warning
                }  
                fprintf(fp, "NEW Data from nRF\n ");      // Write Starting sintaxe
                fprintf(fp, "i\t Acc_x\t Acc_y\t Acc_z\t Gyro_x\t Gyro_y\t Gyro_z\t \n");    // Writing measured data
                fclose(fp);
                              
                i = 0;
                j = 0;     
                                                                 // Setting global variable to 0
                Saving_Ticker.attach(&measuring, PERIOD);       // attach Function for saveing data
            };
            break;
            
            
        case 103 :                                      // g - get data
            {                                           // This block will stop measuring and start getting data via bluetooth 
                Saving_Ticker.detach();             // Detach function for saveing
                led2 = 1;
                led1 = led3 = led4 = 0;               
                sprintf(fname, "/SDCard/%s.txt", value);
                FILE *fp = fopen(fname, "a");
                if(fp == NULL) 
                {                                    // Controll for opened SDCard
                        nRF.printf("\nConnecting ERROR before getting\n");            // Printing warning
                }
                
                fprintf(fp, "End of DATA\n Q ");                 // Adding mark for end of measured data
                fclose(fp);                                     // Closing file
                
                Get_data();
                
                nRF.printf("Data are transfered\n");
                int rem = remove(fname);
                if (rem == 0){
                    nRF.printf("File was deleted -> everything is fine :) ");
                }else{
                    nRF.printf("NOT Deleted FILE !!!!!!!");
                }
                i = 0;
            };
            break;
            
        case 114:
            led1 = led2 = led3 = led4 = 1;
            w.kick(2);
            while(1);
        break;
              
        default : 
            led4 = 1;
            led1 = led2 = led3 = 0;
            nRF.printf("\nWrong key, try  S for saveing or G for getting measured data\n");
            break;
        }
 
    }
    
}