{"id":510,"date":"2013-06-20T23:32:48","date_gmt":"2013-06-20T23:32:48","guid":{"rendered":"http:\/\/www.redrobe.com\/mike\/?p=510"},"modified":"2013-06-20T23:32:48","modified_gmt":"2013-06-20T23:32:48","slug":"xloborg-compass-accelerometer-for-the-raspberry-pi","status":"publish","type":"post","link":"https:\/\/redrobe.com\/mike\/xloborg-compass-accelerometer-for-the-raspberry-pi\/","title":{"rendered":"XloBorg &#8211; compass &#038; accelerometer for the Raspberry Pi"},"content":{"rendered":"<p> This week, <a href=\"http:\/\/piborg.com\/xloborg\">PiBorg<\/a> released XLoBorg &#8211; a 3 Axis Accelerometer and 3 Axis Magnetometer add-on for the Raspberry Pi<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/piborg.org\/sites\/default\/files\/styles\/medium\/public\/XLoBorg-top-690.png\" alt=\"XloBorg Compass and Accelerometer\" align=right\/><\/p>\n<p>Since its so new the current software only gives the raw magnetometer x y z values:<\/p>\n<p><code>mX = +00371, mY = -00053, mZ = +02539<\/code><\/p>\n<p>Magnetic flux strength values aren&#8217;t much use to us mere humans,<br \/>\nwhat we really want is a nice easy 0-360 degree heading value.<\/p>\n<p>Maths to the rescue ! <\/p>\n<p>We can find the angle from those &#8220;lengths&#8221; with arc tan Y\/X<\/p>\n<p><code>heading = math.atan2 (mY,mX)<\/code><br \/>\n(For simplicity I&#8217;m assuming the Pi is level&#8230; a more complicated method would include accelerometer data)<\/p>\n<p>Here&#8217;s my full python code to read the sensor, and convert to degrees:<\/p>\n<p><code>#!\/usr\/bin\/env python<\/p>\n<p># Load the XLoBorg library<br \/>\nimport XLoBorg<\/p>\n<p># Load maths library<br \/>\nimport math<\/p>\n<p># Tell the library to disable diagnostic printouts<br \/>\nXLoBorg.printFunction = XLoBorg.NoPrint<\/p>\n<p># Start the XLoBorg module (sets up devices)<br \/>\nXLoBorg.Init()<\/p>\n<p># Read and display the raw magnetometer readings<\/p>\n<p>mx,my,mz = XLoBorg.ReadCompassRaw()<\/p>\n<p>print 'mX = %+06d, mY = %+06d, mZ = %+06d' % XLoBorg.ReadCompassRaw()<\/p>\n<p># get the heading in radians<br \/>\nheading = math.atan2 (my,mx)<\/p>\n<p># Correct negative values<\/p>\n<p>if (heading < 0):\n        heading  = heading + (2 * math.pi)\n\n# convert to degrees\n\nheading = heading * 180\/math.pi;\n\nprint 'Heading: ', heading\n<\/code><\/p>\n<p>Much better:<\/p>\n<p><code>mX = +00371, mY = -00053, mZ = +02539<br \/>\nHeading: 351 degrees<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week, PiBorg released XLoBorg &#8211; a 3 Axis Accelerometer and 3 Axis Magnetometer add-on for the Raspberry Pi Since [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/comments?post=510"}],"version-history":[{"count":0,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/posts\/510\/revisions"}],"wp:attachment":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/media?parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/categories?post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/tags?post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}