<?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>Justin Tanner&#039;s Blog</title>
	<atom:link href="http://jwtanner.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://jwtanner.com/blog</link>
	<description>Thoughts on web development and embedded systems.</description>
	<lastBuildDate>Fri, 07 Jan 2011 01:55:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Roombas at UVIC 2008 Wrap-up</title>
		<link>http://jwtanner.com/blog/?p=11</link>
		<comments>http://jwtanner.com/blog/?p=11#comments</comments>
		<pubDate>Tue, 06 Jan 2009 01:53:57 +0000</pubDate>
		<dc:creator>jwtblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jwtanner.com/blog/?p=11</guid>
		<description><![CDATA[Last semester at UVIC I was privileged to work as the teaching assistant for the class CSC 460: Design and Analysis of Real-time Systems. This year the professor bought 6 refurbished Roomba&#39;s to be used by the students. These Roomba&#39;s were programmed with the serial control interface (pdf). This inteface allows API commands to be [...]]]></description>
			<content:encoded><![CDATA[<p>Last semester at <a href="http://www.uvic.ca/">UVIC</a> I was privileged to work as the teaching assistant for the class <a href="http://web.uvic.ca/calendar2008/CDs/CSC/460.html">CSC 460: Design and Analysis of Real-time Systems</a>.  This year <a href="http://webhome.csc.uvic.ca/~mcheng/samples/personal/About%20Me.html">the professor</a> bought 6 refurbished <a href="http://store.irobot.com/corp/index.jsp">Roomba&#39;s</a> to be used by the students.</p>
<p>These Roomba&#39;s were programmed with the serial control interface (<a href="http://www.roombadevtools.com/productcart/pc/docs/docs_roombasci.pdf">pdf</a>).  This inteface allows API commands to be sent directly from a micro-controller to a Roomba via UART.</p>
<p>In under a month each team in this class created an impressive project using more than one combination of Roomba&#39;s, micro-controller&#39;s, and wireless radio&#39;s.  Here are the some of the video highlights.</p>
<p>Mike Waltz and Rayeed Ashir made three Roomba&#39;s dance in synchronization.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/z-7naUQi2TI&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/z-7naUQi2TI&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p></p>
<p>Neil MacMillan and Ryan Downing made two Roomba&#39;s follow each other with <a href="http://www.pololu.com/catalog/product/701">pololu IR sensor&#39;s</a>.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/6RGcUic6UZI&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/6RGcUic6UZI&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p></p>
<p>Here is a link to <a href="http://www.youtube.com/watch?v=WPQOre2YyiE">another video</a> of the follow the leader Roomba from a POV perspective.</p>
<p>Allen River and Duncan Penfold-brown created an ant Roomba that gathers IR beacons.</p>
<p><object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/wlP2403tKLE&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/wlP2403tKLE&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object></p>
<p></p>
<p>More videos of the ant Roomba.</p>
<ul>
<li><a href="http://www.youtube.com/watch?v=axHumg9Xb8A">Ant Roomba Resource Other Angle</a></li>
<li><a href="http://www.youtube.com/watch?v=wxwowPQ7vhA">Ant Roomba Queen Base Station</a></li>
<li><a href="http://www.youtube.com/watch?v=Xq287SYQoC8">Ant Roomba Collision Other Angle</a></li>
<li><a href="http://www.youtube.com/watch?v=7teFD0LKzdQ">Ant Roomba Collision</a></li>
<li><a href="http://www.youtube.com/watch?v=FpWNV0oKhhU">Ant Roomba Init</a></li>
</ul>
<p>Cecelia Redding and Matt Campbell created an attacker Roomba and a defender Roomba.  No video was available.</p>
<p>I also managed a Roomba project of my own this semester.  I created a USB gamepad controlled Roomba ( using wireless Bluetooth ).</p>
<p><object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/TAxUaDayFFg&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/TAxUaDayFFg&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object></p>
<p>More videos of the gamepad controlled Roomba.</p>
<ul>
<li><a href="http://www.youtube.com/watch?v=e_ZfxjYHOJ8">Joystick-controlled Roomba at UVIC</a></li>
<li><a href="http://www.youtube.com/watch?v=tccGOWjfq1w">IR Sensor and the joystick controlled Roomba</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jwtanner.com/blog/?feed=rss2&#038;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The LED Wheel</title>
		<link>http://jwtanner.com/blog/?p=7</link>
		<comments>http://jwtanner.com/blog/?p=7#comments</comments>
		<pubDate>Sat, 27 Dec 2008 01:52:07 +0000</pubDate>
		<dc:creator>jwtblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jwtanner.com/blog/?p=7</guid>
		<description><![CDATA[Last summer I created a project for the University of Victoria that I unimaginatively named the LED Wheel. The LED Wheel is an embedded systems project designed to spin a single row of blinking LEDs and achieve persistence of vision. Here is a picture of the LED Wheel displaying the pattern &#34;UVIC OPEN HOUSE&#34;. More [...]]]></description>
			<content:encoded><![CDATA[<p>Last summer I created a project for the <a href="http://www.uvic.ca">University of Victoria</a> that I unimaginatively named the <a href="http://www.jwtanner.com/led_wheel/">LED Wheel</a>.  The <a href="http://www.jwtanner.com/led_wheel/">LED Wheel</a> is an embedded systems project designed to spin a single row of blinking LEDs and achieve persistence of vision.</p>
<p>Here is a picture of the LED Wheel displaying the pattern &quot;UVIC OPEN HOUSE&quot;.</p>
<p><img src="http://www.jwtanner.com/led_wheel/images/action1.jpg" border="0" alt="led wheel displaying pattern"></img></p>
<p>More pictures, video and all the details can be found in the <a href="http://www.jwtanner.com/led_wheel/">report</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jwtanner.com/blog/?feed=rss2&#038;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Burned by Embedded Systems: Volatile</title>
		<link>http://jwtanner.com/blog/?p=1</link>
		<comments>http://jwtanner.com/blog/?p=1#comments</comments>
		<pubDate>Thu, 10 Jan 2008 17:42:35 +0000</pubDate>
		<dc:creator>jwtblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jwtanner.com/blog/?p=1</guid>
		<description><![CDATA[The following is a excerpt from this report on the construction of a real time operating system. Volatile During development of the RTOS we ran into a nasty bug. When we chose an optimization option in the gcc compiler such as -O2, our OS behaved differently than when compiled without optimization. Upon closer inspection we [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a excerpt from <a href="http://www.jwtanner.com/csc460/Report2/">this report</a> on the construction of a real time operating system.</p>
<h4>Volatile</h4>
<p>During development of the RTOS we ran into a nasty bug.  When we chose an optimization<br />
option in the gcc compiler such as <code>-O2</code>, our OS behaved differently than when<br />
compiled without optimization. Upon closer inspection we found that certain C statements were ignored.</p>
<h4>The Problem</h4>
<p>To illustrate this problem imagine we have a sensor that takes 50ms to read.<br />
Code to read this sensor might look like this:</p>
<pre>
<span>int</span> <span>finished_reading</span> = 0;
<span>void</span> <span>main</span>(void)
{
<span>init_sensor</span>();
<span>for</span>(;;)
{
<span>finished_reading</span> = 0;
<span>read_sensor</span>();
<span>while</span>(<span>finished_reading</span> == 0) {};
}
}
<span>ISR</span>()
{
<span>/* read the sensor value */</span>
<span>finished_reading</span> = 1;
}
</pre>
<p>The expected behavior of this code inside the <code>for(;;)</code> loop is as follows,</p>
<ol>
<li>call <code>read_sensor()</code> and enter <code>while()</code> loop</li>
<li>ISR fires, gets the sensor value, and sets <code>finished_reading</code> to 1</li>
<li>the <code>while()</code> loop exits</li>
<li>go back to step one</li>
</ol>
<p>But the optimization of this code causes a problem: step 3 never occurs.  The ISR executes as expected, but the value of <code>finished_reading</code> never gets set to 1.</p>
<h4>Upon Closer Inspection</h4>
<p>To see what is going on we must go down to the assembly level.</p>
<p><b>C code</b></p>
<pre>
<span>while</span>(<span>finished_reading</span> == 0) {};
</pre>
<p><b>AVR RISC assembler</b></p>
<pre>
+000004A8:   9180010B    <span>LDS</span>     R24,0x010B    <span>Load direct from data space</span>
+000004AA:   2388        <span>TST</span>     R24           <span>Test for Zero or Minus</span>
+000004AB:   F4A9        <span>BRNE</span>    PC-0x01       <span>Branch if not equal</span>
</pre>
<p>The first line of assembly gets finished_reading from memory and places it in register 24.  The next instruction tests if the value in register 24 is zero.  And the last instruction branches back to the previous instruction.</p>
<p>But a problem lies in the last line of assembler.<br />
We want the instruction to branch back to the top of the loop.<br />
Since GCC thinks the value of finished_reading will never change it jumps back<br />
to the second instruction, not the first.<br />
The last instruction should read <code>BRNE PC-0x0<b>3</b></code>, not <code>BRNE PC-0x01</code>.</p>
<p>From the compilers perspective the value of finished_reading is set once in main() and never changed.  The behavior of the ISR() and how it interacts with the globals used in main() is completely unknown to the compiler.  So it is making the right optimization, saving 2 cpu cycles for each loop iteration.</p>
<h4>The Solution</h4>
<p>The solution to this problem is to change the definition of finished_reading to be volatile.</p>
<pre>
<span>int <b>volatile</b></span> <span>finished_reading</span>;
</pre>
<p>This will change the third instruction to read BRNE PC-0&#215;03, getting a fresh value of finished_reading from its location in memory.</p>
<h4>How does volatile work?</h4>
<p>A variable declared with a type qualifier <code>volatile</code>, informs the compiler not to optimize references to or modifications of the variable across <em>sequence points</em>, such as the end of statements or expressions in a test.<br />
In practice,  declaring a variable with the <code>volatile</code> qualifier forces the compiler to produce machine instructions that read or write directly to the memory location of the variable every time the variable is accessed.</p>
<p>In the example above, there are several sequence points between the initial setting of the <code>finished_reading</code> variable, and its occurrence in the loop condition. Under optimization, the compiler noticed<br />
that the variable never gets modified between these two points, and so optimized the accesses to it.<br />
But the ISR can execute at any time, which the compiler cannot expect.<br />
By using <code>volatile</code>, we forced the compiler to put the variable access back in.</p>
<p>Choosing an optimization option in the compiler typically makes the execution of the code faster,<br />
at the expense of a larger executable. For example, sections of code may be repeated several times,<br />
rather than using a loop variable. Or functions may be copied and placed inline to save the time of<br />
a function call. Higher levels of optimization perform even deeper analysis on the code, to skip<br />
unnecessary machine instructions. Sometimes, this is not desirable.</p>
<p>In our project, we qualified as <code>volatile</code> every variable used within an ISR. This sometimes makes the<br />
code run more slowly than it could, but at least it runs correctly.</p>
<p>More information about <code>volatile</code> can be found here,</p>
<p><a href="http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html">The C Book &#8211; Const and volatile</a></p>
<p>And here,</p>
<p><a href="http://www.embedded.com/story/OEG20010615S0107">Embedded.com &#8211; Introduction to the Volatile Keyword</a></p>
<p>The following is a excerpt from <a href="http://www.jwtanner.com/csc460/Report2/">this report</a> on the construction of a real time operating system.</p>
<h4>Volatile</h4>
<p>During development of the RTOS we ran into a nasty bug.  When we chose an optimizationoption in the gcc compiler such as <code>-O2</code>, our OS behaved differently than whencompiled without optimization. Upon closer inspection we found that certain C statements were ignored.</p>
<h4>The Problem</h4>
<p>To illustrate this problem imagine we have a sensor that takes 50ms to read.Code to read this sensor might look like this:</p>
<pre><span>int</span> <span>finished_reading</span> = 0;
<span>void</span> <span>main</span>(void){    <span>init_sensor</span>();
<span>for</span>(;;)    {        <span>finished_reading</span> = 0;        <span>read_sensor</span>();
<span>while</span>(<span>finished_reading</span> == 0) {};    }}

<span>ISR</span>(){    <span>/* read the sensor value */</span>    <span>finished_reading</span> = 1;}</pre>
<p>The expected behavior of this code inside the <code>for(;;)</code> loop is as follows,</p>
<ol>
<li>call <code>read_sensor()</code> and enter <code>while()</code> loop</li>
<li>ISR fires, gets the sensor value, and sets <code>finished_reading</code> to 1</li>
<li>the <code>while()</code> loop exits</li>
<li>go back to step one</li>
</ol>
<p>But the optimization of this code causes a problem: step 3 never occurs.  The ISR executes as expected, but the value of <code>finished_reading</code> never gets set to 1.</p>
<h4>Upon Closer Inspection</h4>
<p>To see what is going on we must go down to the assembly level.</p>
<p><b>C code</b></p>
<pre><span>while</span>(<span>finished_reading</span> == 0) {};</pre>
<p><b>AVR RISC assembler</b></p>
<pre>+000004A8:   9180010B    <span>LDS</span>     R24,0x010B    <span>Load direct from data space</span>+000004AA:   2388        <span>TST</span>     R24           <span>Test for Zero or Minus</span>+000004AB:   F4A9        <span>BRNE</span>    PC-0x01       <span>Branch if not equal</span></pre>
<p>The first line of assembly gets finished_reading from memory and places it in register 24.  The next instruction tests if the value in register 24 is zero.  And the last instruction branches back to the previous instruction.</p>
<p>But a problem lies in the last line of assembler.We want the instruction to branch back to the top of the loop. Since GCC thinks the value of finished_reading will never change it jumps backto the second instruction, not the first.The last instruction should read <code>BRNE PC-0x0<b>3</b></code>, not <code>BRNE PC-0x01</code>.</p>
<p>From the compilers perspective the value of finished_reading is set once in main() and never changed.  The behavior of the ISR() and how it interacts with the globals used in main() is completely unknown to the compiler.  So it is making the right optimization, saving 2 cpu cycles for each loop iteration.</p>
<h4>The Solution</h4>
<p>The solution to this problem is to change the definition of finished_reading to be volatile.</p>
<pre><span>int <b>volatile</b></span> <span>finished_reading</span>;</pre>
<p>This will change the third instruction to read BRNE PC-0&#215;03, getting a fresh value of finished_reading from its location in memory.</p>
<h4>How does volatile work?</h4>
<p>A variable declared with a type qualifier <code>volatile</code>, informs the compiler not to optimize references to or modifications of the variable across <em>sequence points</em>, such as the end of statements or expressions in a test.In practice,  declaring a variable with the <code>volatile</code> qualifier forces the compiler to produce machine instructions that read or write directly to the memory location of the variable every time the variable is accessed.</p>
<p>In the example above, there are several sequence points between the initial setting of the <code>finished_reading</code> variable, and its occurrence in the loop condition. Under optimization, the compiler noticedthat the variable never gets modified between these two points, and so optimized the accesses to it.But the ISR can execute at any time, which the compiler cannot expect.By using <code>volatile</code>, we forced the compiler to put the variable access back in.</p>
<p>Choosing an optimization option in the compiler typically makes the execution of the code faster,at the expense of a larger executable. For example, sections of code may be repeated several times,rather than using a loop variable. Or functions may be copied and placed inline to save the time ofa function call. Higher levels of optimization perform even deeper analysis on the code, to skipunnecessary machine instructions. Sometimes, this is not desirable.</p>
<p>In our project, we qualified as <code>volatile</code> every variable used within an ISR. This sometimes makes thecode run more slowly than it could, but at least it runs correctly.</p>
<p>More information about <code>volatile</code> can be found here,</p>
<p><a href="http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html">The C Book &#8211; Const and volatile</a>
<p>And here,</p>
<p><a href="http://www.embedded.com/story/OEG20010615S0107">Embedded.com &#8211; Introduction to the Volatile Keyword</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jwtanner.com/blog/?feed=rss2&#038;p=1</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

