Einstein Filho / Mbed 2 deprecated MANGUELOGGER_K64F

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Data Logger implementation in K64F.
00003     Inputs:
00004         x6 Analog Inputs;
00005         x2 Digital (Frequency) Inputs;
00006         x1 Internal Accelerometer (FXOS8700);
00007         x1 External Accelerometer and Gyroscope (LSM6DS3);
00008     In this set, it is designed for a 200Hz sample rate.
00009     All the data are saved periodically (every 0.25s) to a folder in the SD card.
00010     To read the data, use the file "read_struct.c" in the folder results.
00011     
00012     Implemented by Diego "sid" Hamilton, Electronics Coordinator at Mangue Baja Team, UFPE.
00013 */
00014 
00015 #include "mbed.h"
00016 #include "LSM6DS3.h"
00017 #include "FXOS8700CQ.h"
00018 #include "SDFileSystem.h"
00019 #include <cstdlib>
00020 
00021 #define SERIAL_BAUD 1000000                     // Board baud rate
00022 #define BUFFER_SIZE 200                         // Acquisition buffer
00023 #define SAVE_WHEN 50                            // Number of packets to save (fail safe)
00024 #define SAMPLE_FREQ 200                         // Frequency in Hz
00025 
00026 /* Debug */
00027 PwmOut signal_wave(D3);                         // Debug wave to test frequency channels
00028 /* I/O */
00029 Serial pc(USBTX, USBRX, SERIAL_BAUD);           // Debug purposes
00030 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");  // MOSI, MISO, SCK, CS
00031 //DigitalOut warning(D3);                         // When device is ready, led is permanently OFF
00032 //DigitalOut logging(D4);                         // When data is beign acquired, led is ON
00033 InterruptIn start(D2, PullUp);                  // Press button to start/stop acquisition
00034 InterruptIn freq_chan1(D0, PullUp);             // Frequency channel 1
00035 InterruptIn freq_chan2(D1, PullUp);             // Frequency channel 2
00036 AnalogIn pot0(A0),
00037          pot1(A1),
00038          pot2(A2),
00039          pot3(A3),
00040          pot4(A4),
00041          pot5(A5);
00042 LSM6DS3 LSM6DS3(PTE25,PTE24);                   // External accelerometer and gyroscope
00043 FXOS8700CQ fxos(PTE25,PTE24);                   // Internal accelerometer (and magnetometer, not used)
00044 
00045 //Ajuste para usar o led da placa
00046 DigitalOut logging(LED1);
00047 DigitalOut warning(LED2);
00048 //DigitalOut led3(LED3);
00049 
00050 /* Data structure */
00051 typedef struct
00052 {
00053     int16_t acclsmx;
00054     int16_t acclsmy;
00055     int16_t acclsmz;
00056     int16_t anglsmx;
00057     int16_t anglsmy;
00058     int16_t anglsmz;
00059     int16_t accfxox;
00060     int16_t accfxoy;
00061     int16_t accfxoz;
00062     uint16_t analog0;
00063     uint16_t analog1;
00064     uint16_t analog2;
00065     uint16_t analog3;
00066     uint16_t analog4;
00067     uint16_t analog5;
00068     uint16_t pulses_chan1;
00069     uint16_t pulses_chan2;
00070     uint32_t time_stamp;
00071 } packet_t;
00072 
00073 Timer t;                                        // Device timer
00074 Ticker acq;                                     // Acquisition timer interrupt source
00075 CircularBuffer<packet_t, BUFFER_SIZE> buffer;   // Acquisition buffer
00076 int buffer_counter = 0;                         // Packet currently in buffer
00077 bool running = false;                           // Device status
00078 //bool running = true;                            // Einstein's Adaptation
00079 uint16_t pulse_counter1 = 0,
00080          pulse_counter2 = 0;                    // Frequency counter variables
00081 uint16_t acc_addr = 0;                          /* LSM6DS3 address, if not connected
00082                                                    address is 0 and data is not stored */
00083 
00084 void sampleISR();                               // Data acquisition ISR
00085 uint32_t count_files_in_sd(const char *fsrc);   // Compute number of files in SD
00086 void freq_channel1_ISR();                       // Frequency counter ISR, channel 1
00087 void freq_channel2_ISR();                       // Frequency counter ISR, channel 2
00088 void toggle_logging();                          // Start button ISR
00089 
00090 int main()
00091 {
00092     uint32_t t1,t2;
00093     logging = 0;                                // logging led OFF
00094     int num_parts = 0,                          // Number of parts already saved
00095         num_files = 0,                          // Number of files in SD
00096         svd_pck = 0;                            // Number of saved packets (in current part)
00097     char name_dir[12];                          // Name of current folder (new RUN)
00098     char name_file[20];                         // Name of current file (partX)
00099     FILE* fp;                                 
00100     packet_t temp;
00101     packet_t trash;
00102     signal_wave.period_ms(50);
00103     signal_wave.write(0.5f);
00104     
00105     /* Initialize accelerometers */
00106     acc_addr = LSM6DS3.begin(LSM6DS3.G_SCALE_245DPS, LSM6DS3.A_SCALE_2G, \
00107                            LSM6DS3.G_ODR_208, LSM6DS3.A_ODR_208);
00108     fxos.init();
00109 
00110     /* Wait for SD ready */
00111     while(sd.disk_status())
00112     {
00113         sd.disk_initialize();
00114         warning = 0;
00115         wait(1.0);
00116         warning = 1;
00117         wait(1.0);
00118     }
00119     
00120     num_files = count_files_in_sd("/sd");
00121     sprintf(name_dir, "%s%d", "/sd/RUN", num_files + 1);
00122     
00123     start.fall(&toggle_logging);                 // Attach start button ISR
00124     while(!running)                             // Wait button press
00125         warning = 1;                            // For some reason if this line is empty the code doesn't run
00126 
00127     /* Create RUN directory */
00128     mkdir(name_dir, 0777);
00129     warning = 0;
00130     sprintf(name_file, "%s%s%d", name_dir, "/part", num_parts++);
00131     fp = fopen(name_file, "w");                 // Creates first data file
00132     t.start();                                  // Start device timer
00133     freq_chan1.fall(&freq_channel1_ISR);
00134     freq_chan2.fall(&freq_channel2_ISR);
00135     acq.attach(&sampleISR, 1.0/SAMPLE_FREQ);    // Start data acquisition
00136     logging = 1;          
00137                         // logging led ON
00138         
00139     while(running)
00140     {
00141 
00142         if(buffer.full())
00143         {
00144             warning = 1;                        // Turn warning led ON if buffer gets full (abnormal situation)
00145             //pc.putc('X');                       // Debug message
00146             //buffer.pop(trash);
00147         }
00148         else if(!buffer.empty())
00149         {
00150             //pc.putc('G');                       // Debug message
00151             /* Remove packet from buffer and writes it to file */
00152             buffer.pop(temp);                   
00153             buffer_counter--;
00154             //t1 = t.read_ms();
00155             fwrite((void *)&temp, sizeof(packet_t), 1, fp);
00156             //t2 = t.read_ms()-t1;
00157             //pc.printf("%d\r\n",t2);
00158             svd_pck++;
00159             /* Create new data file */
00160             if(svd_pck == SAVE_WHEN)
00161             {   
00162                 fclose(fp);
00163                 sprintf(name_file, "%s%s%d", name_dir, "/part", num_parts++);
00164                 fp = fopen(name_file, "w");
00165                 //pc.printf("%d\n", buffer_counter);  // Debug message
00166                 svd_pck = 0;
00167 
00168             }   
00169         }
00170         /* Software debounce for start button */
00171         if((t.read_ms() > 10) && (t.read_ms() < 20))
00172             start.fall(toggle_logging);
00173     }
00174     
00175     /* Reset device if start button is pressed while logging */
00176     logging = 0;
00177     NVIC_SystemReset();
00178     return 0;
00179 }
00180 
00181 void sampleISR()
00182 {
00183     static uint16_t last_acq = t.read_ms();     // Time of last acquisition
00184     packet_t acq_pck;                           // Current data packet
00185     Data fxos_acc;
00186     fxos_acc = fxos.get_values();               // Read FXOS8700 data
00187     /* Store LSM6DS3 data if it's connected */
00188     if (acc_addr != 0)
00189     {
00190         LSM6DS3.readAccel();                    // Read Accelerometer data
00191         LSM6DS3.readGyro();                     // Read Gyroscope data
00192         
00193         acq_pck.acclsmx = LSM6DS3.ax_raw;
00194         acq_pck.acclsmy = LSM6DS3.ay_raw;   
00195         acq_pck.acclsmz = LSM6DS3.az_raw;
00196         acq_pck.anglsmx = LSM6DS3.gx_raw;
00197         acq_pck.anglsmy = LSM6DS3.gy_raw;
00198         acq_pck.anglsmz = LSM6DS3.gz_raw;
00199     }
00200     else
00201     {
00202         acq_pck.acclsmx = 0;
00203         acq_pck.acclsmy = 0;   
00204         acq_pck.acclsmz = 0;
00205         acq_pck.anglsmx = 0;
00206         acq_pck.anglsmy = 0;
00207         acq_pck.anglsmz = 0;
00208     }
00209     acq_pck.accfxox = fxos_acc.ax;
00210     acq_pck.accfxoy = fxos_acc.ay;
00211     acq_pck.accfxoz = fxos_acc.az;
00212     acq_pck.analog0 = pot0.read_u16();          // Read analog sensor 0            
00213     acq_pck.analog1 = pot1.read_u16();          // Read analog sensor 1
00214     acq_pck.analog2 = pot2.read_u16();          // Read analog sensor 2
00215     acq_pck.analog3 = pot3.read_u16();          // Read analog sensor 3
00216     acq_pck.analog4 = pot4.read_u16();          // Read analog sensor 4
00217     acq_pck.analog5 = pot5.read_u16();          // Read analog sensor 5
00218     acq_pck.pulses_chan1 = pulse_counter1;      // Store frequence channel 1
00219     acq_pck.pulses_chan2 = pulse_counter2;      // Store frequence channel 2
00220     acq_pck.time_stamp = t.read_ms();           // Timestamp of data acquistion
00221     
00222     pulse_counter1= 0;
00223     pulse_counter2= 0;
00224     buffer.push(acq_pck);
00225     buffer_counter++;
00226 }
00227 
00228 uint32_t count_files_in_sd(const char *fsrc)
00229 {   
00230     DIR *d = opendir(fsrc);
00231     struct dirent *p;
00232     uint32_t counter = 0;
00233     
00234     while ((p = readdir(d)) != NULL) {
00235         if(strcmp(p->d_name, ".Trash-1000"))
00236             counter++;
00237     }
00238     closedir(d);
00239     return counter;
00240 }
00241 
00242 void freq_channel1_ISR()
00243 {
00244     pulse_counter1++;
00245 }
00246 
00247 void freq_channel2_ISR()
00248 {
00249     pulse_counter2++;
00250 }
00251 
00252 void toggle_logging()
00253 {
00254     running = !running;
00255     start.fall(NULL);
00256 }