{"id":783,"date":"2014-11-06T15:32:01","date_gmt":"2014-11-06T15:32:01","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=783"},"modified":"2014-11-06T15:32:01","modified_gmt":"2014-11-06T15:32:01","slug":"controlling-the-sbrick-with-a-wiimote","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2014\/controlling-the-sbrick-with-a-wiimote","title":{"rendered":"Controlling the SBrick with a wiimote"},"content":{"rendered":"<p>I found out that is really easy to use a wiimote with Linux for so as a sequel to my previous &#8216;<a href=\"http:\/\/ofalcao.pt\/blog\/2014\/sbrick-remote-control-with-a-wireless-gamepad\">Sbrick &#8211; remote control with a wireless gamepad<\/a>&#8216; I now present you my &#8216;Controlling the SBrick with a wiimote&#8217; (actually a cheap clone, a <a href=\"https:\/\/www.worten.pt\/acessorio-wii-n-play-comando-remote-plus-preto.html\">N-Play Remote Plus,<\/a> unfortunately I haven&#8217;t found a version with Motion Plus).<\/p>\n<p>The wiimote uses Bluetooth, but doesn&#8217;t strictly follow the rules. If we have bluetooth on our PC and want to check if it will work, just press both &#8216;1&#8217; and &#8216;2&#8217;buttons for it to advertise for a while and then:<\/p>\n<pre>$ hcitool -i hci0 scan\r\nScanning ...\r\n    04:02:16:01:1C:E7    Nintendo RVL-CNT-01<\/pre>\n<p>It&#8217;s not possible to pair with the wiimote but there are tools for that, like <a href=\"http:\/\/abstrakraft.org\/cwiid\/\">cwiid<\/a>. As I&#8217;m going to use python I installed their python library:<\/p>\n<pre>$ sudo apt-get install python-cwiid<\/pre>\n<p>There are lots of examples so I&#8217;ll show only my final result:<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"Vengit SBrick controlled with a Wiimote\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/aVT57uZQ6IQ?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><\/div>\n<p>&nbsp;<\/p>\n<p>And the script I used for the video above:<\/p>\n<pre># apt-get install python-cwiid\r\nimport cwiid\r\nfrom time import sleep\r\nfrom subprocess import call\r\nfrom math import log10\r\n\r\n# macros for the SBrick\r\nDRIVE_A=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=0102\"\r\nDRIVE_B=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=0103\"\r\nCOAST_A=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=01020000\"\r\nCOAST_B=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=01030000\"\r\nBREAK_A=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=0002\"\r\nBREAK_B=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=0003\"\r\n\r\n\r\n# connecting to the wiimote. This allows several attempts\r\n# as first few often fail.\r\n\r\nprint 'Press 1+2 on your Wiimote now...'\r\nwm = None\r\ni=1\r\nwhile not wm:\r\n    try:\r\n        wm=cwiid.Wiimote()\r\n    except RuntimeError:\r\n        if (i&gt;5):\r\n            print(\"cannot create connection\")\r\n            quit()\r\n        print \"Error opening wiimote connection\"\r\n        print \"attempt \" + str(i)\r\n        i +=1\r\n\r\n#set wiimote to report button presses and accelerometer state\r\nwm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC\r\n\r\n#turn on all led and ruble to show connected\r\nwm.led=15\r\nwm.rumble=True\r\nsleep(0.5)\r\nwm.rumble=False\r\nwm.led=0\r\nsleep(1.5)\r\n\r\n# roll = accelerometer[0], standby ~125\r\n# pitch = accelerometer[1], standby ~125\r\n\r\nwhile True:\r\n    buttons = wm.state['buttons']\r\n\r\n    #only pay attention when button '1' pressed\r\n    if (buttons &amp; cwiid.BTN_1):\r\n\r\n        roll=(wm.state['acc'][0]-125)\r\n        pitch=(wm.state['acc'][1]-125)\r\n        \r\n        if (roll&lt;0):\r\n            if (roll&lt;-4):\r\n                if (roll&lt;-25):\r\n                    roll=-100\r\n                else:\r\n                    roll=-50*log10(-4*roll)\r\n            else:\r\n                roll=0\r\n\r\n        if (roll&gt;0):\r\n            if (roll&gt;4):\r\n                if (roll&gt;25):\r\n                    roll=100\r\n                else:\r\n                    roll=50*log10(4*roll)\r\n            else:\r\n                roll=0\r\n\r\n        if (pitch&gt;0):\r\n            if (pitch&gt;4):\r\n                if (pitch&gt;25):\r\n                    pitch=100\r\n                else:\r\n                    pitch=50*log10(4*pitch)\r\n\r\n            else:\r\n                pitch=0\r\n\r\n        if (pitch&lt;0):\r\n            if (pitch&lt;-4):\r\n                if(pitch&lt;-25):\r\n                    pitch=-100\r\n                else:\r\n                    pitch=-50*log10(-4*pitch)\r\n            else:\r\n                pitch=0\r\n\r\n        if ((pitch&lt;&gt;0)or(roll&lt;&gt;0)):\r\n\r\n            roll=2.5*roll\r\n            pitch=2.5*pitch\r\n\r\n            if(pitch&lt;&gt;0):\r\n                if(roll&gt;0):\r\n                    # turn right\r\n                    motor_L=pitch\r\n                    motor_R=-pitch-roll\/2\r\n\r\n                else:\r\n                    # turn left\r\n                    motor_R=-pitch\r\n                    motor_L=pitch+roll\/2\r\n\r\n            elif(roll&lt;&gt;0):\r\n                #just rotate\r\n                motor_R=motor_L=roll;\r\n\r\n            else:\r\n                # does nothing\r\n                motor_R=motor_L=0\r\n\r\n            if((motor_R&lt;&gt;0)or(motor_L&lt;&gt;0)):\r\n\r\n                if(motor_R&lt;0):\r\n                    duty=str(hex(int(-motor_R)))\r\n                    command_A=DRIVE_A+\"00\"+duty[2:]                    \r\n                else:\r\n                    duty=str(hex(int(motor_R)))\r\n                    command_A=DRIVE_A+\"01\"+duty[2:]\r\n\r\n                if(motor_L&lt;0):\r\n                    duty=str(hex(int(-motor_L)))\r\n                    command_B=DRIVE_B+\"00\"+duty[2:]                    \r\n                else:\r\n                    duty=str(hex(int(motor_L)))\r\n                    command_B=DRIVE_B+\"01\"+duty[2:]\r\n\r\n                #send motors commands to SBrick\r\n                call(command_A, shell=True);\r\n                call(command_B, shell=True);\r\n                sleep(0.1)\r\n\r\n                #send COAST commands to SBrick\r\n                call(COAST_A, shell=True);\r\n                call(COAST_B, shell=True);\r\n\r\n    else:\r\n        # inactive\r\n        sleep(0.01)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I found out that is really easy to use a wiimote with Linux for so as a sequel to my previous &#8216;Sbrick &#8211; remote control with a wireless gamepad&#8216; I now present you my &#8216;Controlling the SBrick with a wiimote&#8217; (actually a cheap clone, a N-Play Remote Plus, unfortunately I haven&#8217;t found a version with &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2014\/controlling-the-sbrick-with-a-wiimote\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;Controlling the SBrick with a wiimote&#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":[160,148,159],"tags":[],"series":[],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-geek-en","category-linux-en","category-wiimote"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-cD","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/783","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=783"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=783"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}