<?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>wuetender-junger-mann.de &#187; Ruby</title>
	<atom:link href="http://wuetender-junger-mann.de/wordpress/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://wuetender-junger-mann.de/wordpress</link>
	<description>Der Freiheit eine Gasse</description>
	<lastBuildDate>Tue, 20 Sep 2011 12:40:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Train Build Monitor</title>
		<link>http://wuetender-junger-mann.de/wordpress/2010/08/the-train-build-monitor/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2010/08/the-train-build-monitor/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 12:52:13 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Mechanik]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=1003</guid>
		<description><![CDATA[On our current project we came up with a model train build monitor. The objective was to have the train move, while the build is green and to stop, when it goes red. The whole thing looks some what like this: As a USB interface we chose to go for the Velleman K8055, which is [...]]]></description>
			<content:encoded><![CDATA[<p>On our current project <a href="http://clipboarded.blogspot.com/">we</a> came up with a model train build monitor. The objective was to have the train move, while the build is green and to stop, when it goes red. The whole thing looks some what like this:</p>
<p>
<a href="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2010/08/train.jpg"><img src="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2010/08/train.jpg" alt="" title="Build Monitor" width="770" height="451" class="aligncenter size-full wp-image-1005" /></a>
</p>
<p>
As a USB interface we chose to go for the  <a href="http://www.velleman.eu/distributor/products/view/?country=be&#038;lang=en&#038;id=351346">Velleman K8055</a>, which is available for about forty pounds and provides eight digital and two analogue outputs as well as two analogue and five digital inputs. The analogue outputs are provided as <a href="http://en.wikipedia.org/wiki/Pulse-width_modulation">pwm</a> signal.<br />
The outputs are all open-collectors. Being a good developer I did of course anticipate a few more use-cases and hence designed the controller, so we could actually control the speed as well as the direction in which the train in moving. Essentially it uses a transistor to switch the train (using the pwm) and a relay to reverse the direction (yes this is somewhat lame). Also there is two free-wheeling diodes to protect the electronics from the high inductivity of the relay and the trains motor. This is the circuit diagram of the controller that goes between the K8055 and the train:
</p>
<p><a href="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2010/08/circuit-diagram.jpg"><img src="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2010/08/circuit-diagram.jpg" alt="" title="circuit diagram" width="818" height="531" class="aligncenter size-full wp-image-1004" /></a></p>
<p>
The K0855-board ships with a DLL to control the IO. Apparently there is a linux driver, which is much better than the windows version, but we are in a bit of a windows shop, so we went with the DLL. It turned out wrapping a DLL in a ruby script is fairly trivial:
</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'Win32API'</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">open</span> = Win32API.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;K8055D&quot;</span>, <span style="color:#996600;">&quot;OpenDevice&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'L'</span><span style="color:#006600; font-weight:bold;">&#93;</span> , <span style="color:#996600;">'L'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
outputAnalog = Win32API.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;K8055D&quot;</span>, <span style="color:#996600;">&quot;OutputAnalogChannel&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'L'</span>,<span style="color:#996600;">'L'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#996600;">'V'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
outputDigital = Win32API.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;K8055D&quot;</span>, <span style="color:#996600;">&quot;WriteAllDigital&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'L'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#996600;">'V'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
inputDigital = Win32API.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;K8055D&quot;</span>, <span style="color:#996600;">&quot;ReadAllDigital&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#996600;">'L'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">open</span>.<span style="color:#9900CC;">Call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
outputAnalog.<span style="color:#9900CC;">Call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># setting DA channel 1 to 10</span>
outputDigital.<span style="color:#9900CC;">Call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">128</span><span style="color:#006600; font-weight:bold;">&#41;</span>    <span style="color:#008000; font-style:italic;"># reversing</span></pre></div></div>

<p>Future plans include figuring out where the train currently is to stop in the station. The original plan was to use a reed switch, but that proved to be a bit unreliable, so the current thinking involves using a camera and something like <a href="http://www.wedesoft.demon.co.uk/hornetseye-api/files/HornetsEye-txt.html">hornetseye</a> to get the exact position of the train.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2010/08/the-train-build-monitor/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Generische Programmierung</title>
		<link>http://wuetender-junger-mann.de/wordpress/2006/05/generische-programmierung/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2006/05/generische-programmierung/#comments</comments>
		<pubDate>Tue, 16 May 2006 21:20:10 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software, alt]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=112</guid>
		<description><![CDATA[Heute stellte sich mir zum zweiten Mal innert kurzer Zeit die aufgabe Elemente einer flachen Liste nach bestimmten Kriterien zu gruppieren. Da der Code dafür eher fehleranfällig ist, hatte ich mir vorgenommen die Sache wiederverwendbar zu implementieren. Dazu griff ich zunächst zu Ruby. Die entstandene Funktion wird so verwendet, um eine Liste von Namen in [...]]]></description>
			<content:encoded><![CDATA[<p>Heute stellte sich mir zum zweiten Mal innert kurzer Zeit die aufgabe Elemente einer flachen Liste nach bestimmten Kriterien zu gruppieren. Da der Code dafür eher fehleranfällig ist, hatte ich mir vorgenommen die Sache wiederverwendbar zu implementieren. Dazu griff ich zunächst zu Ruby.</p>
<p>Die entstandene Funktion wird so verwendet, um eine Liste von Namen in eine Map zu überführen, die für jeden Anfangsbuchstaben eine Liste von Namen enthält:</p>
<p><code>require 'pp'
</code></p>
<blockquote><p><code>names = ["Felix","Alexey","Frank","Ivan","Irina"]</code><br />
<code>grouped_names = group(names) { |element|</code><br />
<code>element[0..0]</code><br />
<code>&lt;/blockquote&gt;
&lt;code&gt; }
pp grouped_names</code></p>
<p>Die Ausgabe sieht ganz vernünftig aus:</p>
<p><code>{"A"=&gt;["Alexey"], "F"=&gt;["Felix", "Frank"], "I"=&gt;["Ivan", "Irina"]}</code></p>
<p>Und nun zur Implementierung:</p>
<p><code>  def group(list, &amp;category)
</code></p>
<blockquote><p><code> map=Hash.new</code><br />
<code> list.each{|element|</code></p></blockquote>
<blockquote><p><code> cat=category.call(element)</code><br />
<code> if (map.has_key?(cat))</code></p></blockquote>
<blockquote><p><code> grp=map[cat]</code></p></blockquote>
<p><code> else</code></p>
<blockquote><p><code> grp=Array.new</code><br />
<code> map[cat]=grp;</code></p></blockquote>
<p><code> end</code><br />
<code> grp&lt; &lt;(element);</code><br />
</code><code> }</code><br />
<code> return map</code><br />
<code>
</code><code> end </code></p>
<p>Ich fand's so schön, dass ich beinahe weinen musste, aber nur beinahe.</p>
<p>Ich wurde allerdings gleich wieder wütend, als ich feststellte, dass ich bei WordPress nicht weiß, wie man code snippets einbindet...</p>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2006/05/generische-programmierung/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

