Unit testing and development for 9DOF sparkfun sensor stick
Dependencies: ADXL345 HMC5883L ITG3200 mbed
Revision 1:dc730a26cdc2, committed 2012-10-30
- Comitter:
- tylerjw
- Date:
- Tue Oct 30 23:47:14 2012 +0000
- Parent:
- 0:ac2f55940442
- Child:
- 2:d7e66940541d
- Commit message:
- HMC5883L prof of concept
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Oct 30 17:30:03 2012 +0000 +++ b/main.cpp Tue Oct 30 23:47:14 2012 +0000 @@ -8,23 +8,161 @@ the temperature drift line. Data will be logged to help determine the thermal drift line. See: http://mbed.org/users/gltest26/code/ITG3200/wiki/Thermal-Drift + + The second versoin of this will test the HMC5883L 3 axis magnometer */ #include "mbed.h" -#include "ITG3200.h" +//#include "ITG3200.h" + +// configuration register a +#define AVG1_SAMPLES 0x00 +#define AVG2_SAMPLES 0x20 +#define AVG4_SAMPLES 0x80 +#define AVG8_SAMPLES 0xC0 + +#define OUTPUT_RATE_0_75 0x00 +#define OUTPUT_RATE_1_5 0x04 +#define OUTPUT_RATE_3 0x08 +#define OUTPUT_RATE_7_5 0x0C +#define OUTPUT_RATE_15 0x10 +#define OUTPUT_RATE_30 0x14 +#define OUTPUT_RATE_75 0x18 + +#define NORMAL_MEASUREMENT 0x00 +#define POSITIVE_BIAS 0x01 +#define NEGATIVE_BIAS 0x02 + +// mode register +#define CONTINUOUS_MODE 0x00 +#define SINGLE_MODE 0x01 +#define IDLE_MODE 0x02 + +// status register +#define LOCK 0x02 +#define READY 0x01 + +char addr = 0x3D; + +I2C i2c_(p28, p27); // sda, scl + +void setConfigurationA(char,char,char); +char getConfigurationA(); +void setConfigurationB(char); +char getConfigurationB(); +void setMode(char); +char getMode(); +void getXYZ(int*); +char getStatus(); + +void setConfigurationA(char averaged_samples = AVG1_SAMPLES, char output_rate = OUTPUT_RATE_15, char measurement_mode = NORMAL_MEASUREMENT) +{ + char cmd[2]; + cmd[0] = 0x00; // register a address + cmd[1] = averaged_samples | output_rate | measurement_mode; + + i2c_.write(addr, cmd, 2); +} + +char getConfigurationA() +{ + char cmd[2]; + cmd[0] = 0x00; // register a address + i2c_.write(addr, cmd, 1, true); + i2c_.read(addr, &cmd[1], 1, false); + return cmd[1]; +} + +void setConfigurationB(char gain = 0x20) // default value 0x20 +{ + char cmd[2]; + cmd[0] = 0x01; + cmd[1] = gain; + + i2c_.write(addr, cmd, 2); +} + +char getConfigurationB() +{ + char cmd[2]; + cmd[0] = 0x01; // register b address + i2c_.write(addr, cmd, 1, true); + i2c_.read(addr, &cmd[1], 1, false); + return cmd[1]; +} + +void setMode(char mode = SINGLE_MODE) +{ + char cmd[2]; + cmd[0] = 0x02; // mode register address + cmd[1] = mode; + i2c_.write(addr,cmd,2); +} + +char getMode() +{ + char cmd[2]; + cmd[0] = 0x02; // mode register address + i2c_.write(addr, cmd, 1, true); + i2c_.read(addr, &cmd[1], 1, false); + return cmd[1]; +} + +void getXYZ(int output[3]) +{ + char cmd[2]; + char data[6]; + cmd[0] = 0x03; // starting point for reading + i2c_.write(addr, cmd, 1, true); // set the pointer to the start of x + i2c_.read(addr, data, 6, false); + + for(int i = 0; i < 3; i++) // fill the output variables + output[i] = (data[i*2] << 8) + data[i*2+1]; +} + +char getStatus() +{ + char cmd[2]; + cmd[0] = 0x09; // status register address + i2c_.write(addr, cmd, 1, true); + i2c_.read(addr, &cmd[1], 1, false); + return cmd[1]; +} int main() { - DigitalOut myled(p24); + Serial pc(USBTX, USBRX); + + pc.baud(9600); + + int data[3]; + + // configure + setConfigurationA(AVG8_SAMPLES); // 8 sample average, 15Hz, normal mode + setConfigurationB(); // default (0x20) + setMode(CONTINUOUS_MODE); // continuous sample mode + wait(0.006); // 6 milisecond pause to allow registers to fill up + + while(1) + { + getXYZ(data); + pc.printf("x: 0x%4x, y: 0x%4x, z: 0x%4x\r\n", data[0], data[1], data[2]); + wait(0.067); + } +} + +/* +void itg3200_test() +{ + DigitalOut myled(LED1); LocalFileSystem local("local"); // Create the local filesystem under the name "local" ITG3200 gyro(p28, p27); // sda, scl - gyro - //const float offset[3] = {99.53, -45.26, -29.53}; // taken from itg3200_05.xls curve fit - //const float slope[3] = {-0.95, 0.95, 0.47}; - - //gyro.setCalibrationCurve(offset, slope); - //gyro.calibrate(1.0); - gyro.setLpBandwidth(LPFBW_5HZ); - + const float offset[3] = {99.5, -45.0, -29.7}; // taken from itg3200.xls curve fit test + const float slope[3] = {-1.05, 0.95, 0.47}; + + gyro.setCalibrationCurve(offset, slope); + gyro.setLpBandwidth(LPFBW_5HZ); // lowest rate low-pass filter + Serial pc(USBTX, USBRX); pc.baud(9600); @@ -38,11 +176,11 @@ for(int i = 0; i < 120; i++) { // 120 seconds - 600 samples myled = 1; - gyro.calibrate(1.0); - //wait(0.5); + //gyro.calibrate(1.0); + wait(0.5); myled = 0; - //gyro.getGyroXYZ(gyro_readings, ITG3200::Calibration); - gyro.getOffset(gyro_readings); + gyro.getGyroXYZ(gyro_readings, ITG3200::Calibration); + //gyro.getOffset(gyro_readings); temperature = gyro.getTemperature(); pc.printf("%3d,%f,%d,%d,%d\r\n",i,temperature,gyro_readings[0],gyro_readings[1],gyro_readings[2]); fprintf(fp, "%f,%d,%d,%d\r\n",temperature,gyro_readings[0],gyro_readings[1],gyro_readings[2]); @@ -50,3 +188,4 @@ fclose(fp); myled = 0; } +*/ \ No newline at end of file