{"id":370,"date":"2011-04-23T19:57:56","date_gmt":"2011-04-23T17:57:56","guid":{"rendered":"http:\/\/mclightning.com\/?p=370"},"modified":"2011-04-23T19:57:56","modified_gmt":"2011-04-23T17:57:56","slug":"self-balancing-robot-first-stepthe-interaction","status":"publish","type":"post","link":"https:\/\/maximusabrams.com\/?p=370","title":{"rendered":"Self-Balancing Robot First Step:The Interaction"},"content":{"rendered":"<p><object width=\"480\" height=\"390\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/aCyUgayTXpU?fs=1&amp;hl=en_US\" \/><param name=\"allowFullScreen\" value=\"true\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><\/object><\/p>\n<p>Here is the first step in making of <a href=\"http:\/\/mclightning.com\/self-balancing-robot-project-starts\/announcements\">Self Balancing Robot<\/a>. In this step i worked on Bluetooth &amp; Serial Communication.<br \/>\nCommunication scheme:<br \/>\n<a href=\"http:\/\/mclightning.com\/alertme\"><strong>N95 <\/strong><\/a>&#8212;<em>(Bluetooth)<\/em>&#8212;&gt; <strong>PC <\/strong>&#8212;&#8211;<em>( Serial)<\/em>&#8212;-&gt; <a href=\"http:\/\/mclightning.com\/my-arduino-diy-shields\/uncategorized\"><strong>Arduino<\/strong><\/a><br \/>\nI used the same <a href=\"http:\/\/mclightning.com\/narduino-20\/uncategorized\">motor driver shield<\/a> which i already made back in <a href=\"http:\/\/mclightning.com\/narduino\">Narduino Project<\/a>.<br \/>\nIf you havent heard about Narduino Shield and its simple design done on veroboard <a href=\"http:\/\/mclightning.com\/narduino-20\/uncategorized\">click here to see it and its circuit diagram<\/a>.<br \/>\nAfter the Narduino shield properly connected to motor wires and the supply connection ,\u00a0 it is time to talk about coding&#8230;<br \/>\nHere below , i shared all the codes used in the project.<br \/>\n<!--more--><\/p>\n<h4>Desktop_AccBalance.py<\/h4>\n<pre lang=\"python\">from bluetooth import *\nimport serial\nserver_sock=BluetoothSocket( RFCOMM )\nserver_sock.bind((\"\",PORT_ANY))\nserver_sock.listen(1)\nport = server_sock.getsockname()[1]\ns=serial.Serial(2,9600)\nuuid = \"94f39d29-7d6d-437d-973b-fba39e49d4ee\"\nadvertise_service( server_sock, \"SampleServer\",\n                   service_id = uuid,\n                   service_classes = [ uuid, SERIAL_PORT_CLASS ],\n                   profiles = [ SERIAL_PORT_PROFILE ],\n#                   protocols = [ OBEX_UUID ]\n                    )\nprint \"Waiting for connection on RFCOMM channel %d\" % port\nclient_sock, client_info = server_sock.accept()\nprint \"Accepted connection from \", client_info\ntry:\n    while True:\n        data = client_sock.recv(1024)\n        if len(data) == 0:\n\t\t    break\n        print data\n        s.write(data)\nexcept IOError:\n    pass\nprint \"disconnected\"\nclient_sock.close()\nserver_sock.close()\nprint \"all done\"<\/pre>\n<h4>N95_AccBalance.py<\/h4>\n<pre lang=\"python\">import socket,axyz,appuifw,e32\nsock=0\nw=True\ntp=0\ndef connect():\n    global sock\n    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)\n    addr,services=socket.bt_discover()\n    print \"Discovered: %s, %s\"%(addr,services)\n    if len(services)&gt;0:\n        import appuifw\n        choices=services.keys()\n        choices.sort()\n        choice=appuifw.popup_menu([unicode(services[x])+\": \"+x\n                                   for x in choices],u'Choose port:')\n        port=services[choices[choice]]\n    else:\n        port=services[services.keys()[0]]\n    address=(addr,port)\n    print \"Connecting to \"+str(address)+\"...\",\n    sock.connect(address)\n    print \"OK.\"\nconnect()\ndef quit():\n sock.close()\n global w\n w=False\nappuifw.app.menu = [(u\"Exit\",quit)]\ndef acc(x,y,z):\n    global sock,acc,tp,j,w\n    j=z+100\n    if( abs(tp-j)&gt;1):\n        sock.send(str(j))\n        tp=j\n    axyz.disconnect()\n    e32.ao_sleep(0.05)\n    if w==True:\n        axyz.connect(acc)\naxyz.connect(acc)<\/pre>\n<h4>Arduino_AccBalance<\/h4>\n<pre lang=\"c\">int mot1ana=3;\nint mot1a=2;\nint mot1b=4;\n\/\/motor1\nint val=100;\nvoid setup()\n{\n Serial.begin(9600);\n pinMode(mot1ana,OUTPUT);\n pinMode(mot1a,OUTPUT);\n pinMode(mot1b,OUTPUT);\n}\nvoid loop()\n{\n int i, serAva;\n char inputBytes [7];\n char * inputBytesPtr = &amp;inputBytes[0];\n if (Serial.available()&gt;0)\n {\n   delay(5);\n   serAva = Serial.available();\n     for (i=0; i&lt;serAva; i++)\u00a0\u00a0\u00a0\u00a0\n {\ninputBytes[i] = Serial.read();\n}\n inputBytes[i] =\u00a0 '\u0000';\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n val= atoi(inputBytesPtr);\u00a0\n }\n if(val&gt;105)\n {\n  digitalWrite(mot1a,LOW);\n  digitalWrite(mot1b,HIGH);\n  analogWrite(mot1ana,200);\n }\n  if(val&lt;85)\n {\n  digitalWrite(mot1a,HIGH);\n  digitalWrite(mot1b,LOW);\n  analogWrite(mot1ana,200);\n }\n else if(val&lt;105 &amp;&amp; val&gt;85)\n {\n  digitalWrite(mot1a,LOW);\n  digitalWrite(mot1b,LOW);\n  analogWrite(mot1ana,0);\n }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is the first step in making of Self Balancing Robot. In this step i worked on Bluetooth &amp; Serial Communication. Communication scheme: N95 &#8212;(Bluetooth)&#8212;&gt; PC &#8212;&#8211;( Serial)&#8212;-&gt; Arduino I used the same motor driver shield which i already made back in Narduino Project. If you havent heard about Narduino Shield and its simple design [&hellip;]<\/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":""},"categories":[2,3,5,6,1],"tags":[18,36,41,136,197,205,264,276,278],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-diy","category-electronics-2","category-hobby-rc","category-mobile","category-uncategorized","tag-accelerometer","tag-arduino","tag-balancing","tag-gyro","tag-motor-driver","tag-n95","tag-robot","tag-self","tag-sensor"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/maximusabrams.com\/index.php?rest_route=\/wp\/v2\/posts\/370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/maximusabrams.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maximusabrams.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maximusabrams.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maximusabrams.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=370"}],"version-history":[{"count":0,"href":"https:\/\/maximusabrams.com\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions"}],"wp:attachment":[{"href":"https:\/\/maximusabrams.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maximusabrams.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maximusabrams.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}