{"id":3661,"date":"2014-05-02T19:54:25","date_gmt":"2014-05-03T02:54:25","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/2014\/05\/accelerometer-woes\/"},"modified":"2018-11-03T13:36:24","modified_gmt":"2018-11-03T20:36:24","slug":"accelerometer-woes","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2014\/05\/accelerometer-woes\/","title":{"rendered":"Accelerometer woes"},"content":{"rendered":"<p id=\"top\" \/>\n<p>I have a accelerometer\/gyro\/compass combo in my wheel LSM303DHLHC, data sheet: <a href=\"http:\/\/www.pololu.com\/file\/download\/LSM303DLHC.pdf?file_id=0J564\">http:\/\/www.pololu.com\/file\/download\/LSM303DLHC.pdf?file_id=0J564<\/a> found via Pololu&#8217;s link: <a href=\"http:\/\/www.pololu.com\/product\/1268\">http:\/\/www.pololu.com\/product\/1268<\/a><\/p>\n<p>It has been tough to figure out consistent data. First of all, I realized the compass has to be calibrated. There is a sample app included with the LSM303 Adruino code called &#8220;Calibrate&#8221;. It just reads the compass\/magnet data a keeps a running max and min value of what it sees. First off, I kept getting strange values out of it; specifically <b>-4096<\/b>. I later learned from the Polo website that this is an overflow condition in the chip and it is documented on the data sheet. First of all, it is documented on <b>a data sheet,<\/b> but not the one for the LSM303DHLHC. I&#8217;m sure they use the same accelerometer chip though. The fix according to Pololu is to adjust the gain (and that works). Here are possible values:<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2014\/05\/Screen-Shot-2014-05-02-at-7.43.06-PM.png\" width=\"903\" height=\"411\" alt=\"Screen Shot 2014-05-02 at 7.43.06 PM.png\" \/><\/p>\n<p>The default is 0,0,0 which means a &#8220;Sensor input field range&#8221; of +\/- 1.3 Gauss. I don&#8217;t really know what <a href=\"http:\/\/en.wikipedia.org\/wiki\/Gauss_(unit)\">a gauss<\/a> is, and I&#8217;m not sure why a higher or lower gain is important. But with the default value it was overflowing the buffers in the accelerometer chip, so I changed it to: <b>1, 0, 0<\/b> with this:<\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo; color: #008400;\"><span style=\"color: #000000\">&nbsp;&nbsp;<\/span><span style=\"color: #4f8187\">_compass<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #31595d\">writeMagReg<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #4f8187\">LSM303<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #31595d\">CRB_REG_M<\/span><span style=\"color: #000000\">,<\/span> <span style=\"color: #272ad8\">0<\/span><span style=\"color: #000000\">b10000000);<\/span> \/\/ gain: \u00b14.0 Gauss (see datasheet)<\/p>\n<p>\nWhich is +\/- 4.0 gauss. The gain reading values are lower, and don&#8217;t overflow anymore. That&#8217;s a plus. I&#8217;m using the Pololu modified sample &#8220;<a href=\"https:\/\/github.com\/pololu\/minimu-9-ahrs-arduino\">Pololu MinIMU-9 + Arduino AHRS (Attitude and Heading Reference System)<\/a>&#8221; to get cool graphs\/pictures:<\/p>\n<p>\n.<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2014\/05\/Screen-Shot-2014-05-02-at-7.50.08-PM.png\" width=\"530\" height=\"691\" alt=\"Screen Shot 2014-05-02 at 7.50.08 PM.png\" \/><br \/>\nIt is a python script, and has a bunch of dependancies, but they are easy to find and download for the Mac. I had to tweak the script to get it to run:<\/p>\n<p>1. Fix the serial port:<\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\">ser = serial.Serial(port=<span style=\"color: #272ad8\">&#8216;\/dev\/tty.usbmodem23981&#8217;<\/span>,baudrate=<span style=\"color: #272ad8\">115200<\/span>, timeout=<span style=\"color: #272ad8\">1<\/span>)<\/p>\n<p>\n2. Toss in a &#8220;rate(100)&#8221; in the while loop:<\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\"><span style=\"color: #bb2ca2\">while<\/span> True:<\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\">rate(<span style=\"color: #272ad8\">100<\/span>)<\/p>\n<p>It took me a while to discover that last bit, and I only figured it out after reading a demo script.<\/p>\n<p><\/p>\n<p style=\"margin: 0px;\">I still was getting strange results; not quite an accurate position. So, I outputted the raw x\/y\/z magnet data to a comma delimitated file and opened it in Grapher:<\/p>\n<p><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/>\n<a href=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2014\/05\/Screen-Shot-2014-05-02-at-7.52.16-PM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2014\/05\/Screen-Shot-2014-05-02-at-7.52.16-PM-tm.jpg\" width=\"500\" height=\"405\" alt=\"Screen Shot 2014-05-02 at 7.52.16 PM.png\" \/><\/a><\/span><\/p>\n<p><\/p>\n<p style=\"margin: 0px;\">You can create a Grapher.app doc on the Mac (the app comes with OSX) and do a 3d graph. Add a Point Set, and at the top of the window click Edit Points\u2026and import a file. I imported two; one is red, one is blue. Notice I have some rather odd stray points; I think these are causing part of my problem.<\/p>\n<p style=\"margin: 0px;\"><\/p>\n<p style=\"margin: 0px;\">I might try to do some type of better averaging of the min\/max values and through out the highest and lowest absolute values. More fun math to figure out\u2026.but that might correct my problems.<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a accelerometer\/gyro\/compass combo in my wheel LSM303DHLHC, data sheet: http:\/\/www.pololu.com\/file\/download\/LSM303DLHC.pdf?file_id=0J564 found via Pololu&#8217;s link: http:\/\/www.pololu.com\/product\/1268 It has been tough to figure out consistent data. First of all, I realized the compass has to&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2014\/05\/accelerometer-woes\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":3657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-3661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-circus-and-silk"],"jetpack_featured_media_url":"https:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2014\/05\/Screen-Shot-2014-05-02-at-7.43.06-PM.png","jetpack_shortlink":"https:\/\/wp.me\/p8zn47-X3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/3661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=3661"}],"version-history":[{"count":2,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/3661\/revisions"}],"predecessor-version":[{"id":5377,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/3661\/revisions\/5377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media\/3657"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=3661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=3661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=3661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}