{"id":161,"date":"2012-03-29T03:58:04","date_gmt":"2012-03-29T03:58:04","guid":{"rendered":"https:\/\/peterklemperer.com\/blog\/?p=161"},"modified":"2012-03-30T18:53:32","modified_gmt":"2012-03-30T18:53:32","slug":"nxt-bluetooth-echo-terminal-with-bonus-mobile-robot","status":"publish","type":"post","link":"https:\/\/peterklemperer.com\/blog\/2012\/03\/29\/nxt-bluetooth-echo-terminal-with-bonus-mobile-robot\/","title":{"rendered":"NXT Bluetooth Echo Terminal with Bonus Mobile Robot"},"content":{"rendered":"<p>Today I worked on making my NXT communicate with my laptop over Bluetooth. This is great because you can untether your projects and control your robots programmatically.<\/p>\n<ol>\n<li>Set up the NXT Bluetooth with your mac as shown here,\u00a0http:\/\/tonybuser.com\/bluetooth-serial-port-to-nxt-in-osx<\/li>\n<li>Open a terminal and type:\u00a0screen \/dev\/tty.NXT-DevB<\/li>\n<li>Start the sample code listed below in RobotC.<\/li>\n<li>In the mac terminal, type &#8216;S&#8217; and verify that it is echo&#8217;d back to the terminal and a start message displayed on the NXT Brick LCD.<\/li>\n<\/ol>\n<p>See the code for explanation of how to set up the motors and control them remotely.<\/p>\n<p><!--more--><\/p>\n<p>Sample code is as follows, it is shameless stolen from the RobotC sample code BT Lowlevel.c:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 16px; color: #444444; font-family: Georgia, 'Bitstream Charter', serif; line-height: 24px; white-space: normal;&quot;&gt;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;\/span&gt;\r\n\/\/\r\n\/\/ Simple Bluetooth &quot;Echo&quot; Terminal Server\r\n\/\/\r\n\/\/ The NXT Bluecore module supports connects using the SPP (Serial Port Profile) defined in the Bluetooth\r\n\/\/ protocol specifications. There may be occasion when it is desirable to connect the NXT to another\r\n\/\/ Bluetooth device in a &quot;raw&quot; format where the end user application program has direct control over\r\n\/\/ every byte transmitted and received via the Bluecore module. For example, connecting to a BT enabled\r\n\/\/ GPS receiver or connecting to a BT enabled cell phone or PDA.\r\n\/\/\r\n\/\/ This sample program contains a short program demonstrating raw access to the Bluetooth data stream.\r\n\/\/\r\n\/\/ NOTE: Normally the data transmission uses a higher level protocol that LEGO has defined for sending\r\n\/\/ messages back and forth between NXT devices. This protocol is bypassed by this programs.\r\n\/\/\r\n\/\/ There are only three functions and one variable that are needed to operate the NXT Blucecore in &quot;raw&quot;\r\n\/\/\r\n\/\/ setBluetoothRawDataMode() This function initiates Bluecore module to switch to &quot;raw&quot; mode.\r\n\/\/\r\n\/\/ bBTRawMode Read only boolean variable that can be tested to confirm when Blucore\r\n\/\/ has entered &quot;raw&quot; mode.\r\n\/\/\r\n\/\/ nxtReadRawBluetooth(pData, nMaxBufferSize)\r\n\/\/ Reads one or more &quot;raw&quot; bytes from Bluecore. Returned value is the\r\n\/\/ number of bytes read.\r\n\/\/\r\n\/\/ nxtWriteRawBluetooth(pData, nLength)\r\n\/\/ Writes 'nLength' bytes in raw mode from buffer 'pData'\r\n\/\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n#pragma platform(NXT)\r\nshort nMsgXmit = 0;\r\nshort nLastCharReceived = 0;\r\nconst int kPacketSize = 8; \/\/ Number of hex digits that can be displayed on one line\r\n\/\/\r\n\/\/ Function and task prototypes\r\n\/\/\r\nvoid checkBTLinkConnected();\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/\r\n\/\/ Main Task\r\n\/\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ntask main()\r\n{\r\nconst int kSendSize = 1;\r\nstatic ubyte BytesToSend&#x5B;kSendSize];\r\nint nNumbBytesRead;\r\nconst int kHexDigitsPerLine = 1;\r\nint nIndex = 0;\r\nubyte BytesRead&#x5B;kHexDigitsPerLine]; \/\/ Circular buffer of last bytes read.\r\n\/\/\r\n\/\/ Test Ability to Turn Bluetooth On or Off\r\n\/\/\r\ncheckBTLinkConnected();\r\n\/\/\r\n\/\/ Initialize Bluecore to &quot;raw data&quot; mode. This will be automatically reset when application terminates\r\n\/\/\r\nsetBluetoothRawDataMode();\r\nwhile (!bBTRawMode)\r\n{\r\n\/\/ Wait for Bluecore to enter raw data mode\r\nwait1Msec(1);\r\n}\r\n\/\/\r\n\/\/ Start separate tasks to read and write raw data over BT. The link operates in full-duplex mode.\r\n\/\/\r\neraseDisplay();\r\nbNxtLCDStatusDisplay = true; \/\/ Enable top status line display\r\n\/\/StartTask(sendRawData);\r\n\/\/StartTask(readRawData);\r\nwhile (true)\r\n{\r\n\/\/\r\n\/\/ Display progress results on the NXT LCD screen.\r\n\/\/\r\n\/\/nxtDisplayBigStringAt(0, 31, &quot;Xmit Rcv&quot;);\r\n\/\/nxtDisplayBigStringAt(0, 15, &quot; %02X %02X&quot;, nMsgXmit, nLastCharReceived &amp; 0x00FF);\r\n\/\/\r\n\/\/ Loop continuously, reading one byte at a time. The interface will support larger reads.\r\n\/\/\r\nnNumbBytesRead = nxtReadRawBluetooth(BytesRead&#x5B;nIndex], 1);\r\nif (nNumbBytesRead == 0)\r\n{\r\nwait1Msec(10);\r\ncontinue;\r\n}\r\nelse if (nNumbBytesRead == 1 &amp;&amp; BytesRead&#x5B;0] == 'S')\r\n{\r\nnxtDisplayString(1, &quot;Received Start&quot;);\r\n}\r\nelse if (nNumbBytesRead == 1 &amp;&amp; BytesRead&#x5B;0] == 'w')\r\n{\r\nnxtDisplayString(4, &quot;Forward Signal&quot;);\r\nmotor&#x5B;motorA] = 25;\r\nmotor&#x5B;motorB] = 25;\r\n}\r\nelse if (nNumbBytesRead == 1 &amp;&amp; BytesRead&#x5B;0] == 's')\r\n{\r\nnxtDisplayString(4, &quot;Backward Signal&quot;);\r\nmotor&#x5B;motorA] = -25;\r\nmotor&#x5B;motorB] = -25;\r\n}\r\nelse if (nNumbBytesRead == 1 &amp;&amp; BytesRead&#x5B;0] == 'd')\r\n{\r\nnxtDisplayString(4, &quot;Right Turn Signal&quot;);\r\nmotor&#x5B;motorA] = 25;\r\nmotor&#x5B;motorB] = -25;\r\n}\r\nelse if (nNumbBytesRead == 1 &amp;&amp; BytesRead&#x5B;0] == 'a')\r\n{\r\nnxtDisplayString(4, &quot;Left Turn Signal&quot;);\r\nmotor&#x5B;motorA] = -25;\r\nmotor&#x5B;motorB] = 25;\r\n}\r\nelse if (nNumbBytesRead == 1 &amp;&amp; BytesRead&#x5B;0] == 'z')\r\n{\r\nnxtDisplayString(4, &quot;ALL STOP Signal&quot;);\r\nmotor&#x5B;motorA] = 0;\r\nmotor&#x5B;motorB] = 0;\r\n}\r\nmemset(BytesToSend, BytesRead&#x5B;nIndex], sizeof(BytesToSend));\r\nnxtWriteRawBluetooth(BytesToSend, sizeof(BytesToSend));\r\n}\r\nreturn;\r\n}\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/\r\n\/\/ checkBTLinkConnected\r\n\/\/\r\n\/\/ Utility function to check whether Bluetooth link is set up. It should be manually set up before\r\n\/\/ launching this program. &#x5B;You can also set it up within an application program; there's a separate\r\n\/\/ sample program for this.\r\n\/\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nvoid checkBTLinkConnected()\r\n{\r\nif (nBTCurrentStreamIndex &gt;= 0)\r\nreturn; \/\/ An existing Bluetooth connection is present.\r\n\/\/\r\n\/\/ Not connected. Audible notification and LCD error display\r\n\/\/\r\nPlaySound(soundLowBuzz);\r\nPlaySound(soundLowBuzz);\r\neraseDisplay();\r\nnxtDisplayCenteredTextLine(3, &quot;BT not&quot;);\r\nnxtDisplayCenteredTextLine(4, &quot;Connected&quot;);\r\nwait1Msec(3000);\r\nStopAllTasks();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I worked on making my NXT communicate with my laptop over Bluetooth. This is great because you can untether your projects and control your robots programmatically. Set up the NXT Bluetooth with your mac as shown here,\u00a0http:\/\/tonybuser.com\/bluetooth-serial-port-to-nxt-in-osx Open a &hellip; <a href=\"https:\/\/peterklemperer.com\/blog\/2012\/03\/29\/nxt-bluetooth-echo-terminal-with-bonus-mobile-robot\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[38,6],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-line-following","category-projects"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1VqWo-2B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":0,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}