Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed tsi_sensor FreescaleIAP MMA8451Q MPL3115A2
Revision 3:93bc37d442df, committed 2018-11-30
- Comitter:
- tuscasp
- Date:
- Fri Nov 30 18:52:10 2018 +0000
- Parent:
- 2:1bd31ca8a126
- Child:
- 4:64f3383f2c43
- Commit message:
- working code, no real time available
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Nov 28 00:31:25 2018 +0000
+++ b/main.cpp Fri Nov 30 18:52:10 2018 +0000
@@ -40,15 +40,15 @@
#define SECTOR_SIZE 1024
#define RESERVED_SECTOR 32
-#define ACQ_TIMER_PERIOD 0.05 // Time between 2 acquisitions in seconds
+#define ACQ_TIMER_PERIOD 0.5 // Time between 2 acquisitions in seconds
-#define AVERAGE_SAMPLES 15
-#define BOARD_REST 0
-#define BOARD_UPSIDE_DOWN 1
+#define N_PER_AVERAGE 8
+
+#define BOARD_STRAIGHT 0
+#define BOARD_UPSIDE_DOWN 1
#define ACCEL_MINIMUM_FOR_STORAGE 0
-
-// Structure of sensors Data !!!!! SIZE MUST BE A MULTIPLE OF 4 bytes !!!!!/
+// 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
@@ -56,18 +56,17 @@
float Temperature; // 4 bytes
unsigned short Analog_PTE20; // 2 bytes
unsigned short Analog_PTE21; // 2 bytes
- unsigned short Analog_PTE22; // 2 bytes
+ unsigned short Empty; // 2 bytes
} Sensor_Data; // TOTAL = 16 bytes
-
-typedef struct
-{
+typedef struct{
unsigned short Accel_Z;
- unsigned short Ethanol_Concentration;
-// unsigned short No_Use
+ unsigned short Sallen_Key;
+ unsigned short Interface;
+ unsigned short Useless;
} Stored_Data;
-Stored_Data Stored_Data_Separator;
+int Number_Stored_Points;
// --- Setup I2C for MMA8451 accelerometer
// --- The last argument is the full scale range (FSR). 0x00 for 2G, 0x01 for 4G, 0x02 for 8G
@@ -83,7 +82,6 @@
// Analog inputs (ADCs)
AnalogIn myPTE20(PTE20);
AnalogIn myPTE21(PTE21);
-AnalogIn myPTE22(PTE22);
// Analog output (DAC)
AnalogOut myDAC(PTE30);
@@ -103,8 +101,6 @@
int Nb_Sector;
uint32_t KL25_Flash_Size;
-int Number_Stored_Points;
-
// Functions declaration
void Clear_Led(void);
int Acquisition_Flash(void);
@@ -115,16 +111,10 @@
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
- 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
@@ -144,7 +134,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 Beer project acquisition logger V1.0 UCL 2018\n\r");
+ Host_Comm.printf("\n\rLELEC2811 Multiple sensors logger V2.0 UCL 2018\n\r");
if ((sizeof(Sensor_Data) % 4) != 0)
{
@@ -188,7 +178,7 @@
}
}
}
-
+
void Read_Task()
{
char host_cmd;
@@ -290,15 +280,11 @@
int Status;
int Flash_Ptr ;
int Led_Counter;
- int Count_Measurements;
+ Sensor_Data myData;
int Board_Position;
- Sensor_Data myData;
+ int Count_Measurements;
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)
@@ -315,10 +301,21 @@
Led_Blue = LED_ON;
Led_Counter = 0;
+
+/*** Reset new variables **/
+
+ myStoredData.Accel_Z= 0;
+ myStoredData.Interface = 0;
+ myStoredData.Sallen_Key = 0;
-/***** Begin of Loop Acquisition - Write in Flash ***/
-
- while (Flash_Ptr < (KL25_Flash_Size - sizeof(Stored_Data)) ) // Acq Loop
+ Board_Position = BOARD_STRAIGHT;
+
+ Number_Stored_Points = 0;
+
+
+/***** Begin of Loop Acquisition - Write in Flash ***/
+
+ while (Flash_Ptr < (KL25_Flash_Size - sizeof(Sensor_Data)) ) // Acq Loop
{
while (bTimer == 0) // Wait Acq Tick Timer Done
{
@@ -332,74 +329,73 @@
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;
- Host_Comm.printf("%d \n", Count_Measurements);
-
- // Verify if acquisition precess must be started
- if (myData.Accel_Z <= ACCEL_MINIMUM_FOR_STORAGE)
+
+ /*** verify if it is upside down ***/
+ 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();
+ if (Board_Position == BOARD_UPSIDE_DOWN){
+
+ Count_Measurements ++;
- // 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;
+ // Get ADC values
+ myData.Analog_PTE20 = myPTE20.read_u16();
+ myData.Analog_PTE21 = myPTE21.read_u16();
+
+ // add data to stored variable
+ myStoredData.Accel_Z = myData.Accel_Z;
+
+ myStoredData.Sallen_Key += myData.Analog_PTE20;
+ myStoredData.Interface += myData.Analog_PTE21;
- /*** Creating the average filter **/
-
- if (Count_Measurements == AVERAGE_SAMPLES)
- {
- Count_Measurements = 0; // reset the counter
+ Host_Comm.printf("\n\r%d %d", Count_Measurements, myStoredData.Accel_Z);
+
+ // after N_PER_AVERAGE measurements, take average and store
+ if (Count_Measurements >= N_PER_AVERAGE){
+
+ Count_Measurements = 0;
+
+ myStoredData.Sallen_Key = myStoredData.Sallen_Key / N_PER_AVERAGE;
+ myStoredData.Interface = myStoredData.Interface / N_PER_AVERAGE;
/*** Save Data in Flash ***/
- myStoredData.Ethanol_Concentration = myStoredData.Ethanol_Concentration / AVERAGE_SAMPLES;
+ Number_Stored_Points ++;
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);
-
+
+ // verifies if system comes back straight orientation
+ if (myData.Accel_Z > ACCEL_MINIMUM_FOR_STORAGE)
+ Board_Position = BOARD_STRAIGHT;
+
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 ++;
- }
}
}
- else if (Check_Jumper() != JUMPER_PRESENT) // If not acquiring and jumper removed -> Stop acquisition
- return FLASH_ACQ_DONE ;
-
+ else {
+ if (Check_Jumper() != JUMPER_PRESENT) // If jumper removed -> Stop acquisition
+ return FLASH_ACQ_DONE ;
+ }
+
}
return FLASH_ACQ_DONE ;
@@ -407,25 +403,25 @@
int Read_Data_Logging()
{
- Stored_Data * data = (Stored_Data * )Flash_Base_Address; // Stored_Data pointer of data stored in Flash
+ Stored_Data * data = (Stored_Data * )Flash_Base_Address; // Sensor_Data pointer of data stored in Flash
int Flash_Record_Ptr;
char cmd;
int Record_Counter;
int Max_Record;
Stored_Data myRead_Data; // Data Structure used to retrieve saved value from Flash
- float Ethanol_Voltage;
+ float Ethanol_Sallen_Key;
+ float Ethanol_Interface;
Clear_Led();
- Max_Record = (Nb_Sector * SECTOR_SIZE) / sizeof(Stored_Data);
+ Max_Record = (Nb_Sector * SECTOR_SIZE) / sizeof(Sensor_Data);
Record_Counter = 0;
Flash_Record_Ptr = 0;
- Host_Comm.printf("\n\rCount AccZ Eth_Volt");
+ Host_Comm.printf("\n\r# AccZ Eth_SK Eth_Int");
-// while (Record_Counter < Max_Record)
- while (Record_Counter < Number_Stored_Points)
+ while (Record_Counter < Number_Stored_Points && Record_Counter < Max_Record)
{
Led_Green = !Led_Green;
Led_Blue = !Led_Green;
@@ -449,11 +445,12 @@
}
else
{
- Ethanol_Voltage = ((float) myRead_Data.Ethanol_Concentration / (0XFFFF)) * KL25Z_VDD;
+ Ethanol_Sallen_Key = ((float) myRead_Data.Sallen_Key / 0XFFFF) * KL25Z_VDD; // Convert to voltage
+ Ethanol_Interface = ((float) myRead_Data.Interface / 0XFFFF) * KL25Z_VDD;
Host_Comm.printf("\n\r%d ", Record_Counter);
Host_Comm.printf("%d ", myRead_Data.Accel_Z);
- Host_Comm.printf("%1.3f", Ethanol_Voltage);
+ Host_Comm.printf("%1.3f %1.3f ", Ethanol_Sallen_Key, Ethanol_Interface);
}
Record_Counter ++;