Production Test Program (PTP) for the LPC4088 Experiment Base Board
Dependencies: EALib I2S LM75B SDFileSystem mbed
TestAcc.cpp
00001 /* 00002 * Copyright 2013 Embedded Artists AB 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /****************************************************************************** 00018 * Includes 00019 *****************************************************************************/ 00020 00021 #include "mbed.h" 00022 #include "TestAcc.h" 00023 #include "MMA7455.h" 00024 00025 /****************************************************************************** 00026 * Defines and typedefs 00027 *****************************************************************************/ 00028 00029 #define ACC_XMIN (1<<0) 00030 #define ACC_XMAX (1<<1) 00031 #define ACC_YMIN (1<<2) 00032 #define ACC_YMAX (1<<3) 00033 #define ACC_ZMIN (1<<4) 00034 #define ACC_ZMAX (1<<5) 00035 00036 #define ACC_MIN_LIMIT (-50) 00037 #define ACC_MAX_LIMIT ( 50) 00038 00039 #define ACC_THRESHOLD (2) 00040 00041 /****************************************************************************** 00042 * Public Functions 00043 *****************************************************************************/ 00044 00045 /* 00046 Prerequisites: 00047 00048 - For this test to work jumpers JP8 and JP9 on the LPC4088 Experiment Base Board 00049 must both be in positions 1-2 00050 */ 00051 00052 bool TestAcc::runTest() { 00053 MMA7455 sensor(P0_27, P0_28); 00054 00055 // Initialize the accelerometer 00056 if (!sensor.setMode(MMA7455::ModeMeasurement)) { 00057 printf("Unable to set mode for MMA7455!\n"); 00058 return false; 00059 } 00060 00061 // Calibrate it. It does not matter if it is on a level surface 00062 // as this test is only interested in relative values. 00063 if (!sensor.calibrate()) { 00064 printf("Failed to calibrate MMA7455!\n"); 00065 return false; 00066 } 00067 00068 int val[3] = { 0, 0, 0}; 00069 int max[3] = {-1000,-1000,-1000}; 00070 int min[3] = { 1000, 1000, 1000}; 00071 int i; 00072 Timer t; 00073 t.start(); 00074 00075 // Read the x, y and z values every 100ms for up to five seconds. 00076 // When the accelerometer is working those values will sightly vary 00077 // even if the board is still. When a ACC_THRESHOLD difference has 00078 // been detected in each of the three axis the test is considered 00079 // successful. 00080 while (t.read() < 5) { 00081 if (!sensor.read(val[0], val[1], val[2])) { 00082 printf("Failed to read accelerometer data!\n"); 00083 return false; 00084 } 00085 printf("ACC: x,y,z = {%5d, %5d, %5d}\n", val[0], val[1], val[2]); 00086 for (i = 0; i < 3; i++) { 00087 if (val[i] < min[i]) { 00088 min[i] = val[i]; 00089 } 00090 if (val[i] > max[i]) { 00091 max[i] = val[i]; 00092 } 00093 } 00094 for (i = 0; i < 3; i++) { 00095 if ((max[i] - min[i]) < ACC_THRESHOLD) { 00096 break; 00097 } 00098 if (i == 2) { 00099 printf("All three axis work\n"); 00100 return true; 00101 } 00102 } 00103 wait(0.1); 00104 } 00105 printf("Accelerometer data invalid\n"); 00106 printf("Not enough variation X {%d..%d}, Y {%d..%d}, Z {%d..%d}!\n", 00107 min[0], max[0], min[1], max[1], min[2], max[2]); 00108 return false; 00109 } 00110 00111 bool TestAcc::alternativeTest() { 00112 MMA7455 sensor(P0_27, P0_28); 00113 00114 // Initialize the accelerometer 00115 if (!sensor.setMode(MMA7455::ModeMeasurement)) { 00116 printf("Unable to set mode for MMA7455!\n"); 00117 return false; 00118 } 00119 00120 // Calibrate it. The board must be on a flat surface 00121 // during calibration! 00122 if (!sensor.calibrate()) { 00123 printf("Failed to calibrate MMA7455!\n"); 00124 return false; 00125 } 00126 00127 printf("Now tilt the board in all directions (max 10 seconds)...\n"); 00128 int x=0, y=0, z=0; 00129 uint8_t done = 0; 00130 char msg[30] = {0}; 00131 00132 Timer t; 00133 t.start(); 00134 00135 // Read the x, y and z values every 100ms for up to ten seconds. 00136 // Lift and tilt the board along all axis and look at the printed 00137 // output. There are ACC_MIN_LIMIT and ACC_MAX_LIMIT limits and 00138 // as the value of an axis passes that limit it is printed and 00139 // if all six limits (min/max for x,y and z) are passed within the 00140 // time limit the test is considered successful. 00141 while ((t.read() < 10) && (done != 0x3f)) { 00142 if (!sensor.read(x, y, z)) { 00143 printf("Failed to read accelerometer data!\n"); 00144 return false; 00145 } 00146 printf("ACC: x,y,z = {%5d, %5d, %5d} %s\n", x, y, z, msg); 00147 if ((x < ACC_MIN_LIMIT) && !(done & ACC_XMIN)) { 00148 done |= ACC_XMIN; 00149 printf("Tilted XMIN\n"); 00150 } else if ((x > ACC_MAX_LIMIT) && !(done & ACC_XMAX)) { 00151 done |= ACC_XMAX; 00152 printf("Tilted XMAX\n"); 00153 } 00154 if ((y < ACC_MIN_LIMIT) && !(done & ACC_YMIN)) { 00155 done |= ACC_YMIN; 00156 printf("Tilted YMIN\n"); 00157 } else if ((y > ACC_MAX_LIMIT) && !(done & ACC_YMAX)) { 00158 done |= ACC_YMAX; 00159 printf("Tilted XMAX\n"); 00160 } 00161 if ((z < ACC_MIN_LIMIT) && !(done & ACC_ZMIN)) { 00162 done |= ACC_ZMIN; 00163 printf("Tilted ZMIN\n"); 00164 } else if ((z > ACC_MAX_LIMIT) && !(done & ACC_ZMAX)) { 00165 done |= ACC_ZMAX; 00166 printf("Tilted ZMAX\n"); 00167 } 00168 wait(0.1); 00169 } 00170 printf("Done with ACC tests!\n"); 00171 return (t.read() < 10); 00172 } 00173
Generated on Sun Jul 17 2022 08:29:10 by 1.7.2