{"id":1783,"date":"2021-07-17T11:12:04","date_gmt":"2021-07-17T10:12:04","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1783"},"modified":"2021-07-17T11:13:29","modified_gmt":"2021-07-17T10:13:29","slug":"lego-air-drums","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2021\/lego-air-drums","title":{"rendered":"LEGO Air Drums"},"content":{"rendered":"\n<p>This is  a quick holiday project.<\/p>\n\n\n\n<p>I had some LEGO Move Hubs with me with a new Pybricks firmware image and a new version of &#8216;pybricksdev&#8217; python library that works with them.<\/p>\n\n\n\n<p>Move Hub has an internal IMU sensor. And batteries. And wireless communication. So I could make a kind of wireless game controller.<\/p>\n\n\n\n<p>But Facebook keeps hitting me with ads of wireless MIDI drumsticks. I can do this with LEGO:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LEGO AirDrum v0.1\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/bZcKz5frq9k?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>It wasn&#8217;t difficult &#8211; the Pybricks script is very short, inspired on an Arduino project from <a href=\"https:\/\/create.arduino.cc\/projecthub\/arichetta\">Andrea Richetta<\/a> (&#8216;<a href=\"https:\/\/create.arduino.cc\/projecthub\/arichetta\/air-drum-with-arduino-nano-33-iot-8e9d45\" data-type=\"URL\" data-id=\"https:\/\/create.arduino.cc\/projecthub\/arichetta\/air-drum-with-arduino-nano-33-iot-8e9d45\">Air Drum with Arduino Nano 33 IoT<\/a>&#8216;) &#8211; I just calculate the size of the acceleration vector (in fact it&#8217;s square since Move Hub doesn&#8217;t support float numbers so no complex math functions) and print something out whenever it changes &#8216;suddenly&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pybricks.hubs import MoveHub\nfrom pybricks.tools import wait\n\nACC_THRESHOLD = 170\nACC_MARGIN = 50\nTIME = 40\nKEY = '1'\n\nhub = MoveHub()\nmax_v = 0\n\nwhile True:\n    x,y,z = hub.imu.acceleration()\n    v = x*x+y+y+z+z\n    \n    if v > ACC_THRESHOLD:\n        max_v = v\n    elif v &lt; max_v-ACC_MARGIN:\n        print(KEY)\n        max_v = 0\n        wait(TIME)<\/code><\/pre>\n\n\n\n<p> Then I just used &#8216;pybricksdev&#8217; library in a pyhton script on my laptop to capture the output of the Move Hub and redirect it with &#8216;xdotool&#8217; to a MIDI controller (VMPK) that generates MIDI events usable by Hydrogen (a MIDI drum engine).<\/p>\n\n\n\n<p>I could have used something on python to directly generate the MIDI events instead of using VMPK&#8230; but I&#8217;m a lazy guy on holidays (or call it &#8216;Agile&#8217; \ud83d\ude00 ) <\/p>\n\n\n\n<p>So I finally made a kind of tutorial:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LEGO Air Drums with 2x Move Hubs - HowTo\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/3BFpTzSQscc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>All the source code available <a href=\"https:\/\/github.com\/JorgePe\/randomideas\/tree\/master\/AirDrums\" data-type=\"URL\" data-id=\"https:\/\/github.com\/JorgePe\/randomideas\/tree\/master\/AirDrums\">at github<\/a> as usual.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick holiday project. I had some LEGO Move Hubs with me with a new Pybricks firmware image and a new version of &#8216;pybricksdev&#8217; python library that works with them. Move Hub has an internal IMU sensor. And batteries. And wireless communication. So I could make a kind of wireless game controller. But &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2021\/lego-air-drums\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;LEGO Air Drums&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[368,367,279,362],"series":[],"class_list":["post-1783","post","type-post","status-publish","format-standard","hentry","category-sem-categoria","tag-air-drums","tag-drums","tag-midi","tag-pybricks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-sL","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1783"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1783\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1783"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}