{"id":721,"date":"2014-10-31T06:25:47","date_gmt":"2014-10-31T06:25:47","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=721"},"modified":"2014-10-31T10:03:57","modified_gmt":"2014-10-31T10:03:57","slug":"sbrick-beta-testing","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2014\/sbrick-beta-testing","title":{"rendered":"SBrick beta testing"},"content":{"rendered":"<p>Na campanha Kickstarter do <a href=\"https:\/\/www.sbrick.com\/\">SBrick<\/a> (ou SmartBrick) inscrevi-me como beta tester. O meu SBrick chegou finalmente esta semana, vindo da Hungria numa encomenda verdadeiramente espartana, sem qualquer folheto ou instru\u00e7\u00f5es:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.brickshelf.com\/gallery\/jorgepereira\/SBrick\/sbrick-01.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/i0.wp.com\/www.brickshelf.com\/gallery\/jorgepereira\/SBrick\/sbrick-01.jpg?resize=580%2C344\" alt=\"\" width=\"580\" height=\"344\" \/><\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.brickshelf.com\/gallery\/jorgepereira\/SBrick\/sbrick-02.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/i0.wp.com\/www.brickshelf.com\/gallery\/jorgepereira\/SBrick\/sbrick-02.jpg?resize=580%2C344\" alt=\"\" width=\"580\" height=\"344\" \/><\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.brickshelf.com\/gallery\/jorgepereira\/SBrick\/sbrick-03.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/i0.wp.com\/www.brickshelf.com\/gallery\/jorgepereira\/SBrick\/sbrick-03.jpg?resize=580%2C344\" alt=\"\" width=\"580\" height=\"344\" \/><\/a><\/p>\n<p>\u00c9 ainda uma vers\u00e3o muito tosca e com alguns defeitos (um dos quatro canais parece estar avariado e a ficha de outro n\u00e3o encaixa com firmeza nos cabos Power Functions) mas j\u00e1 permite ensaiar a conectividade com o Linux e em especial com o ev3dev &#8211; o meu prop\u00f3sito como beta tester \u00e9 sobretudo ajudar na liga\u00e7\u00e3o entre o Mindstorms EV3 e o Sbrick.<\/p>\n<p>O SBrick exp\u00f5e 6 servi\u00e7os Bluetooth BT4.0\/BLE: Generic Access, Device Information e mais 4 espec\u00edficos do fabricante. Os 2 primeiros exp\u00f5em esta informa\u00e7\u00e3o:<\/p>\n<p>Device Name = SBrick<br \/>\nAppearance = Generic Remote Control<br \/>\nModel Number\u00a0 = 4.0<br \/>\nFirmware Revision = 4.1<br \/>\nHardware Revision = 4.0<br \/>\nSoftware Revision = 4.1<br \/>\nManufacturer Name String = Vengit Ltd.<\/p>\n<p>Os outros 4 servi\u00e7os s\u00e3o espec\u00edficos da Vengit e exp\u00f5em ao todo 8 campos:<br \/>\n&#8211; 5 Read Only<br \/>\n&#8211; 1 Write Only<br \/>\n&#8211; 1 Read\/Write<br \/>\n&#8211; 1 Unknown<\/p>\n<p>Entretanto recebi de um dos engenheiros da Vengit a informa\u00e7\u00e3o m\u00ednima mas suficiente para controlar um motor ligado a um dos 4 canais: o UUID do servi\u00e7o de controlo remoto \u00e9 &#8216;4dc591b0-857c-41de-b5f1-15abda665b0c&#8217;, sendo a caracteristica do controlo remoto &#8216;2b8cbcc-0e25-4bda-8790-a15f53e6010f&#8217;.<\/p>\n<p>Isso em termos pr\u00e1ticos, utilizando o comando gatttool que vem com o BlueZ 5 (o stack de Bluetooth para Linux) corresponde a escrever no handle 0x0025 os comandos reconhecidos pelo firmware.<\/p>\n<p>At\u00e9 agora apenas me foram apresentados dois comandos (00h = BRAKE e 01h = DRIVE)<\/p>\n<ul>\n<li>BRAKE Channel<\/li>\n<li>DRIVE Channel Direction DutyCycle<\/li>\n<\/ul>\n<p>Channel corresponde a uma das 4 portas dispon\u00edveis, podendo ser 00h, 01h, 02h ou 03h.<\/p>\n<p>Direction corresponde ao sentido da rota\u00e7\u00e3o, podendo ser clokwise (00h) ou anticlockwise (01h).<\/p>\n<p>DutyCycle corresponde \u00e0 pot\u00eancia transmitida ao motor, variando entre 00h (&#8220;Coast&#8221;) e FFh (full power).<\/p>\n<p>Assim para enviar um comando a partir de um sistema Linux (PC com Ubuntu, Raspberry Pi com Raspbian ou Mindstorms EV3 com ev3dev) equipado com dongle USB BT4.0\/BLE basta invocar o comando gatttool:<\/p>\n<pre>$ gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=010000FF<\/pre>\n<p>No exemplo acima \u00e9 enviado para o dispositivo BT com identificativo 00:07:80:7F:28:E1 (o meu SBrick) a partir do device hci0 (o meu dongle BT4.0\/BLE) o comando<\/p>\n<pre>DRIVE Channel#0 Clockwise 100%<\/pre>\n<p>Como este comando n\u00e3o mant\u00e9m a sess\u00e3o bluetooth aberta, ao fim de cerca de 3 segundos ela fecha e os canais deixam de comandar os motores (se invocarmos o comando gatttool em modo interactivo com a op\u00e7\u00e3o &#8220;-I&#8221; ou &#8220;&#8211;interactive&#8221; e dermos a partir da\u00ed os comandos equivalentes a sess\u00e3o persiste e os motores continuam activos indefinidamente).<\/p>\n<p>No video abaixo s\u00e3o mostrados dois motores a rodar em sentidos diferentes:<\/p>\n<ul>\n<li>DRIVE Channel#0 Clockwise 22%<\/li>\n<li>DRIVE Channel#2 Anticlockwise 17%<\/li>\n<\/ul>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"LEGO Mindstorms EV3 and Vengit SBrick - wireless motor control\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/xc2LMRMaQl8?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>No pr\u00f3ximo video \u00e9 mostrado um motor EV3 a servir de refer\u00eancia a um motor Servo Power Functions:<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"LEGO Mindstorms + Vengit SBrick: Servo motor remote control\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/QJ3JhFhZJPc?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>E finalmente uma vers\u00e3o um pouco mais refinada do mesmo exemplo com introdu\u00e7\u00e3o de um factor de escala de modo a que os movimentos sejam equipar\u00e1veis e ainda uma limita\u00e7\u00e3o ao intervalo [-90\u00ba,+90\u00ba]<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"LEGO Mindstorms + Vengit SBrick: Servo motor remote control v2\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/UFtGBdK7zcY?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>Para este \u00faltimo video foi usado o script abaixo, em python:<\/p>\n<pre>import traceback\r\nfrom sys import exit\r\nfrom time import sleep\r\nfrom subprocess import call\r\nfrom subprocess import check_output\r\nfrom math import trunc\r\n\r\ndef reset_ev3motor():\r\n\u00a0 call (\"echo 0 &gt; \/sys\/class\/tacho-motor\/motor0\/position\", shell=True);\r\n\u00a0 return;\r\n\r\ndef rd_ev3motor():\r\n\r\n\u00a0 v=check_output(\"cat \/sys\/class\/tacho-motor\/motor0\/position\",shell=True);\r\n\u00a0 return(trunc(float(v)));\r\n\r\ndef sbrick_drive(channel,direction,dutycycle):\r\n\u00a0 \" calls gatttool command from BlueZ, the official Linux Bluetooth protocol stack\"\r\n\r\n\u00a0 if(dutycycle &gt; 255):\r\n\u00a0\u00a0\u00a0 dt_hexa=\"0xFF\";\r\n\u00a0 else:\r\n\u00a0\u00a0\u00a0 dt_hexa=str(hex(int(dutycycle)));\r\n\r\n\u00a0 command=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=01\"+channel+direction+dt_hexa[2:];\r\n#\u00a0 print(command);\r\n\u00a0 call (command, shell=True);\r\n\r\n\u00a0 return;\r\n\r\ndef main():\r\n\u00a0 try:\r\n\u00a0\u00a0\u00a0 pos=0.0;\r\n\u00a0\u00a0\u00a0 SCALE=255.0\/90.0;\r\n\r\n\u00a0\u00a0\u00a0 reset_ev3motor();\r\n\r\n\u00a0\u00a0\u00a0 while(True):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 pos=rd_ev3motor()*SCALE;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if(pos&gt;0):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sbrick_drive(\"02\",\"00\",pos);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sbrick_drive(\"02\",\"01\",-pos)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 sleep(0.1);\r\n\r\n\u00a0 except (KeyboardInterrupt, SystemExit):\r\n\u00a0\u00a0\u00a0 print \"Exiting...\";\r\n\u00a0 except Exception:\r\n\u00a0\u00a0\u00a0 traceback.print_exc(file=sys.stdout);\r\n\r\n\u00a0 exit(0);\r\n\r\nif __name__ == \"__main__\":\r\n\u00a0 main()<\/pre>\n<p>Recorro \u00e0 fun\u00e7\u00e3o &#8216;call&#8217; da library &#8216;subprocess&#8217; para invocar o comando gatttool. \u00c9 feio mas funciona, ainda n\u00e3o tive tempo de procurar uma library espec\u00edfica para bluetooth BLE.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Na campanha Kickstarter do SBrick (ou SmartBrick) inscrevi-me como beta tester. O meu SBrick chegou finalmente esta semana, vindo da Hungria numa encomenda verdadeiramente espartana, sem qualquer folheto ou instru\u00e7\u00f5es: \u00c9 ainda uma vers\u00e3o muito tosca e com alguns defeitos (um dos quatro canais parece estar avariado e a ficha de outro n\u00e3o encaixa com &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2014\/sbrick-beta-testing\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;SBrick beta testing&#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":[13,18,19,20],"tags":[217,219,225],"series":[],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-ev3dev","category-lego","category-lego-mindstorms","category-linux","tag-ble","tag-bluetooth","tag-sbrick"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-bD","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/721","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=721"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=721"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}