Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
J_Satchell
Date:
Thu Aug 17 06:58:29 2017 +0000
Revision:
91:cd9fcd45ecf6
Parent:
90:38dfa3f350aa
Added mutex

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 91:cd9fcd45ecf6 22 Mutex mutex2;
J_Satchell 90:38dfa3f350aa 23 //Pushes data on to buffer using modulo operation.
J_Satchell 90:38dfa3f350aa 24 void log_push(AccelData data) {
J_Satchell 91:cd9fcd45ecf6 25 mutex2.lock();
J_Satchell 90:38dfa3f350aa 26
J_Satchell 91:cd9fcd45ecf6 27 rear = (rear + 1) % 10;
J_Satchell 91:cd9fcd45ecf6 28
J_Satchell 91:cd9fcd45ecf6 29 if (rear == front) {
J_Satchell 91:cd9fcd45ecf6 30 front = (front + 1) % 10;
J_Satchell 90:38dfa3f350aa 31 }
J_Satchell 90:38dfa3f350aa 32
J_Satchell 91:cd9fcd45ecf6 33 circular_buffer_array[rear] = data;
J_Satchell 91:cd9fcd45ecf6 34 mutex2.unlock();
J_Satchell 90:38dfa3f350aa 35 }
J_Satchell 90:38dfa3f350aa 36
J_Satchell 90:38dfa3f350aa 37 //Retrieves a single data struct using the argument to specify which.
J_Satchell 90:38dfa3f350aa 38 AccelData log_get(int index)
J_Satchell 90:38dfa3f350aa 39 {
J_Satchell 91:cd9fcd45ecf6 40
J_Satchell 90:38dfa3f350aa 41 AccelData record;
J_Satchell 90:38dfa3f350aa 42 record = circular_buffer_array[(front + index) % 10];
J_Satchell 90:38dfa3f350aa 43
J_Satchell 90:38dfa3f350aa 44 return record;
J_Satchell 90:38dfa3f350aa 45
J_Satchell 90:38dfa3f350aa 46 }
J_Satchell 90:38dfa3f350aa 47
J_Satchell 90:38dfa3f350aa 48 /*This function loops through the array and calculates the average.
J_Satchell 90:38dfa3f350aa 49 It clears the sum totals each time around so that the average is
J_Satchell 90:38dfa3f350aa 50 updated with the new value pushed on to the buffer, creating a
J_Satchell 90:38dfa3f350aa 51 moving average*/
J_Satchell 90:38dfa3f350aa 52 void print_averages(){
J_Satchell 90:38dfa3f350aa 53
J_Satchell 90:38dfa3f350aa 54 while(1){
J_Satchell 91:cd9fcd45ecf6 55 mutex2.lock();
J_Satchell 90:38dfa3f350aa 56 for (int i = 0; i < 10; i++) {
J_Satchell 90:38dfa3f350aa 57
J_Satchell 90:38dfa3f350aa 58 AccelData entry = log_get(i);
J_Satchell 90:38dfa3f350aa 59 sumX += entry.x;
J_Satchell 90:38dfa3f350aa 60 sumY += entry.y;
J_Satchell 90:38dfa3f350aa 61 sumZ += entry.z;
J_Satchell 90:38dfa3f350aa 62
J_Satchell 90:38dfa3f350aa 63 /*The two lines below can be uncomented to show the circular buffer working by
J_Satchell 90:38dfa3f350aa 64 outputting the contents of the buffer every time a peice of data is pushed on to it.
J_Satchell 90:38dfa3f350aa 65 This also makes it possible to verify the average is correct.*/
J_Satchell 90:38dfa3f350aa 66
J_Satchell 90:38dfa3f350aa 67 //printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", entry.x, entry.y, entry.z);
J_Satchell 90:38dfa3f350aa 68 //printf("\r\n");
J_Satchell 90:38dfa3f350aa 69
J_Satchell 90:38dfa3f350aa 70 }
J_Satchell 90:38dfa3f350aa 71
J_Satchell 90:38dfa3f350aa 72 averageX = sumX / 10;
J_Satchell 90:38dfa3f350aa 73 averageY = sumY / 10;
J_Satchell 90:38dfa3f350aa 74 averageZ = sumZ / 10;
J_Satchell 90:38dfa3f350aa 75 printf("average [acc/mg]: %6ld, %6ld, %6ld\r\n", averageX, averageY, averageZ);
J_Satchell 90:38dfa3f350aa 76 printf("\r\n");
J_Satchell 90:38dfa3f350aa 77 sumX = 0;
J_Satchell 90:38dfa3f350aa 78 sumY = 0;
J_Satchell 90:38dfa3f350aa 79 sumZ = 0;
J_Satchell 91:cd9fcd45ecf6 80 mutex2.unlock();
J_Satchell 90:38dfa3f350aa 81 }
J_Satchell 90:38dfa3f350aa 82 }