{"id":773,"date":"2014-10-31T06:25:47","date_gmt":"2014-10-31T06:25:47","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=773"},"modified":"2014-10-31T10:03:46","modified_gmt":"2014-10-31T10:03:46","slug":"773","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2014\/773","title":{"rendered":"SBrick beta testing"},"content":{"rendered":"<p>At <a href=\"https:\/\/www.sbrick.com\/\">SBrick<\/a> (or SmartBrick) Kickstarter campaign I pledged for beta tester. My beta SBrick arrived at last from Hungary:<\/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>As it&#8217;s still an early version it has some issues (one of\u00a0 the four channels seems\u00a0 to be damaged and LEGO Power Function cables don&#8217;t fit tight to another channel&#8217;s plug) but it&#8217;s good enough for testing connectivity with Linux, particularly with ev3dev &#8211; my main goal as beta tester is to help in connecting LEGO Mindstorms EV3 to the Sbrick.<\/p>\n<p>SBrick exposes 6 Bluetooth 4.0 Low Energy \u00abservices\u00bb (I am not familiar with the BLE jargon yet): Generic Access, Device Information and another 4 specific from the vendor. The first and second services expose this information:<\/p>\n<p>Device Name = SBrick<br \/>\nAppearance = Generic Remote Control<br \/>\nModel Number = 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>The other 4 services are specific from Vengit and expose a total of 8 \u00abfields\u00bb:<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>Meanwhile I got from Vengit the minimal information needed for controlling a motor connected to one of the 4 channels: the reomote control service UUID is &#8216;4dc591b0-857c-41de-b5f1-15abda665b0c&#8217; and the remote control characteristic is &#8216;2b8cbcc-0e25-4bda-8790-a15f53e6010f&#8217;.<\/p>\n<p>For practical uses, using the &#8216;gatttool&#8217; command from BlueZ 5 (the Linux Bluetooth stack), the above information translates to writing to handle 0x0025 the commands supported by the SBrick firmware.<\/p>\n<p>I only know two of those commands (00h = BRAKE e 01h = DRIVE)<\/p>\n<ul>\n<li>BRAKE Channel<\/li>\n<li>DRIVE Channel Direction DutyCycle<\/li>\n<\/ul>\n<p>&#8216;Channel&#8217; is one of the 4 output ports available and can be 00h, 01h, 02h or 03h.<\/p>\n<p>&#8216;Direction&#8217; can be clockwise (00h) or anticlockwise (01h).<\/p>\n<p>&#8216;DutyCycle&#8217; is the power pretended for the motor, can go from 00h (none or &#8220;Coast&#8221;) to FFh (&#8220;full power&#8221;).<\/p>\n<p>So to send a command from Linux (a Ubuntu PC, a Raspberry Pi with Raspbian or a Mindstorms EV3 with ev3dev) with a USB BT4.0\/BLE dongle one just need to use the &#8216;gatttool&#8217;:<\/p>\n<pre>$ gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=010000FF<\/pre>\n<p>The exemple above, sends to the Bluetooth device with address &#8217;00:07:80:7F:28:E1&#8242; (my SBrick) from the BT controller &#8216;hci0&#8217; (my dongle) the command<\/p>\n<pre>DRIVE Channel#0 Clockwise 100%<\/pre>\n<p>As this command doesn&#8217;t keep the bluetooth connection open, the motor spins for around\u00a0 3 secondss, then the connection drops and it stops (but if we use &#8216;gatttool&#8217; in interactive mode with option &#8220;-I&#8221; or &#8220;&#8211;interactive&#8221; and send the equivalent commands, the motor will keep spinning).<\/p>\n<p>Next video shows 2 motors spinning in opposite directions:<\/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>Next video shows an EV3 motor actiing as the reference for a Power Functionn Servo:<\/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>And a little better version of the same example, with a scale factor for matching the motor positions and a limitation of range to [-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>For that last video it was used this python script:<\/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  call (\"echo 0 &gt; \/sys\/class\/tacho-motor\/motor0\/position\", shell=True);\r\n  return;\r\n\r\ndef rd_ev3motor():\r\n\r\n  v=check_output(\"cat \/sys\/class\/tacho-motor\/motor0\/position\",shell=True);\r\n  return(trunc(float(v)));\r\n\r\ndef sbrick_drive(channel,direction,dutycycle):\r\n  \" calls gatttool command from BlueZ, the official Linux Bluetooth protocol stack\"\r\n\r\n  if(dutycycle &gt; 255):\r\n    dt_hexa=\"0xFF\";\r\n  else:\r\n    dt_hexa=str(hex(int(dutycycle)));\r\n\r\n  command=\"gatttool -b 00:07:80:7F:28:E1 -i hci0 --char-write --handle=0x0025 --value=01\"+channel+direction+dt_hexa[2:];\r\n#  print(command);\r\n  call (command, shell=True);\r\n\r\n  return;\r\n\r\ndef main():\r\n  try:\r\n    pos=0.0;\r\n    SCALE=255.0\/90.0;\r\n\r\n    reset_ev3motor();\r\n\r\n    while(True):\r\n      pos=rd_ev3motor()*SCALE;\r\n\r\n      if(pos&gt;0):\r\n        sbrick_drive(\"02\",\"00\",pos);\r\n      else:\r\n        sbrick_drive(\"02\",\"01\",-pos)\r\n\r\n      sleep(0.1);\r\n\r\n  except (KeyboardInterrupt, SystemExit):\r\n    print \"Exiting...\";\r\n  except Exception:\r\n    traceback.print_exc(file=sys.stdout);\r\n\r\n  exit(0);\r\n\r\nif __name__ == \"__main__\":\r\n  main()<\/pre>\n<p>I use the &#8216;call&#8217; function from library &#8216;subprocess&#8217; to get to &#8216;gatttool&#8217;. It&#8217;s an ugly trick but it works, I did&#8217;t find a python bluetooth BLE library yet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At SBrick (or SmartBrick) Kickstarter campaign I pledged for beta tester. My beta SBrick arrived at last from Hungary: As it&#8217;s still an early version it has some issues (one of\u00a0 the four channels seems\u00a0 to be damaged and LEGO Power Function cables don&#8217;t fit tight to another channel&#8217;s plug) but it&#8217;s good enough for &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2014\/773\" 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-773","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\/s2Mhyv-773","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/773","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=773"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=773"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}