{"id":109,"date":"2012-07-24T21:37:00","date_gmt":"2012-07-25T01:37:00","guid":{"rendered":"http:\/\/www.mcgurrin.com\/robots\/?p=109"},"modified":"2012-07-24T21:37:00","modified_gmt":"2012-07-25T01:37:00","slug":"compass-trials-and-tribulations","status":"publish","type":"post","link":"https:\/\/www.mcgurrin.info\/robots\/109\/","title":{"rendered":"Compass Trials and Tribulations"},"content":{"rendered":"<p>I finally got back to working on my 1st robotic vehicle, and it&#8217;s been 1 step forward, two steps back, with one of the back steps being self-inflicted.\u00a0 My first go at a robotic vehicle used only wheel encoders for dead reckoning, working alright for measuring distance traveled, but suffering the well known lack of both precision and accuracy in heading.\u00a0 So for determining heading, I decided to add an electronic compass, in particular the <a href=\"http:\/\/www.robot-electronics.co.uk\/htm\/cmps10doc.htm\">Devantech CMPS10<\/a> tilt-compensated compass.\u00a0 I don&#8217;t really need the tilt compensation, since I&#8217;m running over flat ground and flooring, but I figured I may want to reuse the compass later on another project and it&#8217;s inexpensive for a tilt-compensated compass.<\/p>\n<p>So, I add the compass and run a simple test program.\u00a0 First issue is that there&#8217;s clearly interference from the metal, motors, and\/or electronics.\u00a0 So I&#8217;ve mounted in on an aluminum mast, and that seems to clear things up.\u00a0 One problem down.<\/p>\n<p>Next I modified my code and went into a several hour debugging nightmare.\u00a0 As is often the case, the bug is obvious in hindsight, with the symptoms pointing to it.\u00a0 My robot ran forward the set distance, then, when it should have turned left, it spun right in endless circles, bringing to mind the Tommy Roe song Dizzy for those of us of a certain age. In debugging, I notice that while the simple compass reading test program works fine, when I load the full robot code, the bearing jumps in large discrete increments of about 22 degrees.\u00a0 Curious, obviously a clue, but I couldn&#8217;t figure out what it meant.\u00a0 Only after a couple hours of staring at code and trying small incremental changes did I notice the problem. Where I should have typed: lowerByte = compass.read(), I had instead typed lowerByte &#8211; compass.read().\u00a0 The higher precision result from the compass is sent in two bytes, and I was never actually setting the lowerByte value, resulting in the large discrete jumps.\u00a0 One self-inflicted problem solved.<\/p>\n<p>Why did the robot turn right instead of left? Either the code was erroneously jumping to the middle of the obstacle avoidance routine, where it try turning right to avoid obstacles, or something else.\u00a0 This was relatively easy to isolate, as when the robot is hooked up to the PC with debugging serial.print statements on, it dumps its current state and parameters each time through the loop.\u00a0 So I quickly saw it wasn&#8217;t a bad state change.\u00a0 The problem was self-inflicted wound two: a sign error.\u00a0\u00a0 A clockwise turn is positive in the coordinate system, but it&#8217;s a negative turn in terms of compass bearing (e.g., turning right from heading 90 degrees to heading 0 degrees).\u00a0 So, flip the sign and I&#8217;m in business.\u00a0 An easy one.<\/p>\n<p>Now it moves straight to the first waypoint, turns in the proper direction, but not to the correct heading.\u00a0 There&#8217;s a lag in the compass reading that I need to account for.\u00a0 At least that&#8217;s not a silly mistake, and I know the source of the problem.<\/p>\n<p>Here&#8217;s a picture of my initial compass test (without the robot) , and then MARV-1 with the compass mounted on the mast:<\/p>\n<p><a href=\"http:\/\/www.mcgurrin.com\/robots\/wp-content\/uploads\/2012\/07\/tilt-compensated-compass.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-110 aligncenter\" title=\"Tilt Compensated Compass Test\" src=\"http:\/\/www.mcgurrin.com\/robots\/wp-content\/uploads\/2012\/07\/tilt-compensated-compass-300x201.png\" alt=\"Tilt Compensated Compass Test displaying bearing\" width=\"300\" height=\"201\" srcset=\"https:\/\/www.mcgurrin.info\/robots\/wp-content\/uploads\/2012\/07\/tilt-compensated-compass-300x201.png 300w, https:\/\/www.mcgurrin.info\/robots\/wp-content\/uploads\/2012\/07\/tilt-compensated-compass.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.mcgurrin.com\/robots\/wp-content\/uploads\/2012\/07\/photo1-e1343179966346.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-113\" title=\"MARV-1 with compass on mast\" src=\"http:\/\/www.mcgurrin.com\/robots\/wp-content\/uploads\/2012\/07\/photo1-e1343179966346-224x300.jpg\" alt=\"MARV-1 robotic vehicle with electronic compass on mast\" width=\"224\" height=\"300\" srcset=\"https:\/\/www.mcgurrin.info\/robots\/wp-content\/uploads\/2012\/07\/photo1-e1343179966346-224x300.jpg 224w, https:\/\/www.mcgurrin.info\/robots\/wp-content\/uploads\/2012\/07\/photo1-e1343179966346-764x1024.jpg 764w, https:\/\/www.mcgurrin.info\/robots\/wp-content\/uploads\/2012\/07\/photo1-e1343179966346.jpg 765w\" sizes=\"(max-width: 224px) 100vw, 224px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I finally got back to working on my 1st robotic vehicle, and it&#8217;s been 1 step forward, two steps back, with one of the back steps being self-inflicted.\u00a0 My first go at a robotic vehicle used only wheel encoders for &hellip; <a href=\"https:\/\/www.mcgurrin.info\/robots\/109\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3,25,4],"_links":{"self":[{"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":4,"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/posts\/109\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mcgurrin.info\/robots\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}