Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
J_Satchell
Date:
Thu Aug 17 02:42:30 2017 +0000
Revision:
90:38dfa3f350aa
Child:
91:cd9fcd45ecf6
Completed project.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
J_Satchell 90:38dfa3f350aa 1 /*buffer.cpp creates the circular buffer array and contains functions
J_Satchell 90:38dfa3f350aa 2 responsible for pushing new data on to the queue and also pulling data
J_Satchell 90:38dfa3f350aa 3 from it. It also contains the function to workout the moving average of
J_Satchell 90:38dfa3f350aa 4 the 10 values stored within the circular buffer*/
J_Satchell 90:38dfa3f350aa 5
J_Satchell 90:38dfa3f350aa 6 #include "mbed.h"
J_Satchell 90:38dfa3f350aa 7 #include "rtos.h"
J_Satchell 90:38dfa3f350aa 8 #include "buffer.h"
J_Satchell 90:38dfa3f350aa 9
J_Satchell 90:38dfa3f350aa 10 //Creates a struct array, using the custom struct that holds the acceleration measurements.
J_Satchell 90:38dfa3f350aa 11 AccelData circular_buffer_array[10];
J_Satchell 90:38dfa3f350aa 12
J_Satchell 90:38dfa3f350aa 13 int rear = 0;
J_Satchell 90:38dfa3f350aa 14 int front = 0;
J_Satchell 90:38dfa3f350aa 15 int32_t sumX;
J_Satchell 90:38dfa3f350aa 16 int32_t sumY;
J_Satchell 90:38dfa3f350aa 17 int32_t sumZ;
J_Satchell 90:38dfa3f350aa 18 int32_t averageX;
J_Satchell 90:38dfa3f350aa 19 int32_t averageY;
J_Satchell 90:38dfa3f350aa 20 int32_t averageZ;
J_Satchell 90:38dfa3f350aa 21
J_Satchell 90:38dfa3f350aa 22 //Pushes data on to buffer using modulo operation.
J_Satchell 90:38dfa3f350aa 23 void log_push(AccelData data) {
J_Satchell 90:38dfa3f350aa 24
J_Satchell 90:38dfa3f350aa 25 rear = (rear + 1) % 10;
J_Satchell 90:38dfa3f350aa 26
J_Satchell 90:38dfa3f350aa 27 if (rear == front) {
J_Satchell 90:38dfa3f350aa 28 front = (front + 1) % 10;
J_Satchell 90:38dfa3f350aa 29 }
J_Satchell 90:38dfa3f350aa 30
J_Satchell 90:38dfa3f350aa 31 circular_buffer_array[rear] = data;
J_Satchell 90:38dfa3f350aa 32
J_Satchell 90:38dfa3f350aa 33 }
J_Satchell 90:38dfa3f350aa 34
J_Satchell 90:38dfa3f350aa 35 //Retrieves a single data struct using the argument to specify which.
J_Satchell 90:38dfa3f350aa 36 AccelData log_get(int index)
J_Satchell 90:38dfa3f350aa 37 {
J_Satchell 90:38dfa3f350aa 38
J_Satchell 90:38dfa3f350aa 39 AccelData record;
J_Satchell 90:38dfa3f350aa 40 record = circular_buffer_array[(front + index) % 10];
J_Satchell 90:38dfa3f350aa 41
J_Satchell 90:38dfa3f350aa 42 return record;
J_Satchell 90:38dfa3f350aa 43
J_Satchell 90:38dfa3f350aa 44 }
J_Satchell 90:38dfa3f350aa 45
J_Satchell 90:38dfa3f350aa 46 /*This function loops through the array and calculates the average.
J_Satchell 90:38dfa3f350aa 47 It clears the sum totals each time around so that the average is
J_Satchell 90:38dfa3f350aa 48 updated with the new value pushed on to the buffer, creating a
J_Satchell 90:38dfa3f350aa 49 moving average*/
J_Satchell 90:38dfa3f350aa 50 void print_averages(){
J_Satchell 90:38dfa3f350aa 51
J_Satchell 90:38dfa3f350aa 52 while(1){
J_Satchell 90:38dfa3f350aa 53
J_Satchell 90:38dfa3f350aa 54 for (int i = 0; i < 10; i++) {
J_Satchell 90:38dfa3f350aa 55
J_Satchell 90:38dfa3f350aa 56 AccelData entry = log_get(i);
J_Satchell 90:38dfa3f350aa 57 sumX += entry.x;
J_Satchell 90:38dfa3f350aa 58 sumY += entry.y;
J_Satchell 90:38dfa3f350aa 59 sumZ += entry.z;
J_Satchell 90:38dfa3f350aa 60
J_Satchell 90:38dfa3f350aa 61 /*The two lines below can be uncomented to show the circular buffer working by
J_Satchell 90:38dfa3f350aa 62 outputting the contents of the buffer every time a peice of data is pushed on to it.
J_Satchell 90:38dfa3f350aa 63 This also makes it possible to verify the average is correct.*/
J_Satchell 90:38dfa3f350aa 64
J_Satchell 90:38dfa3f350aa 65 //printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", entry.x, entry.y, entry.z);
J_Satchell 90:38dfa3f350aa 66 //printf("\r\n");
J_Satchell 90:38dfa3f350aa 67
J_Satchell 90:38dfa3f350aa 68 }
J_Satchell 90:38dfa3f350aa 69
J_Satchell 90:38dfa3f350aa 70 averageX = sumX / 10;
J_Satchell 90:38dfa3f350aa 71 averageY = sumY / 10;
J_Satchell 90:38dfa3f350aa 72 averageZ = sumZ / 10;
J_Satchell 90:38dfa3f350aa 73 printf("average [acc/mg]: %6ld, %6ld, %6ld\r\n", averageX, averageY, averageZ);
J_Satchell 90:38dfa3f350aa 74 printf("\r\n");
J_Satchell 90:38dfa3f350aa 75 sumX = 0;
J_Satchell 90:38dfa3f350aa 76 sumY = 0;
J_Satchell 90:38dfa3f350aa 77 sumZ = 0;
J_Satchell 90:38dfa3f350aa 78 }
J_Satchell 90:38dfa3f350aa 79 }