{"id":4735,"date":"2017-01-24T09:40:46","date_gmt":"2017-01-24T09:40:46","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4735"},"modified":"2017-01-24T09:40:46","modified_gmt":"2017-01-24T09:40:46","slug":"raspberry-apl-pi-and-python-on-the-microbit-2","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2017\/01\/raspberry-apl-pi-and-python-on-the-microbit-2\/","title":{"rendered":"Raspberry APL Pi and Python on the micro:bit"},"content":{"rendered":"<p>A couple of years ago I spent many happy hours writing APL code to <a href=\"http:\/\/www.dyalog.com\/blog\/category\/robots\/\" title=\"Robot Blog Entries\" target=\"_blank\">control robots which each embedded a Raspberry Pi<\/a>. It was fun but it was a bumpy ride &#8211; my interest eventually faded when I discovered that it was just too difficult to make sense of raw accelerometer data, which I was hoping to use for precise navigation. After I managed to <a title=\"Turning to a Heading with an MPU-9150\" href=\"http:\/\/www.dyalog.com\/blog\/2014\/09\/turn-to-compass-heading-using-mpu-9150\/\">fry my MPU-9150<\/a>,\u00a0I decided to pursue other interests, as they say.<\/p>\n<p>Recently, <a title=\"The RAREblog\" href=\"http:\/\/blog.rareschool.com\/2017\/01\/micropython-on-bbc-microbit-easy-as-pi.html\" target=\"_blank\">Romilly Cocking has been waxing lyrical<\/a> about a number of new components which\u00a0he thinks will make the whole experience more enjoyable &#8211; and I have decided to dive back in. So far, it has indeed been a very pleasant experience.<\/p>\n<h2>The Raspberry Pi Model 3<\/h2>\n<p><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/pi3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-4737 size-thumbnail\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/pi3-150x150.jpg\" alt=\"The Raspbery Pi Model 3\" width=\"150\" height=\"150\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/pi3-150x150.jpg 150w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/pi3.jpg 225w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>While I was away, the Raspberry Pi and the software available for it have come a VERY long way. The speed of a Pi 3 makes it a perfectly usable computer. I am writing this blog entry on the Pi using a standard Chromium browser. APL starts up immediately, using our new graphical IDE. It is quite simply amazing!<\/p>\n<h2>The BBC micro:bit and MicroPython<\/h2>\n<p>The first generation of APL-based robots used an Arduino to manage the motors and sensors because timing was too critical for the Pi to handle it. I spent some rather unhappy hours writing a control program in C, debugging that without any tools. You could not even do &#8220;printf debugging&#8221; to the serial port because that interfered with the timing so much that it stopped everything from working.<\/p>\n<p>Romilly suggested I take a look at the micro:bit, and since I have been using an interactive REPL all my life, he suggested that I run MicroPython on it.<\/p>\n<p>He was right, I&#8217;m loving it!<\/p>\n<h2>Getting Started<\/h2>\n<p>I purchased the <a title=\"micro:bit Inventor's Kit\" href=\"https:\/\/shop.pimoroni.com\/products\/inventors-kit-for-the-bbc-micro-bit\" target=\"_blank\">Kitronik Inventors&#8217;s Kit for the micro:bit from Pimoroni<\/a>, and quickly found videos on Youtube explaining how to assemble it. I installed the &#8220;mu&#8221; editor on my Pi and was immediately able to edit a simple Python program and run it on the micro:bit.<\/p>\n<p>My next step was, of course, to create a little class to drive MicroPython from APL. This involves using serial I\/O: when the micro:bit is attached to your Pi via USB it appears as a folder and as a serial (tty) device, typically \/dev\/ttyACM0. You can find my code in the <a title=\"APLPi\/microbit on GitHub\" href=\"https:\/\/github.com\/APLPi\/microbit\" target=\"_blank\">APLPi\/microbit GitHub repository<\/a>.<\/p>\n<p>If you clone the repository to \/home\/pi\/microbit and start APL, you can make calls to the MicroPython repository along the lines of:<\/p>\n<pre><code>    ]load \/home\/pi\/microbit\/microbit    \r\n    mb\u2190\u2395NEW microbit ''\r\n    mb.PyREPL '2+2'\r\n4<\/code><\/pre>\n<p>Using Python on the micro:bit to do maths isn&#8217;t that interesting when you have APL on the Pi; the exciting thing is that we can call the <a title=\"The Microbit MicroPython API\" href=\"http:\/\/microbit-micropython.readthedocs.io\/en\/latest\/microbit_micropython_api.html\" target=\"_blank\">micro:bit Python API<\/a>\u00a0and access the hardware. My first experiment was to\u00a0display my favourite APL symbol (Grade Up &#8211; aka Imperial Cruiser) in the built-in 5&#215;5 LED display:<\/p>\n<div id=\"attachment_4738\" style=\"width: 2602px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/ImperialCruiseronMicroBit.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4738\" class=\"size-full wp-image-4738\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/ImperialCruiseronMicroBit.jpg\" alt=\"Imperial Cruiser on  the micro:bit\" width=\"2592\" height=\"1944\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/ImperialCruiseronMicroBit.jpg 2592w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/ImperialCruiseronMicroBit-300x225.jpg 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/ImperialCruiseronMicroBit-1024x768.jpg 1024w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2017\/01\/ImperialCruiseronMicroBit-400x300.jpg 400w\" sizes=\"auto, (max-width: 2592px) 100vw, 2592px\" \/><\/a><p id=\"caption-attachment-4738\" class=\"wp-caption-text\">APL displaying an Imperial Cruiser on the micro:bit<\/p><\/div>\n<p>Which of the <a href=\"https:\/\/www.kitronik.co.uk\/5603-inventors-kit-for-the-bbc-microbit.html\" title=\"Kitrokik Inventor's Kit\" target=\"_blank\">Inventor&#8217;s Kit experiments<\/a> would you like to see me do next?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of years ago I spent many happy hours writing APL code to control robots which each embedded a Raspberry Pi. It was fun but it was a bumpy ride &#8211; my interest eventually faded when I discovered that &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2017\/01\/raspberry-apl-pi-and-python-on-the-microbit-2\/\">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":[27,9],"tags":[],"class_list":["post-4735","post","type-post","status-publish","format-standard","hentry","category-cxo","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4735","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=4735"}],"version-history":[{"count":10,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4735\/revisions"}],"predecessor-version":[{"id":4747,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4735\/revisions\/4747"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}