base code for beer_project

Dependencies:   mbed tsi_sensor FreescaleIAP MMA8451Q MPL3115A2

Files at this revision

API Documentation at this revision

Comitter:
tuscasp
Date:
Wed Nov 28 00:31:25 2018 +0000
Parent:
1:3a57bceb88f8
Child:
3:93bc37d442df
Commit message:
working code, with one unexplained bug of writing some constant values to the memory at the end of the data acquisition.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Nov 23 17:07:30 2018 +0000
+++ b/main.cpp	Wed Nov 28 00:31:25 2018 +0000
@@ -40,9 +40,15 @@
 #define SECTOR_SIZE             1024
 #define RESERVED_SECTOR         32
 
-#define ACQ_TIMER_PERIOD        0.20   // Time between 2 acquisitions in seconds
+#define ACQ_TIMER_PERIOD        0.05   // Time between 2 acquisitions in seconds
 
-// Structure of sensors Data !!!!! SIZE MUST BE A MULTIPLE OF 4 bytes !!!!!
+#define AVERAGE_SAMPLES             15
+#define BOARD_REST                  0
+#define BOARD_UPSIDE_DOWN           1
+#define ACCEL_MINIMUM_FOR_STORAGE   0
+
+
+// Structure of sensors Data !!!!! SIZE MUST BE A MULTIPLE OF 4 bytes !!!!!/
 typedef struct{
     int16_t Accel_X;                // 2 bytes
     int16_t Accel_Y;                // 2 bytes
@@ -53,6 +59,16 @@
     unsigned short Analog_PTE22;    // 2 bytes
 } Sensor_Data;                      // TOTAL = 16 bytes 
 
+
+typedef struct 
+{
+    unsigned short Accel_Z;
+    unsigned short Ethanol_Concentration;
+//    unsigned short No_Use
+} Stored_Data;
+
+Stored_Data Stored_Data_Separator;
+
 // --- Setup I2C for MMA8451 accelerometer
 // --- The last argument is the full scale range (FSR). 0x00 for 2G, 0x01 for 4G, 0x02 for 8G
 MMA8451Q my8451(PTE25, PTE24, MMA8451_I2C_ADDRESS, FSR);
@@ -87,6 +103,8 @@
 int Nb_Sector;
 uint32_t KL25_Flash_Size;
 
+int Number_Stored_Points;
+
 // Functions declaration
 void Clear_Led(void);
 int Acquisition_Flash(void);
@@ -97,15 +115,21 @@
 void Read_Task(void);
 extern IAPCode verify_erased(int address, unsigned int length);
 
+
+
 int main() {
+        
+    uint8_t Count;                                          // Count defining the number of time the LED blinks before data acquisition
     
-    uint8_t Count;                                          // Count defining the number of time the LED blinks before data acquisition
+    Number_Stored_Points = 0;
+    Stored_Data_Separator.Accel_Z = 0;
+    Stored_Data_Separator.Ethanol_Concentration = 0;
     
     // Set DAC output voltage
     float vdac;
     uint16_t dac_value;                                     // Local variable in 16 bits
      
-    vdac = 0;                                             // Voltage output value
+    vdac = 0;                                               // Voltage output value
     dac_value = (uint16_t) ((vdac * 65536) / KL25Z_VDD);    // Transform desired voltage to fraction of 0xFFFF 
     myDAC.write_u16(dac_value);                             // DAC value in range 0x0000 - 0xFFFF (see mbed's AnalogOut classe ref.)
     
@@ -120,7 +144,7 @@
     Nb_Sector = (KL25_Flash_Size / SECTOR_SIZE) - RESERVED_SECTOR;  // Reserve max 32K for app code
     myTick_Acq.attach(&myTimer_Acq_Task, ACQ_TIMER_PERIOD);         // Initialize timer interrupt
   
-    Host_Comm.printf("\n\rLELEC2811 Multiple sensors logger V2.0 UCL 2018\n\r");
+    Host_Comm.printf("\n\rLELEC2811 Beer project acquisition logger V1.0 UCL 2018\n\r");
     
     if ((sizeof(Sensor_Data) % 4) != 0)
     {
@@ -164,7 +188,7 @@
         }
     }
 }
-    
+   
 void Read_Task()
 {
     char host_cmd;
@@ -266,7 +290,15 @@
     int Status;  
     int Flash_Ptr ;
     int Led_Counter;
-    Sensor_Data myData;  
+    int Count_Measurements;
+    int Board_Position;
+    Sensor_Data myData;
+    Stored_Data myStoredData;
+
+/*** Initializing inserted variables for initial value **/
+    Count_Measurements = 0;
+    myStoredData.Accel_Z = 0;
+    myStoredData.Ethanol_Concentration = 0;
     
 /*** Erase all Flash Page **/          
     for (Flash_Ptr = Flash_Base_Address ; Flash_Ptr < KL25_Flash_Size ; Flash_Ptr += 0x400)
@@ -286,7 +318,7 @@
     
 /***** Begin of Loop Acquisition - Write in Flash ***/  
 
-    while (Flash_Ptr < (KL25_Flash_Size - sizeof(Sensor_Data)) )    // Acq Loop
+    while (Flash_Ptr < (KL25_Flash_Size - sizeof(Stored_Data)) )    // Acq Loop
     {              
         while (bTimer == 0)                                         // Wait Acq Tick Timer Done
         {
@@ -300,38 +332,74 @@
             Led_Blue = !Led_Blue;
         }
         
-        Led_Counter++; 
-        
+        Led_Counter++;
+
         // Get accelerometer data        
         Accel_Enable = ENABLE_STATE;                                // Rising edge -> Start accelerometer measurement
   
-        myData.Accel_X = my8451.getAccAxis(REG_OUT_X_MSB);
-        myData.Accel_Y = my8451.getAccAxis(REG_OUT_Y_MSB);
-        myData.Accel_Z = my8451.getAccAxis(REG_OUT_Z_MSB);       
+//        myData.Accel_X = my8451.getAccAxis(REG_OUT_X_MSB);
+//        myData.Accel_Y = my8451.getAccAxis(REG_OUT_Y_MSB);
+        myData.Accel_Z = my8451.getAccAxis(REG_OUT_Z_MSB);     
         
         Accel_Enable = DISABLE_STATE;
         
-        // Get temperature value       
-        myData.Temperature = myMPL3115.getTemperature();     
+        Host_Comm.printf("%d \n", Count_Measurements);
+                
+        // Verify if acquisition precess must be started
+        if (myData.Accel_Z <= ACCEL_MINIMUM_FOR_STORAGE)
+            Board_Position = BOARD_UPSIDE_DOWN;
+
+        // If board is upside down, start the acquistion task
+        if (Board_Position == BOARD_UPSIDE_DOWN)
+        {
+            // Get ADC value
+            myData.Analog_PTE21 = myPTE21.read_u16();  
+
+            // Adding sensor values to the stored structure
+            myStoredData.Accel_Z                = myData.Accel_Z;
+            myStoredData.Ethanol_Concentration  += myData.Analog_PTE21;     //stored here AVERAGE_MEASUREMENTS times the measured voltage correspondent to the ppm
+            Count_Measurements += 1;
+            
+             /*** Creating the average filter **/
+
+            if (Count_Measurements == AVERAGE_SAMPLES)
+            {
+                Count_Measurements = 0;         // reset the counter
 
-        // Get ADC values
-        myData.Analog_PTE20 = myPTE20.read_u16(); 
-        myData.Analog_PTE21 = myPTE21.read_u16();  
-        myData.Analog_PTE22 = myPTE22.read_u16();  
+ /*** Save Data in Flash ***/
+                myStoredData.Ethanol_Concentration = myStoredData.Ethanol_Concentration / AVERAGE_SAMPLES;
+                Status = program_flash(Flash_Ptr, (char *) &myStoredData, sizeof(Stored_Data));   // Write in the Flash
+                Number_Stored_Points ++;
+                
+                if (Status != 0) 
+                {
+                     Host_Comm.printf("\n\rFlash_Write Error = %d", Status); 
+                     return WRITE_FLASH_ERROR;
+                }
+                Flash_Ptr += sizeof(Stored_Data); 
+
+                if (Check_Jumper() != JUMPER_PRESENT)                       // If jumper removed -> Stop acquisition
+                {
+                    return FLASH_ACQ_DONE ;   
+                }
+
+                myStoredData.Ethanol_Concentration = 0;                      // Resets the average of the measurement
+
+                // Verify if acquisition process must be stopped
+                if (myStoredData.Accel_Z > ACCEL_MINIMUM_FOR_STORAGE)
+                {
+                    Board_Position = BOARD_REST;
+//                    Status = program_flash(Flash_Ptr, (char *) &Stored_Data_Separator, sizeof(Stored_Data));   // Write in the Flash a separator for each sampling
+//                    Flash_Ptr ++;
+//                    Number_Stored_Points ++;
+                }
+            }
+            
+        }
         
- /*** Save Data in Flash ***/   
-        Status = program_flash(Flash_Ptr, (char *) &myData, sizeof(Sensor_Data));   // Write in the Flash
-        if (Status != 0) 
-        {
-             Host_Comm.printf("\n\rFlash_Write Error = %d", Status); 
-             return WRITE_FLASH_ERROR;
-        }
-        Flash_Ptr += sizeof(Sensor_Data); 
+        else if (Check_Jumper() != JUMPER_PRESENT)                       // If not acquiring and jumper removed -> Stop acquisition
+            return FLASH_ACQ_DONE ;   
 
-        if (Check_Jumper() != JUMPER_PRESENT)                       // If jumper removed -> Stop acquisition
-        {
-            return FLASH_ACQ_DONE ;   
-        }
     }
     
     return FLASH_ACQ_DONE ;
@@ -339,26 +407,25 @@
 
 int Read_Data_Logging()
 {  
-    Sensor_Data * data = (Sensor_Data * )Flash_Base_Address;    // Sensor_Data pointer of data stored in Flash
+    Stored_Data * data = (Stored_Data * )Flash_Base_Address;    // Stored_Data pointer of data stored in Flash
     int Flash_Record_Ptr;
     char cmd;
     int Record_Counter;
     int Max_Record;
-    Sensor_Data myRead_Data;                                    // Data Structure used to retrieve saved value from Flash
+    Stored_Data myRead_Data;                                    // Data Structure used to retrieve saved value from Flash
     
-    float Voltage_PTE20;
-    float Voltage_PTE21;
-    float Voltage_PTE22;
+    float Ethanol_Voltage;
     
     Clear_Led(); 
   
-    Max_Record = (Nb_Sector * SECTOR_SIZE) / sizeof(Sensor_Data);
+    Max_Record = (Nb_Sector * SECTOR_SIZE) / sizeof(Stored_Data);
     Record_Counter = 0;
     Flash_Record_Ptr = 0;
     
-    Host_Comm.printf("\n\rti AccX AccY AccZ Temp PTE20 PTE21 PTE22");  
+    Host_Comm.printf("\n\rCount AccZ Eth_Volt");  
      
-    while (Record_Counter < Max_Record) 
+//    while (Record_Counter < Max_Record) 
+    while (Record_Counter < Number_Stored_Points)
     {         
         Led_Green = !Led_Green;
         Led_Blue = !Led_Green;
@@ -377,19 +444,16 @@
         
         Flash_Record_Ptr ++;
                        
-        if ((myRead_Data.Accel_X == -1) && (myRead_Data.Accel_Y == -1) && (myRead_Data.Accel_Z == -1))  // Valid data ? (!= 0xFFFFFFFF from empty Flash sector)
+        if (myRead_Data.Accel_Z == -1)  // Valid data ? (!= 0xFFFFFFFF from empty Flash sector)
         {
         }
         else
         {
-            Voltage_PTE20 = ((float) myRead_Data.Analog_PTE20 / 0XFFFF) * KL25Z_VDD;    // Convert to voltage
-            Voltage_PTE21 = ((float) myRead_Data.Analog_PTE21 / 0XFFFF) * KL25Z_VDD;          
-            Voltage_PTE22 = ((float) myRead_Data.Analog_PTE22 / 0XFFFF) * KL25Z_VDD;                 
+            Ethanol_Voltage = ((float) myRead_Data.Ethanol_Concentration / (0XFFFF)) * KL25Z_VDD;                 
             
             Host_Comm.printf("\n\r%d ", Record_Counter);
-            Host_Comm.printf("%d %d %d ", myRead_Data.Accel_X, myRead_Data.Accel_Y, myRead_Data.Accel_Z);
-            Host_Comm.printf("%1.2f ", myRead_Data.Temperature);
-            Host_Comm.printf("%1.3f %1.3f %1.3f ", Voltage_PTE20, Voltage_PTE21, Voltage_PTE22);               
+            Host_Comm.printf("%d ", myRead_Data.Accel_Z);
+            Host_Comm.printf("%1.3f", Ethanol_Voltage);               
         }
         
         Record_Counter ++;