First draft HMC5883 magnetometer sensor using physical quantities, outputting via serial port using std::cout on mbed os 5
main.cpp@4:53e468c49c17, 2020-03-24 (annotated)
- Committer:
- skyscraper
- Date:
- Tue Mar 24 23:21:11 2020 +0000
- Revision:
- 4:53e468c49c17
- Parent:
- 3:2834be4e10ef
- Child:
- 5:47026facc081
add adjusted values function to output adjusted data with offsets subtracted
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
skyscraper | 0:37dbfb036586 | 1 | |
skyscraper | 0:37dbfb036586 | 2 | #include "mbed.h" |
skyscraper | 2:9ffb2f18756b | 3 | #include "hmc5883.h" |
skyscraper | 0:37dbfb036586 | 4 | #include <quan/max.hpp> |
skyscraper | 0:37dbfb036586 | 5 | #include <quan/min.hpp> |
skyscraper | 3:2834be4e10ef | 6 | #include <quan/time.hpp> |
skyscraper | 3:2834be4e10ef | 7 | |
skyscraper | 4:53e468c49c17 | 8 | void mag_adjusted_values( |
skyscraper | 4:53e468c49c17 | 9 | quan::time::ms const & update_rate, |
skyscraper | 4:53e468c49c17 | 10 | quan::three_d::vect<quan::magnetic_flux_density::uT> const & offset |
skyscraper | 4:53e468c49c17 | 11 | ); |
skyscraper | 3:2834be4e10ef | 12 | void mag_raw_values(quan::time::ms const & update_rate); |
skyscraper | 3:2834be4e10ef | 13 | void mag_offset_calibrate(); |
skyscraper | 0:37dbfb036586 | 14 | |
skyscraper | 2:9ffb2f18756b | 15 | namespace { |
skyscraper | 2:9ffb2f18756b | 16 | |
skyscraper | 2:9ffb2f18756b | 17 | QUAN_QUANTITY_LITERAL(magnetic_flux_density,gauss); |
skyscraper | 2:9ffb2f18756b | 18 | QUAN_QUANTITY_LITERAL(magnetic_flux_density,milli_gauss); |
skyscraper | 2:9ffb2f18756b | 19 | QUAN_QUANTITY_LITERAL(magnetic_flux_density,uT); |
skyscraper | 3:2834be4e10ef | 20 | QUAN_QUANTITY_LITERAL(time,ms); |
skyscraper | 0:37dbfb036586 | 21 | |
skyscraper | 2:9ffb2f18756b | 22 | // terminal loop, printing message periodically |
skyscraper | 2:9ffb2f18756b | 23 | void loop_forever(std::string const & str) |
skyscraper | 2:9ffb2f18756b | 24 | { |
skyscraper | 3:2834be4e10ef | 25 | DigitalOut led2(LED2); |
skyscraper | 2:9ffb2f18756b | 26 | // stop but print error dynamically |
skyscraper | 2:9ffb2f18756b | 27 | int count = 0; |
skyscraper | 2:9ffb2f18756b | 28 | for (;;) { |
skyscraper | 2:9ffb2f18756b | 29 | led2 = 1; |
skyscraper | 2:9ffb2f18756b | 30 | std::cout << str << " " << count++ << '\n'; |
skyscraper | 2:9ffb2f18756b | 31 | ThisThread::sleep_for(200U); |
skyscraper | 2:9ffb2f18756b | 32 | led2 = 0; |
skyscraper | 2:9ffb2f18756b | 33 | ThisThread::sleep_for(800U); |
skyscraper | 2:9ffb2f18756b | 34 | } |
skyscraper | 0:37dbfb036586 | 35 | } |
skyscraper | 0:37dbfb036586 | 36 | }// namespace |
skyscraper | 0:37dbfb036586 | 37 | |
skyscraper | 0:37dbfb036586 | 38 | int main() |
skyscraper | 0:37dbfb036586 | 39 | { |
skyscraper | 0:37dbfb036586 | 40 | std::cout << "HMC5883 test\n"; |
skyscraper | 0:37dbfb036586 | 41 | //wait for mag to init |
skyscraper | 0:37dbfb036586 | 42 | ThisThread::sleep_for(500U); |
skyscraper | 0:37dbfb036586 | 43 | |
skyscraper | 3:2834be4e10ef | 44 | if ( mag_detected() ) { |
skyscraper | 0:37dbfb036586 | 45 | std::cout << "Detected a HMC5883\n"; |
skyscraper | 0:37dbfb036586 | 46 | } else { |
skyscraper | 0:37dbfb036586 | 47 | loop_forever("Failed to detect HMC5883"); |
skyscraper | 0:37dbfb036586 | 48 | } |
skyscraper | 0:37dbfb036586 | 49 | |
skyscraper | 0:37dbfb036586 | 50 | // N.b after offsets removed mag was reading around 33.6 uT, so not bad! |
skyscraper | 0:37dbfb036586 | 51 | constexpr auto earth_magnetic_field_flux_density = 31.869_uT; |
skyscraper | 3:2834be4e10ef | 52 | bool const success = |
skyscraper | 0:37dbfb036586 | 53 | mag_set_samples_average(8) && |
skyscraper | 0:37dbfb036586 | 54 | mag_set_range( earth_magnetic_field_flux_density * 2U); |
skyscraper | 0:37dbfb036586 | 55 | if ( !success) { |
skyscraper | 0:37dbfb036586 | 56 | loop_forever("HMC5883 setup failed"); |
skyscraper | 0:37dbfb036586 | 57 | } |
skyscraper | 3:2834be4e10ef | 58 | std::cout << " mag range set to " << mag_get_range() << '\n'; |
skyscraper | 2:9ffb2f18756b | 59 | |
skyscraper | 4:53e468c49c17 | 60 | // offsets from calib engine |
skyscraper | 4:53e468c49c17 | 61 | quan::three_d::vect< |
skyscraper | 4:53e468c49c17 | 62 | quan::magnetic_flux_density::uT |
skyscraper | 4:53e468c49c17 | 63 | > offsets{7.96466_uT,-8.10162_uT,1.50555_uT}; |
skyscraper | 4:53e468c49c17 | 64 | |
skyscraper | 4:53e468c49c17 | 65 | mag_adjusted_values(20_ms,offsets); |
skyscraper | 4:53e468c49c17 | 66 | |
skyscraper | 4:53e468c49c17 | 67 | // mag_raw_values(20_ms); |
skyscraper | 3:2834be4e10ef | 68 | |
skyscraper | 3:2834be4e10ef | 69 | // mag_offset_calibrate(); |
skyscraper | 3:2834be4e10ef | 70 | |
skyscraper | 0:37dbfb036586 | 71 | } |