<?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>Oli&#039;s 2 cents</title>
	<atom:link href="http://www.oelerich.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oelerich.org</link>
	<description>webdevelopment, physics &#039;n&#039; stuff</description>
	<lastBuildDate>Tue, 14 Feb 2012 08:48:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>I love free software!</title>
		<link>http://www.oelerich.org/i-love-free-software/</link>
		<comments>http://www.oelerich.org/i-love-free-software/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 08:48:06 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=422</guid>
		<description><![CDATA[And today is the &#8220;I love free software day&#8221; where everybody is encouraged to express his love for free software. I am doing it with this post. Have a nice valentines and free software day!]]></description>
			<content:encoded><![CDATA[<p>And today is the &#8220;I love free software day&#8221; where everybody is encouraged to express his love for free software. I am doing it with this post.</p>
<p><a href="http://fsfe.org/campaigns/ilovefs/2012/ilovefs-2012.html"><img class="aligncenter" style="border: 0 !important;" src="http://fsfe.org/campaigns/valentine/vd-i-love-fs-trans-150x90.png" alt="I love Free Software!" /></a></p>
<p>Have a nice valentines and free software day!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/i-love-free-software/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Openbox pipemenu to control virtual machines of VirtualBox</title>
		<link>http://www.oelerich.org/openbox-pipemenu-to-control-virtual-machines-of-virtualbox/</link>
		<comments>http://www.oelerich.org/openbox-pipemenu-to-control-virtual-machines-of-virtualbox/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 16:05:21 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[openbox]]></category>
		<category><![CDATA[virtualbox]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=411</guid>
		<description><![CDATA[Openbox, Virtualbox&#8230; Coincidence? I don&#8217;t know. Anyway, I am a huge fan of VirtualBox, for the virtual machines are extremely fast, stable and nice to control. With the command line tool VBoxManage comes a nice tool for all those CLI-lovers out there. You can literally do anything with it, what the GUI can do. (at [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://openbox.org/">Openbox</a>, <a href="https://www.virtualbox.org/">Virtualbox</a>&#8230; Coincidence? I don&#8217;t know. Anyway, I am a huge fan of VirtualBox, for the virtual machines are extremely fast, stable and nice to control. With the command line tool <code class="codecolorer text default"><span class="text">VBoxManage</span></code> comes a nice tool for all those CLI-lovers out there. You can literally do anything with it, what the GUI can do. (at least it looks like that) </p>
<p>Now, starting the GUI to start a VM is a bit slow, so <code class="codecolorer text default"><span class="text">VBoxManage startvm &quot;vm&quot;</span></code> is a huge time- and memorysaver.<br />
Since I work with Openbox, the natural thing for me was to google for a suitable <strong>pipemenu</strong>. Unfortunately, I couldn&#8217;t find one. Long story short: Here is my self-written Openbox pipemenu to control VirtualBox&#8217;s VMs. (It can start and stop them, at least.)<br />
<span id="more-411"></span><br />
Put it somewhere, make it executable and set it as a pipemenu in your menu.xml file.</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span><span style="color: #66cc66;">,</span><span style="color: #dc143c;">re</span><br />
<br />
<span style="color: #808080; font-style: italic;"># kill action: 0 -&gt; power off, 1 -&gt; save state</span><br />
killAction <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span><br />
<br />
<span style="color: #808080; font-style: italic;"># Only change anything below this comment if you know, what you're doing!!</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;?xml version=<span style="color: #000099; font-weight: bold;">\&quot;</span>1.0<span style="color: #000099; font-weight: bold;">\&quot;</span> encoding=<span style="color: #000099; font-weight: bold;">\&quot;</span>UTF-8<span style="color: #000099; font-weight: bold;">\&quot;</span>?&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;openbox_pipe_menu&gt;&quot;</span><span style="color: black;">&#41;</span><br />
<br />
killActions &nbsp; <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">'poweroff'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'savestate'</span><span style="color: black;">&#93;</span><br />
notRunningVms <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
runningVms &nbsp; &nbsp;<span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
vmNamePattern <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&quot;(.+)&quot; .*'</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> vm <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">check_output</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'VBoxManage'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'list'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'vms'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">decode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;utf-8&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; match <span style="color: #66cc66;">=</span> vmNamePattern.<span style="color: black;">match</span><span style="color: black;">&#40;</span>vm<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> match:<br />
&nbsp; &nbsp; &nbsp; &nbsp; notRunningVms.<span style="color: black;">append</span><span style="color: black;">&#40;</span>match.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> vm <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">check_output</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'VBoxManage'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'list'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'runningvms'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">decode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;utf-8&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; match <span style="color: #66cc66;">=</span> vmNamePattern.<span style="color: black;">match</span><span style="color: black;">&#40;</span>vm<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> match:<br />
&nbsp; &nbsp; &nbsp; &nbsp; runningVms.<span style="color: black;">append</span><span style="color: black;">&#40;</span>match.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; notRunningVms.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>match.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>notRunningVms<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;separator label=<span style="color: #000099; font-weight: bold;">\&quot;</span>Start VM<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> vm <span style="color: #ff7700;font-weight:bold;">in</span> notRunningVms:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;item label=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> + vm + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;action name=<span style="color: #000099; font-weight: bold;">\&quot;</span>Execute<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;execute&gt;VBoxManage startvm <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> + vm + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&lt;/execute&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/action&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;/item&gt;&quot;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>runningVms<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;separator label=<span style="color: #000099; font-weight: bold;">\&quot;</span>Stop VM<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> vm <span style="color: #ff7700;font-weight:bold;">in</span> runningVms:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;item label=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> + vm + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;action name=<span style="color: #000099; font-weight: bold;">\&quot;</span>Execute<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;execute&gt;VBoxManage controlvm <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> + vm + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> &quot;</span> + killActions<span style="color: black;">&#91;</span>killAction<span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;&lt;/execute&gt;&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/action&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;/item&gt;&quot;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/openbox_pipe_menu&gt;&quot;</span><span style="color: black;">&#41;</span></div></div>
<p>To set whether VMs should be powered off or if their state should be saved, edit the value of <code class="codecolorer text default"><span class="text">killAction</span></code> in the script. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/openbox-pipemenu-to-control-virtual-machines-of-virtualbox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My new emacs setup (no .emacs here, don&#8217;t worry)</title>
		<link>http://www.oelerich.org/my-new-emacs-setup/</link>
		<comments>http://www.oelerich.org/my-new-emacs-setup/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 19:10:29 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=345</guid>
		<description><![CDATA[After having used emacs for a couple of months now, I discovered the emacs --daemon option today and was astonished. Having quite a big emacs config, it takes ages for emacs to start preventing me from using it as the standard editor for config files etc. The --daemon mode starts emacs in the background acting [...]]]></description>
			<content:encoded><![CDATA[<p>After having used emacs for a couple of months now, I discovered the <code class="codecolorer text default"><span class="text">emacs --daemon</span></code> option today and was astonished. Having quite a big emacs config, it takes ages for emacs to start preventing me from using it as the standard editor for config files etc. The <code class="codecolorer text default"><span class="text">--daemon</span></code> mode starts emacs in the background acting as a server and allows one to create emacs frames using the <code class="codecolorer text default"><span class="text">emacsclient</span></code> in various ways. EMacs shows up lightning-fast then and becomes an option for the always-to-use text editor.</p>
<p><a href="http://www.oelerich.org/wp-content/uploads/2011/10/10848648.jpg"><img src="http://www.oelerich.org/wp-content/uploads/2011/10/10848648.jpg" alt="" title="10848648" width="400" height="300" class="aligncenter size-full wp-image-349" /></a><br />
<span id="more-345"></span><br />
Here is my setup on how to work with emacs and use it as an editor for quickly editing files on the command line.</p>
<h3>Starting the daemon using systemd</h3>
<p>I switched from sysVinit to systemd some time ago, so the most convenient thing to do is to start the emacs daemon using a custom systemd service file. Put the following code into the file  <code class="codecolorer text default"><span class="text">/etc/systemd/system/multi-user.target.wants/emacs.service</span></code>:</p>
<div class="codecolorer-container ini twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>Unit<span style="">&#93;</span></span><br />
<span style="color: #000099;">Description</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">Emacs: the extensible, self-documenting text editor</span><br />
<br />
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>Service<span style="">&#93;</span></span><br />
<span style="color: #000099;">Type</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">forking</span><br />
<span style="color: #000099;">ExecStart</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">/usr/bin/emacs --daemon</span><br />
<span style="color: #000099;">ExecStop</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">/usr/bin/emacsclient --eval </span><span style="color: #933;">&quot;(progn (setq kill-emacs-hook 'nil) (kill-emacs))&quot;</span><br />
<span style="color: #000099;">Restart</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">always</span><br />
<span style="color: #000099;">User</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">YOUR_USERNAME</span></div></div>
<p>Make sure you insert your username in the bottom line. Now, enable the service executing <code class="codecolorer text default"><span class="text">systemctl enable emacs.service</span></code> and after the next boot the daemon is started automatically.</p>
<h3>Shell aliases and sudo-ing</h3>
<p>One thing you will quickly notice is that <code class="codecolorer text default"><span class="text">sudo emacsclient ...</span></code> doesn&#8217;t work since the daemon runs under your username. To be able to edit files as root, we need to load the tramp plugin of emacs. Put the following line somewhere in your <code class="codecolorer text default"><span class="text">.emacs</span></code> or <code class="codecolorer text default"><span class="text">.emacs.d/init.el</span></code>:</p>
<div class="codecolorer-container lisp twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>require 'tramp<span style="color: #66cc66;">&#41;</span></div></div>
<p>Now, you would use <code class="codecolorer text default"><span class="text">emacsclient -c /sudo::/etc/somefile</span></code> which is still a bit too long for quick file editing. (As well as the emacsclient command itself). This is why we create shell aliases. I am using zsh which is why I will use its syntax here.</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EDITOR</span>=<span style="color: #ff0000;">&quot;emacsclient -t -n -a nano&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ec</span>=<span style="color: #ff0000;">'emacsclient -c -n -a nano'</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">et</span>=<span style="color: #ff0000;">'emacsclient -t -a nano'</span><br />
ecs<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> emacsclient <span style="color: #660033;">-c</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-a</span> emacs <span style="color: #ff0000;">&quot;/sudo::$*&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
ets<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> emacsclient <span style="color: #660033;">-t</span> <span style="color: #660033;">-a</span> emacs <span style="color: #ff0000;">&quot;/sudo::$*&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span></div></div>
<p>It sets emacs as the default editor and defines the commands <code class="codecolorer text default"><span class="text">ec</span></code> (emacs in a separate window) and <code class="codecolorer text default"><span class="text">et</span></code> (emacs in the terminal) as well as its sudo friends for write-protected files, <code class="codecolorer text default"><span class="text">ecs</span></code> and <code class="codecolorer text default"><span class="text">ets</span></code>. Simply put the code in your .zshrc or .bashrc (after editing it to fit bash&#8217;s syntax) and restart the terminal. Then you can use <code class="codecolorer text default"><span class="text">ec/et/ecs/ets /some/directory/some/file</span></code> to edit the file. <del datetime="2011-11-20T18:53:37+00:00">I think tramp uses ssh to connect to your pc as root, so you might need to install ssh to use it.</del></p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/my-new-emacs-setup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to make ReMoot work with mpd</title>
		<link>http://www.oelerich.org/how-to-make-remoot-work-with-mpd/</link>
		<comments>http://www.oelerich.org/how-to-make-remoot-work-with-mpd/#comments</comments>
		<pubDate>Tue, 31 May 2011 16:22:30 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[mpd]]></category>
		<category><![CDATA[remoot]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=329</guid>
		<description><![CDATA[Heya, I was struggling with ReMoot commands not having effect on the MPD (music player daemon), which is a nifty tool I discovered yesterday. Anyway, even though ReMoot lists this software as supported on it&#8217;s website, nothing happened when I executed &#8220;remoot playpause&#8221; or something like that. The reason is that ReMoot only looks for [...]]]></description>
			<content:encoded><![CDATA[<p>Heya, I was struggling with <a href="http://www.remoot.org/">ReMoot</a> commands not having effect on the <a href="https://wiki.archlinux.org/index.php/Mpd">MPD</a> (music player daemon), which is a nifty tool I discovered yesterday. Anyway, even though ReMoot lists this software as supported on it&#8217;s website, nothing happened when I executed &#8220;remoot playpause&#8221; or something like that.</p>
<p>The reason is that ReMoot only looks for programs that belong to the current user, while the mpd most of the times has it&#8217;s own user. (At least that is what&#8217;s proposed for example here)</p>
<p>To get ReMoot running with mpd you have to do the following two things:</p>
<ul>
<li>ReMoot uses mpc to interface with mpd, so make sure it is installed!</li>
<li>edit <em>/usr/bin/daemoot</em> (or whereever it may be) and change line 470 (version 0.9 of ReMoot) to <em>my @apps = `ps -L -u $ENV{USER},<strong>mpd</strong> -o comm,lwp`;</em> where, of course, mpd has to be the user the mpd runs under.</li>
</ul>
<p>That should be it. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/how-to-make-remoot-work-with-mpd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hide the ugly status bar in Pidgin</title>
		<link>http://www.oelerich.org/hide-the-ugly-status-bar-in-pidgin/</link>
		<comments>http://www.oelerich.org/hide-the-ugly-status-bar-in-pidgin/#comments</comments>
		<pubDate>Wed, 25 May 2011 20:47:33 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[hide statusbar]]></category>
		<category><![CDATA[pidgin]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=325</guid>
		<description><![CDATA[Hello together, a short summary what happened since the last blog entry and led me to the creation of a small Pidgin plugin. I switched to linux I decided to use Pidgin instant messanger I hated the big, fat, ugly status bar in pidgin I wasn&#8217;t able to find a plugin that hides it (and [...]]]></description>
			<content:encoded><![CDATA[<p>Hello together, a short summary what happened since the last blog entry and led me to the creation of a small Pidgin plugin.</p>
<ul>
<li>I switched to linux</li>
<li>I decided to use Pidgin instant messanger</li>
<li>I hated the big, fat, ugly status bar in pidgin</li>
<li>I wasn&#8217;t able to find a plugin that hides it (and does nothing more)</li>
<li>So I wrote my own</li>
</ul>
<p>It is just for Unix (I guess..) and you have to build it yourself. A small README is included in the tarball. </p>
<p><a href='http://www.oelerich.org/wp-content/uploads/2011/05/hidestatusbar.tar.gz'>Sourcecode hidestatusbar.tar</a></p>
<p>I hope it helps anybody.</p>
<p>All the best, Oli</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/hide-the-ugly-status-bar-in-pidgin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integrate Doctrine 2.0.1 with Zend Framework 1.11.3</title>
		<link>http://www.oelerich.org/integrate-doctrine-2-with-zend-framework-1-11-3/</link>
		<comments>http://www.oelerich.org/integrate-doctrine-2-with-zend-framework-1-11-3/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 16:56:28 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Doctrine ORM]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[doctrine 2]]></category>
		<category><![CDATA[integration]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=315</guid>
		<description><![CDATA[Hello readers! The most frequently visited and linked article on my blog is by far the tutorial, how to integrate Doctrine 2 with Zend Framework 1.09. However &#8211; it is written in german and since I published it, a couple of new versions of both libraries were released. So I decided to rewrite it in [...]]]></description>
			<content:encoded><![CDATA[<p>Hello readers! The most frequently visited and linked article on my blog is by far the tutorial, how to <a href="http://www.oelerich.org/?p=193">integrate Doctrine 2 with Zend Framework 1.09</a>. However &#8211; it is written in german and since I published it, a couple of new versions of both libraries were released. So I decided to rewrite it in english and adjust it so that it fits the most up-to-date versions of both <a href="http://www.doctrine-project.org">Doctrine</a> and the <a href="http://framework.zend.com/">Zend Framework</a>.</p>
<p>I will presume you know more or less exactly what these software packages do, and how to use them. Also, I assume that you are able to set up a virtual host by yourself. You should be familiar with how to use a command line, too!<br />
<span id="more-315"></span></p>
<h3>Step 1 &#8211; Download libraries and set up the project</h3>
<p>First of all, we need to download the libraries and set up our project.<br />
Start with the Zend Framework. Go to it&#8217;s <a href="http://framework.zend.com/download/latest">download page</a> and download (at least) the minimal version. Right now, it is ZF <em>1.11.3</em>. Unpack the download, go to a command line (whichever your system provides), change to the <em>bin/</em> folder of the package and type the following lines:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">chmod +x zf.sh<br />
./zf.sh create project /path/to/App</div></div>
<p>This is what you do on a Unix shell. It is slightly different on Windows machines, there you would have to use the <em>zf.bat</em> file. However, you will have to figure out how to do that on your own, since I don&#8217;t work on Windows and am too lazy to google it right now.</p>
<p>Your project should be created now, meaning that the folder <em>/path/to/App</em> should contain several subfolder such as <em>application/</em>, <em>library/</em> and so on. We create a subfolder with the name <em>bin/</em> in this folder. </p>
<p>What has to be done next is to move all the libraries into our project. First of all, in the downloaded package of the Zend Framework, there should be a <em>libraries/</em> folder containing another folder named <em>Zend/</em>. We move that <em>Zend/</em> folder into the <em>library/</em> folder of our project. </p>
<p>We then go on by downloading the Doctrine packages. I say packages, because we need the Doctrine ORM as well as the DBAL and the Doctrine Commons project. Luckily, all of them are included in one package, which can be downloaded on the <a href="http://www.doctrine-project.org/projects/orm/download">Doctrine ORM&#8217;s download page</a>. It is the latest Doctrine package. (As of now, it is version <em>2.0.1</em>) Unpack it and simply copy the <em>Doctrine/</em> folder into our project&#8217;s <em>library/</em> folder. </p>
<p>The last step in setting up the project is to move or copy the contents of the <em>bin/</em> folders of both downloaded packages into the <em>bin/</em> folder that we just created in our project.</p>
<h3>Step 2 &#8211; The configuration file</h3>
<p>In this tutorial, I will show how to integrate Doctrine using MySQL as database engine. The config settings might be different for other engines or when you use different database interfaces. However, we open the <em>application/configs/application.ini</em> of our project and insert the following lines into the <em>[production]</em> block of the ini file.</p>
<div class="codecolorer-container ini twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">doctrine.conn.host <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> '127.0.0.1'</span><br />
doctrine.conn.user <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 'root'</span><br />
doctrine.conn.pass <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> ''</span><br />
doctrine.conn.driv <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 'pdo_mysql'</span><br />
doctrine.conn.dbname <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 'app'</span><br />
doctrine.path.models <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/models&quot;</span></div></div>
<p>At this point, you have to fill in your personal database connection settings. These might be completely different from the ones shown here. You should also adjust the database driver now, when you don&#8217;t want to use MySQL. </p>
<h3>Step 3 &#8211; Bootstrapping</h3>
<p>Now comes the fun part. We have to load all the Doctrine libraries and set them up correctly. To do that, we open the <em>application/Bootstrap.php</em> file of our project and fill it with the required functions. I will first post the whole file here and explain it below.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">class</span> Bootstrap <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Bootstrap_Bootstrap<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009933; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* generate registry<br />
&nbsp; &nbsp; &nbsp;* @return Zend_Registry<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _initRegistry<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$registry</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$registry</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009933; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Register namespace Default_<br />
&nbsp; &nbsp; &nbsp;* @return Zend_Application_Module_Autoloader<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _initAutoload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Application_Module_Autoloader<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'namespace'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Default_'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'basePath'</span> &nbsp;<span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$autoloader</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009933; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Initialize Doctrine<br />
&nbsp; &nbsp; &nbsp;* @return Doctrine_Manager<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _initDoctrine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// include and register Doctrine's class loader</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Doctrine/Common/ClassLoader.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Doctrine'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../library/'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// create the Doctrine configuration</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\ORM\Configuration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// setting the cache ( to ArrayCache. Take a look at</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// the Doctrine manual for different options ! )</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\Cache\ArrayCache<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMetadataCacheImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setQueryCacheImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// choosing the driver for our database schema</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// we'll use annotations</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$driver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newDefaultAnnotationDriver</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/models'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMetadataDriverImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$driver</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// set the proxy dir and set some options</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProxyDir</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/models/Proxies'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAutoGenerateProxyClasses</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProxyNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'App\Proxies'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// now create the entity manager and use the connection</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// settings we defined in our application.ini</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$connectionSettings</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'doctrine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'driver'</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$connectionSettings</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conn'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'driv'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'user'</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$connectionSettings</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conn'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'password'</span> &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$connectionSettings</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conn'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'dbname'</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$connectionSettings</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conn'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dbname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'host'</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$connectionSettings</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conn'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$entityManager</span> <span style="color: #339933;">=</span> \Doctrine\ORM\EntityManager<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// push the entity manager into our registry for later use</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$registry</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entitymanager</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$entityManager</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$entityManager</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The first two methods don&#8217;t have anything to to with Doctrine. We just want to use a registry in our project (later, our <em>EntityManager</em> instance will be stored here!) and we want to register a namespace <em>Default_</em> for use in our project. These methods are pure Zend Framework-ish, so I assume you know what they do. If not, consult the <a href="http://framework.zend.com/docs/overview">ZF manual</a>. </p>
<p>Now for the <em>_initDoctrine()</em> method. The first thing we do in line <strong>33</strong> is to include the class loader of Doctrine. Unfortunately, Doctrine already uses the new namespacing of PHP 5 (or 6?). The Zend Framework doesn&#8217;t. So we need another class loader with the correct namespacing scheme. We set it up in the following five lines <strong>34-38</strong>, telling it that we need the namespace <em>Doctrine</em> and where the include path of the library is. </p>
<p>We then create a Doctrine Configuration instance and configure Doctrine (lines <strong>41-59</strong>). Depending on how you want to control the database scheme and stuff like that, you might want to adjust some of the settings here. The <em>ArrayCache</em> can be changed and set to something more performant and if you don&#8217;t want to use annotations in the Entities to define the database scheme, you should use a different <em>MetadataDriver</em>. In this tutorial, we use both the <em>ArrayCache</em> and the <em>DefaultAnnotationDriver</em>. When we instantiate the last one, we have to tell it where our classes with the annotations (I will talk about these more, later) are. </p>
<p>I never _really_ understood what the <em>Proxy</em> classes are. They are created at runtime by Doctrine and my guess is, they are to speed up the database interaction by sort of caching joined requests. Anyways, we need to define where these classes should be saved and what namespace that is. This is done in lines <strong>57-59</strong>. </p>
<p>After that, we get our connection settings from the <em>application.ini</em> file and can finally create the <em>EntityManager</em> instance by telling it all our configurations and connection information. This<em> EntityManager</em> can be understood as the interface between the Database and our models (called Entities). We are saving it&#8217;s instance in the Zend Registry we created earlier. </p>
<p>And that&#8217;s it. We can now start creating Entities (models) and trying to do some database interaction!</p>
<h3>Step 4 &#8211; The Entity</h3>
<p>To test everything, we create a small dummy Entity. I will not go into detail on how to create Entites and define the database scheme of their tables. As we defined in the Doctrine config earlier, we want to use annotations (in the Docblock of our Entity) to define the database scheme. We create a file <em>application/models/Test.php</em> and fill it with the following code:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp;<br />
<span style="color: #009933; font-style: italic;">/**<br />
&nbsp;* @Entity<br />
&nbsp;* @Table(name=&quot;test123&quot;)<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">class</span> Default_Model_Test<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009933; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* @Id @Column(type=&quot;integer&quot;)<br />
&nbsp; &nbsp; &nbsp;* @GeneratedValue(strategy=&quot;AUTO&quot;)<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009933; font-style: italic;">/** @Column(type=&quot;string&quot;) */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As you see, we want a models with the fields <em>id</em> and <em>name</em>, <em>id</em> being an integer and auto_incremented and <em>name</em> being a string. The table name is set to <strong>test123</strong>. As you can see, an Entity is nothing more than a PHP class with some members and methods. Only the definitions for how Doctrine should build the table have to be given in the annotations. You could as well use YAML or XML files to define these. </p>
<p>The next step is to adjust the command line tool of Doctrine to fit into our project.</p>
<h3>Step 5 &#8211; the command line tool</h3>
<p>Simply copy the following code into <em>bin/doctrine.php</em> replacing all of the code already in the file.</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'development'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_PATH'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/realpath"><span style="color: #990000;">realpath</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../application'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.php.net/set_include_path"><span style="color: #990000;">set_include_path</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span>PATH_SEPARATOR<span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/realpath"><span style="color: #990000;">realpath</span></a><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/get_include_path"><span style="color: #990000;">get_include_path</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Doctrine and Symfony Classes</span><br />
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Doctrine/Common/ClassLoader.php'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Doctrine'</span><span style="color: #339933;">,</span> APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Symfony'</span><span style="color: #339933;">,</span> APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../library/Doctrine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Entities'</span><span style="color: #339933;">,</span> APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/models'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNamespaceSeparator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Zend Components</span><br />
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Application.php'</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #666666; font-style: italic;">// Create application</span><br />
<span style="color: #000088;">$application</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Application<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; APPLICATION_ENV<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/configs/application.ini'</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// bootstrap doctrine</span><br />
<span style="color: #000088;">$application</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBootstrap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bootstrap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'doctrine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$em</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$application</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBootstrap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'doctrine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// generate the Doctrine HelperSet</span><br />
<span style="color: #000088;">$helperSet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Symfony\Component\Console\Helper\HelperSet<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'db'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$em</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'em'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$em</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
\Doctrine\ORM\Tools\Console\ConsoleRunner<span style="color: #339933;">::</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$helperSet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>We do some bootstrapping of our project here to obtain the <em>EntityManager</em> instance. We then create a <em>HelperSet</em> with the EntityManager and run the cli tool. </p>
<p>To create our database table based on the Entity we created earlier, we open a command line, change into the <em>bin/</em> folder of our project and run</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">chmod +x ./doctrine<br />
./doctrine orm:schema-tool:create</div></div>
<p>Again, this is for the Unix shell and is different on Windows machines!<br />
Fire up your favourite database administration tool and have a look! The table <em>test123</em> should have been created properly.</p>
<h3>Step 6 &#8211; Using the database</h3>
<p>I will just post a small example of how to use everything. Replace the content of your <em>application/controllers/IndexController.php</em> with the following:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$registry</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_em <span style="color: #339933;">=</span> <span style="color: #000088;">$registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entitymanager</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$testEntity</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Model_Test<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$testEntity</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zaphod Beeblebrox'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_em<span style="color: #339933;">-&gt;</span><span style="color: #004000;">persist</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$testEntity</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_em<span style="color: #339933;">-&gt;</span><a href="http://www.php.net/flush"><span style="color: #990000;">flush</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This is just some quick and dirty dummy code to check if everything works. We get our <em>EntityManager</em> out of the registry and save it as a member of the <em>Controller.</em> We then create a new model (Entity), set the name and by using the <em>EntityManager</em>&#8216;s persist method, we save it. <em>EntityManager->flush()</em> actually invokes the database action and the row should occur in our table.</p>
<p>Test this by simply browse to your project in your favourite webbrowser. If everything worked fine, the table should now have one entry with the name <em>Zaphod Beeblebrox</em>.</p>
<p>If anything did not work, feel free to ask in the comments here! I will be glad to answer any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/integrate-doctrine-2-with-zend-framework-1-11-3/feed/</wfw:commentRss>
		<slash:comments>51</slash:comments>
		</item>
		<item>
		<title>On the accelerometer of the HTC Desire</title>
		<link>http://www.oelerich.org/on-the-accelerometer-of-the-htc-desire/</link>
		<comments>http://www.oelerich.org/on-the-accelerometer-of-the-htc-desire/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 21:06:19 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[gingerbread]]></category>
		<category><![CDATA[sensor]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=309</guid>
		<description><![CDATA[It has been a looong time since I wrote my last blog entry, please excuse me. I haven&#8217;t been busy or anything, it was just lazyness. Since I recently got one _real_ comment among all the spam saying, it would be nice if I wrote in english, I will do that from now on. So [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a looong time since I wrote my last blog entry, please excuse me. I haven&#8217;t been busy or anything, it was just lazyness. Since I recently got one _real_ comment among all the spam saying, it would be nice if I wrote in english, I will do that from now on.</p>
<p>So I started diving into Android development last autumn and today, just for fun, I tried to use some sensors included in my phone. (HTC Desire running Android 2.3.1)</p>
<p>My idea was to <strong>use the accelerometer along with the orientation measurement to measure both the distance the phone was moved and also it&#8217;s trajectory</strong>. Both values, of course, relative to the starting point of the motion.<br />
<span id="more-309"></span><br />
So the first difficulty was to transform the measures of acceleration into the coordinate system of the lab (room&#8230;). Luckily, Android provides methods that need the acceleration and the magnetic field as input and return the transformation matrix for vectors. (Have a look at <strong>SensorManager.getRotationMatrix()</strong>). </p>
<p>The next thing I had to take care of was the earth acceleration. Trying to just remove a constant offset from the z-component of my acceleration vector wasn&#8217;t really accurate. (Somehow my phone measured the gravity to somewhat around 10.5 m/s^2&#8230;) So I just moved to the SDK version 9, which is Android 2.3, and used the sensor type <strong>Sensor.TYPE_LINEAR_ACCELERATION</strong>. This is the measured acceleration where the gravity was already taken care of. </p>
<p>Now, I took care of everything: I measured the acceleration and transformed it into the room&#8217;s coordinate system (before each measurement step, so that I could rotate the phone however I like and it wouldn&#8217;t change the measured values). After integrating everything my app was perfect and ready for a tryout.</p>
<p>Walking through my room and trying to measure the distances from wall to wall didn&#8217;t work at all. Moving the phone in a circle resulted in a measured distance of about 20+ m, which had to be wrong, too. </p>
<p>I don&#8217;t know yet what went wrong or whether it&#8217;s just the unprecision of the sensors. However, I will attach the source code of my activity and the layout file. Keep in mind that you need a phone running Android 2.3 to try it. The emulator can&#8217;t be moved, so It would be useless to emulate the app.) Please excuse the bad coding style, it was just meant to be an experiment.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.janoliver.andmote</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.DecimalFormat</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.Sensor</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEvent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEventListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.LayoutInflater</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AndMote <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> SensorEventListener <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SensorManager mSensorManager<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Sensor mAccelerometer<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Sensor mLinearAccelerometer<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Sensor mMagneto<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> TextView one,two,three<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> matrix_size <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> inR <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span>matrix_size<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> I <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span>matrix_size<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> values <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> accels <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> laccels <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> mags <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> loc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> veloc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> distance <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> time <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> startTime <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> measure <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> counter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> correction<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onResume<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onResume</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mSensorManager.<span style="color: #006633;">registerListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, mAccelerometer, SensorManager.<span style="color: #006633;">SENSOR_DELAY_NORMAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mSensorManager.<span style="color: #006633;">registerListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, mMagneto, SensorManager.<span style="color: #006633;">SENSOR_DELAY_NORMAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mSensorManager.<span style="color: #006633;">registerListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, mLinearAccelerometer, SensorManager.<span style="color: #006633;">SENSOR_DELAY_NORMAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onPause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mSensorManager.<span style="color: #006633;">unregisterListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSensorChanged<span style="color: #009900;">&#40;</span>SensorEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">accuracy</span> <span style="color: #339933;">==</span> SensorManager.<span style="color: #006633;">SENSOR_STATUS_UNRELIABLE</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">sensor</span>.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> Sensor.<span style="color: #006633;">TYPE_MAGNETIC_FIELD</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mags <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span>.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> Sensor.<span style="color: #006633;">TYPE_LINEAR_ACCELERATION</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; laccels <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span>.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accels <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span>.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mags <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> accels <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> laccels <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> measure<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adecimalformat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DecimalFormat</span></a> formatter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adecimalformat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DecimalFormat</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#,###.##&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SensorManager.<span style="color: #006633;">getRotationMatrix</span><span style="color: #009900;">&#40;</span>inR, I, accels, mags<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values <span style="color: #339933;">=</span> convertVector<span style="color: #009900;">&#40;</span>laccels, inR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> now <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> dt <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>now<span style="color: #339933;">-</span>time<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1000.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> dx <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>veloc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt <span style="color: #339933;">+</span> <span style="color: #cc66cc;">0.5</span><span style="color: #339933;">*</span>values<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt<span style="color: #339933;">*</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> dy <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>veloc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt <span style="color: #339933;">+</span> <span style="color: #cc66cc;">0.5</span><span style="color: #339933;">*</span>values<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt<span style="color: #339933;">*</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> dz <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>veloc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt <span style="color: #339933;">+</span> <span style="color: #cc66cc;">0.5</span><span style="color: #339933;">*</span>values<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt<span style="color: #339933;">*</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> dx<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> dy<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> dz<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; veloc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> values<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; veloc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> values<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; veloc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> values<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>dt<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distance <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">sqrt</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>dx, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>dy, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>dz, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter<span style="color: #339933;">++;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; one.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Distance: &quot;</span><span style="color: #339933;">+</span>formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>distance<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; m&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; two.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location (x;y;z): (&quot;</span><span style="color: #339933;">+</span>formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>loc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;;&quot;</span><span style="color: #339933;">+</span>formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>loc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;;&quot;</span><span style="color: #339933;">+</span>formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>loc<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;) m&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; three.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Elapsed time: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>time<span style="color: #339933;">-</span>startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1000.0</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> convertVector<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> vec, <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrix<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; result<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; result<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; result<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>matrix<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>vec<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> startMeasure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; time <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; startTime <span style="color: #339933;">=</span> time <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; loc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; veloc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; distance <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; measure <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> stopMeasure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; measure <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; mSensorManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SensorManager<span style="color: #009900;">&#41;</span>getSystemService<span style="color: #009900;">&#40;</span>SENSOR_SERVICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mLinearAccelerometer <span style="color: #339933;">=</span> mSensorManager.<span style="color: #006633;">getDefaultSensor</span><span style="color: #009900;">&#40;</span>Sensor.<span style="color: #006633;">TYPE_LINEAR_ACCELERATION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mAccelerometer <span style="color: #339933;">=</span> mSensorManager.<span style="color: #006633;">getDefaultSensor</span><span style="color: #009900;">&#40;</span>Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mMagneto <span style="color: #339933;">=</span> mSensorManager.<span style="color: #006633;">getDefaultSensor</span><span style="color: #009900;">&#40;</span>Sensor.<span style="color: #006633;">TYPE_MAGNETIC_FIELD</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getLayoutInflater</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> row <span style="color: #339933;">=</span> inflater.<span style="color: #006633;">inflate</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; one <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> row.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">eins</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; two <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> row.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">zwei</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; three <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> row.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">drei</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; setContentView<span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> button <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; button.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startMeasure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> button2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">stop</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; button2.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stopMeasure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccuracyChanged<span style="color: #009900;">&#40;</span>Sensor sensor, <span style="color: #000066; font-weight: bold;">int</span> accuracy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span> &nbsp;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Blablub&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/eins&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span> &nbsp;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Blablub&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/zwei&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span> &nbsp;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Blablub&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/drei&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Start&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/start&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Stop&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/stop&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/on-the-accelerometer-of-the-htc-desire/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ESPResSo gefällig?</title>
		<link>http://www.oelerich.org/espresso-gefallig/</link>
		<comments>http://www.oelerich.org/espresso-gefallig/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 14:24:34 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[moleküldynamik]]></category>
		<category><![CDATA[simulation]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=305</guid>
		<description><![CDATA[Anmerkung vorab: Dies ist eine Kopie meines Artikels im artofcode.de Blog. Als kleinen Einstand für das artofcode.de Blog möchte ich Euch physikalisch interessierten Lesern heute die Software Bibliothek ESPResSo an einem Beispiel vorstellen. Wir werden eine beliebig lange Kette von Teilchen simulieren (d.h. ihre Bewegungsgleichung numerisch integrieren), die mit Federn untereinander verbunden sind und uns [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Anmerkung vorab:</strong> Dies ist eine Kopie meines Artikels im <a href="http://www.artofcode.de">artofcode.de</a> Blog.</p>
<p>Als kleinen Einstand für das <strong>artofcode.de</strong> Blog möchte ich Euch physikalisch interessierten Lesern heute die Software Bibliothek <a href="http://espressowiki.mpip-mainz.mpg.de/wiki/index.php/Main_Page">ESPResSo</a> an einem Beispiel vorstellen. Wir werden eine beliebig lange Kette von Teilchen simulieren (d.h. ihre <a href="http://de.wikipedia.org/wiki/Bewegungsgleichung">Bewegungsgleichung</a> numerisch integrieren), die mit Federn untereinander verbunden sind und uns sowohl die Energiebilanz als auch die Bewegung der Teilchen anschauen.<br />
<span id="more-305"></span></p>
<h2>Voraussetzungen</h2>
<p>Es werden weder physikalische noch programmiertechnische Vorkenntnisse vorausgesetzt. Eine Unix Shell sollte vorhanden sein. Die Übersetzung und Installation der Bibliothek wird am Beispiel von Mac OS X erklärt, sollte unter Linux Derivaten aber weitaus einfacher sein. <a href="http://www.macports.org/" rel="nofollow">Macports</a> wird für Mac OS X bei der Installationsanleitung vorausgesetzt, ist aber nicht unbedingt nötig.</p>
<h2>Was ist ESPResSo?</h2>
<p>ESPResSo ist eine freie Software Bibliothek für <a href="http://de.wikipedia.org/wiki/Molekulardynamik" rel="nofollow">Molekular Dynamik</a> Simulationen. Entwickelt wird es am Max Planck Institut in Mainz. Molekular Dynamik bezeichnet das Verhalten von Atomen und Molekülen unter Einfluss von äußeren Einflüssen und Teilchen-Teilchen Wechselwirkungen. Viele physikalische Probleme können mit steigender Komplexität analytisch (d.h. rechnerisch) nicht gelöst werden, weshalb man auf numerische Methoden wie etwa der Simulation zurückgreifen muss. ESPResSo ist eine sehr umfangreiche Bibliothek, welche eine ganze Reihe von Wechselwirkungen und Einflüssen simulieren kann. Damit ist es möglich, sowohl thermodynamische Größen (wie etwa Druck, Temperatur, Volumen) des physikalischen Systems als auch das Verhalten der einzelnen Teilchen zu untersuchen.</p>
<h2>Der harmonische Oszillator</h2>
<p>In diesem Artikel werden wir ESPResSo nutzen, um eine Kette von N Teilchen zu simulieren, welche durch Federn mit dem jeweils rechten und linken Nachbarn verbunden sind. Wird ein Teilchen aus seiner Ausgangsposition ausgelenkt, entsteht eine periodische Schwingung (Oszillation). Der <a href="http://de.wikipedia.org/wiki/Harmonischer_Oszillator" rel="nofollow">harmonische Oszillator</a> ist das wichtigste und allgegenwertigste Problem in nahezu allen Bereichen der Physik. Gelöst wird es durch eine lineare Kombination von Winkelfunktionen, sin() und cos(). In diesem Artikel werden wir den Fall von (nur) zwei Teilchen betrachten, von denen eines im Raum fixiert ist. </p>
<h2>Installation von ESPResSo</h2>
<p>ESPResSo ist in C geschrieben und nutzt zur Steuerung ein <a href="http://www.tcl.tk/">TCL</a> Interface. Für gewisse Funktionen, in denen Fourier Transformationen benötigt werden, ist außerdem die Software <a href="http://www.fftw.org/" rel="nofollow">FFTW</a> Voraussetzung. TCL und FFTW installieren wir mittels Macports folgendermaßen:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> fftw3 +universal tcl +universal</div></div>
<p>Anschließend laden wir <a href="http://espressowiki.mpip-mainz.mpg.de/wiki/index.php/Agreement" rel="nofollow">hier die aktuelle ESPResSo Version herunter</a> und entpacken den Quellcode in ein beliebiges Verzeichnis. Nun wechseln wir in das <em>espresso-xx.x.x</em> Verzeichnis und kompilieren die Bibliothek folgendermaßen:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--with-fftw</span>=<span style="color: #000000;">3</span> <span style="color: #007800;">LDFLAGS</span>=-L<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib <span style="color: #007800;">CPPFLAGS</span>=-I<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>include<br />
<span style="color: #c20cb9; font-weight: bold;">make</span><br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #007800;">prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span></div></div>
<p>Unter <em>/usr/local/bin</em> finden wir nun unsere ESPResSo binary und können loslegen.</p>
<h2>Das Simulationsscript</h2>
<p>ESPResSo wird mit einem TCL Script aufgerufen, in welchem anschließend gewisse Befehle der Software verwendet werden können. Ich werde das Script Schritt für Schritt aufbauen und erklären. Am Ende dieses Artikel wird das Script nochmals als Ganzes zum Download bereit stehen.</p>
<h3>Randbedingungen und Parameter festlegen</h3>
<p>Wir starten mit dem Festlegen von Parametern und Randbedingungen. Die im folgenden Codeschnipsel definierten Variablen sind einfache TCL Variablen, es findet noch keine Interaktion mit ESPResSo statt. Der Ausdruck <em>[expr xxx]</em> wird für Berechnungen genutzt. Funktionsaufrufe funktionieren bei TCL ähnlich.</p>
<div class="codecolorer-container tcl twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="tcl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># set up all the parameters of the simulation</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Length of the harmonic chain: 2 is the most simple case</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> l_chain &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff4500;">2</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># length of the springs and strength of the interaction</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> harmonic_r &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff4500;">1.0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> harmonic_k &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff4500;">1.0</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Integration parameters</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> int_steps &nbsp; &nbsp;<span style="color: #ff4500;">10</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> int_n_times &nbsp;<span style="color: #ff4500;">1000</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> time_step &nbsp; &nbsp;<span style="color: #ff4500;">0.005</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Length and center of the box</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> box_l &nbsp; &nbsp; &nbsp; <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$l_chain</span><span style="color: #66cc66;">*</span><span style="color: #ff3333;">$harmonic_r</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> center &nbsp; &nbsp; &nbsp;<span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$box_l</span>/<span style="color: #ff4500;">2.0</span><span style="color: black;">&#93;</span></div></div>
<p>In Zeile 2 legen wir die Anzahl der Knoten in der Kette fest: In diesem Fall, zwei. Für eine längere Kette reicht es also, allein diesen Wert zu vergrößern. Die Federlänge und -konstante legen wir in den Zeilen 5 und 6 fest: Es gilt zu beachten, dass ESPResSo komplett <a href="http://de.wikipedia.org/wiki/Entdimensionalisierung" rel="nofollow">dimensionslos</a> arbeitet, die angegeben Werte sind also als Vielfache einer charakteristischen physikalischen Einheit zu betrachten. Es folgen Integrationsparameter: <em>int_steps</em> sind die Schritte einer Integration, <em>int_n_times</em> die Anzahl der Integrationen. <em>time_step</em> definiert den Zeitschritt der Integration. In unserem Falle wird die Bewegungsgleichung der Kettenglieder also insgesamt 10000 mal gelöst, und zwar alle 0.005 T, wobei T eine beliebige Zeiteinheit ist. Wir betrachten also gewissermaßen ein Zeitintervall von insgesamt 50 T. Anschließend wird noch die Länge und der Mittelpunkt unseres Testraumes festgelegt, welcher hier keine so große Rolle spielt, jedoch für ESPResSo als Randbedingung wichtig ist.</p>
<div class="codecolorer-container tcl twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="tcl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># setting up Espresso variables</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># box parameters</span><br />
&nbsp; &nbsp; setmd box_l &nbsp; &nbsp; <span style="color: #ff3333;">$box_l</span> <span style="color: #ff3333;">$box_l</span> <span style="color: #ff3333;">$box_l</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># turn off periodic boundaries</span><br />
&nbsp; &nbsp; setmd periodic &nbsp;<span style="color: #ff4500;">0</span> <span style="color: #ff4500;">0</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># set the time step of the integration</span><br />
&nbsp; &nbsp; setmd time_step <span style="color: #ff3333;">$time_step</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># set the skin of the verlet list.</span><br />
&nbsp; &nbsp; setmd skin &nbsp; &nbsp; &nbsp;<span style="color: #ff4500;">0.5</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># turn off the thermostat</span><br />
&nbsp; &nbsp; thermostat off<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># set up the interaction</span><br />
&nbsp; &nbsp; inter <span style="color: #ff4500;">0</span> harmonic <span style="color: #ff3333;">$harmonic_k</span> <span style="color: #ff3333;">$harmonic_r</span></div></div>
<p>In diesem Stückchen Code, teilen wir unsere Parameter durch den von ESPResSo definierten Ausdruck <em>setmd</em> der Software mit. Die meisten Einstellungen sind selbsterklärend; <em>periodic x y z</em> legt fest, in welchen Dimensionen sich der Testraum wiederholen soll (in unserem Falle, in keiner), <em>skin</em> beschreibt die Hautdicke der <a href="http://de.wikipedia.org/wiki/Discrete_Element_Method">Verlet-Liste</a> unserer Teilchen und mit <em>thermostat off</em> stellen wir das Thermostat aus. (Es ist natürlich möglich, dem System Wärme zuzuführen, was hier aber keine Rolle spielt.) <em>inter 0 harmonic</em> definiert als Wechselwirkung (welcher wir die ID 0 geben) eine harmonische Wechselwirkung, also eine <a href="http://de.wikipedia.org/wiki/Feder_(Technik)">Feder</a>, welche die zuvor definierten Parameter besitzt. </p>
<h3>Knoten/Partikel definieren</h3>
<p>Im nächsten Schritt, müssen wir die Partikel festlegen, sprich ihre Positionen und Eigenschaften definieren und ESPResSo mitteilen. Da wir ein 1-dimensionales Problem lösen, sollen die y und z Koordinaten bei allen Teilchen gleich liegen, und zwar in der Mitte des Testraumes.</p>
<div class="codecolorer-container tcl twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:300px;"><div class="tcl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># set up the particles</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># initial values</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> part_id <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> posx <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$center</span> - <span style="color: #ff3333;">$l_chain</span>/<span style="color: #ff4500;">2.0</span> <span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> posy <span style="color: #ff3333;">$center</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> posz <span style="color: #ff3333;">$center</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># iterate over $l_chain to set up all the other particles</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#123;</span><span style="color: #ff7700;font-weight:bold;">set</span> j <span style="color: #ff4500;">0</span><span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span> <span style="color: #ff3333;">$j</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff3333;">$l_chain</span> <span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span><span style="color: #008000;">incr</span> j<span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># set up the particle position</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; part <span style="color: #ff3333;">$part_id</span> pos <span style="color: #ff3333;">$posx</span> <span style="color: #ff3333;">$posy</span> <span style="color: #ff3333;">$posz</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># create the bond to the previous particle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#123;</span> <span style="color: #ff3333;">$j</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span> <span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; part <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$part_id</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> bond <span style="color: #ff4500;">0</span> <span style="color: #ff3333;">$part_id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> posx <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$posx</span>+<span style="color: #ff3333;">$harmonic_r</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">incr</span> part_id<br />
&nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># particle 0 should be fixed in space</span><br />
&nbsp; &nbsp; part <span style="color: #ff4500;">0</span> fix <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># set up the initial kick: the last particle gets a velocity v_x = 0.1</span><br />
&nbsp; &nbsp; part <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$l_chain</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> v <span style="color: #ff4500;">0.1</span> <span style="color: #ff4500;">0</span> <span style="color: #ff4500;">0</span></div></div>
<p>In den ersten Zeilen wird das Partikel mit der ID 0 im Raum positioniert: Es ist der Start unserer Kette. In der darauffolgenden for-Schleife teilen wir mit dem Befehl <em>part ID pos x y z</em> die Partikeleigenschaften der Software mit. Anschließend stellen wir mit <em>part ID bond BONDID PARTNERID</em> die Wechselwirkung BONDID zwischen den Teilchen ID und PARTNERID ein: In unserem Falle unser harmonisches Potential. Die Schleife sollte weiterhin selbsterklärend sein: Die x-Position wird um eine Federlänge erhöht und part_id wird ebenfalls um 1 erhöht, sodass nach und nach eine Kette von $l_chain Teilchen entsteht, welche jeweils mit dem rechten und linken Nachbarn verbunden sind.<br />
In den letzten Zeilen des Schnipsels wird das erste Teilchen mit <em>part 0 fix</em> im Raum befestigt und dem letzten Teilchen (da wir nur zwei Teilchen haben: Dem anderen) ein Stoß in x-Richtung gegeben: Wir stellen seine Anfangsgeschwindigkeit mit <em>part ID v 0.1 0 0</em> ein. Ohne diesen Kick würden beide Teilchen im Raum ruhen.</p>
<h3>Die Integration</h3>
<p>Da wir jetzt alle Voraussetzungen für die Simulation geschaffen haben, können wir mit der Integration beginnen. Diese beschränkt sich bei ESPResSo im Prinzip auf den einen Befehl <em>integrate STEPS</em>.</p>
<div class="codecolorer-container tcl twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="tcl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># integration</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># data files</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> energy_file <span style="color: black;">&#91;</span><span style="color: #008000;">open</span> <span style="color: #483d8b;">&quot;energy.txt&quot;</span> <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">set</span> part_file &nbsp; <span style="color: black;">&#91;</span><span style="color: #008000;">open</span> <span style="color: #483d8b;">&quot;parts.txt&quot;</span> &nbsp;<span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># integrate $int_n_times and write energy and particle properties to the data files</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#123;</span><span style="color: #ff7700;font-weight:bold;">set</span> i <span style="color: #ff4500;">0</span><span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span> <span style="color: #ff3333;">$i</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff3333;">$int_n_times</span> <span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span> <span style="color: #008000;">incr</span> i<span style="color: black;">&#125;</span> <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># syntax: time kinetic energy potential energy</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">puts</span> &nbsp;<span style="color: #ff3333;">$energy_file</span> <span style="color: #483d8b;">&quot;[setmd time] [analyze energy kinetic] [analyze energy bonded 0]&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># syntax: time pos0_x pos0_y pos0_z v0_x v0_y v0_z pos1_x pos1_y pos1_z v1_x v1_y v1_z </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">puts</span> &nbsp;<span style="color: #ff3333;">$part_file</span> &nbsp; <span style="color: #483d8b;">&quot;[setmd time] [part 0 print pos v] [part 1 print pos v]&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># finally the integration</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; integrate <span style="color: #ff3333;">$int_steps</span><br />
&nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
<span style="color: #ff7700;font-weight:bold;">exit</span></div></div>
<p>Es werden zwei Dateien zur Auswertung festgelegt: Wir wollen zum Einen eine Energiebilanz machen, und außerdem die Positionen und Geschwindigkeiten der Partikel zu jedem Zeitpunkt festhalten. Wir wiederholen nun mittels der for-Schleife $int_n_times mal die durch <em>integrate STEPS</em> initiierte Integration der Bewegungsgleichungen. <em>[setmd time]</em> gibt uns den Zeitstempel des aktuellen Durchlaufes, mit <em>[analyze energy kinetic/bonded 0]</em> rufen wir die <a href="http://de.wikipedia.org/wiki/Kinetische_Energie">kinetische Energie</a> des Systems order die <a href="http://de.wikipedia.org/wiki/Potentielle_Energie" rel="nofollow">potentielle Energie</a> unserer Wechselwirkung ab. <em>[part ID print pos v]</em> ergibt einen durch Leerzeichen getrennten String mit der Position im Raum (3 Koordinaten) und der Geschwindigkeit (3 Koordinaten) des Teilchens ID. <em>exit</em> beendet das Script.</p>
<h3>Ausführen der Simulation</h3>
<p>DMit einem schlichten Aufruf von der ESPResSo binary mit dem TCL Script als Parameter wird dieses ausgeführt.</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Espresso oszi.tcl</div></div>
<p>Die benötigten Daten werden wie erwähnt in den Textdateien gespeichert und wir können mit der Auswertung anfangen.</p>
<h2>Auswertung</h2>
<p>Die Auswertung ist in diesem Beispiel nicht allzu spannend; ist der eindimensionale, harmonische Oszillator doch zur Genüge bekannt. Trotzdem wollen wir an zwei Beispielen die Richtigkeit der Simulation überprüfen. Zu erst schauen wir uns die Bewegung im <a href="http://de.wikipedia.org/wiki/Phasenraum" rel="nofollow">Phasenraum</a> an; das heißt wir plotten Position gegen die Geschwindigkeit (bzw. den Impuls) des zweiten Teilchens. (Erinnerung: Unser erstes Teilchen ist ohnehin im Raum fixiert!)<br />
<a href="http://www.artofcode.de/wp-content/uploads/2010/08/phasenraum.jpg"><img src="http://www.artofcode.de/wp-content/uploads/2010/08/phasenraum-300x225.jpg" alt="" title="phasenraum" width="300" height="225" class="aligncenter size-medium wp-image-262" /></a>Der charakteristische Kreis ist sehr schön zu erkennen: Ist die Geschwindigkeit maximal, so ist die Position minimal und umgekehrt. Mit anderen Worten: Wenn das Teilchen durch seine Ruhelage schwingt, ist es am schnellsten und die Geschwindigkeit verschwindet, wenn es im Umkehrpunkt bei der maximalen Auslenkung ist. Der Phasenraum scheint also schonmal zu stimmen. Als nächstes schauen wir uns noch die Energiebilanz an:<br />
<a href="http://www.artofcode.de/wp-content/uploads/2010/08/energy.jpg"><img src="http://www.artofcode.de/wp-content/uploads/2010/08/energy-300x225.jpg" alt="" title="energy" width="300" height="225" class="aligncenter size-medium wp-image-261" /></a>Auch diese scheint zu stimmen: Die Gesamtenergie bleibt konstant bei 0.25 E (E: charakteristische Energieeinheit), jedoch oszilieren kinetische und potentielle Energie um diesen Wert herum. </p>
<p>Die Simulation scheint also das zu tun, was sie soll. Als nächsten Schritt könnte man die Bewegung verkomplizieren: Mehr Dimensionen einführen, mehr Teilchen oder unterschiedliche Federn. Oder aber man lässt das System Wärme verlieren &#8211; oder gewinnen. Man könnte den Teilchen auch weitere Wechselwirkungen geben (magnetische, elektrostatische, &#8230;) oder oder oder&#8230; Das Experimentieren überlasse ich euch. Die <a href="http://espressowiki.mpip-mainz.mpg.de/espresso/ug.pdf"> ESPResSo Dokumentation</a> findet ihr hier. Außerdem lohnt es sich, einmal in das <em>samples/</em> Verzeichnis des ESPResSo Downloads zu schauen!</p>
<p>Hier der Link zu dem kompletten Simulationsscript: <a href='http://www.artofcode.de/wp-content/uploads/2010/08/oszi.tcl_.txt' rel="nofollow">oszi.tcl</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/espresso-gefallig/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doctrine 2 Beta 1 ins Zend Framework 1.10 integrieren</title>
		<link>http://www.oelerich.org/doctrine-2-beta-1-ins-zend-framework-1-9-integrieren/</link>
		<comments>http://www.oelerich.org/doctrine-2-beta-1-ins-zend-framework-1-9-integrieren/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 19:49:39 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Doctrine ORM]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[doctrine 2]]></category>
		<category><![CDATA[zf-doctrine-integrator]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=285</guid>
		<description><![CDATA[Update 21.08.2010 &#8211; Ich habe soeben ein Update für die neuste Doctrine Beta hochgeladen! Sooo. Einige Leser hatten mich gefragt, wann ich meine Zend Framework Integrations-Anleitung denn auf die Beta 1 von Doctrine updaten würde. Das mache ich in den nächsten Tagen noch, sobald ich Zeit finde. Heute habe ich allerdings schon ein kleines Projekt [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update 21.08.2010 &#8211; Ich habe soeben ein Update für die neuste Doctrine Beta hochgeladen!</strong></p>
<p>Sooo. Einige Leser hatten mich gefragt, wann ich meine <a href="http://framework.zend.com">Zend Framework</a> Integrations-Anleitung denn auf die <strong>Beta 1</strong> von <a href="http://www.doctrine-project.com">Doctrine</a> updaten würde.</p>
<p>Das mache ich in den nächsten Tagen noch, sobald ich Zeit finde. Heute habe ich allerdings schon ein kleines Projekt gestartet: Den <a href="http://code.google.com/p/zf-doctrine-integrator">zf-doctrine-integrator</a> (<strong>Zend Framework Doctrine Integrator</strong>). </p>
<p>Prinzipiell besteht das ganze Projekt, welches derzeit auf <a href="http://code.google.com">google code</a> gehostet ist, aus einem <strong>Shellscript</strong> und den nötigen Dateien zur <strong>Integration von Doctrine 2 Beta 1 ins Zend Framework 1.10</strong>. Das Shellscript erstellt euch automatisch ein Projekt mit den ZF und Doctrine Bibliotheken, lädt die nötigen Dateien zur Integration aus dem Repository herunter und erstellt automatisch eine vhost Datei. </p>
<p>Das erspart euch bei der Projekterstellung die Arbeit, den Code zur Integration aus alten Projekten oder irgendwelchen Tutorials zu kopieren, außerdem könnt ihr so sicher stellen, immer die<strong> aktuellsten</strong> ZF und Doctrine Versionen zu nutzen.</p>
<p>Das ganze ist mehr oder weniger überhaupt nicht getestet. Ich würde mich sehr freuen, wenn ihr einfach mal ausprobiert, ob das tool bei euch läuft. <a href="http://code.google.com/p/zf-doctrine-integrator/issues/list">Issues</a> könnt ihr auf der Projektseite direkt posten. Die <a href="http://code.google.com/p/zf-doctrine-integrator/wiki/Systemvoraussetzungen">Systemvoraussetzungen</a> und eine (in Zukunft) ausführliche Dokumentation findet ihr im <a href="http://code.google.com/p/zf-doctrine-integrator/w/list">Wiki</a>.</p>
<ul>
<li><a href="http://code.google.com/p/zf-doctrine-integrator/">zum Projekt</a></li>
<li><a href="http://code.google.com/p/zf-doctrine-integrator/downloads/list">zum Download des Shell Scripts</a></li>
<li><a href="http://code.google.com/p/zf-doctrine-integrator/wiki/Systemvoraussetzungen">Systemvoraussetzungen</a></li>
<li><a href="http://code.google.com/p/zf-doctrine-integrator/wiki/Bedienungsanleitung">Bedienungsanleitung</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/doctrine-2-beta-1-ins-zend-framework-1-9-integrieren/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Mini Blog in PHP</title>
		<link>http://www.oelerich.org/mini-blog-in-php/</link>
		<comments>http://www.oelerich.org/mini-blog-in-php/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 12:29:40 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[klein]]></category>

		<guid isPermaLink="false">http://www.oelerich.org/?p=220</guid>
		<description><![CDATA[Auf phpgangsta.de gibt es einen kleinen Wettbewerb, wer die kleinste Blogsoftware schreibt. Das klingt nach einer interessanten Aufgabe, derer ich mich eben mal gewidmet habe. Größe derzeit: 165 Byte (unix: 148 Byte) grauenvoller Code! Auseinanderwurschteln könnt ihr das selbst, in schön gibts nur die endgültige Version. ohne unix befehle (165 Byte) &#60;?extract&#40;$_GET&#41;;$i='&#62;&#60;input';$x=join&#40;'',file&#40;c&#41;&#41;;if&#40;$p==p&#41;echo&#34;&#60;form$i name=t$i name=c$i type=submit&#62;&#34;;if&#40;$t&#41;fwrite&#40;fopen&#40;c,w&#41;,&#34;&#60;hr&#62;$t(&#34;.date&#40;c&#41;.&#34;)&#60;p&#62;$c$x&#34;&#41;;echo$x; [...]]]></description>
			<content:encoded><![CDATA[<p>Auf <a href="http://www.phpgangsta.de/aktion-die-kleinste-blogsoftware-der-welt">phpgangsta.de</a> gibt es einen kleinen Wettbewerb, wer die kleinste Blogsoftware schreibt. </p>
<p>Das klingt nach einer interessanten Aufgabe, derer ich mich eben mal gewidmet habe. </p>
<p>Größe derzeit: <strong>165 Byte</strong> (unix: 148 Byte) grauenvoller Code!</p>
<p>Auseinanderwurschteln könnt ihr das selbst, in schön gibts nur die endgültige Version.</p>
<p>ohne unix befehle (165 Byte)</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><a href="http://www.php.net/extract"><span style="color: #990000;">extract</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&gt;&lt;input'</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><a href="http://www.php.net/join"><span style="color: #990000;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><a href="http://www.php.net/file"><span style="color: #990000;">file</span></a><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">==</span>p<span style="color: #009900;">&#41;</span><span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;&lt;form<span style="color: #006699; font-weight: bold;">$i</span> name=t<span style="color: #006699; font-weight: bold;">$i</span> name=c<span style="color: #006699; font-weight: bold;">$i</span> type=submit&gt;&quot;</span><span style="color: #339933;">;</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">,</span>w<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&lt;hr&gt;<span style="color: #006699; font-weight: bold;">$t</span>(&quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&lt;p&gt;<span style="color: #006699; font-weight: bold;">$c</span><span style="color: #006699; font-weight: bold;">$x</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #b1b100;">echo</span><span style="color: #000088;">$x</span><span style="color: #339933;">;</span></div></div>
<p>mit unix befehlen (148 Byte)</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><a href="http://www.php.net/extract"><span style="color: #990000;">extract</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$d</span><span style="color: #339933;">=</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&gt;&lt;input'</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span>`cat c`<span style="color: #339933;">;</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">==</span>p<span style="color: #009900;">&#41;</span><span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;&lt;form<span style="color: #006699; font-weight: bold;">$i</span> name=t<span style="color: #006699; font-weight: bold;">$i</span> name=c<span style="color: #006699; font-weight: bold;">$i</span> type=submit&gt;&quot;</span><span style="color: #339933;">;</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span>`<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;hr&gt;$t($d)&lt;p&gt;$c$x'</span><span style="color: #339933;">&gt;</span>c`<span style="color: #339933;">;</span><span style="color: #b1b100;">echo</span><span style="color: #000088;">$x</span><span style="color: #339933;">;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.oelerich.org/mini-blog-in-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

