Code to find max and avg deviation, max swing, or simple readings from a still HMC6352 compass. Can be used with motors to test error from magnetic interference.
Dependencies: HMC6352 Motordriver mbed
Fork of HMC6352_HelloWorld by
Revision 2:ca1d3bef09a6, committed 2012-12-14
- Comitter:
- theschrade54
- Date:
- Fri Dec 14 05:05:35 2012 +0000
- Parent:
- 1:67f60f68719f
- Commit message:
- Code to find max and avg deviation, max swing, or simple readings from a still HMC6352 compass. Can be used with motors to test error from magnetic interference.
Changed in this revision
Motordriver.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motordriver.lib Fri Dec 14 05:05:35 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/littlexc/code/Motordriver/#3110b9209d3c
--- a/main.cpp Sat Nov 27 12:15:56 2010 +0000 +++ b/main.cpp Fri Dec 14 05:05:35 2012 +0000 @@ -1,21 +1,44 @@ -#include "HMC6352.h" - -HMC6352 compass(p9, p10); -Serial pc(USBTX, USBRX); - -int main() { - - pc.printf("Starting HMC6352 test...\n"); - - //Continuous mode, periodic set/reset, 20Hz measurement rate. - compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); - - while (1) { - - wait(0.1); - - pc.printf("Heading is: %f\n", compass.sample() / 10.0); - - } - -} +#include "mbed.h" +#include "motordriver.h" +#include "HMC6352.h" + +// test program to verify compass integrity when run on motor and to determine filtering values. uncomment/comment motor sections to test. +HMC6352 compass(p9, p10); +Serial pc(USBTX, USBRX); +//Motor left(p21, p22, p20, 1); // pwm, fwd, rev, has brake feature +//Motor right(p24, p25, p27, 1); + +int main() { + float avgdev, dev, maxdev, swing, prevval, curval, minval, maxval = 0.0; //variables to measure swing, maxdev, and avg dev of measurements + long count = 0; + //float speed = .1; + pc.printf("Starting HMC6352 test...\n"); + //left.speed(speed); + //right.speed(speed); + //Continuous mode, periodic set/reset, 20Hz measurement rate. + compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); + wait(1); + //init math vals + curval = compass.sample()/10.0; + minval = curval; + maxval = curval; + while (1) { + count++; // for avg + prevval = curval; + curval = compass.sample()/10.0; + dev = abs(curval - prevval); + if (curval > maxval) {maxval = curval;} + if (curval < minval) {minval = curval;} + if (dev > maxdev) {maxdev = dev;} //largest deviation between two successive readings. + avgdev += dev; //divide avg dev in print statement + swing = maxval - minval; //swing is largest reading change possible from remaining stationary. + if (count % 10 == 1) { + pc.printf("Heading is: %f, Max Dev: %f, Avg Dev: %f, Swing: %f\n", curval, maxdev, avgdev/count, swing); + //if (speed < .8) {speed += .05; + //left.speed(speed); + //right.speed(speed);} + } + wait(0.05); + } + +}