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.
Fork of ReferredCoursework2016 by
buffer.cpp
00001 /*buffer.cpp creates the circular buffer array and contains functions 00002 responsible for pushing new data on to the queue and also pulling data 00003 from it. It also contains the function to workout the moving average of 00004 the 10 values stored within the circular buffer*/ 00005 00006 #include "mbed.h" 00007 #include "rtos.h" 00008 #include "buffer.h" 00009 00010 //Creates a struct array, using the custom struct that holds the acceleration measurements. 00011 AccelData circular_buffer_array[10]; 00012 00013 int rear = 0; 00014 int front = 0; 00015 int32_t sumX; 00016 int32_t sumY; 00017 int32_t sumZ; 00018 int32_t averageX; 00019 int32_t averageY; 00020 int32_t averageZ; 00021 00022 Mutex mutex2; 00023 //Pushes data on to buffer using modulo operation. 00024 void log_push(AccelData data) { 00025 mutex2.lock(); 00026 00027 rear = (rear + 1) % 10; 00028 00029 if (rear == front) { 00030 front = (front + 1) % 10; 00031 } 00032 00033 circular_buffer_array[rear] = data; 00034 mutex2.unlock(); 00035 } 00036 00037 //Retrieves a single data struct using the argument to specify which. 00038 AccelData log_get(int index) 00039 { 00040 00041 AccelData record; 00042 record = circular_buffer_array[(front + index) % 10]; 00043 00044 return record; 00045 00046 } 00047 00048 /*This function loops through the array and calculates the average. 00049 It clears the sum totals each time around so that the average is 00050 updated with the new value pushed on to the buffer, creating a 00051 moving average*/ 00052 void print_averages(){ 00053 00054 while(1){ 00055 mutex2.lock(); 00056 for (int i = 0; i < 10; i++) { 00057 00058 AccelData entry = log_get(i); 00059 sumX += entry.x; 00060 sumY += entry.y; 00061 sumZ += entry.z; 00062 00063 /*The two lines below can be uncomented to show the circular buffer working by 00064 outputting the contents of the buffer every time a peice of data is pushed on to it. 00065 This also makes it possible to verify the average is correct.*/ 00066 00067 //printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", entry.x, entry.y, entry.z); 00068 //printf("\r\n"); 00069 00070 } 00071 00072 averageX = sumX / 10; 00073 averageY = sumY / 10; 00074 averageZ = sumZ / 10; 00075 printf("average [acc/mg]: %6ld, %6ld, %6ld\r\n", averageX, averageY, averageZ); 00076 printf("\r\n"); 00077 sumX = 0; 00078 sumY = 0; 00079 sumZ = 0; 00080 mutex2.unlock(); 00081 } 00082 }
Generated on Sat Jul 16 2022 05:47:21 by
