<?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>Coert Vonk</title>
	<atom:link href="http://www.coertvonk.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.coertvonk.com</link>
	<description>Open-Source Software Engineer</description>
	<lastBuildDate>Mon, 10 Jun 2013 04:44:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>American Symbols 2013</title>
		<link>http://www.coertvonk.com/family/school/american-symbols-2013-6816</link>
		<comments>http://www.coertvonk.com/family/school/american-symbols-2013-6816#comments</comments>
		<pubDate>Wed, 29 May 2013 19:31:21 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6816</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/2013symbols2-150x150.jpg" class="attachment-post-thumbnail wp-post-image" alt="2013symbols2" /></div><form action="http://www.coertvonk.com/wp-login.php?action=postpass" method="post">
	<p>This post is password protected. To view it please enter your password below:</p>
	<p><label for="pwbox-6816">Password: <input name="post_password" id="pwbox-6816" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
</form>
	]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/american-symbols-2013-6816/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/2013symbols2.jpg" length="198572" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/2013symbols2.jpg" width="600" height="600" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Music Lesson 2013</title>
		<link>http://www.coertvonk.com/family/school/music-lesson-2013-6811</link>
		<comments>http://www.coertvonk.com/family/school/music-lesson-2013-6811#comments</comments>
		<pubDate>Wed, 29 May 2013 04:24:16 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6811</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/2013music2-150x150.jpg" class="attachment-post-thumbnail wp-post-image" alt="2013music2" /></div><form action="http://www.coertvonk.com/wp-login.php?action=postpass" method="post">
	<p>This post is password protected. To view it please enter your password below:</p>
	<p><label for="pwbox-6811">Password: <input name="post_password" id="pwbox-6811" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
</form>
	]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/music-lesson-2013-6811/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/2013music2.jpg" length="160444" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/2013music2.jpg" width="600" height="600" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Spring Sing 2013</title>
		<link>http://www.coertvonk.com/family/school/spring-sing-2013-6797</link>
		<comments>http://www.coertvonk.com/family/school/spring-sing-2013-6797#comments</comments>
		<pubDate>Sat, 25 May 2013 15:53:24 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6797</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/2013sing4-150x150.jpg" class="attachment-post-thumbnail wp-post-image" alt="2013sing4" /></div><form action="http://www.coertvonk.com/wp-login.php?action=postpass" method="post">
	<p>This post is password protected. To view it please enter your password below:</p>
	<p><label for="pwbox-6797">Password: <input name="post_password" id="pwbox-6797" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
</form>
	]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/spring-sing-2013-6797/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/2013sing4.jpg" length="196359" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/2013sing4.jpg" width="600" height="600" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Dance 2013</title>
		<link>http://www.coertvonk.com/family/school/dance-2013-6833</link>
		<comments>http://www.coertvonk.com/family/school/dance-2013-6833#comments</comments>
		<pubDate>Fri, 29 Mar 2013 20:57:02 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6833</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/2013dance-150x150.jpg" class="attachment-post-thumbnail wp-post-image" alt="2013dance" /></div><form action="http://www.coertvonk.com/wp-login.php?action=postpass" method="post">
	<p>This post is password protected. To view it please enter your password below:</p>
	<p><label for="pwbox-6833">Password: <input name="post_password" id="pwbox-6833" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
</form>
	]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/dance-2013-6833/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/2013dance.jpg" length="147438" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/2013dance.jpg" width="600" height="600" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Camcorder Sound, reducing noise and controlling loudness</title>
		<link>http://www.coertvonk.com/technology/videoediting/dynamic-range-compression-in-ac3-audio-6325</link>
		<comments>http://www.coertvonk.com/technology/videoediting/dynamic-range-compression-in-ac3-audio-6325#comments</comments>
		<pubDate>Sat, 02 Mar 2013 04:35:11 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[Video Editing]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6325</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/videoediting-sound-150x150.png" class="attachment-post-thumbnail wp-post-image" alt="videoediting-sound" /></div><p>Over the past years, camcorders have excelled in capturing video, but the quality of the audio remains lagging.  Adding an external microphone is a must, but will only get you so far.  Quiet voices will barely rise above the noise floor, while it feels like the videographer yelling in your ear.</p><p>This article describes a three stroke approach to improve sound from camcorders.  The first step is to reduce the noise.  In the following steps the dialog loudness is set to a standard level and the dynamic range is reduced.  The last two steps are an integral part of AC-3 (A/52, Dolby Digital) but can also be applied to other streams such as Advanced Audio Codec (AAC) as shown in the last section.</p><h3><span style="font-size: 13px;">Noise reduction</span></h3><p>First things first.  Start with a good external microphone and a wind muff.  Personally, I use a Canon 5.1 microphone because it is small and mounts on the hot shoe.  The <a href="http://kbsupport.cusa.canon.com/system/selfservice.controller?CONFIGURATION=1011&amp;PARTITION_ID=1&amp;secureFlag=false&amp;TIMEZONE_OFFSET=&amp;CMD=VIEW_ARTICLE&amp;ARTICLE_ID=34983">specs</a> are not that impressive, but even an ideal microphone will pick up a significant amount of ambient noise.</p><p>The <em>noise level</em> is defined as the loudness perceived by the human ear.  The loudness is commonly measured using a frequency filter that mimics the human hearing and then measuring the energy as root-mean-square (LAeq).  Note that for a sine wave the RMS level is <em>√2 × peak level</em>.  Another popular measure is the K-scale for which there are several plugins such as <a href="http://code.mzuther.de/kmeter/">mzuther</a>'s, <a href="http://www.bluecataudio.com/Products/Product_DPeakMeterPro/">DPMP</a>, and <a href="http://www.meterplugs.com/kmeter">meterplugs</a>.</p><p>Personally, I take a shortcut by not applying the filter and simply measuring the RMS level of noise samples.  For this, I use the <em>stats</em> function from <a href="http://sox.sourceforge.net/">Sound Exchange</a>.  In practice this appears to be good enough.  My typical <em>indoor</em> measurements are shown in the table below.  The values in the table are expressed at decibels below digital full-scale.</p><table border="1"><tbody><tr><th colspan="2">source material</th></tr><tr><td>noise</td><td>-43 dBFS</td></tr><tr><td>quiet dialog</td><td>-41 dBFS</td></tr><tr><td>normal dialog</td><td>-31 dBFS</td></tr><tr><td>loud dialog</td><td>-21 dBFS</td></tr></tbody></table><p>With the quiet dialog level this close to the noise floor, we can't amplify this dialog without also significantly amplifying the noise.  As a first step we lower the noise floor in post using an audio editor such as Adobe Sound Booth.  This usually reduce the noise by about 8 dB without causing significant distortion.</p><table border="1"><tbody><tr><th colspan="2">reduced noise</th></tr><tr><td><i>noise</i></td><td><i>-51 dBFS</i></td></tr><tr><td>quiet dialog</td><td>-41 dBFS</td></tr><tr><td>normal dialog</td><td>-31 dBFS</td></tr><tr><td>loud dialog</td><td>-21 dBFS</td></tr></tbody></table><h3>Dialog Normalization</h3><p>The volume dial on AC-3 decoders controls the loudness of a normal dialog.  The listener is thus able to reliably set the volume level of the dialogue no matter what program is playing.  This does however require the audio to indicate the normal dialog level at which it is recorded.  In AC-3 this is accomplished using the metadata tag "dialnorm" as described in the <a href="http://www.atsc.org/cms/standards/a_52-2010.pdf">AC-3 specification</a> §5.4.2.8 and §7.6.  The decoder uses this parameter to automatically adjust the volume so that the dialog is always played at the same loudness.</p><p><em>Dialog loudness</em> expresses how an average person perceives the volume of a dialog [<a href="http://web.archive.org/web/20040716131627/http://www.dolby.com/tech/L.mn.0002.DDPEG1.pdf">DD1</a>; <a href="http://web.archive.org/web/20031206104650/http://dolby.com/pro/digaudio/pa.ma.1102.Standards.S.pdf">DD2</a>].  The film industry has long standardized the normal dialog level at -31 dBFS.  The corresponding Sound Pressure Level (SPL) for most movie theaters is 85 dB.</p><p>At home we have a volume dial that selects the SPL for normal dialog.  Assuming the volume dial is set to a sound pressure level of 67 dB for normal dialog.  When this decoder plays a stream with a normal dialog level of -31 dBFS, it will adjust the amplifier gain so that <em>0 dBFS ≡ 98 dB</em>, what causes the normal dialog to reproduce at <em>-31 dBFS ≡ 67 dB</em>.  If the listener switches to another program with a normal dialog at -25 dBFS, the amplifier gain will be reduced so that <em>0 dBFS ≡ 92 dB</em>, and the normal dialog stays at a 67 dB.</p><h3>Dynamic Range Compression</h3><p>The sound from camcorders often has an undesirable large dynamic range.  On one end, there may be people whispering in the distance, while on the other end we may have the videographer holding the camcorder.  The difference between these loudness levels in called <em>dynamic range</em>.  We can reduce the dynamic range by both amplifying the quiet sounds and attenuating the loud sounds.  Note that to successfully boost the quiet sounds, it is essential that they are well above the noise floor.</p><p>With AC-3 we can adjust the gain using the metadata tag "dynrng" as described in §7.7 of the <a href="http://www.atsc.org/cms/standards/a_52-2010.pdf">AC-3 specification</a>.   The AC-3 encoder generates these tags based on the loudness combined with a compression profile.  All standard profiles have a null-band that is centered around the normal dialog level.  Loudness levels within this null-band are left intact.  For the <em>film compression profile</em> at a <em>normal dialog level of -31 dBFS</em>, the transfer function can be visualized as shown below.</p><p><a href="http://www.coertvonk.com/technology/videoediting/dynamic-range-compression-in-ac3-audio-6325/attachment/film5" rel="attachment wp-att-6411"><img class="aligncenter size-full wp-image-6411" alt="film5" src="http://www.coertvonk.com/wp-content/uploads/film5.png" width="471" height="299" /></a></p><p>As shown in the graph above, signals from -33.5 to -28.5 dBFS stay the same.  The 12 dB below is boosted, while the 10 dB above is attenuated as also shown in the table below.</p><p><a href="http://www.coertvonk.com/technology/videoediting/dynamic-range-compression-in-ac3-audio-6325/attachment/filmtbl3" rel="attachment wp-att-6366"><img class="aligncenter size-full wp-image-6366" alt="filmtbl3" src="http://www.coertvonk.com/wp-content/uploads/filmtbl3.png" width="207" height="141" /></a></p><p>This <em>film </em>at<em> dialnorm -31dBFS</em> profile is a good fit for my camcorder recordings.  The quiet dialog is more or less in the middle of the boost range, the normal dialog is at the normal dialog level, while the loud dialog is in the early cut range.</p><table border="1"><tbody><tr><th> </th><th>source material</th><th>noise reduced</th><th>range compressed</th></tr><tr><td>noise</td><td>-43 dBFS</td><td>-51 dBFS</td><td>-45 dBFS</td></tr><tr><td>quiet dialog</td><td>-41 dBFS</td><td>-41 dBFS</td><td>-37 dBFS</td></tr><tr><td>normal dialog</td><td>-31 dBFS</td><td>-31 dBFS</td><td>-31 dBFS</td></tr><tr><td>loud dialog</td><td>-21 dBFS</td><td>-21 dBFS</td><td>-25 dBFS</td></tr></tbody></table><p>The table above shows that the soft dialog is amplified by 4 dB, but at the cost of raising the noise level 2 dB.  If we had a constant background noise level, we could consider alter the transfer function so that it attenuate all signals below -45.5 dBFS.</p><h3>Let's do it</h3><p>As shown above, applying dialog normalization and dynamic range is straightforward using the metadata in AC-3.  For other audio streams, we will need to alter the samples to get the same effect.</p><h4>AC-3</h4><p>For AC-3, we only need to extract the audio stream using <a href="http://www.ffmpeg.org/">FFmpeg</a> and compress it using <a href="http://aften.sourceforge.net/">Aften</a>.</p><pre>ffmpeg -y -vn -i in.avi out.wav
aften -v 0 -dnorm 31 -dynrng 1 out.wav out.ac3</pre><h4>AAC</h4><p>The Advanced Audio Codec (AAC) doesn't support the metadata for dialog normalization or dynamic range compression.  Instead, we modify the samples directly using <a href="http://sox.sourceforge.net/">SoX</a> as shown below.  Note that in this particular example it also applies a 13 dB overall gain.  The values printed in bold represent the compression curve.  The resulting stream is then compressed using Nero's <a href="http://www.nero.com/enu/company/about-nero/nero-aac-codec.php">AAC encoder</a>.</p><pre style="padding-left: 30px;">ffmpeg -y -vn -i <em>in.avi</em> <em>tmp.wav</em>
set CURVE=<em><strong>-90.0,-84.0,-45.5,-39.5,-33.5,-33.5,-28.5,-28.5,-18.5,-23.5,0.0,-22.6
</strong></em>sox <em>tmp.wav</em> <em>out.wav</em> compand 0.1,3.0 %CURVE% 13.0 -90 1.6 stats
neroaacenc -lc -br 226000 -if <em>out.wav</em> -of <em>out.aac</em></pre><p>Those who like to experiment themselves find the transfer curves for other profiles at various dialog levels.</p><ul><li>film light<pre>-31:   -90.0,-84.0, -53.0,-47.0, -41.0,-41.0, -21.0,-21.0, -11.0,-16.0, 0.0,-15.5
-28.5: -90.0,-84.0, -50.5,-44.5, -38.5,-38.5, -18.5,-18.5,  -8.5,-13.5, 0.0,-13.1
-27:   -90.0,-84.0, -49.0,-43.0, -37.0,-37.0, -17.0,-17.0,  -7.0,-12.0, 0.0,-11.7</pre></li><li>film<pre>-31:   -90.0,-84.0, -45.5,-39.5, -33.5,-33.5, -28.5,-28.5, -18.5,-23.5, 0.0,-22.6
-28.5: -90.0,-84.0, -43.0,-37.0, -31.0,-31.0, -26.0,-26.0, -16.0,-21.0, 0.0,-20.2
-27:   -90.0,-84.0, -41.5,-35.5, -29.5,-29.5, -24.5,-24.5, -14.5,-19.5, 0.0,-18.8</pre></li><li>speech<pre>-31:   -90.0,-74.8, -52.5,-37.3, -33.5,-33.5, -28.5,-28.5, -18.5,-23.5, 0.0,-22.6
-28.5: -90.0,-74.8, -50.0,-34.8, -31.0,-31.0, -26.0,-26.0, -16.0,-21.0, 0.0,-20.2
-27:   -90.0,-74.8, -48.5,-33.3, -29.5,-29.5, -24.5,-24.5, -14.5,-19.5, 0.0,-18.8</pre></li><li>music light<pre>-31:   -90.0,-78.0, -65.0,-53.0, -41.0,-41.0, -21.0,-21.0, -21.0,-21.0, 0.0,-20.0
-28.5: -90.0,-78.0, -62.5,-50.5, -38.5,-38.5, -18.5,-18.5, -18.5,-18.5, 0.0,-17.6
-27:   -90.0,-78.0, -61.0,-49.0, -37.0,-37.0, -17.0,-17.0, -17.0,-17.0, 0.0,-16.2</pre></li><li>music<pre>-31:   -90.0,-78.0, -57.5,-45.5, -33.5,-33.5, -28.5,-28.5, -18.5,-23.5, 0.0,-22.6
-28.5: -90.0,-78.0, -55.0,-43.0, -31.0,-31.0, -26.0,-26.0, -16.0,-21.0, 0.0,-20.2
-27:   -90.0,-78.0, -53.5,-41.5, -29.5,-29.5, -24.5,-24.5, -14.5,-19.5, 0.0,-18.8</pre></li></ul><h3>For more information</h3><ul><li><a href="http://forum.doom9.org/showthread.php?t=56020">Properly encoding AC-3</a></li><li><a href="http://catalogs.infocommiq.com/AVCat/images/documents/pdfs/MeasDialogLAeq.pdf">Measuring LAeq</a></li><li><a href="http://forum.blu-ray.com/showthread.php?t=38765">Callibrate SPL</a></li></ul>]]></description>
		<wfw:commentRss>http://www.coertvonk.com/technology/videoediting/dynamic-range-compression-in-ac3-audio-6325/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/videoediting-sound.png" length="54801" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/videoediting-sound.png" width="300" height="300" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Christmas Carols 2012</title>
		<link>http://www.coertvonk.com/family/school/christmas-carols-2012-6827</link>
		<comments>http://www.coertvonk.com/family/school/christmas-carols-2012-6827#comments</comments>
		<pubDate>Mon, 24 Dec 2012 21:53:53 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6827</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/2012carols-150x150.jpg" class="attachment-post-thumbnail wp-post-image" alt="2012carols" /></div><form action="http://www.coertvonk.com/wp-login.php?action=postpass" method="post">
	<p>This post is password protected. To view it please enter your password below:</p>
	<p><label for="pwbox-6827">Password: <input name="post_password" id="pwbox-6827" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
</form>
	]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/christmas-carols-2012-6827/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/2012carols.jpg" length="146625" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/2012carols.jpg" width="600" height="600" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Gold Rush in Coloma 2012</title>
		<link>http://www.coertvonk.com/family/school/gold-rush-in-coloma-2012-6830</link>
		<comments>http://www.coertvonk.com/family/school/gold-rush-in-coloma-2012-6830#comments</comments>
		<pubDate>Thu, 20 Dec 2012 21:56:42 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6830</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/2012gold-150x150.jpg" class="attachment-post-thumbnail wp-post-image" alt="2012gold" /></div><form action="http://www.coertvonk.com/wp-login.php?action=postpass" method="post">
	<p>This post is password protected. To view it please enter your password below:</p>
	<p><label for="pwbox-6830">Password: <input name="post_password" id="pwbox-6830" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
</form>
	]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/gold-rush-in-coloma-2012-6830/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/2012gold.jpg" length="166513" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/2012gold.jpg" width="600" height="600" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>Lego Mindstorms Graphical Programming (NXT-G 2.0)</title>
		<link>http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107</link>
		<comments>http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107#comments</comments>
		<pubDate>Wed, 12 Dec 2012 20:12:30 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=6107</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/school-lego-150x150.png" class="attachment-post-thumbnail wp-post-image" alt="school-lego" /></div><a href="http://www.coertvonk.com/family/school/notes-on-lego-mindstorms-nxt-6107/attachment/nxt" rel="attachment wp-att-6140"><img class="alignright  wp-image-6140" title="nxt" alt="" src="http://www.coertvonk.com/wp-content/uploads/nxt.jpg" width="150" height="121" /></a>The NXT-G graphical integrated development environment (IDE) provides the five basic elements of programming: input/output, conditionals, loops, variables and functions.  It combines research ideas from MIT, with a graphical IDE from National Instruments.

The graphical IDE reduces the initial learning curve.  Novices can start programming by combining Action blocks to make the motors move and generate light or sound.  Once these concepts they can introduce flow elements such as conditionals, loops and wait.  These flow elements can used embedded sensor readings.  More advanced programmers can read the sensors directly and process the outputs using math and logic functions.

Coming from a more traditional text based programming environment, and trying stay a step ahead of my children, I read Terry Griffin's excellent book [1] along with some other resources [2,3,4,5,6,7,8].  This is a write up for my future reference.
<h2>Work Flow</h2>
To create a program, you connect blocks.
<ul>
	<li>Drag and drop blocks from the palette onto the work space.  Each block performs an unique function such as turning the motors, or measuring a distance.</li>
	<li>Use the tabs on the bottom of the palette to switch between sets of blocks.</li>
	<li>To save your work as a package use Tools &gt; Create Pack and Go.</li>
	<li>As always, keep it simple.  Divide up the problem in small steps, and test each step before combining them.</li>
</ul>
To compile, download to the NXT robot and run, you use the controls in the bottom-right of the work space.<a href="http://www.coertvonk.com/family/school/notes-on-lego-mindstorms-nxt-6107/attachment/nxt-control" rel="attachment wp-att-6129"><img class="alignright  wp-image-6129" title="nxt-control" alt="" src="http://www.coertvonk.com/wp-content/uploads/nxt-control.png" width="76" height="77" /></a>
<ul>
	<li><em>middle-button</em>, compiles the program, downloads and runs it to  the NXT</li>
	<li><em>top-right button</em>, compiles selected blocks, downloads and runs it to  the NXT</li>
	<li><em>bottom-left button</em>, compiles the program and downloads it to  the NXT</li>
	<li><em>bottom-right button</em>, stops the program on the NXT.</li>
	<li><em>top-left button</em>, manages the NXT memory space, click the top-left button; click on the bar graph on the left to select a memory.  This will also let you transfer files.</li>
</ul>
<div>Helpful</div>
<div>
<ul>
	<li>Bluetooth dongle, such as the <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16833340012">AZiO BTD-V201</a> (don't install the drivers from CD).  To configure the connection use the top-left button on the NXT controls shown above.</li>
	<li>Gyro Sensor, such as the <a href="http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&amp;key=NGY1044">HiTechnic par</a>t to build a <a href="http://robotsquare.com/2012/03/13/tutorial-segway-with-nxt-g/">Segway</a>.</li>
	<li>To change the default project directory: change settings.ini located in  "shell:UsersFilesFolder\Documents\LEGO Creations\MINDSTORMS Projects\Profiles\Default".  For example:
<ul>
	<li>Data Directory="//FILESERVER/Your Name/Lego Mindstorms/Default"</li>
</ul>
</li>
	<li>Organizer to store the set, such as DeWalt Deep Pro Organizer.</li>
	<li>Rechargeable batteries, max voltage is 1.75 V for each of the six AA batteries.  NiMH appears to be a good fit.</li>
</ul>
</div>
<h2>I/O blocks (equiv. to library functions in C)</h2>
<img class="alignright  wp-image-6184" title="block" alt="" src="http://www.coertvonk.com/wp-content/uploads/block.png" width="99" height="67" />

A program is composed by connecting blocks on a sequence beam.  Blocks are executed sequentially.

<strong>Output</strong>

Outputs functions are called "Action blocks" in NXT-G jargon.  The Motor block controls a <em>single</em> motor that for example moves a robot arm, or rotate a wheel.  The Move block, on the other hand, drives <em>two motors</em>, such as to rotate the wheels on a robot to make it go forward.  The Move block synchronizes the motors to keep the robot travelling in a straight line.  In all cases, you specify how far the motor turns, not how far an arm moves or a wheel travels.  For a wheel, the distance traveled depends on the number of motor rotations, optional gears and the circumference of the wheel (14 cm for the snap-on tires).

Other output blocks generate sound, light or write to the display or Bluetooth device.
<ul>
	<li>Motor action
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Motor block</span>
<ul>
	<li>Controls a single motor</li>
	<li>Automatically corrects for rotation errors.</li>
	<li>Parameters:
<ul>
	<li><em><em>action</em></em>, constant or ramp up/down until target is met.</li>
	<li><em>power</em>, &lt;10 will not move; &gt;75 moves out of control.</li>
	<li><em>control</em>, automatically adjust the power to correct for resistance or slipping.</li>
	<li><em>duration</em>, unlimited = switches the motor on and continues with the next block on the Sequence beam.</li>
	<li><em>wait</em>, when unchecked, it will move on to the next block on the Sequence beam while the motor meanwhile completes its action.</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Move block</span>
<ul>
	<li>Controls two motors, synchronizing the B and C motors</li>
	<li>Automatically corrects for rotation errors.</li>
	<li>Parameters (also see the Motor block):
<ul>
	<li><em>steering</em>, middle = straight; far left/right = spin in place (motors turn opposite directions).</li>
	<li><em>next action</em>, brake = provides accurate travel distance and then hold the motor in place; coast = stop the power to the motor so it rolls to a stop (Because the motor coasts to a stop, it will move a little further than the Duration setting. When a following Move or Motor block runs, it will automatically adjust for the rotation error.)</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Reset</span>
<ul>
	<li>Resets the accumulated rotation error, causing the next Motor or Move block to disregard prior rotation errors.  The Reset block should precede the first Motor or Move block in a program.</li>
</ul>
</li>
</ul>
</li>
	<li>Other action blocks
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Sound</span>
<ul>
	<li>Plays a sound file or a single tone</li>
	<li>Parameters:
<ul>
	<li><em>wait</em>, when unchecked, the next block on the Sequence beam will start while the sound is playing.</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Display</span>
<ul>
	<li>Writes a text string or shapes to the NXT display</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Send message</span>
<ul>
	<li>Transmits a wireless message to another NXT assuming it has been paired before.  Each NXT has 10 mailboxes each 5 messages deep.  Head-drop when mailbox is full.</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Color lamp</span>
<ul>
	<li>Turns a color lamp on or off.</li>
</ul>
</li>
</ul>
</li>
</ul>
<strong>Input</strong>

<a href="http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107/attachment/ultrasonic" rel="attachment wp-att-6215"><img class="alignright  wp-image-6215" alt="ultrasonic" src="http://www.coertvonk.com/wp-content/uploads/ultrasonic.png" width="79" height="86" /></a>Most input block read data from external sources and compare the data read to a preset value or range.  The results are made available at its data hub.  The data hub can be shown by clicking on the tab at the bottom of the block.  Data wires connect the outputs to other blocks.  Configuration parameters can also be set by incoming wires.
<ul>
	<li>Sensor blocks
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Ultrasonic Sensor block</span>
<ul>
	<li>Measures the distance to an object and compares it to a criterion.</li>
	<li>Outputs:
<ul>
	<li><em>yes/no</em>, true if criterion is met, false otherwise</li>
	<li><em>distance</em>, scaled value from sensor [0..255 cm | 0..100 inches].</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Touch Sensor block</span>
<ul>
	<li>Reads the touch sensor's state and compares it to a criterion (pressed, released, bumped). Note that a program may not be fast enough to detect the "bumped" state.</li>
	<li>Outputs:
<ul>
	<li><em>yes/no</em>, true if criterion is met, false otherwise</li>
	<li><em>raw value</em>, unscaled value from sensor [0..1024]</li>
	<li><em>logic number</em>, 0=released; 1=pressed.</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">NXT Buttons block</span>
<ul>
	<li>Reads a NXT button state and compares it to a criterion (pressed, released, bumped). Note that your program may not be fast enough to detect a condition as "bumped".</li>
	<li>Output:
<ul>
	<li><em>yes/no</em>, true if criterion is met, false otherwise.</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Color Sensor block</span>
<ul>
	<li>Measures color or light intensity and compares it to a criterion.  When the parameter "light" is checked it generates its own light and measure the reflected light.</li>
	<li>Outputs:
<ul>
	<li><em>yes/no</em>, true if criterion is met, false otherwise</li>
	<li><em>detected color</em>, light intensity [0..100] or detected color (1=black; 2=blue; 3=green; 4=yellow; 5=red; 6=white).</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Rotation sensor block</span>
<ul>
	<li>Measures the number of degrees that the motor turned and compares it to a criterion.  The same block can also be used to reset the rotation counter.</li>
	<li>Output:
<ul>
	<li><em>yes/no</em>, true if criterion is met, false otherwise</li>
	<li><em>direction</em>, true=forwards; false=backwards</li>
	<li><em>degrees</em>, scaled value from sensor [0..2147483647].</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
	<li>Other blocks
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Timer block</span>
<ul>
	<li>Reads one of the tree timers and compares it against a criterion.  Time is specified in seconds on the configuration panel, but in milliseconds when using input data wires.  The block can also be used to reset a timer.</li>
	<li>Output:
<ul>
	<li><em>yes/no</em>, true if criterion is met, false otherwise</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Receive Message block</span>
<ul>
	<li>Receives a Bluetooth message and compare it against a criterion.</li>
	<li>Outputs:
<ul>
	<li><em><em>yes/no</em></em>, true if criterion is met, false otherwise</li>
	<li><em>message received</em>, true when a message has been received, false otherwise</li>
	<li><em>{text|number|logic} out</em>, message data.</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Random block</span>
<ul>
	<li>Generates a random number within a range.  Can be used to make the robot a little unpredictable and thus more interesting.</li>
	<li>Output:
<ul>
	<li><em>number</em>, random value between specified lower and upper bounds.</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<strong>Data manipulation</strong>

<a href="http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107/attachment/math" rel="attachment wp-att-6195"><img class="alignright  wp-image-6195" title="math" alt="" src="http://www.coertvonk.com/wp-content/uploads/math.png" width="83" height="117" /></a>Sophisticated data manipulations are supported through combining several number and text blocks.
<ul>
	<li>Numbers
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Math Block</span>
<ul>
	<li>Performs basic math operations (addition, subtraction, multiplication, division, square root, absolute value).</li>
	<li>Output:
<ul>
	<li><em>result</em>, result of the operation [-2147483648 .. 2147483647]</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Logic Block</span>
<ul>
	<li>Performs Boolean operations (AND, OR, XOR or NOT)</li>
	<li>Output
<ul>
	<li><em>result</em>, result of the operation [true|false]</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Compare Block</span>
<ul>
	<li>Performs less than (&lt;), greater than (&gt;) or equal (=) operations</li>
	<li>Output
<ul>
	<li><em>result</em>, result of the operation [true|false]</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Range Block</span>
<ul>
	<li>Determines if a value is within a range of numbers</li>
	<li>Output
<ul>
	<li><em>yes/no</em>, true of the value is within range, false otherwise</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
	<li>Text strings
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Number to Text block</span>
<ul>
	<li>Converts a number to a text string</li>
	<li>Output
<ul>
	<li><em>text</em>, text representation of number [string]</li>
</ul>
</li>
</ul>
</li>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Text block</span>
<ul>
	<li>Concatenates a maximum of three text strings</li>
	<li>Output
<ul>
	<li><em>text</em>, concatenation of text strings [string]</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2>Program Flow</h2>
<a href="http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107/attachment/switch" rel="attachment wp-att-6188"><img class="alignright  wp-image-6188" title="switch" alt="" src="http://www.coertvonk.com/wp-content/uploads/switch.png" width="97" height="103" /></a>Flow elements can take input from an embedded sensor or from a data wire.
<ul>
	<li>Conditionals
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Switch block</span> (equiv. to switch())
<ul>
	<li>Checks for a condition, and runs blocks on the sequence beam corresponding to that condition.</li>
	<li>More then two conditions are allowed.</li>
	<li>The '*' button selects the default condition.</li>
	<li>Floating point inputs are rounded to the nearest integer before comparing them.</li>
	<li>Comments should be left outside a switch block.</li>
</ul>
</li>
</ul>
</li>
	<li>Wait
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Wait block</span>
<ul>
	<li>Busy-loop to poll an input until the specified condition is met.</li>
</ul>
</li>
</ul>
</li>
	<li>Loops
<ul>
	<li><span style="text-decoration: underline;" data-mce-mark="1">Loop block</span> (equiv. to do while())
<ul>
	<li>Repeats a set of blocks until the end condition is met.</li>
	<li>The loop count output is updated at the start of the last run through the loop, it will be one fewer than the total number of times the loop repeats.</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2><strong>My blocks (equiv. to user functions)</strong></h2>
My blocks should be used to abstract program sections that perform specific functions.  Dividing a program into smaller pieces, makes the program easier to read and maintain.  Each My block can be tested independently and reused in the same or other programs.
<ul>
	<li>Usage
<ul>
	<li><em>Create</em>, select the blocks; "Edit &gt; Make a new My Block" from the menu</li>
	<li><em>Open</em>, double-click the My Block</li>
	<li><em>Use</em>, select from the Custom Palette (using the tab at the bottom of the Palettes)</li>
	<li><em>Close</em>, "File &gt; Close" from the menu</li>
	<li><em>Organize</em>, "Edit &gt; Manage Custom Palette" from the menu; then use the file system to organize.</li>
</ul>
</li>
	<li>Creating My blocks that have a data hub
<ul>
	<li>Use compare, text or math blocks to supply input values to the blocks that will become a new My block.  These additional blocks are only needed to supply the data wire, and will not become part of the My block.</li>
	<li>Select the blocks for the new My Block and call "Edit &gt; Make a new My Block" from the menu.  All the data wires will remain connected. The value of the wire will also become configurable using the Configuration Panel.</li>
	<li>Note that once a My block is made, you can change the name of data plugs, but you can not add more plugs.  You will have to recreate the My block instead.</li>
</ul>
</li>
	<li>Appears broken if
<ul>
	<li>two data plugs have the same name, or</li>
	<li>there is problem with the data wire connections, or</li>
	<li>the source file for the block has been moved or renamed in the computer's file system.</li>
</ul>
</li>
	<li>Note
<ul>
	<li>If you absolutely have to copy a My block, then use the file system.  Don't use "save as" from the menu.</li>
	<li>When moving a program to another computer, also move the My Blocks or better yet create a package that contains all the related files ("Tools &gt; Create Pack and Go")</li>
</ul>
</li>
</ul>
<h2>Data</h2>
<a href="http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107/attachment/data" rel="attachment wp-att-6225"><img class="alignright  wp-image-6225" alt="data" src="http://www.coertvonk.com/wp-content/uploads/data.png" width="79" height="79" /></a>Most blocks have input plugs that correspond to the parameters in its configuration panel.  Using the data inputs, a program can change these parameters.  For example, the output of an ultrasound sensor could be used to specify the power of the motors, thereby slowing down the robot when it approaches an object.

<strong>Data wires (equiv. to mailboxes)</strong>

In the NXT-G world, data passes over so called "Data wires".  A Data wire connects <em>one output</em> to one or more inputs.
<ul>
	<li>Scope
<ul>
	<li>Local to the blocks that it connects to the wire.</li>
</ul>
</li>
	<li>Usage
<ul>
	<li><em>draw a wire between blocks</em>
<ul>
	<li>expand the data hubs (click on the tab at the bottom of a block).</li>
	<li>the plugs on the left are inputs; the plugs on the right are outputs.</li>
	<li>click on an output terminal</li>
	<li>click on the input terminal that you want to connect.</li>
</ul>
</li>
	<li><em>draw a wire into the boundary of a Switch block</em>
<ul>
	<li>enable tabbed view (disable flat view)</li>
	<li>connect a wire to the input</li>
	<li>switch back to flat view</li>
</ul>
</li>
</ul>
<ul>
	<li><em>draw wires from within the boundary of a Switch block</em>
<ul>
	<li>enable tabbed view</li>
	<li>select the "true" condition and connect a wire</li>
	<li>select the "false" condition and connect a wire from the output to the edge of the block where the existing wire crosses it.</li>
	<li>switch back to flat view</li>
</ul>
</li>
</ul>
<ul>
	<li><em>delete a wire</em>
<ul>
	<li>click the data plug at the right of the wire.</li>
</ul>
</li>
</ul>
</li>
	<li>Note
<ul>
	<li>Use variables instead of passing data wires in/out of a Loop or Switch block.</li>
	<li>Occasionally, a wire may be routed behind another block, making it appear as if its terminals are connected.</li>
	<li>A dashed grey wire indicates a problem, such as
<ul>
	<li>two plugs have different data types (Boolean, integer, float, string)</li>
	<li>the output of a pass-through plug is not used without the input side connected</li>
	<li>multiple inputs are connected to one output plug.</li>
	<li>an input is connected to an output that appears later on the sequence beam.</li>
</ul>
</li>
</ul>
</li>
</ul>
<strong>Variable block (equiv. to variables)</strong>

Variables are expected to change over time, such as a distance to a wall.  A variable can be used to connect <em>one or more outputs</em> to one or more inputs.
<ul>
	<li>Scope
<ul>
	<li>Local to the program (and its My blocks) that defines it.</li>
</ul>
</li>
	<li>Usage
<ul>
	<li><em>create a variable</em>, "Edit &gt; Define Variables" from the menu.</li>
	<li><em>use a variable</em>, insert a Variable block.</li>
	<li><em>delete a variable</em>, first remove any Variable blocks that use it.</li>
</ul>
</li>
	<li>Notes
<ul>
	<li>A program shares its variables with its My blocks if they have the same name and data type.</li>
	<li>Perhaps not the best coding practice, but variables can be used when a My Block needs to remember a value.</li>
</ul>
</li>
</ul>
<strong>Constant block (equiv. to const or #defines)</strong>

As the name implies, constant values do not change.  An example is the diameter of the wheels on a robot.  A constant block supplies a <em>constant value</em> to more or one inputs.
<ul>
	<li>Scope
<ul>
	<li>Global to all programs.</li>
</ul>
</li>
	<li>Usage
<ul>
	<li><em>create</em>, "Edit &gt; Define Constants" from the menu.  Specify the action "choose from list".</li>
	<li><em>use</em>, insert a Constant block.</li>
	<li><em>change</em>, save any open programs that use that constant.</li>
	<li><em>delete</em>, "Edit &gt; Define constants" from the menu.  Note that any Constant block that uses that constant will change to Custom.</li>
</ul>
</li>
	<li>Note
<ul>
	<li>When working with a program that uses <em>custom constants</em>, note that:
<ul>
	<li>changing the value in one Constant block won’t affect the other programs.</li>
	<li>creating a constant using the Edit Constant dialog will change all "Custom" constants with the same name.</li>
</ul>
</li>
</ul>
</li>
</ul>
<strong>File Access block</strong>

Files can store settings or input/output data for a program.
<ul>
	<li>Scope
<ul>
	<li>Global to all programs running on a NXT.  Remain available after the program ends.</li>
</ul>
</li>
	<li>Usage
<ul>
	<li><em>create</em>, insert a File Access block; select create as the action.  A list of files already on the NXT will appear (assuming it is connected using either USB or BT).</li>
	<li><em>write</em>, insert a File Access block; select write as the action to appends to a file.</li>
	<li><em>read</em>, insert a File Access block; select read as the action.</li>
	<li><em>delete</em>, insert a File Access block; select delete as the action.</li>
	<li><em>close</em>, insert a File Access block; select close.  Remember to close a file after writing to it.  Otherwise you can not delete or read from it.</li>
</ul>
</li>
	<li>Note
<ul>
	<li>A program can write to or read from up to four files at once.</li>
	<li>The Initial File Size data plug that allows you to specify how much memory to allocate for the file. If the file grows past the initial size, the NXT will move the file to larger memory space, thereby introducing a slight delay.</li>
</ul>
</li>
</ul>
<div>
<h2>Sequence Beams (=threads).</h2>
<a href="http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107/attachment/sequencebeam" rel="attachment wp-att-6185"><img class="alignright  wp-image-6185" title="sequencebeam" alt="" src="http://www.coertvonk.com/wp-content/uploads/sequencebeam.png" width="94" height="70" /></a>Multitasking means doing more than one thing at a time. The sequence beam can fork, allowing your robot to do more than one thing at a time.  But first, a short word about execution order:

<strong>Execution order</strong>
<ul>
	<li>In general, a block will finish what it’s doing before the next block on the Sequence Beam runs.  Exceptions are when "wait for completion" is unchecked, or a Move/Motor block is set to run indefinitely.</li>
	<li>A block starts once all input values on the data wires are available.</li>
	<li>A Loop/Switch block starts only once all the input values are available on data wires that enter the block (even though they might not be immediately used)</li>
	<li>A data wire that starts inside a Loop/Switch block and connects to a block outside will only have a value after the Loop/Switch block finishes.</li>
	<li>Only one copy of a particular My Block can run at the same time (non-re-entrant)</li>
	<li>A program ends once it reaches the end of all the Sequence beams.</li>
</ul>
<strong>To add a task</strong>
<ul>
	<li>Expand vertical space:
<ul>
	<li>Add a Display block (that has a large data hub) at the end of the Loop block.</li>
	<li>Open the data hub to create space within the loop.</li>
	<li>Add a small comment to the bottom-left side of the Loop block to keep it open after the Display block is removed.</li>
	<li>Delete the display block.</li>
</ul>
</li>
	<li>Add a new Sequence beam:
<ul>
	<li>Add a new block under the existing Sequence beam.</li>
	<li>Shift-click the existing Sequence Beam, and connect it to the new block.</li>
</ul>
</li>
	<li>To expand horizontal space:
<ul>
	<li>Select the outer Loop block.</li>
	<li>Click-and-hold the mouse on the Sequence Beam between the top-right block and the edge of the Loop block.  Slowly drag the mouse to the right to widen the loop block.</li>
</ul>
</li>
</ul>
<strong>Note</strong>
<ul>
	<li>Especially when working with multiple Sequence beams, allow the NXT-G some time to catch up with your editing.</li>
	<li>Do not access the same sensor or motor from different Sequence beams.</li>
	<li>Blocks on different sequence beams can communicate using variables, not pure data wires.</li>
</ul>
<h2>Debugging</h2>
Often debugging a program proves to be more challenging than writing it.  The NXT-G environment offers the following debug approaches:

</div>
<div>
<ul>
	<li><img class="alignright" title="feedback" alt="" src="http://www.coertvonk.com/wp-content/uploads/feedback.png" width="59" height="81" />Using Feedback boxes
<ul>
	<li>The Feedback Boxes, at the right side of the Configuration Panel show the value of sensors or loop variables while the NXT is connected to the computer using an USB cable or Bluetooth connection.</li>
</ul>
</li>
	<li>Using the NXT display
<ul>
	<li><a href="http://robotsquare.com/2012/02/23/tutorial-displaying-numbers/">displaying numbers</a> (Laurens Valk)</li>
	<li><a href="http://courses.washington.edu/engr100/Section_Wei/NXT/tricks.pdf">using the display</a></li>
</ul>
</li>
	<li>Using Bluetooth
<ul>
	<li><a href="http://www.norgesgade14.dk/networkbtremote.php">BT remote</a> (Anders)</li>
	<li><a href="http://www.codeproject.com/Articles/18857/Communicating-with-LEGO-NXT-via-Bluetooth-in-C">nxt sending bluetooth messages to pc</a></li>
	<li><a href="http://mindstorms.lego.com/en-us/support/files/default.aspx">bluetooth development kit</a> (lego)</li>
</ul>
</li>
	<li><a href="http://find.botmag.com/100701">programming and debugging options</a></li>
</ul>
<h2>References</h2>
<table border="0">
<tbody>
<tr>
<td dir="" id="" lang="" scope="" align="" valign="top">[1]</td>
<td><a href="http://www.amazon.com/Art-LEGO-MINDSTORMS-NXT-G-Programming/dp/1593272189/?s=books&amp;ie=UTF8&amp;qid=1354928857">The Art of Lego Mindstorms NXT-G Programming</a> (book)
Terry Griffin, 2010</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[2]</td>
<td rowspan="1" colspan="1"><a href="http://www.amazon.com/gp/product/1593272111?ie=UTF8&amp;tag=valkdiscovery-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1593272111">The Lego Mindstorms NXT 2.0 Discovery Book</a> (book)
Laurens Valk, 2010</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[3]</td>
<td rowspan="1" colspan="1"><a href="http://www.nxtprograms.com/help/MyBlocks/tutorial.html">Creating and using your own blocks with My Blocks</a>
Dave Parker, 2007-2012</td>
</tr>
<tr>
<td dir="" id="" lang="" scope="" align="" valign="top">[4]</td>
<td><a href="http://www.nytimes.com/2008/12/02/technology/techspecial2/02robots.html">Robots in a box</a> (article)
New York Times, 2008</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[5]</td>
<td rowspan="1" colspan="1"><a href="http://www.firstlegoleague.org/sites/default/files/Challenge/TeamResources/SeniorSolutions/2012Programming.pdf" target="_blank">Basic Programming: Tips, Skills, &amp; Resources</a> (presentation)
Tony Ayad and LeRoy Nelson, 2012</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[6]</td>
<td rowspan="1" colspan="1"><a href="http://www.stemcentric.com/nxt-tutorial/">NXT Tutorial</a> (video)
Dale Yocum at Catlin Gabel School, 2012</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[7]</td>
<td rowspan="1" colspan="1"><a href="http://www.hightechkids.org/sites/default/files/CoachingLibrary/fll_programming_101_nxt_g.pdf">FFL Programming 101</a>
Innovations in Science and Technology Education, 2007</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[8]</td>
<td rowspan="1" colspan="1"><a href="http://www.nxtprograms.com/projects2.html#ProjectsByProgram">Example projects by complexity</a>
Dave Parker, 2007-2012</td>
</tr>
<tr>
<td dir="" lang="" rowspan="1" colspan="1" scope="" align="" valign="top">[9]</td>
<td rowspan="1" colspan="1"><a href="http://find.botmag.com/100702">Programming solution for NXT - debugging</a>
Robot Magazine, 2012</td>
</tr>
</tbody>
</table>
</div>]]></description>
		<wfw:commentRss>http://www.coertvonk.com/family/school/lego-mindstorms-nxt-g-6107/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/school-lego.png" length="83684" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/school-lego.png" width="300" height="300" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>DD-WRT and Wireshark</title>
		<link>http://www.coertvonk.com/technology/networking/dd-wrt-and-wireshark-5928</link>
		<comments>http://www.coertvonk.com/technology/networking/dd-wrt-and-wireshark-5928#comments</comments>
		<pubDate>Wed, 19 Sep 2012 00:05:36 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[DD-WRT]]></category>
		<category><![CDATA[etherpuppet]]></category>
		<category><![CDATA[named pipe]]></category>
		<category><![CDATA[tcpdump]]></category>
		<category><![CDATA[wireshark]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=5928</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-and-wireshark-150x150.png" class="attachment-post-thumbnail wp-post-image" alt="networking-ddwrt-and-wireshark" /></div><p><a href="http://www.coertvonk.com/technology/networking/dd-wrt-and-wireshark-5928/attachment/networking-ddwrt-and-wireshark" rel="attachment wp-att-6650"><img class="alignright size-thumbnail wp-image-6650" alt="networking-ddwrt-and-wireshark" src="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-and-wireshark-150x150.png" width="150" height="150" /></a>Wireshark is a open-source protocol analyzer that can visualize the frames passing through the router. This analyzer is available on many platforms, but can not run directly on the router.</p><h4>using tcpdump</h4><p>While one could run tcpdump directly on the router, interpreting its output takes a bit of work. Another option run tcpdump on the router and pipe its output to wireshark on a Linux host. Details can be found in <a href="http://www.seangri-la.com/cgi-bin/moin.cgi/Remote_packet_capture">Remote Packet Capture</a>. An excerpt.  To capture traffic on bridge br0, use the following commands on a Linux host.</p><pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">mkfifo /tmp/$PPID
ssh rtr "tcpdump -i br0 -w - 'not ((src host rtr and src port 22) or (dst host rtr and dst port 22))'" &gt; /tmp/$PPID. &amp;
wireshark -k -i /tmp/$PPID</pre><h4>using etherpuppet</h4><p>Instead we forward all packets from an interface on the router to a Linux host, where they the protocol analyzer runs.</p><p>Prepare the router</p><pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">cd /jffs
wget http://www.secdev.org/projects/etherpuppet/files/etherpuppet-mipsel
chmod 755 etherpuppet-mipsel</pre><p>Prepare the Linux host</p><pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">wget http://hg.secdev.org/etherpuppet/raw-files/top/etherpuppet.c
gcc -o etherpuppet etherpuppet.c
sudo yum install wireshark-gnome
sudo usermod -a -G wireshark $NAME</pre><p>Instruct the router to forward all packets on the bridge that joins the LAN and wireless traffic (<em>br0</em>).</p><pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">./etherpuppet-mipsel -i br0 -s 999 -C</pre><p>Receive the packets on the Linux host and start the protocol analyzer.</p><pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">sudo ./etherpuppet -m -c rtr2.vonk:999 &amp;</pre><pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">sudo ifconfig puppet0 up
sudo wireshark # select interface puppet0</pre><p>In Wireshark select the<em> puppet0</em> interface.</p>]]></description>
		<wfw:commentRss>http://www.coertvonk.com/technology/networking/dd-wrt-and-wireshark-5928/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-and-wireshark.png" length="128394" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-and-wireshark.png" width="300" height="300" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
		<item>
		<title>DD-WRT heading two seperate networks</title>
		<link>http://www.coertvonk.com/technology/networking/dd-wrt-leading-two-seperate-networks-5829</link>
		<comments>http://www.coertvonk.com/technology/networking/dd-wrt-leading-two-seperate-networks-5829#comments</comments>
		<pubDate>Thu, 13 Sep 2012 16:15:37 +0000</pubDate>
		<dc:creator>Coert</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[DD-WRT]]></category>
		<category><![CDATA[segregation]]></category>

		<guid isPermaLink="false">http://www.coertvonk.com/?p=5829</guid>
		<description><![CDATA[<div><img width="150" height="150" src="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-leading-two-seperate-networks-150x150.png" class="attachment-post-thumbnail wp-post-image" alt="networking-ddwrt-leading-two-seperate-networks" /></div>This article shows how you can use a $75 consumer class router to support two different networks.  Besides the usual primary network, it will create a secondary network supporting both wireless and wired connections.  The secondary network could  be used to provide internet access to customers or visitors.<a href="http://www.coertvonk.com/technology/networking/dd-wrt-leading-two-seperate-networks-5829/attachment/seperate-networks2" rel="attachment wp-att-5876"><img class="alignright size-full wp-image-5876" title="seperate-networks2" alt="" src="http://www.coertvonk.com/wp-content/uploads/seperate-networks2.png" width="235" height="261" /></a>

The router is assumed to be flashed with open source DD-Wrt firmware.  This firmware adds some business class features to your router.  The router will be configured by taking advantage of two features that allow network traffic to be separated:
<ul>
	<li><em>VLANs</em>, allows wired LAN ports to be grouped and each group to be treated differently.</li>
	<li><em>multiple SSIDs</em>, allows multiple wireless network names (SSID) and security settings.  The traffic over each SSID can be treated differently.</li>
</ul>
<span style="color: #ffffff;">:</span>
<h2>Before we start</h2>
The router is assumed to be running open source <a href="http://www.dd-wrt.com/">DD-Wrt</a> firmware and configured for a single wired and wireless network.  The router should also be configured for shell access (<a href="http://www.dd-wrt.com/wiki/index.php/SSH">ssh</a>) or telnet.  Further, the reader should have a basic understanding of
<ul>
	<li>the web based user interface (WebGUI)</li>
	<li>networking concepts such as IP addresses, sub nets, routing, bridging and network interfaces.  Some of the more specific terminology used in this article is:
<ul>
	<li>A <em>broadcast domain</em>, is a network segment, in which all nodes can communicate directly by layer-2 broadcasts.  Examples are a wired GigE network, or a wireless network.</li>
	<li>A <em>switch</em>, handles frames at the data link layer (layer 2).</li>
	<li>A <em>router</em>, handles packets at the network layer (layer 3).  A router forms a boundary between broadcast domains.</li>
	<li>A <em>bridge</em>, connects two or more network segments into a single broadcast domain (layer 2).</li>
	<li>A <em>virtual LAN</em> <em>(VLAN)</em>, creates distinct broadcast domains within one ore more physical networks.  A VLAN is a switched network that is logically segmented by functions, project teams, or applications without regard to the physical location of users.</li>
</ul>
</li>
</ul>
The configuration presented here has been tested on an <a href="http://www.anandtech.com/print/6180">Asus RT-N16</a> router running <a href="ftp://ftp.dd-wrt.com/others/eko/V24-K26/svn15943-snow/dd-wrt.v24-15943_NEWD-2_K2.6_mega.bin">DD-Wrt mega build 15943</a>.  The approach outlined might also apply to other routers or builds, albeit with minor changes.

<span style="color: #ffffff;">:</span>
<h2>Inside the router</h2>
To create a secondary network, we need to separate the four LAN ports and create an virtual wireless network.  The following section builds a basic understanding of the default data paths in the router.  The following section describes how the software configuration
<h3>Hardware block diagram</h3>
Many consumer class routers are based on a chipset consisting of a IP switch and System-on-Chip.  For example, the Asus RT-N16 router that is build around:
<ul>
	<li><em>System-on-Chip</em> <a href="http://www.broadcom.com/products/Wireless-LAN/802.11-Wireless-LAN-Solutions/BCM4718">BCM4718</a>, provides the CPU, WiFi and USB.</li>
	<li><em>Switch Fabric</em> <a href="http://www.broadcom.com/products/Switching/Home-and-Small-Business/BCM53115">BCM53115</a>, provides  the WAN and four LAN ports</li>
</ul>
Other GigE routers such as the Cisco/Linksys E2000 or E3000 are very similar.  Older routers such as the WRT54G have slightly different port numbers  (<a href="http://www.dd-wrt.com/wiki/index.php/Default_Configuration_Overview">details)</a>.

<a href="http://www.coertvonk.com/technology/networking/dd-wrt-leading-two-seperate-networks-5829/attachment/block-before3" rel="attachment wp-att-5871"><img class="aligncenter  wp-image-5871" title="block-before3" alt="" src="http://www.coertvonk.com/wp-content/uploads/block-before3.png" width="598" height="218" /></a>

The block diagram above shows the data paths within the router.  Note:
<ul>
	<li>the LAN port numbers on the case do not correspond to the port numbers on the switch;</li>
	<li>the WAN port connects to a special interface on the switch and can't be reassigned;</li>
	<li>port 8 connects the VLAN trunk from the switch to interface <em>eth0</em> on the CPU.</li>
</ul>
The switch tags incoming frames with a VLAN identifier.  Frames arriving on the WAN port are tagged as VLAN2, while frames from the LAN ports are tagged as VLAN1.  The frames destined for the CPU are sent on <em>port 8</em>.

The CPU receives the frames over port <em>eth0</em>.  Frames with a VLAN2 tag are treated as WAN traffic.  Frames with a VLAN1 tag are combined (bridged) with frames from the wireless module (<em>eth1</em>) and treated as LAN traffic.
<h3>Firmware mapping</h3>
The configuration for the DD-Wrt is stored in nonvolatile memory (nvram).  The configuration can be shown as described in the DD-Wrt document <a href="http://www.dd-wrt.com/wiki/index.php/Switched_Ports">Switched Ports</a>.  An excerpt:

The tagging configuration is stored in <em>vlan#ports</em> variables where '*' symbolizes the default path.  Note that the <em>vlan0ports</em> variable appears to be unused on GigE routers.  For the switch to move frames outside of any vlan, it needs to include port 8.  This allows the SoC to route the packet.  The vlan with the default is used for packets that do not have a vlan tag (see <a href="http://www.dd-wrt.com/wiki/index.php/Default_Configuration_Overview">here</a>).
<pre class="brush: bash; highlight: 1; gutter: false; toolbar: off; tab-size: 8">nvram show | grep vlan.*ports | sort
vlan0ports=1 2 3 4 5*
vlan1ports=4 3 2 1 8*
vlan2ports=0 8</pre>
An other important variable is <em>port5vlans</em>.  It identifies every active VLAN plus the number 16 that indicates that tagging is enabled.  The other variables appear only for the WebGUI.
<pre class="brush: bash; highlight: 1; gutter: false; toolbar: off; tab-size: 8">nvram show | grep port.*vlan | sort
port0vlans=2 18 19
port1vlans=1 18 19
port2vlans=1 18 19
port3vlans=1 18 19
port4vlans=1 18 19
port5vlans=1 2 16</pre>
Every active VLAN needs to have its name set to <em>et0</em>.
<pre class="brush: bash; highlight: 1; gutter: false; toolbar: off; tab-size: 8">nvram show | grep vlan.*hwname | sort
vlan0hwname=et0
vlan1hwname=et0
vlan2hwname=et0</pre>
<span style="color: #ffffff;">:</span>
<h2>Creating to separate networks</h2>
To create a second network, we need to introduce an additional VLAN and an additional SSID for the wireless.  The two can then be bridged together and given an unique subnet address.  Before you start, I highly recommend making a backup of your current configuration.

<a href="http://www.coertvonk.com/technology/networking/dd-wrt-leading-two-seperate-networks-5829/attachment/block-after2" rel="attachment wp-att-5872"><img class="aligncenter size-full wp-image-5872" title="block-after2" alt="" src="http://www.coertvonk.com/wp-content/uploads/block-after2.png" width="598" height="218" /></a>
<h3>Create a virtual wireless network (wl0.1)</h3>
Using a web browser, connect to the router and bring up the WebGUI.
<ol>
	<li>Wireless &gt; Virtual Interfaces &gt; Add.  Specify the basic wireless settings.  For the network configuration, choose bridged.  Click Save.</li>
	<li>Wireless &gt; Wireless Security &gt; Virtual Interface wl0.1.  Specify the security settings.  Click Apply Settings, to save and apply the changes.</li>
</ol>
<h3>Create a virtual local area network (vlan3)</h3>
We will use the shell (ssh) interface to configure the VLANs, because on Broadcom based routers the GUI does not always do so correctly.  Login using ssh, and run the following commands
<pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">nvram set vlan3hwname=et0       # VLAN3 is enabled
nvram set vlan1ports="3 4 8*"   # assign LAN1/LAN2 to VLAN1
nvram set vlan3ports="1 2 8*"   # assign LAN3/LAN4 to VLAN3
nvram set port4vlans="1 18 19"  # LAN1 is part of VLAN1 (GUI only)
nvram set port3vlans="1 18 19"  # LAN2 is part of VLAN1 (GUI only)
nvram set port2vlans="3 18 19"  # LAN3 is part of VLAN3 (GUI only)
nvram set port1vlans="3 18 19"  # LAN4 is part of VLAN3 (GUI only)
nvram commit
reboot</pre>
<h3>Bridge vlan3 and wl0.1 (br1)</h3>
To make the new wired and wireless network to behave as one, we logically combine interfaces <em>vlan3</em> and <em>wl0.1</em> using a new bridge interface <em>br1</em>:
<ol>
	<li>Bridge the <em>vlan3</em> and <em>wl0.1</em> interfaces together as <em>br1</em>
<ul>
	<li>Setup &gt; Networking &gt; Bridging &gt; Create Bridge &gt; Add.  Enter <em>br1</em> in the first field and click Apply Settings.</li>
	<li>Setup &gt; Networking &gt; Bridging &gt; Create Bridge &gt; For <em>br1</em> enter a private IP address and subnet mask.  E.g. 10.0.4.1 and 255.255.255.0.  Click Apply Settings.</li>
	<li>Setup &gt; Networking &gt; Assign to bridge &gt; Add.  Select <em>br1</em>, interface <em>vlan3</em> (LAN3 and LAN4) and click Apply Settings.</li>
	<li>Setup &gt; Networking &gt; Assign to bridge &gt; Add.  Select <em>br1</em>, interface <em>wl0.1</em> (the virtual wireless interface) and click Apply Settings.</li>
</ul>
</li>
	<li>Configure the DHCP server for <em>br1</em>
<ul>
	<li>Setup &gt; Networking &gt; DHCPD &gt; Multiple DHCP Server &gt; Add.  Select br1 and fill in the first m maximum number of DHCP addresses and lease time in minutes.  E.g. 200, 50 and 1440.  Click Apply Settings.</li>
</ul>
</li>
</ol>
<h3>Separate the networks</h3>
Now that we have the two networks up and running, it is time to separate them.  The firewall rules listed below added to Administration &gt; Commands &gt; Save Firewall.  If you use an USB memory stick, the rules can also be stored in an executable <em>/jffs/etc/config/*.wanup</em> script.  Such a script runs automatically each time the WAN link and firewall are up (see <a href="http://www.dd-wrt.com/wiki/index.php/Script_Execution" target="_blank">Script Execution</a>).

To restrict <em>br1</em> from accessing <em>br0</em> and visa versa,
<pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">iptables -I FORWARD -i br0 -o br1 -m state --state NEW -j DROP
iptables -I FORWARD -i br1 -o br0 -m state --state NEW -j DROP</pre>
Optionally, to restrict <em>br1</em> from accessing the management interface of the router.
<pre class="brush: bash; gutter: false; toolbar: off; tab-size: 8">iptables -I INPUT -i br1 -m state --state NEW -j DROP
iptables -I INPUT -i br1 -p udp --dport 67 -j ACCEPT
iptables -I INPUT -i br1 -p udp --dport 53 -j ACCEPT
iptables -I INPUT -i br1 -p tcp --dport 53 -j ACCEPT</pre>
<h3>Test</h3>
To test the interfaces, configure a space computer interface for DHCP and connect it to each of the LAN ports.  Then verify that the assigned IP address matches the subnet of the network.  Do the same with the wireless networks.]]></description>
		<wfw:commentRss>http://www.coertvonk.com/technology/networking/dd-wrt-leading-two-seperate-networks-5829/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-leading-two-seperate-networks.png" length="150751" type="image/jpg" />
<media:content xmlns:media="http://search.yahoo.com/mrss/" url="http://www.coertvonk.com/wp-content/uploads/networking-ddwrt-leading-two-seperate-networks.png" width="300" height="300" medium="image" type="image/jpeg">
<media:copyright>Coert Vonk</media:copyright>
</media:content>
	</item>
	</channel>
</rss>
