{"id":914,"date":"2017-03-02T22:05:00","date_gmt":"2017-03-02T21:05:00","guid":{"rendered":"http:\/\/heppg.de\/ikg\/wordpress\/?p=914"},"modified":"2017-03-03T07:50:46","modified_gmt":"2017-03-03T06:50:46","slug":"python-loop-timing-accuracy","status":"publish","type":"post","link":"https:\/\/heppg.de\/ikg\/wordpress\/?p=914","title":{"rendered":"python timing loop accuracy"},"content":{"rendered":"<p>On a raspberry pi in python, it is not possible to produce accurate timings in \u03bcs range. The question is how accurate the timing is in real live.<\/p>\n<p>The issue was raised by someone asking a question in raspberry pi forum on an 80Hz timing loop &#8220;the values may not always be accurate at the milliseconds level&#8221;.<\/p>\n<p>To measure this, a simple timing loop was set up in python, and the timing was measured with the help of an arduino due. The arduino due runs at 84MHz and is ideal as a versatile measuring tool for this sort of problems.<\/p>\n<p>Python timing loop. The loop should produce a square wave with 12.5ms or 80Hz.<\/p>\n<pre>import RPi.GPIO as GPIO\r\nimport time\r\n\r\nGPIO.setmode(GPIO.BCM)\r\nGPIO.setwarnings(False)\r\n\r\npin_AO = 17\r\nGPIO.setup(pin_AO, GPIO.OUT)\r\n\r\ndt = 0\r\ndiff = 0.0115\r\nwhile True:\r\n\u00a0\u00a0\u00a0 drun = diff - dt\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 t0 = time.time()\r\n\u00a0\u00a0\u00a0 for _ in range(0, 1000):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 GPIO.output(pin_AO, GPIO.LOW)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 time.sleep(0.001)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 GPIO.output(pin_AO, GPIO.HIGH)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 time.sleep(drun)\r\n\u00a0\u00a0\u00a0 t1 = time.time();\r\n\u00a0\u00a0 \u00a0# calculate the deviation from ideal value 12.5 ms.\r\n\u00a0\u00a0\u00a0 dt = (t1 - t0) \/ 1000 - 0.0115 - 0.001\r\n\u00a0\u00a0\u00a0 print(dt)<\/pre>\n<p>The idea is to adjust timing based on average of last 1000 pulses.<\/p>\n<p>The output of the arduino due is printed to Serial and looks like (values in \u03bcs):<\/p>\n<pre>...\r\n12508\r\n12509\r\n12509\r\n12514\r\n12513\r\n12516\r\n12513\r\n12509\r\n12510\r\n12512\r\n12510\r\n12515\r\n12510\r\n12515\r\n12514\r\n12515\r\n12508\r\n12512\r\n12508\r\n12514\r\n12514\r\n...<\/pre>\n<p>Grouping the output values into 10 \u03bcs slots and calculating the the number of events per group yields this bar chart. There are prox 27.000 events included the chart.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/03\/12.5ms.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-911\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2017\/03\/12.5ms.png\" alt=\"12-5ms\" width=\"605\" height=\"512\" \/><\/a><\/p>\n<p>X-axis is slot times in \u03bcs; y-axis is count of events in the slot.<\/p>\n<p>The printout of the script is (shortened)<\/p>\n<pre>9.77396965027e-06\r\n0.000155053138733\r\n1.0488986969e-05\r\n0.000154378175735\r\n1.00150108337e-05\r\n0.000154267787933\r\n9.80496406555e-06\r\n0.000156002044678\r\n8.39877128601e-06\r\n0.000155970096588<\/pre>\n<p>This demonstrates some oscillation. Values like 0.00015 and &#8216;something- e-05&#8217; are repeating. The two peaks in the diagram reflect this behavior.<\/p>\n<p>Summary: precise timing with loops in python is accurate only to prox 0.2 ms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On a raspberry pi in python, it is not possible to produce accurate timings in \u03bcs range. The question is how accurate the timing is in real live. The issue was raised by someone asking a question in raspberry pi forum on an 80Hz timing loop &#8220;the values may not always be accurate at the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-914","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=914"}],"version-history":[{"count":6,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/914\/revisions"}],"predecessor-version":[{"id":920,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/914\/revisions\/920"}],"wp:attachment":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}