{"id":695,"date":"2014-09-08T08:00:36","date_gmt":"2014-09-08T08:00:36","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=695"},"modified":"2014-09-08T09:18:22","modified_gmt":"2014-09-08T09:18:22","slug":"turn-to-compass-heading-using-mpu-9150","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/09\/turn-to-compass-heading-using-mpu-9150\/","title":{"rendered":"Turning to a Heading with an MPU-9150"},"content":{"rendered":"<p>As the odour of fried electronics dissipates in the air, I&#8217;m unexpectedly afforded the opportunity to write this blog post a day or two earlier than planned. The on-board compass was exhibiting significant deviation, so I consulted my nephew <a title=\"Our Family Rocket Scientist\" href=\"http:\/\/www.dtu.dk\/Service\/Telefonbog\/Person?id=17566\">Thorbj\u00f8rn Christensen at DTU-Space<\/a>. Thorbj\u00f8rn makes a living designing magnetometers for space agencies around the world, and he suggested I use a ribbon cable to\u00a0move the IMU away from the bot as a first step towards understanding the issue. He did warn me to be <em><strong>very<\/strong><\/em> careful when attaching it. I still don&#8217;t understand what I did wrong, but the evidence that I screwed up is pretty definitive: there are burn marks around every single connector and the IMU chip looks a bit soft in the middle. Most importantly, it no longer reports for duty on the Raspberry Pi&#8217;s I2C bus!<\/p>\n<div id=\"attachment_699\" style=\"width: 592px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/fried9150.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-699\" class=\"size-full wp-image-699\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/fried9150-thumb.jpg\" alt=\"A fried IMU-9150 \" width=\"582\" height=\"299\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/fried9150-thumb.jpg 582w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/fried9150-thumb-300x154.jpg 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/fried9150-thumb-500x256.jpg 500w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/a><p id=\"caption-attachment-699\" class=\"wp-caption-text\">A fried IMU-9150 (click to enlarge)<\/p><\/div>\n<p>The good news (apart from the fact that the Raspberry Pi, the Arduino and all other &#8216;bot components seem to have survived) is perhaps that, since I cannot play with the compass until I have a replacement, I found the time to write about where I am at the moment&#8230;and take some time out to work on my presentations for <a title=\"Dyalog'14 in Eastbourne, UK\" href=\"http:\/\/dyalog.com\/user-meetings\/dyalog14.htm\">Dyalog &#8217;14<\/a> (for those who managed to register before the user meeting in Eastbourne sold out, I still hope to do a &#8216;bot presentation on the evening of Tuesday September 23rd).<\/p>\n<h2>Introducing the MPU-9150<\/h2>\n<p>For the last few weeks, I have been using my &#8220;bot time&#8221; to play with the <a title=\"MPU-9150 Breakout from SparkFun\" href=\"https:\/\/www.sparkfun.com\/products\/11486\" target=\"_blank\">MPU-9150 breakout board<\/a>\u00a0that is attached to our &#8216;bot. The MPU-9150 is more or less identical to the 6050 that we were using\u00a0<a title=\"Making Controlled Turns with the DyaBot\" href=\"http:\/\/www.dyalog.com\/blog\/2014\/08\/making-controlled-turns-with-the-dyabot\/\" target=\"_blank\">earlier this summer to make gyro-controlled turns<\/a> but also includes a magnetic compass, which will allow us to know the direction that we are heading in \u2013 making it a 9 degree of freedom inertial measurement unit, or &#8220;9-dof IMU&#8221; (9 = 3 gyro axes + 3 accelerometer axes\u00a0+ 3 compass axes).<\/p>\n<h2>RTIMULib<\/h2>\n<p>I was extremely happy to discover\u00a0that <a title=\"Richard Barnett on LinkedIn\" href=\"https:\/\/www.linkedin.com\/in\/richardbarnettus\" target=\"_blank\">Richard Barnett<\/a> had\u00a0<a title=\"RTIMULib on GitHub\" href=\"https:\/\/github.com\/richards-tech\/RTIMULib\" target=\"_blank\">shared his RTIMULib on GitHub<\/a>. RTIMULib\u00a0is a Linux library\u00a0that produces &#8220;Kalman-filtered quaternion or Euler angle pose data&#8221; based on data from a 9-dof IMU. Jay Foad at Dyalog was quickly able to provide me with a couple of additional C functions designed to be easy to call from Dyalog APL. <a title=\"RTIMULib for Dyalog APL on GitHub\" href=\"https:\/\/github.com\/jayfoad\/RTIMULib\" target=\"_blank\">Jay&#8217;s fork of RTIMULib is also available on GitHub<\/a>. Since we can assume that we are driving on a flat surface, I have just been using two items from the wealth of information provided by this library: the current rate of rotation and the current compass direction \u2013 in both cases around the vertical (or &#8220;z&#8221;) axis.<\/p>\n<h2>My First Compass-Controlled Rotation<\/h2>\n<p>Due to the fried IMU, I am unable to post a video recording; fortunately, at the moment where I toasted the chip, the Dyalog session output still contained output data from the last run of the function I am working on, which aims to provide smooth rotation to a selected compass heading. In the chart below, the red line tracks the speed of rotation and shows that the &#8220;smooth&#8221; part still needs work \u2013 the speed oscillates quite violently between 60 and 150 degrees per second.\u00a0The blue line shows the number of degrees that the &#8216;bot still needs to turn to reach the desired heading. The dotted red line is slightly mislabeled: rather than acceleration, it actually shows the power applied to the motors via a digital-to-analog converter that accepts values between 0 and 255, producing between 0 and 5v of voltage for the DC motors:<\/p>\n<p><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/RotatingToHeading.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-701\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/RotatingToHeading.png\" alt=\"Rotating to a compass heading\" width=\"827\" height=\"606\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/RotatingToHeading.png 827w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/RotatingToHeading-300x219.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/09\/RotatingToHeading-409x300.png 409w\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" \/><\/a><\/p>\n<p>Commentary:<\/p>\n<ul>\n<li><strong>From 0 to 0.4 seconds<\/strong>, we slowly increase power until we detect that rotation has started. We note the power setting required to start rotation.<\/li>\n<li><strong>0.4 to 0.7<\/strong> (ish), we detect acceleration and hold the throttle steady (increasing it very slightly a couple of times when acceleration appears to stop).<\/li>\n<li><strong>0.7 to 1.1<\/strong> seconds, cruise mode: whenever speed is above the target of 100 degrees\/sec, we idle. When speed is below 100, we re-apply power at a level that was sufficient to start the rotation.<\/li>\n<li><strong>1.1 to 1.4<\/strong> seconds: We are less than 30 degrees from target, and the target velocity and thus power is reduced proportional to the remaining distance (at 15 degrees, we are down to 50 deg\/sec)<\/li>\n<li><strong>1.4 to 1.6<\/strong> seconds: We detect that we rotated too far, and slam on the brakes, coming to a stop about 10 degrees from target.<\/li>\n<li><strong>1.6 to 2.3<\/strong>\u00a0seconds: since we are less than 10 degrees from target, we enter &#8220;step&#8221; mode, giving very brief bursts of power at &#8220;start rotation&#8221; level, idling, and watching the heading until we reach the goal.<\/li>\n<li><strong>2.4 seconds<\/strong>: We count the bursts and, after 10, we\u00a0double the power setting to avoid getting bogged down (you cannot see the bursts in the chart, it only records the power level used for each one). A little more patience would have been good, this probably means we overshot by a bit.<\/li>\n<\/ul>\n<h2>The &#8220;Heading&#8221; Function<\/h2>\n<p>Achieving anything resembling smooth movement is still some way away, mostly due to the fact that motor response to a given input voltage varies enormously from motor to motor and between different applications to the same motor. The strategy described above is implemented by a function named &#8220;Heading&#8221;, which can be found in the file\u00a0<a title=\"Code for driving the DyaBot - on GitHub\" href=\"https:\/\/github.com\/APLPi\/APLBot\" target=\"_blank\">mpu9150.dyalog in the APLBot folder on GitHub<\/a>. An obvious improvement would be to have the robot self-calibrate itself somehow and add a model of how fast rotation speeds up and slows down (based on, and constantly adjusted with, observed data), in order to dampen the oscillations. I intend to return to this when I have a new IMU (and my other user meeting presentations are under control).<\/p>\n<p>This has turned out to be a lot harder than I imagined: suggestions are very welcome \u2013 please leave comments if you have a good idea!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As the odour of fried electronics dissipates in the air, I&#8217;m unexpectedly afforded the opportunity to write this blog post a day or two earlier than planned. The on-board compass was exhibiting significant deviation, so I consulted my nephew Thorbj\u00f8rn &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/09\/turn-to-compass-heading-using-mpu-9150\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12,9,5],"tags":[],"class_list":["post-695","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cto","category-raspberry-pi","category-robots"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=695"}],"version-history":[{"count":39,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":738,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/695\/revisions\/738"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}