{"id":145,"date":"2013-05-24T14:08:46","date_gmt":"2013-05-24T14:08:46","guid":{"rendered":"http:\/\/cto.dyalog.com\/?p=145"},"modified":"2014-07-10T14:13:13","modified_gmt":"2014-07-10T14:13:13","slug":"visualising-sensor-data-using-apl-on-the-robot","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2013\/05\/visualising-sensor-data-using-apl-on-the-robot\/","title":{"rendered":"Visualising Sensor Data using APL on the Robot"},"content":{"rendered":"<p>As <a title=\"C3Pi Opens Eyes at the APL Moot\" href=\"http:\/\/www.dyalog.com\/blog\/2013\/05\/c3pi-opens-eyes\/\">described in a recent post<\/a>, our robot now has an Infra-Red distance sensor, which allows us to measure the distance from the front of the robot to the nearest obstacle. With respect to the autonomous navigation code that we wish to write, this will be the cornerstone! In order to evalute the perfomance of the sensor, we surrounded the robot with obstacles and commanded it to rotate slowly in an anti-clockwise direction, while IR data was collected 20 times per second:<\/p>\n<div id=\"attachment_149\" style=\"width: 494px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/RotatingC3Pi.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-149\" class=\" wp-image-149 \" title=\"The experiment: C3Pi rotates anti-clockwise and returns IR distance data every 0.05 seconds\" alt=\"RotatingC3Pi\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/RotatingC3Pi.jpg\" width=\"484\" height=\"363\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/RotatingC3Pi.jpg 692w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/RotatingC3Pi-300x225.jpg 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/RotatingC3Pi-400x300.jpg 400w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/a><p id=\"caption-attachment-149\" class=\"wp-caption-text\">Surrounded by obstacles, C3Pi rotates anti-clockwise and returns IR distance data every 0.05 seconds<\/p><\/div>\n<h2>Collecting the Data<\/h2>\n<p>We initialized the workspace by loading first the &#8220;RainPro&#8221; graphics package (which is included with Dyalog APL on the Pi), and then the <a title=\"APL Robot code on GitHub\" href=\"https:\/\/github.com\/APLPi\/APLBot\" target=\"_blank\">robot code<\/a>:<\/p>\n<pre><code class=\"language-apl\">\u00a0 \u00a0 )load rainpro\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Loads the graphics workspace\r\n\u00a0 \u00a0 ]load \/home\/pi\/DyaBot\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Loads the robot control code<\/code><\/pre>\n<p>The following function loops 300 times (once every 0.05 seconds), repeatedly collecting the value of the robot\u2019s IRange property (which contains the current distance measured by the IR sensor). The call to the UpdateIRange method of the bot ensures that a fresh sample has just been taken (otherwise, the robot will update the value automatically every 100ms).<\/p>\n<pre><code class=\"language-apl\">      \u2207 r\u2190CollectIRangeData bot;i;rc\r\n [1]\u00a0\u00a0\u00a0 r\u2190\u236c\r\n [2]\u00a0\u00a0\u00a0 :For i :In \u2373300\r\n [3]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :If 0=1\u2283rc\u2190bot.UpdateIRange\r\n [4]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 r\u2190r,bot.Irange\r\n [5]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2395DL 0.05 \u235d Wait 1\/20 sec\r\n [6]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :Else\r\n [7]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2218\u2218\u2218 \u235d Intentional error if update fails\r\n [8]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :EndIf\r\n [9]\u00a0\u00a0\u00a0 :EndFor\r\n      \u2207<\/code><\/pre>\n<p>We can now perform the experiment as follows:<\/p>\n<pre><code class=\"language-apl\">\u00a0\u00a0\u00a0\u00a0  iBot\u2190\u2395NEW DyaBot \u236c \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d Instance of the robot class\r\n      iBot.Speed\u219040 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d\u00a040% power on right wheel only \r\n      \u2374r\u2190CollectIRangeData iBot \u00a0\u00a0\u00a0\u235d Check shape of collected values \r\n300\r\n      iBot.Speed\u21900\u00a0\u00a0\u00a0\u00a0           \u00a0 \u235d Let the robot rest its batteries\r\n      1\u235510\u2191r\u00a0\u00a0\u00a0\u00a0      \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0      \u235d First 10 observations to 1 decimal \r\n15.3 12.8 13.7 12.7 13.7 11.4 9.9 11.1 10.4 9.4<\/code><\/pre>\n<p>Now that we have the data, the following function calls will create our first chart:<\/p>\n<pre>     ch.Set 'head' 'IR Sensor Data' \u235d Set Chart Header\r\n     ch.Set 'ycap' 'Distance (cm)'\u00a0 \u235d\u00a0\u00a0\u00a0\u00a0 Y caption\r\n     ch.Set 'xcap' 'Time (s)'\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d\u00a0\u00a0\u00a0\u00a0 X caption\r\n     ch.Set 'xfactor'(\u00f70.05)        \u235d Scale the x-axis to whole seconds\r\n     ch.Plot data\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0         \u235d Create the chart\r\n     '\/home\/pi\/irline.svg' svg.PS ch.Close \u235d Render it to SVG<\/pre>\n<p style=\"text-align: center;\"><a style=\"font-size: 15.454545021057129px; font-style: normal; line-height: 21.81818199157715px; text-decoration: underline;\" href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-raw.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-148\" style=\"border-color: #bbbbbb; background-color: #eeeeee;\" alt=\"ir-sensor-data-raw\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-raw.jpg\" width=\"682\" height=\"606\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-raw.jpg 853w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-raw-300x266.jpg 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-raw-337x300.jpg 337w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/a><\/p>\n<h2>Removing the Noise with a Moving Average<\/h2>\n<p>The chart above suggests that the robot performed a complete rotation every 5 seconds or so with just under 100 observations per cycle. The signal seems quite noisy, so some very simple smoothing would probably make it easier to understand. The following APL function calculates a moving average for this purpose \u2013 it does this by creating moving sums with a window size given by the left argument, and dividing these sums by the window size:<\/p>\n<pre><code class=\"language-apl\">\u00a0\u00a0\u00a0\u00a0\u00a0  movavg\u2190{(\u237a +\/ \u2375) \u00f7 \u237a}  \u235d Define the function \r\n \u00a0\u00a0\u00a0\u00a0\u00a0 3 movavg 1 2 3 4 5     \u235d Test it\r\n 2 3 4<\/code><\/pre>\n<p>We can re-use the existing chart settings and plot smoothed data as follows:<\/p>\n<pre><code class=\"language-apl\">\u00a0\u00a0\u00a0\u00a0\u00a0 #.ch.Plot 7 movavg data\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n \u00a0\u00a0\u00a0 \u00a0'\/home\/pi\/irline.svg' svg.PS ch.Close<\/code><\/pre>\n<p><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-smooth.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-147\" alt=\"ir-sensor-data-smooth\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-smooth.jpg\" width=\"853\" height=\"758\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-smooth.jpg 853w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-smooth-300x266.jpg 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-smooth-337x300.jpg 337w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/a><\/p>\n<h2>Making Sense of It<\/h2>\n<p>The pattern is now nice and clear &#8211; but how does the map compare to the territory? We can use a &#8220;Polar&#8221; chart of the distance to see how the measured distances compare to reality:<\/p>\n<pre><code class=\"language-apl\">\u00a0 \u00a0 \u00a0\u2207 filename PolarDistance data;\u2395PATH;mat;deg;window;smoothed;angles;movavg\r\n[1]\u00a0\u00a0 \u235d Polar IR Distance plot - \"data\" is one cycle of observations\r\n[2]\u00a0\u00a0 \u235d Note the -ve right margin to get the chart off-centre!\r\n[3]\r\n[4]\u00a0\u00a0\u00a0 \u2395PATH\u2190'#.ch'\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d Using RainPro ch namespace\r\n[5]\u00a0\u00a0\u00a0 window\u21907\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d Smoothing window size\r\n[6]\u00a0\u00a0\u00a0 deg\u2190\u2395UCS 176\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d Degree symbol\r\n[7]\u00a0\u00a0\u00a0 movavg\u2190{(\u237a+\/\u2375)\u00f7\u237a} \u235d Moving average with window size \u237a\r\n[8]\r\n[9]\u00a0\u00a0\u00a0 angles\u2190360\u00d7(\u2373\u2374data)\u00f7\u2374data \u235d All the way round\r\n[10]\u00a0\u00a0 smoothed\u2190window movavg data,(\u00af1+window)\u2191data\r\n[11]\u00a0\u00a0 data\u2190(\u2308window\u00f72)\u233ddata\u00a0\u00a0\u00a0\u00a0 \u235d Rotate data so centre of window is aligned with moving average\r\n[12]\r\n[13]\u00a0\u00a0 Set'head' 'Infra-Red;Distance;Measurement'\r\n[14]\u00a0\u00a0 Set('hstyle' 'left')('mleft' 12)('mright' \u00af60)\r\n[15]\u00a0\u00a0 Set'footer' 'Distance measured;every 0.05 seconds;while 3Pi was rotating'\r\n[16]\u00a0\u00a0 Set'style' 'lines,curves,xyplot,time,grid,hollow'\r\n[17]\u00a0\u00a0 Set'lines' 'solid'\r\n[18]\u00a0\u00a0 Set'nib' 'medium,broad'\r\n[19]\u00a0\u00a0 Set('yr' 0 60)('ytick' 10)\r\n[20]\u00a0\u00a0 Set('xr' 0 360)('xtick' 15)('xpic'('000',deg))\r\n[21]\u00a0\u00a0 Set('key' 'Measured' '7 MovAvg')('ks' 'middle,left,vert')\r\n[22]\r\n[23]\u00a0\u00a0 Polar angles,data,\u236asmoothed\r\n[24]\u00a0\u00a0 filename svg.PS Close\u00a0 \r\n  \u00a0 \u00a0\u2207<\/code><\/pre>\n<p>To align the chart with the picture, we need to:<\/p>\n<ol>\n<li>Extract the first 99 observations &#8211; corresponding to one rotation<\/li>\n<li>Reverse the order of the data, because the robot was rotating anti-clockwise<\/li>\n<li>Finally, rotate the data by 34 samples, to align the data with the photograph (the recording started with the robot in the position shown on the photograph)<\/li>\n<\/ol>\n<p>We can do these three operations using the expression on the next line, and then pass this as an argument to the PolarDistance function, which creates another SVG file:<\/p>\n<pre><code class=\"language-apl\">     onerotation\u2190\u00af34\u233d\u233d99\u2191r\r\n     '\/home\/pi\/irpolar.svg' PolarDistance onerotation<\/code><\/pre>\n<p><a href=\"http:\/\/www.dyalog.com\/blogwp-content\/uploads\/2013\/05\/ir-sensor-data-polar.jpg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-150\" alt=\"ir-sensor-data-polar.jpg\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-polar.jpg.png\" width=\"853\" height=\"758\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-polar.jpg.png 853w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-polar.jpg-300x266.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2013\/05\/ir-sensor-data-polar.jpg-337x300.png 337w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/a>If we compare the red line to the picture we started with, and take into account the fact that the robot was rotating quite fast and the IR sensor probably needs a little time to stabilise, it looks quite reasonable. The accuracy isn&#8217;t great, but with a little smoothing it does seem we should be able to stop little C3Pi from bumping into too many things!<\/p>\n<p>Stay tuned for videos showing some autonomous driving, and the code to do it&#8230;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As described in a recent post, our robot now has an Infra-Red distance sensor, which allows us to measure the distance from the front of the robot to the nearest obstacle. With respect to the autonomous navigation code that we &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2013\/05\/visualising-sensor-data-using-apl-on-the-robot\/\">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":[12,9,5],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-cto","category-raspberry-pi","category-robots"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/145","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=145"}],"version-history":[{"count":30,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":467,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/145\/revisions\/467"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}