{"id":796,"date":"2017-07-10T02:29:15","date_gmt":"2017-07-10T01:29:15","guid":{"rendered":"http:\/\/hirt.se\/blog\/?p=796"},"modified":"2017-07-10T03:11:16","modified_gmt":"2017-07-10T02:11:16","slug":"magnetometer-calibration-in-robo4j","status":"publish","type":"post","link":"https:\/\/hirt.se\/blog\/?p=796","title":{"rendered":"Magnetometer Calibration in Robo4J"},"content":{"rendered":"<p>This article will explain how to calibrate your magnetometer in Robo4J. This will be required if you want to get as precise results as possible from your magnetometer.<\/p>\n<p>Earth\u2019s magnetic field can be distorted in various ways. If there is no distortion, rotating your magnetometer 360 degrees around the&nbsp; Z-axis would result in a perfect circle (in the XY plane) centered around the origo. If there is distortion, the circle could be offset or even turned into a tilted ellipse.<\/p>\n<iframe loading=\"lazy\"  style=\"display: block; margin: 0px auto;\"  id=\"_ytid_58243\"  width=\"560\" height=\"315\"  data-origwidth=\"560\" data-origheight=\"315\" src=\"https:\/\/www.youtube.com\/embed\/gh8yP8cI_7E?enablejsapi=1&autoplay=1&cc_load_policy=0&cc_lang_pref=&iv_load_policy=1&loop=1&playlist=gh8yP8cI_7E&rel=1&fs=1&playsinline=0&autohide=2&theme=dark&color=red&controls=1&disablekb=0&\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe>\n<p>There are two different type of distortions commonly discussed:<\/p>\n<ol>\n<ol>\n<li>Hard iron\n<li>Soft iron<\/li>\n<\/ol>\n<\/ol>\n<p><strong>Hard iron <\/strong>effects are caused by a constant additive field, for example by a magnet. As long as the magnet stays put, relative to the magnetometer, the effect will be constant. This is what we call \u201cbias\u201d in Robo4J.<\/p>\n<p><strong>Soft iron <\/strong>effects are caused by materials that can influence the magnetic field, but which do not necessarily emit a magnetic field. For example iron and nickel. These effects are more complicated and typically turn that perfect circle (from the example above) into an ellipse.<\/p>\n<p>Now, since we are dealing with a 3D magnetometer here, we are really talking about trying to turn an ellipsoid at an offset to origo, into a sphere centered around origo.<\/p>\n<p>So, this is how to calibrate a magnetometer in Robo4J:<\/p>\n<ol>\n<ol>\n<li>Gather data from your magnetometer.\n<li>Calculate the bias vector and transform matrix.\n<li>Use the calculated bias vector and transformation matrix when instantiating your magnetometer.<\/li>\n<\/ol>\n<\/ol>\n<h2><\/h2>\n<h2>Getting Data<\/h2>\n<p>If you have an Adafruit 10 DOF breakout board, or an LSM303 device, you are in luck. This is the first magnetometer we support in Robo4J, but we will add more devices as we go. This blog will talk about how to do the calibration given this device.<\/p>\n<ol>\n<ol>\n<li>First, clone robo4j from <a href=\"https:\/\/github.com\/Robo4J\/robo4j\">github<\/a><\/li>\n<li>Next build and install robo4j:<br \/><font style=\"background-color: #ffffff\" face=\"Courier New\">.\/gradlew install\n<p><\/font><\/li>\n<li>Next source the script with the environment variables:<br \/><font style=\"background-color: #ffffff\" face=\"Courier New\">source \/scripts\/rpi\/environment.sh\n<p><\/font><\/li>\n<li>Then run the test program, with CSV as output format, and redirect the output to a file:<br \/><font face=\"Courier New\">java -cp $ROBO4J_PATH com.robo4j.hw.rpi.i2c.magnetometer.MagnetometerLSM303Test 20 1 CSV &gt; \/home\/pi\/magnetometer.csv<\/font><\/li>\n<\/ol>\n<\/ol>\n<p><font face=\"Courier New\"><\/font><\/p>\n<p>It is important that you try to hit as many points as possible, so rotate that thing like there is no tomorrow. I built a simple <a href=\"https:\/\/www.thingiverse.com\/thing:2377484\">calibration tool<\/a> that can help a little bit (but only with one axis). I have been thinking about building a massive one, using three slip rings and ball bearings, but since it would have to be large enough to accept a full sized robot to be truly useful, I know building one would put an unnecessary strain on my marriage. <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smilewithtongueout\" style=\"border-top-style: none;border-left-style: none;border-bottom-style: none;border-right-style: none\" alt=\"Smile with tongue out\" src=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2017\/07\/wlEmoticon-smilewithtongueout.png\"><\/p>\n<h2><\/h2>\n<h2>Calculate the Bias Vector and Transform Matrix<\/h2>\n<p>This sounds hard, but is rather easy, as we\u2019ve already added a very spiffy application to Robo4J for both visualizing your magnetometer data, and for calculating the bias vector and transform matrix. It is located in the robo4j-tools repo. Simply build and run it with your file as argument.<\/p>\n<p>This is what it will look like when you start it:<\/p>\n<p><a href=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2017\/07\/raw.png\"><img loading=\"lazy\" decoding=\"async\" title=\"raw\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;padding-right: 0px\" border=\"0\" alt=\"raw\" src=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2017\/07\/raw_thumb.png\" width=\"613\" height=\"484\"><\/a><\/p>\n<p>If you have some spurious weird values, you may want to filter out the ones that stand out the most. Simply flip the Filter panel open, and click Filter Points. By default the values within 1.0 standard deviation (range from calculated center) will be kept. You can play with the Stddev value to get the result you want.<\/p>\n<p>You can also show what the result would look like if corrected with the Bias Vector and Transformation Matrix, by clicking the \u201cCorrected\u201d checkbox in the Data Selection pane:<\/p>\n<p><a href=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2017\/07\/corrected.png\"><img loading=\"lazy\" decoding=\"async\" title=\"corrected\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;padding-right: 0px\" border=\"0\" alt=\"corrected\" src=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2017\/07\/corrected_thumb.png\" width=\"613\" height=\"484\"><\/a><\/p>\n<p>The visualization keep rotating so that the points will be shown alternatively from the XY and the ZY axis.<\/p>\n<h2>Using the Data When Instantiating the Magnetometer<\/h2>\n<p>In robo4j-hw-rpi the magnetometer can be instantiated with a Tuple3f, and a Matrix3f. Simply instantiate the tuple with the values from the bias vector, and the matrix with the values from the Transform Matrix:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">Tuple3f bias = new Tuple3f(-44.689f, -2.0665f, -15.240f);\r\nMatrix3f transform = new Matrix3f(1.887f, 5.987f, -5.709f, 5.987f, 1.528f, -2.960f, -5.709f, -2.960f, 9.761f);\r\nMagnetometerLSM303Device magnetometer = new MagnetometerLSM303Device(I2CBus.BUS_1, 0x1e, Mode.CONTINUOUS_CONVERSION, Rate.RATE_7_5, false, bias, transform);<\/pre>\n<h2>Summary<\/h2>\n<p>This blog explained how to use the Robo4J MagViz tool to calibrate a magnetometer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will explain how to calibrate your magnetometer in Robo4J. This will be required if you want to get as precise results as possible from your magnetometer. Earth\u2019s magnetic field can be distorted in various ways. If there is no distortion, rotating your magnetometer 360 degrees around the&nbsp; Z-axis would result in a perfect [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-java-mission-control-fair-dj"],"_links":{"self":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=796"}],"version-history":[{"count":10,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":808,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions\/808"}],"wp:attachment":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}