In this lab, you will: Construct a prototype

Dependencies:   ADXL362 mbed

Committer:
csinders
Date:
Wed Feb 21 21:06:45 2018 +0000
Revision:
2:b56010953f54
Parent:
1:fedb70ea0eaa
Child:
3:33cca9e98fbb
Created a program that can detect vibration.  ThreshHold may need to be adjusted. (These tables are super stable).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csinders 0:d5c236ffbd5e 1 #include "mbed.h"
csinders 0:d5c236ffbd5e 2 #include "ADXL362.h"
csinders 0:d5c236ffbd5e 3
csinders 0:d5c236ffbd5e 4 // Interface pulled from ADXL362.cpp
csinders 0:d5c236ffbd5e 5 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
csinders 0:d5c236ffbd5e 6 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
csinders 0:d5c236ffbd5e 7 Serial pc(USBTX, USBRX);
csinders 2:b56010953f54 8 DigitalOut myled(LED1);
csinders 0:d5c236ffbd5e 9
csinders 1:fedb70ea0eaa 10 int adxl362_reg_print(int start, int length);
csinders 2:b56010953f54 11 void adxl362_get_average(int8_t avg[]);
csinders 1:fedb70ea0eaa 12
csinders 0:d5c236ffbd5e 13 int main() {
csinders 0:d5c236ffbd5e 14 pc.printf("Starting program\n\r");
csinders 0:d5c236ffbd5e 15 adxl362.reset();
csinders 0:d5c236ffbd5e 16 wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
csinders 0:d5c236ffbd5e 17 adxl362.set_mode(ADXL362::MEASUREMENT);
csinders 0:d5c236ffbd5e 18 int8_t x,y,z;
csinders 2:b56010953f54 19 int8_t avg[3]; // Avg values of x,y,z. x = avg[0], y = avg[1] z = avg[2]
csinders 0:d5c236ffbd5e 20
csinders 2:b56010953f54 21 adxl362_reg_print(0,0); // Test of axdl_reg_print
csinders 2:b56010953f54 22 wait_ms(1000); // wait so that values can be seen
csinders 2:b56010953f54 23 int threshHoldRange = 10; // threshhold difference between avg and read values
csinders 2:b56010953f54 24 adxl362_get_average(avg);// Generate an average of what x,y,z are.
csinders 2:b56010953f54 25 //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
csinders 0:d5c236ffbd5e 26 while(1) {
csinders 0:d5c236ffbd5e 27 x=adxl362.scanx_u8();
csinders 0:d5c236ffbd5e 28 y=adxl362.scany_u8();
csinders 0:d5c236ffbd5e 29 z=adxl362.scanz_u8();
csinders 2:b56010953f54 30 if (x > (avg[0] + threshHoldRange) || x < (avg[0] - threshHoldRange)
csinders 2:b56010953f54 31 || y > (avg[1] + threshHoldRange) || y < (avg[1] - threshHoldRange)
csinders 2:b56010953f54 32 //|| z > (zAvg + threshHoldRange) || z < (zAvg - threshHoldRange)) {
csinders 2:b56010953f54 33 // commented out z as it gives weird values
csinders 2:b56010953f54 34 ) {
csinders 2:b56010953f54 35 /* print cases used to test threshhold range
csinders 2:b56010953f54 36 pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
csinders 2:b56010953f54 37 pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
csinders 2:b56010953f54 38 pc.printf("Threshold range is = %d\r\n", threshHoldRange);
csinders 2:b56010953f54 39 */
csinders 2:b56010953f54 40 pc.printf("outside of threshold range\n\r");
csinders 2:b56010953f54 41 myled = 1; // LED is ON
csinders 2:b56010953f54 42 wait_ms(1900); // Wait 1.9s. Last .1 sec is done while getting average
csinders 2:b56010953f54 43 adxl362_get_average(avg); // Generate new average
csinders 2:b56010953f54 44 myled = 0; // LED is OFF
csinders 2:b56010953f54 45 }
csinders 0:d5c236ffbd5e 46 pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
csinders 0:d5c236ffbd5e 47 wait_ms(100);
csinders 0:d5c236ffbd5e 48 }
csinders 2:b56010953f54 49 }
csinders 2:b56010953f54 50
csinders 2:b56010953f54 51 void adxl362_get_average(int8_t avg[]) {
csinders 2:b56010953f54 52 // Generate an average of what x,y,z are.
csinders 2:b56010953f54 53 int numberOfPolls = 10;
csinders 2:b56010953f54 54 // reset average back to zero for each
csinders 2:b56010953f54 55 int tmp[3]; // required because avg was having integer overflow
csinders 2:b56010953f54 56 tmp[0] = 0;
csinders 2:b56010953f54 57 tmp[1] = 0;
csinders 2:b56010953f54 58 tmp[2] = 0;
csinders 2:b56010953f54 59 avg[0] = 0;
csinders 2:b56010953f54 60 avg[1] = 0;
csinders 2:b56010953f54 61 avg[2] = 0;
csinders 2:b56010953f54 62 int8_t x,y,z;
csinders 2:b56010953f54 63 // Poll each value 10 times
csinders 2:b56010953f54 64 for (int i = 0; i < numberOfPolls; i++) {
csinders 2:b56010953f54 65 //pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
csinders 2:b56010953f54 66 x = adxl362.scanx_u8();
csinders 2:b56010953f54 67 tmp[0]+= x;
csinders 2:b56010953f54 68 y = adxl362.scany_u8();
csinders 2:b56010953f54 69 tmp[1]+= y;
csinders 2:b56010953f54 70 z=adxl362.scanz_u8();
csinders 2:b56010953f54 71 tmp[2]+= z;
csinders 2:b56010953f54 72 wait_ms(10);
csinders 2:b56010953f54 73 //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",tmp[0],tmp[1],tmp[2]);
csinders 2:b56010953f54 74
csinders 2:b56010953f54 75 }
csinders 2:b56010953f54 76 // Divide each value by the number of polls to get the average
csinders 2:b56010953f54 77 tmp[0] = tmp[0] / numberOfPolls;
csinders 2:b56010953f54 78 tmp[1] = tmp[1] / numberOfPolls;
csinders 2:b56010953f54 79 tmp[2] = tmp[2] / numberOfPolls;
csinders 2:b56010953f54 80 // store values from the tmp to the passed in array
csinders 2:b56010953f54 81 avg[0] = tmp[0];
csinders 2:b56010953f54 82 avg[1] = tmp[1];
csinders 2:b56010953f54 83 avg[2] = tmp[2];
csinders 2:b56010953f54 84 pc.printf("New Average: xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
csinders 2:b56010953f54 85 wait_ms(5000);
csinders 0:d5c236ffbd5e 86 }
csinders 0:d5c236ffbd5e 87
csinders 0:d5c236ffbd5e 88 int adxl362_reg_print(int start, int length) {
csinders 0:d5c236ffbd5e 89 // Check if start is within registry
csinders 0:d5c236ffbd5e 90 if (start < 0 || start > 0x2E) {
csinders 0:d5c236ffbd5e 91 pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r");
csinders 0:d5c236ffbd5e 92 return -1;
csinders 0:d5c236ffbd5e 93 }
csinders 0:d5c236ffbd5e 94 // check if length is negative
csinders 0:d5c236ffbd5e 95 if (length < 0) {
csinders 0:d5c236ffbd5e 96 pc.printf("Error: length passed to adxl362_reg_print is negative\n\r");
csinders 0:d5c236ffbd5e 97 return -1;
csinders 0:d5c236ffbd5e 98 }
csinders 0:d5c236ffbd5e 99
csinders 0:d5c236ffbd5e 100 // check if valid communication with device going
csinders 1:fedb70ea0eaa 101 // For some reason getting 0xAC instead of 0xAD?
csinders 2:b56010953f54 102 // Printing out what value we are getting
csinders 2:b56010953f54 103 if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) {
csinders 0:d5c236ffbd5e 104 pc.printf("Error: Unable to read from DEVID_AD register\n\r");
csinders 1:fedb70ea0eaa 105 return -1;
csinders 2:b56010953f54 106 }
csinders 0:d5c236ffbd5e 107 // String array with all of the names of the different registers in order
csinders 1:fedb70ea0eaa 108 char regNames [37][20] = {
csinders 0:d5c236ffbd5e 109 "DEVID_AD", "DEVID_MST", "PARTID",
csinders 0:d5c236ffbd5e 110 "REVID", "XDATA", "YDATA",
csinders 0:d5c236ffbd5e 111 "ZDATA", "STATUS", "FIFO_ENTRIES_L",
csinders 0:d5c236ffbd5e 112 "FIFO_ENTRIES_H", "XDATA_L", "XDATA_H",
csinders 0:d5c236ffbd5e 113 "YDATA_L", "YDATA_H", "ZDATA_L",
csinders 0:d5c236ffbd5e 114 "ZDATA_H", "TEMP_L", "TEMP_H",
csinders 0:d5c236ffbd5e 115 "RESERVED", "RESERVED", "SOFT_RESET",
csinders 0:d5c236ffbd5e 116 "THRESH_ACT_L", "THRESH_ACT_H", "TIME_INACT_L",
csinders 0:d5c236ffbd5e 117 "TIME_ACT", "THRESH_INACT_L", "THRESH_INACT_H",
csinders 0:d5c236ffbd5e 118 "TIME_INACT_L", "TIME_INACT_H", "ACT_INACT_CTL",
csinders 0:d5c236ffbd5e 119 "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1",
csinders 0:d5c236ffbd5e 120 "INTMATP2", "FILTER_CTL", "POWER_CTL",
csinders 0:d5c236ffbd5e 121 "SELF_TEST"};
csinders 1:fedb70ea0eaa 122 // Example for first two registers
csinders 1:fedb70ea0eaa 123 // I think their is a better way to do this, but unsure of how.
csinders 1:fedb70ea0eaa 124 for (int i = 0; i < 2; i++) {
csinders 2:b56010953f54 125 uint8_t tmp = 0; // hex value location of the register
csinders 2:b56010953f54 126 uint8_t hexValue = 0; // Hex value of the register
csinders 1:fedb70ea0eaa 127 // May need to increment the registers inside of the if, since the values
csinders 1:fedb70ea0eaa 128 // are not always sequential
csinders 1:fedb70ea0eaa 129 // You can find registers inside of ADXL362.h file in ADXL362 folder
csinders 1:fedb70ea0eaa 130 // they are the ADXL362_register_t
csinders 1:fedb70ea0eaa 131 if (i == 0) {
csinders 1:fedb70ea0eaa 132 tmp = adxl362.read_reg(adxl362.DEVID_AD);
csinders 1:fedb70ea0eaa 133 hexValue = 0x00;
csinders 1:fedb70ea0eaa 134 } else if (i == 1) {
csinders 1:fedb70ea0eaa 135 tmp = adxl362.read_reg(adxl362.DEVID_MST);
csinders 1:fedb70ea0eaa 136 hexValue = 0x01;
csinders 1:fedb70ea0eaa 137 }
csinders 1:fedb70ea0eaa 138 pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[i], tmp); // Print register
csinders 1:fedb70ea0eaa 139 }
csinders 2:b56010953f54 140 return 0;
csinders 0:d5c236ffbd5e 141
csinders 0:d5c236ffbd5e 142 // below is github with data for ADXL362 methods
csinders 0:d5c236ffbd5e 143 //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp
csinders 0:d5c236ffbd5e 144 }