Serge Sozonoff
/
HMC6343_test
Test code for the HMC6343 tilt compensated compass
main.cpp@0:ee865ceb2360, 2011-05-01 (annotated)
- Committer:
- ssozonoff
- Date:
- Sun May 01 13:53:30 2011 +0000
- Revision:
- 0:ee865ceb2360
Basic test code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ssozonoff | 0:ee865ceb2360 | 1 | #include "mbed.h" |
ssozonoff | 0:ee865ceb2360 | 2 | #include "HMC6343.h" |
ssozonoff | 0:ee865ceb2360 | 3 | |
ssozonoff | 0:ee865ceb2360 | 4 | HMC6343 compass(p9, p10); |
ssozonoff | 0:ee865ceb2360 | 5 | Serial pc(USBTX, USBRX); |
ssozonoff | 0:ee865ceb2360 | 6 | |
ssozonoff | 0:ee865ceb2360 | 7 | Ticker t; |
ssozonoff | 0:ee865ceb2360 | 8 | Ticker t1; |
ssozonoff | 0:ee865ceb2360 | 9 | Heading heading; |
ssozonoff | 0:ee865ceb2360 | 10 | |
ssozonoff | 0:ee865ceb2360 | 11 | volatile bool display_values = true; |
ssozonoff | 0:ee865ceb2360 | 12 | volatile bool sample_sensor = true; |
ssozonoff | 0:ee865ceb2360 | 13 | |
ssozonoff | 0:ee865ceb2360 | 14 | void do_display_values() { |
ssozonoff | 0:ee865ceb2360 | 15 | display_values = true; |
ssozonoff | 0:ee865ceb2360 | 16 | } |
ssozonoff | 0:ee865ceb2360 | 17 | |
ssozonoff | 0:ee865ceb2360 | 18 | void do_sample_sensor() { sample_sensor = true; } |
ssozonoff | 0:ee865ceb2360 | 19 | |
ssozonoff | 0:ee865ceb2360 | 20 | int main() { |
ssozonoff | 0:ee865ceb2360 | 21 | pc.baud(115200); |
ssozonoff | 0:ee865ceb2360 | 22 | t.attach(&do_display_values, 1); |
ssozonoff | 0:ee865ceb2360 | 23 | t1.attach_us(&do_sample_sensor, 100 * 500); |
ssozonoff | 0:ee865ceb2360 | 24 | |
ssozonoff | 0:ee865ceb2360 | 25 | int opMode = compass.getOpMode(); |
ssozonoff | 0:ee865ceb2360 | 26 | printf("Op mode %d\r\n", opMode); |
ssozonoff | 0:ee865ceb2360 | 27 | opMode |= HMC6343_CM_MR_5HZ; |
ssozonoff | 0:ee865ceb2360 | 28 | |
ssozonoff | 0:ee865ceb2360 | 29 | //opMode &= ~HMC6343_STDBY; |
ssozonoff | 0:ee865ceb2360 | 30 | //opMode |= HMC6343_RUN; |
ssozonoff | 0:ee865ceb2360 | 31 | |
ssozonoff | 0:ee865ceb2360 | 32 | printf("Op mode %d\r\n", opMode); |
ssozonoff | 0:ee865ceb2360 | 33 | compass.setOpMode(opMode); |
ssozonoff | 0:ee865ceb2360 | 34 | |
ssozonoff | 0:ee865ceb2360 | 35 | printf("Op mode %d\r\n", compass.getOpMode()); |
ssozonoff | 0:ee865ceb2360 | 36 | |
ssozonoff | 0:ee865ceb2360 | 37 | printf("Compass software version %d\r\n", compass.getSoftwareVersion()); |
ssozonoff | 0:ee865ceb2360 | 38 | printf("Compass variation %f \r\n", compass.getMagneticVariation()); |
ssozonoff | 0:ee865ceb2360 | 39 | printf("Compass measurement rate %d\r\n", compass.getMeasurementRate()); |
ssozonoff | 0:ee865ceb2360 | 40 | |
ssozonoff | 0:ee865ceb2360 | 41 | //compass.setMagneticVariation(0.52); |
ssozonoff | 0:ee865ceb2360 | 42 | //printf("Compass variation %f \r\n", compass.getMagneticVariation()); |
ssozonoff | 0:ee865ceb2360 | 43 | |
ssozonoff | 0:ee865ceb2360 | 44 | printf("IIR Filter : %d\r\n", compass.isOpModeFlagSet(HMC6343_FILTER)); |
ssozonoff | 0:ee865ceb2360 | 45 | printf("IIR Filter bias : %d\r\n", compass.getIIRFilter()); |
ssozonoff | 0:ee865ceb2360 | 46 | |
ssozonoff | 0:ee865ceb2360 | 47 | //compass.setIIRFilter(4); |
ssozonoff | 0:ee865ceb2360 | 48 | //printf("IIR Filter bias : %d\r\n", compass.getIIRFilter()); |
ssozonoff | 0:ee865ceb2360 | 49 | |
ssozonoff | 0:ee865ceb2360 | 50 | // seems a restart is needed for this change to happen in the module |
ssozonoff | 0:ee865ceb2360 | 51 | // compass.setOpMode(opMode &= ~HMC6343_FILTER); |
ssozonoff | 0:ee865ceb2360 | 52 | // printf("IIR Filter : %d\r\n", compass.isOpModeFlagSet(HMC6343_FILTER)); |
ssozonoff | 0:ee865ceb2360 | 53 | |
ssozonoff | 0:ee865ceb2360 | 54 | compass.setReset(); |
ssozonoff | 0:ee865ceb2360 | 55 | |
ssozonoff | 0:ee865ceb2360 | 56 | while (1) { |
ssozonoff | 0:ee865ceb2360 | 57 | if (sample_sensor) { |
ssozonoff | 0:ee865ceb2360 | 58 | compass.sampleHeading(&heading); |
ssozonoff | 0:ee865ceb2360 | 59 | sample_sensor = false; |
ssozonoff | 0:ee865ceb2360 | 60 | } |
ssozonoff | 0:ee865ceb2360 | 61 | |
ssozonoff | 0:ee865ceb2360 | 62 | if (display_values) { |
ssozonoff | 0:ee865ceb2360 | 63 | pc.printf("bearing %3.1f \r\n", heading.heading); |
ssozonoff | 0:ee865ceb2360 | 64 | pc.printf("roll %f \r\n", heading.roll); |
ssozonoff | 0:ee865ceb2360 | 65 | pc.printf("pitch %f \r\n", heading.pitch); |
ssozonoff | 0:ee865ceb2360 | 66 | display_values = false; |
ssozonoff | 0:ee865ceb2360 | 67 | } |
ssozonoff | 0:ee865ceb2360 | 68 | } |
ssozonoff | 0:ee865ceb2360 | 69 | |
ssozonoff | 0:ee865ceb2360 | 70 | } |