<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Half-Byte &#187; Serial</title>
	<atom:link href="http://half-byte.com/tag/serial/feed/" rel="self" type="application/rss+xml" />
	<link>http://half-byte.com</link>
	<description>Doing more with less.</description>
	<lastBuildDate>Fri, 23 Sep 2011 17:57:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Servo Controller Driven over a USB-Serial Adapter</title>
		<link>http://half-byte.com/2009/09/20/servo-controller-driven-over-a-usb-serial-adapter/</link>
		<comments>http://half-byte.com/2009/09/20/servo-controller-driven-over-a-usb-serial-adapter/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 07:26:02 +0000</pubDate>
		<dc:creator>nibble</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[DB9]]></category>
		<category><![CDATA[Motor]]></category>
		<category><![CDATA[netbook]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[RS-232]]></category>
		<category><![CDATA[Serial]]></category>
		<category><![CDATA[Servo]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.half-byte.com/2009/09/20/servo-controller-driven-over-a-usb-serial-adapter/</guid>
		<description><![CDATA[<p>I recently thought up a great project idea that involved controlling motors from a PC.  I&#8217;ve done some experimenting with an Atmel USB interface in the past, but stopped when I hit a bug in the open source toolchain.  It was frustrating spending hours debugging only to find out the compiler wasn&#8217;t handing an [...]]]></description>
			<content:encoded><![CDATA[<p>I recently thought up a great project idea that involved controlling motors from a PC.  I&#8217;ve done some experimenting with an Atmel USB interface in the past, but stopped when I hit a bug in the open source toolchain.  It was frustrating spending hours debugging only to find out the compiler wasn&#8217;t handing an instruction correctly.</p>
<p>This time I wanted to build a prototype fairly quickly and increase the budget to do so.  Moving motors from a computer has been done many times before and it has been broken up into modular components.  So, I only had to determine what I wanted to move and get that connected to one of many types of PC ports.</p>
<p>The type of motor I wanted to start with was a servo.  So, I did some searching on the web and located a reasonably priced <a href="http://www.pololu.com/catalog/product/727" title="Pololu 8 servo controller" target="_blank">Pololu Servo controller</a> that receives commands over an RS-232 DB9 port.  Now, since most modern PC&#8217;s don&#8217;t have serial ports, I purchased a cheap USB to Serial adapter, the <a href="http://www.trendnet.com/products/proddetail.asp?prod=150_TU-S9&amp;cat=49" title="TU-S9" target="_blank">TU-S9</a>.  The adapter would also be useful for other projects since the serial interface is fairly basic.  Since I already had a servo from Parallax, that was all I needed.<br />
<a href="http://www.half-byte.com/wp-content/uploads/2009/09/servo_controller.jpg" title="ServoController"><img src="http://www.half-byte.com/wp-content/uploads/2009/09/servo_controller.thumbnail.jpg" alt="ServoController" align="right" /></a><br />
Parts:</p>
<ul>
<li>USB-Serial adapter: <a href="http://www.trendnet.com/products/proddetail.asp?prod=150_TU-S9&amp;cat=49" title="TRENDnet TU-S9" target="_blank">TRENDnet TU-S9</a></li>
<li>Serial Servo Controller: <a href="http://www.pololu.com/catalog/product/727" title="Pololu 8 servo controller" target="_blank">Pololu 8 Servo Serial Controller</a></li>
<li>Servo Motor: <a href="http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/servo/List/0/SortField/4/ProductID/101/Default.aspx" title="Parallax Standard Servo" target="_blank">Parallax Standard Servo</a></li>
</ul>
<p>The TU-S9 is a nice adapter since it is built with the <a href="http://www.google.com/#hl=en&amp;source=hp&amp;q=PL-2303&amp;btnG=Google+Search&amp;aq=f&amp;aqi=g10&amp;oq=PL-2303&amp;fp=ee36edbd3c16a1c5" title="PL-2303" target="_blank">PL-2303</a> chipset and is supported on most Linux distros.  Once you plug it in your PC, you should see &#8216;/dev/ttyUSB0&#8242; appear.  This worked without problems on my <a href="http://www.canonical.com/projects/ubuntu/unr" title="Ubuntu Netbook Remix" target="_blank">Ubuntu Netbook Remix</a> distro.</p>
<p>The servo controller is nice, but I initially had some problems with getting the configuration right.  I started with a modified test application with a tcl UI (created for the 16 servo controller).  When that didn&#8217;t work, I moved on to a Python test script.  The servo&#8217;s didn&#8217;t budge with it either.  The only thing that worked was a .Net test application on an old Windows PC.  It was odd since running the Python script on that same PC wouldn&#8217;t work, even when the transfer signals were identical for both.</p>
<p>Since I didn&#8217;t have the source code for the .Net test app, I couldn&#8217;t find out what serial configuration could be different between the other applications.  At one point, I simply pulled out the default DTR/RTS jumper and gave it a go.  This ended up being the problem.  The .Net app must have handled the RTS line properly so that it didn&#8217;t reset the controller.  Wow!  I wish I tested the other RS-232 lines!</p>
<p>In the end I was able to whip together a simple python demo script (see below) that moves the servo to various positions.  Feel free to use it, I got inspiration from a previous <a href="http://dmt195.wordpress.com/2009/01/19/python-to-interface-with-the-pololu-8-channel-servo-controller/" title="Python Test Script" target="_blank">test script</a>.</p>
<p><span id="more-32"></span><br />
<strong><em>(WordPress doesn&#8217;t like source code.  So bear with me.)</em></strong><br />
<code> # servoDemo.py<br />
import serial<br />
import sys<br />
import time</p>
<p># Helper function<br />
def setpos(n,angle):<br />
   #Check that things are in range<br />
   if angle &gt; 180 or angle &lt;0:<br />
      angle=90<br />
   # Must be between 500-5500<br />
   data = int(5000 * angle / 180) + 500<br />
   datatwo = data &amp; 0x7F<br />
   dataone = (data &gt;&gt; 7) &amp; 0x7F<br />
   buf = chr(0x80)+chr(0x01)+chr(0x04)+chr(n)+chr(dataone)+chr(datatwo)</p>
<p>   # write the buffer then sleep for a while<br />
   ser.write(buf)<br />
   time.sleep(1)<br />
### Main: script start ###</p>
<p># set up the serial port<br />
ser=serial.Serial()<br />
ser.baudrate=9600<br />
ser.baudrate = 9600<br />
ser.port = '/dev/ttyUSB0'<br />
ser.open()</p>
<p># send some position commands and watch it move<br />
setpos(7,30)<br />
setpos(7,90)<br />
setpos(7,180)<br />
setpos(7,45)<br />
setpos(7,135)<br />
setpos(7,90)<br />
setpos(7,45)<br />
setpos(7,30)<br />
setpos(7,45)<br />
setpos(7,30)<br />
setpos(7,45)<br />
setpos(7,30)<br />
setpos(7,135)<br />
setpos(7,30)<br />
setpos(7,90)</code></p>
]]></content:encoded>
			<wfw:commentRss>http://half-byte.com/2009/09/20/servo-controller-driven-over-a-usb-serial-adapter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

