Unit testing and development for 9DOF sparkfun sensor stick
Dependencies: ADXL345 HMC5883L ITG3200 mbed
main.cpp@3:5e21a352e236, 2012-11-05 (annotated)
- Committer:
- tylerjw
- Date:
- Mon Nov 05 18:35:42 2012 +0000
- Revision:
- 3:5e21a352e236
- Parent:
- 2:d7e66940541d
- Child:
- 4:8a77e21d08f1
Working adxl built in test and calibration test in adxl345unit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerjw | 0:ac2f55940442 | 1 | /** |
tylerjw | 2:d7e66940541d | 2 | 9-dof unit testing |
tylerjw | 0:ac2f55940442 | 3 | |
tylerjw | 0:ac2f55940442 | 4 | The purpose of this program is to demonstrate and calibrate the three |
tylerjw | 0:ac2f55940442 | 5 | sensors on teh 9-doft board. |
tylerjw | 0:ac2f55940442 | 6 | |
tylerjw | 0:ac2f55940442 | 7 | The first version of this software will test the ITG3200 gyro to find |
tylerjw | 0:ac2f55940442 | 8 | the temperature drift line. Data will be logged to help determine the |
tylerjw | 0:ac2f55940442 | 9 | thermal drift line. |
tylerjw | 0:ac2f55940442 | 10 | See: http://mbed.org/users/gltest26/code/ITG3200/wiki/Thermal-Drift |
tylerjw | 1:dc730a26cdc2 | 11 | |
tylerjw | 1:dc730a26cdc2 | 12 | The second versoin of this will test the HMC5883L 3 axis magnometer |
tylerjw | 2:d7e66940541d | 13 | |
tylerjw | 2:d7e66940541d | 14 | The thrid version tests the ADXL345 library. |
tylerjw | 0:ac2f55940442 | 15 | */ |
tylerjw | 0:ac2f55940442 | 16 | |
tylerjw | 0:ac2f55940442 | 17 | #include "mbed.h" |
tylerjw | 1:dc730a26cdc2 | 18 | //#include "ITG3200.h" |
tylerjw | 2:d7e66940541d | 19 | //#include "HMC5883L.h" |
tylerjw | 3:5e21a352e236 | 20 | #include "adxl345unit.h" |
tylerjw | 1:dc730a26cdc2 | 21 | |
tylerjw | 2:d7e66940541d | 22 | I2C i2c_bus(p28, p27); |
tylerjw | 3:5e21a352e236 | 23 | ADXL345 accel(i2c_bus); |
tylerjw | 3:5e21a352e236 | 24 | Timer t; |
tylerjw | 3:5e21a352e236 | 25 | Serial pc(USBTX, USBRX); |
tylerjw | 3:5e21a352e236 | 26 | //ADXL345UNIT accel_test(i2c_bus, t); |
tylerjw | 0:ac2f55940442 | 27 | |
tylerjw | 0:ac2f55940442 | 28 | int main() |
tylerjw | 0:ac2f55940442 | 29 | { |
tylerjw | 3:5e21a352e236 | 30 | t.start(); |
tylerjw | 3:5e21a352e236 | 31 | pc.baud(9600); |
tylerjw | 3:5e21a352e236 | 32 | accel.calibrate(&t, true); |
tylerjw | 3:5e21a352e236 | 33 | //accel_test.builtInSelfTest(true); |
tylerjw | 3:5e21a352e236 | 34 | //accel_test.offsetCalibration(true); |
tylerjw | 3:5e21a352e236 | 35 | /* |
tylerjw | 3:5e21a352e236 | 36 | int sample[3]; |
tylerjw | 3:5e21a352e236 | 37 | float elapsed_time; |
tylerjw | 3:5e21a352e236 | 38 | float time_stamp[100]; |
tylerjw | 3:5e21a352e236 | 39 | int samples[100][3]; |
tylerjw | 2:d7e66940541d | 40 | |
tylerjw | 3:5e21a352e236 | 41 | Timer t; |
tylerjw | 3:5e21a352e236 | 42 | Serial pc(USBTX, USBRX); |
tylerjw | 3:5e21a352e236 | 43 | pc.baud(9600); |
tylerjw | 3:5e21a352e236 | 44 | |
tylerjw | 3:5e21a352e236 | 45 | accel.setDataRate(ADXL345_100HZ); // 100Hz data rate |
tylerjw | 3:5e21a352e236 | 46 | accel.setDataFormatControl(ADXL345_16G | ADXL345_FULL_RES); |
tylerjw | 3:5e21a352e236 | 47 | accel.setPowerControl(0x08); // start measurement |
tylerjw | 3:5e21a352e236 | 48 | |
tylerjw | 3:5e21a352e236 | 49 | wait(0.001); |
tylerjw | 3:5e21a352e236 | 50 | |
tylerjw | 3:5e21a352e236 | 51 | t.start(); |
tylerjw | 3:5e21a352e236 | 52 | accel.getOutput(sample); |
tylerjw | 3:5e21a352e236 | 53 | elapsed_time = t.read(); |
tylerjw | 3:5e21a352e236 | 54 | |
tylerjw | 3:5e21a352e236 | 55 | pc.printf("Sample took %f seconds.\r\n", elapsed_time); |
tylerjw | 3:5e21a352e236 | 56 | pc.printf("x: %d, y: %d, z: %d\r\n", sample[0],sample[1],sample[2]); |
tylerjw | 3:5e21a352e236 | 57 | |
tylerjw | 3:5e21a352e236 | 58 | for(int i = 0; i < 100; i++) { |
tylerjw | 3:5e21a352e236 | 59 | t.reset(); |
tylerjw | 3:5e21a352e236 | 60 | accel.getOutput(samples[i]); |
tylerjw | 3:5e21a352e236 | 61 | time_stamp[i] = t.read(); |
tylerjw | 3:5e21a352e236 | 62 | } |
tylerjw | 3:5e21a352e236 | 63 | for(int i = 0; i < 100; i++) { |
tylerjw | 3:5e21a352e236 | 64 | pc.printf("Sample took %f seconds.\r\n", time_stamp[i]); |
tylerjw | 3:5e21a352e236 | 65 | pc.printf("x: %d, y: %d, z: %d\r\n", samples[i][0],samples[i][1],samples[i][2]); |
tylerjw | 3:5e21a352e236 | 66 | } |
tylerjw | 3:5e21a352e236 | 67 | */ |
tylerjw | 2:d7e66940541d | 68 | } |
tylerjw | 2:d7e66940541d | 69 | |
tylerjw | 2:d7e66940541d | 70 | /* |
tylerjw | 2:d7e66940541d | 71 | void hmc5883l_test() |
tylerjw | 2:d7e66940541d | 72 | { |
tylerjw | 1:dc730a26cdc2 | 73 | Serial pc(USBTX, USBRX); |
tylerjw | 2:d7e66940541d | 74 | |
tylerjw | 1:dc730a26cdc2 | 75 | pc.baud(9600); |
tylerjw | 3:5e21a352e236 | 76 | |
tylerjw | 2:d7e66940541d | 77 | HMC5883L compass(i2c_bus); |
tylerjw | 2:d7e66940541d | 78 | |
tylerjw | 1:dc730a26cdc2 | 79 | int data[3]; |
tylerjw | 2:d7e66940541d | 80 | |
tylerjw | 2:d7e66940541d | 81 | while(1) { |
tylerjw | 2:d7e66940541d | 82 | compass.getXYZ(data); |
tylerjw | 2:d7e66940541d | 83 | pc.printf("x: %4d, y: %4d, z: %4d\r\n", data[0], data[1], data[2]); |
tylerjw | 1:dc730a26cdc2 | 84 | wait(0.067); |
tylerjw | 1:dc730a26cdc2 | 85 | } |
tylerjw | 1:dc730a26cdc2 | 86 | } |
tylerjw | 2:d7e66940541d | 87 | */ |
tylerjw | 1:dc730a26cdc2 | 88 | /* |
tylerjw | 1:dc730a26cdc2 | 89 | void itg3200_test() |
tylerjw | 1:dc730a26cdc2 | 90 | { |
tylerjw | 1:dc730a26cdc2 | 91 | DigitalOut myled(LED1); |
tylerjw | 0:ac2f55940442 | 92 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
tylerjw | 0:ac2f55940442 | 93 | ITG3200 gyro(p28, p27); // sda, scl - gyro |
tylerjw | 1:dc730a26cdc2 | 94 | const float offset[3] = {99.5, -45.0, -29.7}; // taken from itg3200.xls curve fit test |
tylerjw | 1:dc730a26cdc2 | 95 | const float slope[3] = {-1.05, 0.95, 0.47}; |
tylerjw | 1:dc730a26cdc2 | 96 | |
tylerjw | 1:dc730a26cdc2 | 97 | gyro.setCalibrationCurve(offset, slope); |
tylerjw | 1:dc730a26cdc2 | 98 | gyro.setLpBandwidth(LPFBW_5HZ); // lowest rate low-pass filter |
tylerjw | 1:dc730a26cdc2 | 99 | |
tylerjw | 0:ac2f55940442 | 100 | Serial pc(USBTX, USBRX); |
tylerjw | 0:ac2f55940442 | 101 | |
tylerjw | 0:ac2f55940442 | 102 | pc.baud(9600); |
tylerjw | 0:ac2f55940442 | 103 | |
tylerjw | 0:ac2f55940442 | 104 | myled = 0; |
tylerjw | 0:ac2f55940442 | 105 | FILE *fp = fopen("/local/itg3200.csv", "w"); // Open "itg3200.csv" for writing |
tylerjw | 0:ac2f55940442 | 106 | fputs("Temp, X, Y, Z\r\n", fp); // place the header at the top |
tylerjw | 0:ac2f55940442 | 107 | |
tylerjw | 0:ac2f55940442 | 108 | float temperature = 0.0; |
tylerjw | 0:ac2f55940442 | 109 | int gyro_readings[3]; |
tylerjw | 0:ac2f55940442 | 110 | |
tylerjw | 0:ac2f55940442 | 111 | for(int i = 0; i < 120; i++) { // 120 seconds - 600 samples |
tylerjw | 0:ac2f55940442 | 112 | myled = 1; |
tylerjw | 1:dc730a26cdc2 | 113 | //gyro.calibrate(1.0); |
tylerjw | 1:dc730a26cdc2 | 114 | wait(0.5); |
tylerjw | 0:ac2f55940442 | 115 | myled = 0; |
tylerjw | 1:dc730a26cdc2 | 116 | gyro.getGyroXYZ(gyro_readings, ITG3200::Calibration); |
tylerjw | 1:dc730a26cdc2 | 117 | //gyro.getOffset(gyro_readings); |
tylerjw | 0:ac2f55940442 | 118 | temperature = gyro.getTemperature(); |
tylerjw | 0:ac2f55940442 | 119 | pc.printf("%3d,%f,%d,%d,%d\r\n",i,temperature,gyro_readings[0],gyro_readings[1],gyro_readings[2]); |
tylerjw | 0:ac2f55940442 | 120 | fprintf(fp, "%f,%d,%d,%d\r\n",temperature,gyro_readings[0],gyro_readings[1],gyro_readings[2]); |
tylerjw | 0:ac2f55940442 | 121 | } |
tylerjw | 0:ac2f55940442 | 122 | fclose(fp); |
tylerjw | 0:ac2f55940442 | 123 | myled = 0; |
tylerjw | 0:ac2f55940442 | 124 | } |
tylerjw | 1:dc730a26cdc2 | 125 | */ |