<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
    <title>Doswa</title>
    <link href="http://doswa.com/atom.xml" rel="self"/>
    <link href="http://doswa.com"/>
    <updated>2011-07-21T16:22:09-07:00</updated>
    <id>http://doswa.com</id>
    
    <entry>
        <title>MathJax in Markdown</title>
        <link href="http://doswa.com/2011/07/20/mathjax-in-markdown.html"/>
        <updated>2011-07-20T00:00:00-07:00</updated>
        <id>http://doswa.com/2011/07/20/mathjax-in-markdown</id>
        <content type="html">&lt;p&gt;I had some issues getting &lt;a href=&quot;http://www.mathjax.org/&quot;&gt;MathJax&lt;/a&gt; running smoothly alongside &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;Markdown&lt;/a&gt;. The main issue stemmed from Markdown not providing a way to temporarily escape from its text transformations, so some &lt;a href=&quot;http://www.latex-project.org/&quot;&gt;L&lt;sub&gt;A&lt;/sub&gt;T&lt;sup&gt;E&lt;/sup&gt;X&lt;/a&gt; would get interpreted as Markdown.&lt;/p&gt;

&lt;p&gt;The only way I know of to prevent text transformations in Markdown is through the code block (prefix with 4+ spaces) and inline code (surround with &lt;code&gt;`&lt;/code&gt;) features. However, both of those wrap the result in some HTML tag (&lt;code&gt;&amp;lt;pre&amp;gt;&lt;/code&gt; for the first, &lt;code&gt;&amp;lt;code&amp;gt;&lt;/code&gt; for the second). I decided to use the inline code feature as the basis for inserting my math.&lt;/p&gt;

&lt;p&gt;I can insert math by typing, for example &lt;code&gt;`\&lt;span&gt;&lt;/span&gt;( \del \cdot \vec{B} = 0 \&lt;span&gt;&lt;/span&gt;)`&lt;/code&gt;. To make this work, I had to use the following MathJax configuration:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MathJax.Hub.Config({
    tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This prevents MathJax from skipping &lt;code&gt;&amp;lt;code&amp;gt;&lt;/code&gt; tags. This could potentially cause issues if I used the sequences &lt;code&gt;\&lt;span&gt;&lt;/span&gt;(&lt;/code&gt;, &lt;code&gt;\&lt;span&gt;&lt;/span&gt;[&lt;/code&gt;, or &lt;code&gt;$&lt;span&gt;&lt;/span&gt;$&lt;/code&gt; in a normal (non-math) code block, but I think it's safe to say that those sequences won't show up in most of my code.&lt;/p&gt;

&lt;p&gt;The next step was to make sure MathJax &lt;code&gt;&amp;lt;code&amp;gt;&lt;/code&gt; tags weren't actually styled like &quot;normal&quot; &lt;code&gt;&amp;lt;code&amp;gt;&lt;/code&gt; tags. To do this, I added this to my MathJax configuration:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MathJax.Hub.Queue(function() {
    // Fix &amp;lt;code&amp;gt; tags after MathJax finishes running. This is a
    // hack to overcome a shortcoming of Markdown. Discussion at
    // https://github.com/mojombo/jekyll/issues/199
    var all = MathJax.Hub.getAllJax(), i;
    for(i = 0; i &amp;lt; all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with this in my CSS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;code.has-jax {font: inherit; font-size: 100%; background: inherit; border: inherit;}
&lt;/code&gt;&lt;/pre&gt;
</content>
    </entry>
    
    <entry>
        <title>Jekyll syntax highlighting with Pygments on Arch Linux</title>
        <link href="http://doswa.com/2011/07/19/jekyll-syntax-highlighting-with-pygments-on-arch-linux.html"/>
        <updated>2011-07-19T00:00:00-07:00</updated>
        <id>http://doswa.com/2011/07/19/jekyll-syntax-highlighting-with-pygments-on-arch-linux</id>
        <content type="html">&lt;p&gt;I had some trouble getting &lt;a href=&quot;http://jekyllrb.com&quot;&gt;Jekyll&lt;/a&gt;'s syntax highlighting working on Arch Linux. Every time I added a code block using the &lt;code&gt;{% highlight %}&lt;/code&gt; tag, the contents of the tag would just disappear.&lt;/p&gt;

&lt;p&gt;My installation was:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Install pacgem
$ wget https://aur.archlinux.org/packages/pacgem/pacgem.tar.gz
$ tar xzf pacgem.tar.gz
$ cd pacgem
$ makepkg -s
$ pacman -U pacgem-*.tar.xz

# Install Jekyll
$ pacgem jekyll

# Install Pygments
$ sudo pacman -S python-pygments
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Jekyll uses the &lt;a href=&quot;https://github.com/github/albino&quot;&gt;Albino&lt;/a&gt; wrapper for the Python &lt;a href=&quot;http://pygments.org&quot;&gt;Pygments&lt;/a&gt; library to handle syntax highlighting. My first test was to see if Albino was working correctly. I tested it out using the example from the project page:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;    &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;albino&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Albino&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colorize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;puts &amp;quot;Hello World&amp;quot;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:ruby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Sure enough, there was no output. That meant I was on the right track: the issue was in Albino, not Jekyll. My next step was to figure out what Albino was doing internally to call &lt;code&gt;pygmentize&lt;/code&gt; (the command line tool for Pygments). Albino spawns the &lt;code&gt;pygmentize&lt;/code&gt; process on line 88 of &lt;code&gt;/usr/lib/ruby/gems/1.9.1/gems/albino-1.3.3/lib/albino.rb&lt;/code&gt;. I inserted a &lt;code&gt;puts command&lt;/code&gt; right before that line to see exactly what it was doing. The command it was calling internally looked like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pygmentize -l ruby -f html -O encoding=utf-8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To test it out, I ran the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ echo 'puts &quot;Hello World&quot;' | pygmentize -l ruby -f html -O encoding=utf-8

*** Error while highlighting:
TypeError: Can't convert 'bytes' object to str implicitly
   (file &quot;/usr/lib/python3.2/codecs.py&quot;, line 355, in write)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;It's one of those dreaded Python 3 bytes/str issues.&lt;/em&gt; I decided to try leaving out the encoding to see if I could easily bypass the issue:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ echo 'puts &quot;Hello World&quot;' | pygmentize -l ruby -f html
&amp;lt;div class=&quot;highlight&quot;&amp;gt;&amp;lt;pre&amp;gt;&amp;lt;span class=&quot;nb&quot;&amp;gt;puts&amp;lt;/span&amp;gt; &amp;lt;span class=&quot;s2&quot;&amp;gt;&amp;amp;quot;Hello World&amp;amp;quot;&amp;lt;/span&amp;gt;
&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks like it worked! There might be some encoding issues later on, so I made a mental note to test it out after I was done. The next step was to prevent Albino from adding the &lt;code&gt;-O encoding=utf-8&lt;/code&gt; part to the command. On line 78 of the Albino file mentioned above (&lt;code&gt;lib/albino.rb&lt;/code&gt;), the command line options for &lt;code&gt;pygmentize&lt;/code&gt; are set:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;    &lt;span class=&quot;vi&quot;&gt;@options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lexer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:O&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;encoding=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I just wanted to get Jekyll running, so I made a quick hack and removed the &lt;code&gt;-O&lt;/code&gt; option:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;    &lt;span class=&quot;vi&quot;&gt;@options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lexer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I went back to by Albino test program and verified that things worked:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ruby -e &quot;require 'albino'; puts Albino.colorize('puts \'Hello World\'', :ruby)&quot; 
&amp;lt;div class=&quot;highlight&quot;&amp;gt;&amp;lt;pre&amp;gt;&amp;lt;span class=&quot;nb&quot;&amp;gt;puts&amp;lt;/span&amp;gt; &amp;lt;span class=&quot;s1&quot;&amp;gt;&amp;amp;#39;Hello World&amp;amp;#39;&amp;lt;/span&amp;gt;
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Things were looking good! I went back to Jekyll and tried out a test page, and the highlighting seemed to work fine. One thing was left to check: did I screw anything up by removing the encoding from the command line call? I threw a couple non-ASCII characters in my code block as a test:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# coding: utf-8&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Calculate ∇·f(x,y,z), where f:ℝ³→ℝ³ is a vector field&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;divergence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;δ&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;δ&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;δ&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;ε&lt;/span&gt;
        &lt;span class=&quot;err&quot;&gt;δ&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;δ&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;δ&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;divergence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;After rendering, it looked like it worked. My input text was in UTF-8, and I haven't tried anything else. There's a good chance it won't work if you use a different encoding.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Screenshot script for GNOME Shell</title>
        <link href="http://doswa.com/2011/05/01/screenshot-script-for-gnome-shell.html"/>
        <updated>2011-05-01T00:00:00-07:00</updated>
        <id>http://doswa.com/2011/05/01/screenshot-script-for-gnome-shell</id>
        <content type="html">&lt;p&gt;I decided to ditch Compiz, but I missed the screenshot plugin that allows you to take a screenshot by holding a key and selecting a region of the screen with the mouse. I made this script to mimic this behavior in GNOME Shell.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;c&quot;&gt;#!/usr/bin/env python&lt;/span&gt;

    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;glob&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;subprocess&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# File to save to&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BASENAME&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;HOME&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;/Desktop/screenshot&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;SUFFIX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;.png&amp;#39;&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Delay after an ImageMagick `import` failure before trying again. This&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# is necessary because the mouse isn&amp;#39;t always available right after&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# the Gnome Shell &amp;#39;Activities&amp;#39; screen closes.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;DELAY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Number of DELAYs before giving up. This is to keep from getting stuck&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# in an infinite loop in case the failure isn&amp;#39;t because of the expected&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# reason.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;RETRIES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;choose_fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fnames&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;max_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;max_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;screenshot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Popen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;import&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;returncode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;returncode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;choose_fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BASENAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SUFFIX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;screenshot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RETRIES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DELAY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
    </entry>
    
    <entry>
        <title>Kaoss Guitar (Update 2)</title>
        <link href="http://doswa.com/2010/08/27/kaoss-guitar-update-2.html"/>
        <updated>2010-08-27T00:00:00-07:00</updated>
        <id>http://doswa.com/2010/08/27/kaoss-guitar-update-2</id>
        <content type="html">&lt;p&gt;&lt;img src=&quot;/images/2010/08/27/touchscreen.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The touchscreen is now working. I'm using the internal pull-up resistor on the ADC pin to detect when the touchscreen isn't touched. The algorithm looks something like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;kt&quot;&gt;uint16_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;touch_get_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ADC values go from 0-1023, so a value over 1000 indicates&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// that the screen isn&amp;#39;t being touched and the resistor is&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// what&amp;#39;s pulling the value high. The value when someone&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// is touching the screen varies from around 100 to 900.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Screen is not being touched&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Screen is being touched&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;touch_normalize_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
    </entry>
    
    <entry>
        <title>AVRDUDE 5.10 with FTDI bitbang on Linux</title>
        <link href="http://doswa.com/2010/08/24/avrdude-5-10-with-ftdi-bitbang.html"/>
        <updated>2010-08-24T00:00:00-07:00</updated>
        <id>http://doswa.com/2010/08/24/avrdude-5-10-with-ftdi-bitbang</id>
        <content type="html">&lt;p&gt;&lt;img src=&quot;/images/2010/08/24/ftdi.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Programming an ATmega168 using &lt;a href=&quot;http://www.sparkfun.com/commerce/product_info.php?products_id=9115&quot;&gt;SparkFun's FTDI Basic Breakout&lt;/a&gt;. This guide works for other FT232R-based devices as well.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, install the dependencies. On Ubuntu 10.04, this should take care of all of them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo apt-get install patch build-essential libreadline-dev libncurses-dev libusb-dev libftdi-dev automake autoconf bison flex
sudo apt-get build-dep avrdude avrdude-doc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next, create a working directory for the build and cd into it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir avrdude
cd avrdude
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now download the D2xx driver from FTDI. Only download the one that corresponds to your OS (either 64 bit or 32 bit).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# For 64-bit:
wget http://www.ftdichip.com/Drivers/D2XX/Linux/libftd2xx0.4.16_x86_64.tar.gz
# For 32-bit:
wget http://www.ftdichip.com/Drivers/D2XX/Linux/libftd2xx0.4.16.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Download AVRDUDE and the &lt;a href=&quot;http://savannah.nongnu.org/patch/?6886&quot;&gt;FTDI bitbang patches&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;wget http://download.savannah.gnu.org/releases-noredirect/avrdude/avrdude-5.10.tar.gz
for i in 8 7 6 5 4 3 2 1 0; do wget -O patch-$i.diff http://savannah.nongnu.org/patch/download.php?file_id=1851$i; done
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Extract everything, put the drivers into place, and apply the patches:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tar xzf avrdude-5.10.tar.gz
tar xzf libftd2xx*.tar.gz
cd avrdude-5.10
for file in ../patch-*.diff; do patch -p0 &amp;lt; $file; done
cp ../libftd2xx*/static_lib/* .
cp ../libftd2xx*/*.h .
cp ../libftd2xx*/*.cfg .
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Configure and compile AVRDUDE.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./configure CFLAGS=&quot;-g -O2 -DSUPPORT_FT245R&quot; LIBS=&quot;./libftd2xx.a.0.4.16 -lpthread -ldl -lrt&quot;
make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you're using &lt;a href=&quot;http://www.sparkfun.com/commerce/product_info.php?products_id=9115&quot;&gt;SparkFun's FTDI Basic Breakout&lt;/a&gt; board, you can use the following to update your AVRDUDE configuration. If you're using some other FTDI breakout board, you'll need to modify the miso, sck, mosi, and reset options. FTDI has a listing of which bits correspond to which pins &lt;a href=&quot;http://www.ftdichip.com/Documents/AppNotes/AN232B-01_BitBang.pdf&quot;&gt;on page 1 of the bit-bang mode datasheet&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;echo '
programmer
  id    = &quot;ftdi&quot;;
  desc  = &quot;SparkFun FTDI Basic Breakout&quot;;
  type  = ft245r;
  miso  = 1;  # RXD
  sck   = 3;  # CTS
  mosi  = 0;  # TXD
  reset = 4;  # DTR
;' &amp;gt;&amp;gt; avrdude.conf;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you're using SparkFun's FTDI Basic Breakout, you can make your connections as follows. Otherwise, make your connections according to the pins you chose in the previous step (more details &lt;a href=&quot;/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer.html&quot;&gt;in my previous post on the subject&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;FTDI -&amp;gt; MCU (ATmega168 PDIP pin)
--------------------------------
DTR -&amp;gt; RESET (1)
RXI -&amp;gt; MISO (18)
TXO -&amp;gt; MOSI (17)
5V -&amp;gt; VCC (7 &amp;amp; 20)
CTS -&amp;gt; SCK (19)
GND -&amp;gt; GND (8 &amp;amp; 22)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now try it out to make sure everything worked. On Ubuntu, sudo is required because of the default permissions applied to USB devices. The &lt;code&gt;-B 1&lt;/code&gt; option sets the programming speed to the lowest possible. You can omit that option if your AVR is clocked higher than 8MHz. If this runs successfully, your AVR's high fuse byte should be printed to the terminal in hexadecimal.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ./avrdude -C avrdude.conf -c ftdi -p m168 -P ft0 -U hfuse:r:-:h -B 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If everything worked well, you can now install AVRDUDE.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# If you're using Ubuntu, you can use checkinstall to build a .deb package and install it:
sudo checkinstall
# Otherwise, just use `make install`:
sudo make install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should now be able to use AVRDUDE without specifying a configuration file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo avrdude -c ftdi -p m168 -P ft0 -U hfuse:r:-:h -B 1
&lt;/code&gt;&lt;/pre&gt;
</content>
    </entry>
    
    <entry>
        <title>Quantum lolcats</title>
        <link href="http://doswa.com/2010/07/02/quantum-lolcats.html"/>
        <updated>2010-07-02T00:00:00-07:00</updated>
        <id>http://doswa.com/2010/07/02/quantum-lolcats</id>
        <content type="html">&lt;p&gt;&quot;Funny fact: lolcats cannot be put in a Schroedinger state, because quantum rules don't work on a macro level.&quot; - &lt;a href=&quot;http://www.metafilter.com/90216/Quantum-weirdness-at-the-large-scale#3000515&quot;&gt;qvantamon on MetaFilter&lt;/a&gt;&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Kaoss Guitar (Update 1)</title>
        <link href="http://doswa.com/2010/04/18/kaoss-guitar-day-1.html"/>
        <updated>2010-04-18T00:00:00-07:00</updated>
        <id>http://doswa.com/2010/04/18/kaoss-guitar-day-1</id>
        <content type="html">&lt;p&gt;For a while, a friend and I have been planning to build a specialized MIDI controller into a guitar to control a Korg &lt;a href=&quot;http://en.wikipedia.org/wiki/Kaoss_Pad&quot;&gt;Kaoss Pad&lt;/a&gt; KP2. The idea came from the guitar used by Matt Bellamy of Muse (check out &lt;a href=&quot;http://www.youtube.com/watch?v=h1cZDQFgHH8&quot;&gt;a video of it&lt;/a&gt;).&lt;/p&gt;

&lt;!--more--&gt;


&lt;p&gt;We ordered the electronics for the guitar from &lt;a href=&quot;http://sparkfun.com&quot;&gt;SparkFun Electronics&lt;/a&gt; and &lt;a href=&quot;http://digikey.com&quot;&gt;Digi-Key&lt;/a&gt;. When the project is over, I plan to add up the total cost and post a bill of materials. I suspect it will total to around $100.&lt;/p&gt;

&lt;p&gt;Today we tested out &lt;a href=&quot;http://www.sparkfun.com/commerce/product_info.php?products_id=8448&quot;&gt;the 4.3&quot; touchpad from SparkFun&lt;/a&gt; using an Arduino. I have just finished setting up an ATmega328P on a breadboard and got a basic blink program running. The final product will be running on a plain AVR without the Arduino libraries/bootloader.&lt;/p&gt;

&lt;p&gt;One problem I ran into using my &lt;a href=&quot;/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer.html&quot;&gt;FTDI programmer&lt;/a&gt; was the clock rate. A fresh ATmega328P comes running at 1 MHz, significantly lower than the default 16 MHz on Arduino-prepared ATmegas. As a result, I kept getting errors like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;david@second:~$ avrdude -c ftdi -p atmega168 -P ft0
avrdude: BitBang OK 
avrdude: pin assign miso 1 sck 3 mosi 0 reset 4
avrdude: drain OK 

 ft245r:  bitclk 230400 -&amp;gt; ft baud 115200
avrdude: ft245r_program_enable: failed
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Eventually I found AVRDUDE's &lt;code&gt;-B&lt;/code&gt; option. It adjusts the clock rate that's fed into the SCK pin on the AVR. I found that a value of &lt;code&gt;-B 76800&lt;/code&gt; works well, but if you're running into this problem, I recommend starting with &lt;code&gt;-B 1&lt;/code&gt; and going up until it stops working. To be explicit, this is the command that I found works:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;david@second:~$ avrdude -c ftdi -p atmega168 -P ft0 -B 76800
&lt;/code&gt;&lt;/pre&gt;
</content>
    </entry>
    
    <entry>
        <title>Client-side Prediction</title>
        <link href="http://doswa.com/2010/04/03/client-side-prediction.html"/>
        <updated>2010-04-03T00:00:00-07:00</updated>
        <id>http://doswa.com/2010/04/03/client-side-prediction</id>
        <content type="html">&lt;p&gt;I was reading about client-side prediction in the context of video game physics today and thought of an idea: client-side prediction on streamed video (e.g. on Skype or similar video chats). Each pixel could be assigned a velocity and &quot;color velocity&quot; (&lt;code&gt;\( \frac{d \, \text{red}}{dt} \)&lt;/code&gt;, &lt;code&gt;\( \frac{d \, \text{blue}}{dt} \)&lt;/code&gt;, &lt;code&gt;\( \frac{d \, \text{green}}{dt} \)&lt;/code&gt;). A velocity grid method like the one described in &lt;a href=&quot;http://www.gamasutra.com/view/feature/1549/practical_fluid_dynamics_part_1.php?print=1&quot;&gt;Practical Fluid Dynamics 1&lt;/a&gt; and &lt;a href=&quot;http://www.gamasutra.com/view/feature/1615/practical_fluid_dynamics_part_2.php?print=1&quot;&gt;2&lt;/a&gt; comes to mind.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Using Cairo in Pygame</title>
        <link href="http://doswa.com/2010/03/29/using-cairo-in-pygame.html"/>
        <updated>2010-03-29T00:00:00-07:00</updated>
        <id>http://doswa.com/2010/03/29/using-cairo-in-pygame</id>
        <content type="html">&lt;p&gt;This is a method of using Cairo with Pygame. It works by sharing the same block of memory for the Cairo drawing surface and the Pygame image surface. The only trick to it is making sure that Cairo and Pygame use the same format to store the pixel data in.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;c&quot;&gt;#!/usr/bin/env python&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;cairo&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;math&lt;/span&gt;
 
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Initialize pygame with 32-bit colors. This setting stores the pixels&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# in the format 0x00rrggbb.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Get a reference to the memory block storing the pixel data.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surfarray&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixels2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Set up a Cairo surface using the same memory block and the same pixel&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# format (Cairo&amp;#39;s RGB24 format means that the pixels are stored as&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# 0x00rrggbb; i.e. only 24 bits are used and the upper 16 are 0).&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cairo_surface&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cairo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ImageSurface&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_for_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pixels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cairo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FORMAT_RGB24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Draw a white circle to the screen using pygame.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Draw a smaller black circle to the screen using Cairo.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cairo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cairo_surface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_source_rgb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Flip the changes into view.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 
    &lt;span class=&quot;c&quot;&gt;# Wait for the user to quit.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Tested on Python 2.6.4, pycairo 1.8.6, Cairo 1.8.8, pygame 1.8.1, numpy 1.3.0.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Relation between motor's torque/speed and its voltage/current.</title>
        <link href="http://doswa.com/2010/01/23/relation-between-motors-torquespeed-and-its-voltagecurrent.html"/>
        <updated>2010-01-23T00:00:00-08:00</updated>
        <id>http://doswa.com/2010/01/23/relation-between-motors-torquespeed-and-its-voltagecurrent</id>
        <content type="html">&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;torque&lt;/strong&gt; of a motor is proportional to the &lt;strong&gt;current&lt;/strong&gt; flowing through it (&lt;code&gt;\(\tau \propto i\)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;angular velocity&lt;/strong&gt; of a motor is proportional to the &lt;strong&gt;voltage&lt;/strong&gt; across it (&lt;code&gt;\(\omega \propto v\)&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Details and derivation here: &lt;a href=&quot;/projects/motor_torque_speed/motor.pdf&quot;&gt;motor.pdf&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Wacom Jitter Fix (for Linux)</title>
        <link href="http://doswa.com/2010/01/01/wacom-jitter-fix-for-linux.html"/>
        <updated>2010-01-01T00:00:00-08:00</updated>
        <id>http://doswa.com/2010/01/01/wacom-jitter-fix-for-linux</id>
        <content type="html">&lt;p&gt;My Wacom Bamboo Fun tablet has been exceptionally jittery on my Lenovo ThinkPad. I assume this is probably caused by EM noise from the computer (though I'm not sure). [&lt;strong&gt;Update:&lt;/strong&gt; it's actually caused by a noisy power supply.] Today I decided I should try to fix that.&lt;/p&gt;

&lt;p&gt;Here's an image to demonstrate the problem (click to enlarge):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2010/01/01/lines1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;On Ubuntu, there's a package called &lt;code&gt;wacom-tools&lt;/code&gt; that includes a configuration utility named &lt;code&gt;xsetwacom&lt;/code&gt;. The &lt;code&gt;xsetwacom&lt;/code&gt; utility has a couple parameters to help against noise: &lt;code&gt;RawSample&lt;/code&gt; and &lt;code&gt;Suppress&lt;/code&gt;. &lt;code&gt;RawSample&lt;/code&gt; allows you to adjust the number of samples taken from the tablet before the coordinates are committed to the mouse pointer on screen. &lt;code&gt;Suppress&lt;/code&gt; prevents the mouse pointer from moving unless it has been moved by a given amount. So, all you need to do is tune those parameters to something that works well for your setup. I found the following to be acceptable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;xsetwacom set &quot;Wacom BambooFun 4x5&quot; RawSample 9
xsetwacom set &quot;Wacom BambooFun 4x5&quot; Suppress 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that those commands are specifically for the Wacom BambooFun 4x5 tablet. You will probably need to replace that part with something else. You can find out what you should replace it with by executing this command and looking over its output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;xidump --list
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, I wanted these commands to be run automatically every time I logged in, so I created a small script at &lt;code&gt;~/.local/bin/wacom_init&lt;/code&gt; and put the following in it:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;    &lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;
    xsetwacom &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Wacom BambooFun 4x5&amp;quot;&lt;/span&gt; RawSample 9
    xsetwacom &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Wacom BambooFun 4x5&amp;quot;&lt;/span&gt; Suppress 10
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Finally, I went in my menu to System → Preferences → Startup Applications and created an entry to automatically run the command &lt;code&gt;/home/david/.local/bin/wacom_init&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All done!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>DIY aux input on Alpine car stereo (M-BUS)</title>
        <link href="http://doswa.com/2010/01/01/diy-aux-input-on-alpine-car-stereo-m-bus.html"/>
        <updated>2010-01-01T00:00:00-08:00</updated>
        <id>http://doswa.com/2010/01/01/diy-aux-input-on-alpine-car-stereo-m-bus</id>
        <content type="html">&lt;p&gt;&lt;strong&gt;If anything in this article is unclear or confusing, please let me know in the comments and I'll do my best to clarify.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: by following the instructions below, you may end up destroying your stereo, car, MP3 player, interpersonal relationships, and everything else you hold dear. I am not responsible for any of this. That being said, it worked fine for me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This method will work on any Alpine stereos with an 8-pin M-BUS connector in the back. (The stereo I performed this on was an Alpine CDM-9821.) The connector itself should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&amp;amp;name=275-1027-ND&quot;&gt;&lt;img src=&quot;/images/2010/01/01/2dj-0108psa2.jpg&quot; alt=&quot;2dj-0108psa2&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After some searching around, I located the functions of each pin on this connector at &lt;a href=&quot;http://pinouts.ru/Home/alpine_cdchanger_pinout.shtml&quot;&gt;pinouts.ru&lt;/a&gt;. The pins on interest are the bottom three:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2010/01/01/2dj-0108psa2asdfasdf.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This corresponds perfectly with the contacts on a 3.5mm headphone plug:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2010/01/01/ak203mm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So all that's left to do is make an adapter between the two. I ordered these parts from Digi-Key: &lt;a href=&quot;http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&amp;amp;name=CP-1080-ND&quot;&gt;CP-1080-ND&lt;/a&gt;, &lt;a href=&quot;http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&amp;amp;name=AE9918-ND&quot;&gt;AE9918-ND&lt;/a&gt;, and &lt;a href=&quot;http://search.digikey.com/scripts/DkSearch/dksus.dll?WT.z_header=search_go&amp;amp;lang=en&amp;amp;site=us&amp;amp;keywords=CP1-3513-ND&amp;amp;x=0&amp;amp;y=0&quot;&gt;CP1-3513-ND&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Gather up some solder, a soldering iron, and wire. Make the following connections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&quot;Audio Ground&quot; pin from the M-BUS connector goes to the pin closest to the hole on the 3.5mm audio jack&lt;/li&gt;
&lt;li&gt;&quot;Right Audio Input&quot; goes to the pin farthest from the hole&lt;/li&gt;
&lt;li&gt;&quot;Left Audio Input&quot; goes to the only remaining pin.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;You should end up with an adapter cable like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2010/01/01/sj1-3513.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Plug the M-BUS connector into the back of your stereo and mount the audio jack somewhere on the dash.&lt;/p&gt;

&lt;p&gt;Now turn on your car. If all went well, nothing will start on fire. Hold the &quot;Setup&quot; button on the stereo and enable aux input in the menu. Now hit the &quot;Source&quot; button until you get to AUX. At this point, you should be able to plug in an MP3 player and have the audio routed to your car's speakers.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Python/Unix Calculator</title>
        <link href="http://doswa.com/2010/01/01/calculator.html"/>
        <updated>2010-01-01T00:00:00-08:00</updated>
        <id>http://doswa.com/2010/01/01/calculator</id>
        <content type="html">

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;    david@second:~&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cat /home/david/.local/bin/&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;c&quot;&gt;#!/usr/bin/env python&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;__import__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;sys&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;__import__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;subprocess&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Popen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;bc&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;-lq&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;?&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;__import__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;subprocess&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;communicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;__import__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;sys&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;?&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;__import__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;os&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;waitpid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;__import__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;subprocess&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;?&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Popen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;bc&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;-lq&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;    david@second:~&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;5*5 + 5&amp;#39;&lt;/span&gt;
    30
    david@second:~&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    3+1
    4
    last*2
    8
    david@second:~&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
    </entry>
    
    <entry>
        <title>SparkFun's FTDI Basic Breakout as an AVR programmer</title>
        <link href="http://doswa.com/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer.html"/>
        <updated>2009-12-20T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer</id>
        <content type="html">&lt;p&gt;&lt;strong&gt;This guide has been merged into my &lt;a href=&quot;/2010/08/24/avrdude-5-10-with-ftdi-bitbang.html&quot;&gt;AVRDUDE 5.10 bitbang guide&lt;/a&gt;. Original post follows (left here for reference).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2009/12/20/screenshot1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now open up &lt;code&gt;avrdude.conf&lt;/code&gt; in a text editor. Add this entry by the other programmers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# FTDI basic breakout
programmer
  id = &quot;ftdi&quot;;
  desc = &quot;FTDI Basic Breakout&quot;;
  type = ft245r;
  miso = 1; # RxD
  sck = 3; # CTS
  mosi = 0; # TxD
  reset = 4; # DTR
;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can attach your FTDI Breakout to the target AVR like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;FTDI -&amp;gt; MCU (ATmega168 PDIP pin)
--------------------------------
DTR -&amp;gt; RESET (1)
RXI -&amp;gt; MISO (18)
TXO -&amp;gt; MOSI (17)
5V -&amp;gt; VCC (7 &amp;amp; 20)
CTS -&amp;gt; SCK (19)
GND -&amp;gt; GND (8 &amp;amp; 22)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With the 3x2 ICSP header on the Arduino, that would look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        +----------------+
[RXI]--o| 1 MISO   +5V 2 |o--[5V]
[CTS]--o| 3 SCK   MOSI 4 |o--[TXO]
[DTR]--o| 5 RESET  GND 6 |o--[GND]
        +----------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that may need to supply the AVR with an external oscillator or clock source. For virgin ATmega168s, you won't need to (because it initially uses the internal oscillator), but you'll need an external crystal oscillator if you're using one that came out of an Arduino.&lt;/p&gt;

&lt;p&gt;From there you can use AVRDUDE like normal using the &lt;code&gt;-c ftdi&lt;/code&gt; switch. For example, to read fuse bytes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ./avrdude -C avrdude.conf -c ftdi -p m168 -P ft0 -U hfuse:r:-:h -U lfuse:r:-:h -U efuse:r:-:h
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Edit 2010-04-18:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I just found out that new AVRs need to have the clock rate adjusted before this will work. This is because AVRs come with the internal clock set to 1 MHz (actually, 8 MHz with the CKDIV8 fuse bit set) instead of the Arduino default 16 MHz. This can be done with AVRDUDE's &lt;code&gt;-B&lt;/code&gt; option. This option determines what clock is fed into the AVR's SCK pin. I recommend starting with a value of &lt;code&gt;-B 1&lt;/code&gt; and working your way up to a reasonable clock speed. Here is the adjusted command for new AVRs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ./avrdude -C avrdude.conf -c ftdi -p m168 -P ft0 -U hfuse:r:-:h -U lfuse:r:-:h -U efuse:r:-:h -B 1
&lt;/code&gt;&lt;/pre&gt;
</content>
    </entry>
    
    <entry>
        <title>Accessing AVR GPIOs via bit fields</title>
        <link href="http://doswa.com/2009/12/08/accessing-avr-gpios-via-bit-fields.html"/>
        <updated>2009-12-08T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/12/08/accessing-avr-gpios-via-bit-fields</id>
        <content type="html">&lt;p&gt;&lt;img src=&quot;/images/2009/12/08/atmega328.png&quot; alt=&quot;atmega328&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I noticed that the &lt;a href=&quot;http://focus.ti.com/docs/prod/folders/print/tms320f2809.html&quot;&gt;TMS320F2809&lt;/a&gt; examples accessed GPIOs by bits rather than as 32-bit ints. For example, it's possible to set a GPIO like this on the TMS320F280x:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;n&quot;&gt;GpioDataRegs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPADAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO12&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Sets GPIO12 to high.&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Normally when working with AVRs, I would set bits like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;n&quot;&gt;PORTB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PB3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Sets port b, bit 3 to high.&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;After a bit of tinkering, I came up with this (for AVRs):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;n&quot;&gt;portb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Sets port b, bit 3 to high.&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I personally think it looks a bit cleaner, so I'll probably be using it in the future. If you're interested in trying it out, you can download a full example here: &lt;a href=&quot;/projects/avr_bitfields/bitfields.zip&quot;&gt;bitfields.zip&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Side note: It is also possible to access GPIOs as 32-bit ints on the TMS320F280x like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;n&quot;&gt;GpioDataRegs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPADAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Sets GPIO12 to high.&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;hr /&gt;

&lt;p&gt;Edit: &lt;a href=&quot;http://www.avrfreaks.net/index.php?name=PNphpBB2&amp;amp;file=viewtopic&amp;amp;t=86521&quot;&gt;Looks like I'm not alone&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Computers are toys, not tools.</title>
        <link href="http://doswa.com/2009/08/25/230.html"/>
        <updated>2009-08-25T00:00:00-07:00</updated>
        <id>http://doswa.com/2009/08/25/230</id>
        <content type="html">
</content>
    </entry>
    
    <entry>
        <title>RK4 in Haskell</title>
        <link href="http://doswa.com/2009/07/31/rk4-in-haskell.html"/>
        <updated>2009-07-31T00:00:00-07:00</updated>
        <id>http://doswa.com/2009/07/31/rk4-in-haskell</id>
        <content type="html">&lt;p&gt;Ran across this today. It's a well written implementation of RK4.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://srihari.ramanathan.googlepages.com/runge-kuttawithhaskell&quot;&gt;4th order Runge-Kutta (RK4) integration in Haskell&lt;/a&gt;&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Line Segment to Circle Collision/Intersection Detection</title>
        <link href="http://doswa.com/2009/07/13/circle-segment-intersectioncollision.html"/>
        <updated>2009-07-13T00:00:00-07:00</updated>
        <id>http://doswa.com/2009/07/13/circle-segment-intersectioncollision</id>
        <content type="html">&lt;h2&gt;Style and Notation&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;All variables are represented in italics except in the diagrams and Python code.&lt;/li&gt;
&lt;li&gt;Vectors are shown in bold except in the diagrams and Python code.&lt;/li&gt;
&lt;li&gt;The notation |&lt;em&gt;a&lt;/em&gt;| is used to represent the length of a vector named &lt;em&gt;&lt;b&gt;a&lt;/b&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Introduction to the problem&lt;/h2&gt;

&lt;p&gt;Start by defining a few points (vectors from the world origin):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2009/07/13/2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, from these points, you can easily calculate two useful values, the segment vector, &lt;em&gt;&lt;b&gt;seg_v&lt;/b&gt;&lt;/em&gt; (from &lt;em&gt;&lt;b&gt;seg_a&lt;/b&gt;&lt;/em&gt; to &lt;em&gt;&lt;b&gt;seg_b&lt;/b&gt;&lt;/em&gt;) and the position of &lt;em&gt;&lt;b&gt;circ_pos&lt;/b&gt;&lt;/em&gt; relative to &lt;em&gt;&lt;b&gt;seg_a&lt;/b&gt;&lt;/em&gt;, &lt;em&gt;&lt;b&gt;pt_v&lt;/b&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2009/07/13/31.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;b&gt;seg_v&lt;/b&gt;&lt;/em&gt; = &lt;em&gt;&lt;b&gt;seg_b&lt;/b&gt;&lt;/em&gt; - &lt;em&gt;&lt;b&gt;seg_a&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;b&gt;pt_v&lt;/b&gt;&lt;/em&gt; = &lt;em&gt;&lt;b&gt;circ_pos&lt;/b&gt;&lt;/em&gt; - &lt;em&gt;&lt;b&gt;seg_a&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Closest point to the circle's center on the segment&lt;/h2&gt;

&lt;p&gt;The next step is to find the closest point to the circle's center on the segment (labeled &quot;&lt;em&gt;&lt;b&gt;closest&lt;/b&gt;&lt;/em&gt;&quot; on the diagram). To do this, we must project &lt;em&gt;&lt;b&gt;pt_v&lt;/b&gt;&lt;/em&gt; onto &lt;em&gt;&lt;b&gt;seg_v&lt;/b&gt;&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2009/07/13/4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To project on vector onto another, take the dot product of the vector and the unit vector of the projection target. That is:&lt;/p&gt;

&lt;p&gt;$$|proj_v| = \mathbf{pt_v} \cdot \frac{\mathbf{seg_v}}{|seg_v|}$$&lt;/p&gt;

&lt;p&gt;Note that this dot product returns a scalar value (the length of &lt;em&gt;&lt;b&gt;proj_v&lt;/b&gt;&lt;/em&gt;), not a vector.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;We now need to take a small break from the main goal and look into a special case: the ends of the segment. If |&lt;em&gt;proj_v&lt;/em&gt;| is less than &lt;em&gt;0&lt;/em&gt; or greater than |&lt;em&gt;seg_v&lt;/em&gt;|, the closest point to &lt;em&gt;&lt;b&gt;circ_pos&lt;/b&gt;&lt;/em&gt; on the segment will be one of the segment's endpoints. That is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if |proj_v| &amp;lt; 0:
    closest = seg_a
if |proj_v| &amp;gt; |seg_v|:
    closest = seg_b
&lt;/code&gt;&lt;/pre&gt;

&lt;hr /&gt;

&lt;p&gt;Next, calculate the actual &lt;em&gt;&lt;b&gt;proj_v&lt;/b&gt;&lt;/em&gt; vector, rather than just its length (|&lt;em&gt;proj_v&lt;/em&gt;|). To do this, simply multiply by the &lt;em&gt;&lt;b&gt;seg_v&lt;/b&gt;&lt;/em&gt; unit vector:&lt;/p&gt;

&lt;p&gt;$$\mathbf{proj_v} = |proj_v| \frac{\mathbf{seg_v}}{|seg_v|}$$&lt;/p&gt;

&lt;p&gt;The only thing left to do is to convert this into world coordinates (rather than coordinates relative to &lt;em&gt;&lt;b&gt;seg_a&lt;/b&gt;&lt;/em&gt;) to get the closest point:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;b&gt;closest&lt;/b&gt;&lt;/em&gt; = &lt;em&gt;&lt;b&gt;seg_a&lt;/b&gt;&lt;/em&gt; + &lt;em&gt;&lt;b&gt;proj_v&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Checking for a intersection&lt;/h2&gt;

&lt;p&gt;Now that we've calculated the closest point on the segment (the variable &quot;&lt;em&gt;&lt;b&gt;closest&lt;/b&gt;&lt;/em&gt;&quot;), we can check if the circle and segment intersect. To do this, first find the vector from &lt;em&gt;&lt;b&gt;closest&lt;/b&gt;&lt;/em&gt; to &lt;em&gt;&lt;b&gt;circ_pos&lt;/b&gt;&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;b&gt;dist_v&lt;/b&gt;&lt;/em&gt; = &lt;em&gt;&lt;b&gt;circ_pos&lt;/b&gt;&lt;/em&gt; - &lt;em&gt;&lt;b&gt;closest&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If the length of this vector is less than the circle's radius, the circle and segment are intersecting:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if |dist_v| &amp;lt; circ_rad:
    They are intersecting
else:
    They are not intersecting
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Collision response&lt;/h2&gt;

&lt;p&gt;A useful bit of data in collision response is the amount of overlap between two shapes and the direction a shape must be moved in order to resolve the collision. This can be represented as a vector that points in the same direction as &lt;em&gt;&lt;b&gt;dist_v&lt;/b&gt;&lt;/em&gt; whose length is equal to the difference between &lt;em&gt;circ_rad&lt;/em&gt; and |&lt;em&gt;dist_v&lt;/em&gt;|.&lt;/p&gt;

&lt;p&gt;$$\mathbf{offset} = (circ_{rad} - |dist_v|) \frac{\mathbf{dist_v}}{|dist_v|}$$&lt;/p&gt;

&lt;h2&gt;Python implementation&lt;/h2&gt;

&lt;p&gt;Here is a Python implementation of everything in this post. For the sake of clarity, I avoided optimizations. &lt;code&gt;vec&lt;/code&gt; is assumed to be a fully implemented 2d vector class:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;closest_point_on_seg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;seg_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pt_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Invalid segment length&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;seg_v_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seg_v_unit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;proj_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_v_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;closest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proj_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closest&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;segment_circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_rad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;closest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closest_point_on_seg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seg_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seg_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dist_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closest&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circ_rad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Circle&amp;#39;s center is exactly on segment&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circ_rad&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;h2&gt;Similar algorithms&lt;/h2&gt;

&lt;p&gt;Collisions involving &lt;a href=&quot;http://mathworld.wolfram.com/Stadium.html&quot;&gt;stadiums&lt;/a&gt; (a type of rounded rectangle) can be calculated in a similar manner. A stadium is essentially a line segment with a radius.&lt;/p&gt;

&lt;p&gt;A stadium-point collision is the same as a segment-circle collision with a circle whose radius is equal to the stadium's radius.&lt;/p&gt;

&lt;p&gt;A stadium-circle collision is the same as a segment-circle collision with a circle whose radius is equal to the sum of the original circle's radius and the stadium's radius.&lt;/p&gt;

&lt;h2&gt;Download&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/projects/segment_circle/segment_circle.zip&quot;&gt;segment_circle.zip&lt;/a&gt; - An implementation in Python and &lt;a href=&quot;http://cython.org&quot;&gt;Cython&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you find this post useful or have any questions, please leave a comment.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>JavaScript Image Animation</title>
        <link href="http://doswa.com/2009/06/24/javascript-image-animation.html"/>
        <updated>2009-06-24T00:00:00-07:00</updated>
        <id>http://doswa.com/2009/06/24/javascript-image-animation</id>
        <content type="html">&lt;p&gt;I made a small script to produce an animation with a series of images and JavaScript. I only tested it in Firefox 3, but I don't see any reason why it wouldn't work in other browsers.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/projects/animate/&quot;&gt;Try it&lt;/a&gt; or &lt;a href=&quot;/projects/animate/animate.zip&quot;&gt;download it&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://trac.sagemath.org/sage_trac/ticket/1483&quot;&gt;Sage Bug #1483&lt;/a&gt; inspired me to make this, since the sagenb.org server doesn't have ImageMagick installed.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>PHPNotify: Debug Notifications From PHP</title>
        <link href="http://doswa.com/2009/05/13/phpnotify-debug-notifications-from-php.html"/>
        <updated>2009-05-13T00:00:00-07:00</updated>
        <id>http://doswa.com/2009/05/13/phpnotify-debug-notifications-from-php</id>
        <content type="html">&lt;p&gt;This is a little program to help debug AJAX (or anything else) from PHP.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2009/05/13/screenshot.png&quot; alt=&quot;PHP Debug Notification&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the README file to instructions on setting it up on Ubuntu. Note that you must first install &lt;code&gt;python-notify&lt;/code&gt; through apt-get for this to work correctly.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/projects/phpnotify/phpnotify.tgz&quot;&gt;Download PHPNotify&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the function name &lt;code&gt;notify()&lt;/code&gt; conflicts with any of your functions, you can change it on lines 2 and 3 of &lt;code&gt;prepend.php&lt;/code&gt;&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Improved RK4 Implementation</title>
        <link href="http://doswa.com/2009/04/21/improved-rk4-implementation.html"/>
        <updated>2009-04-21T00:00:00-07:00</updated>
        <id>http://doswa.com/2009/04/21/improved-rk4-implementation</id>
        <content type="html">&lt;p&gt;&lt;img src=&quot;/images/2009/04/21/sage0.png&quot; alt=&quot;Damped harmonic motion&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is a Python implementation of the RK4 numerical integrator that works with differential functions of all orders. That is, any function in the form &lt;code&gt;\( F(x, y, y\prime, y\prime\!\prime, \dots, y^{(n)}) \)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you're new to numerical integration or even RK4 integration, please &lt;a href=&quot;/2009/01/02/fourth-order-runge-kutta-numerical-integration.html&quot;&gt;read my other post&lt;/a&gt; first. It's easier to understand because it's a less generalized function.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rk4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;RK4 implementation.&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        t = current value of the independent variable&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        h = amount to increase the independent variable (step size)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        s0 = initial state as a list. ex.: [initial_position, initial_velocity]&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        f = function(state, t) to integrate&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;An example usage of this function is:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;damped_spring_accel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stiffness&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;damping&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stiffness&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;damping&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;40.0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rk4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;damped_spring_accel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Final state: t=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; x=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; v=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;On a side note, I recently found out about Sage (&lt;a href=&quot;http://sagemath.org&quot;&gt;sagemath.org&lt;/a&gt; and &lt;a href=&quot;http://sagenb.org&quot;&gt;sagenb.org&lt;/a&gt;). Its plotting capabilities and convenient mathematical notation are especially useful. Plus, it uses Python!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Chain (warning: Java inside)</title>
        <link href="http://doswa.com/2009/01/24/chain.html"/>
        <updated>2009-01-24T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/01/24/chain</id>
        <content type="html">&lt;p&gt;I made a little &lt;a href=&quot;http://processing.org&quot;&gt;Processing&lt;/a&gt; sketch based on the same physics as &lt;a href=&quot;/images/2009/01/16/screenshot1.png&quot;&gt;JavaScript physics&lt;/a&gt;.&lt;/p&gt;

&lt;iframe width=&quot;400&quot; height=&quot;400&quot; src=&quot;/projects/p5/Chain/iframe.html&quot; style=&quot;overflow:hidden;border:none&quot;&gt;&lt;/iframe&gt;


&lt;p&gt;Source code: &lt;a href=&quot;/projects/p5/Chain/Chain.pde&quot;&gt;Chain&lt;/a&gt; &lt;a href=&quot;/projects/p5/Chain/Link.pde&quot;&gt;Link&lt;/a&gt;&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>JavaScript physics</title>
        <link href="http://doswa.com/2009/01/16/javascript-physics.html"/>
        <updated>2009-01-16T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/01/16/javascript-physics</id>
        <content type="html">&lt;p&gt;I made a very basic physics demo in JavaScript to practice using the &lt;a href=&quot;http://mootools.net&quot;&gt;MooTools library&lt;/a&gt;. It uses Verlet integration as outlined in &lt;a href=&quot;http://web.archive.org/web/20080822161636/http://www.teknikus.dk/tj/gdc2001.htm&quot;&gt;Thomas Jakobsen's paper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To try out the demo, go to &lt;a href=&quot;/projects/physics_js/&quot;&gt;the demo page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2009/01/16/screenshot1.png&quot; alt=&quot;Chain of particles attached by joints&quot; /&gt;&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Generalizing derivatives and integrals</title>
        <link href="http://doswa.com/2009/01/03/generalizing-derivatives-and-integrals.html"/>
        <updated>2009-01-03T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/01/03/generalizing-derivatives-and-integrals</id>
        <content type="html">&lt;p&gt;&lt;em&gt;Disclaimer: This is sloppy math. Don't take it too seriously.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start with a simple expression, &lt;code&gt;\( x^k \)&lt;/code&gt;, and take a few derivatives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial}{\partial x} x^k = k x^{k-1} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^2}{\partial x^2} x^k = (k-1) k x^{k-2} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^3}{\partial x^3} x^k = (k-2) (k-1) k x^{k-3} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^4}{\partial x^4} x^k = (k-3) (k-2) (k-1) k x^{k-4} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A pattern is emerging:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^n}{\partial x^n} x^k = (k-n+1)...(k-1)k x^{k-n} = c x^{k-n} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;where c is the coefficient.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Now the hard part is finding the pattern in the coefficient. This needs to be taken out of the '...' form. Focus on that:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ c = (k-n+1)...(k-1)k $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is a series of numbers, each one larger than the next. This looks like a factorial, so divide &lt;em&gt;k!&lt;/em&gt; by that:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{k!}{c} = \frac{k!}{(k-n+1)...(k-1)k} = \frac{1(2)(3)...k}{(k-n+1)...(k-1)k} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note how the top goes from &lt;em&gt;1&lt;/em&gt; to &lt;em&gt;k&lt;/em&gt; and the bottom goes from &lt;em&gt;k-n+1&lt;/em&gt; to &lt;em&gt;k&lt;/em&gt;. That means that &lt;em&gt;k-n+1&lt;/em&gt; to &lt;em&gt;k&lt;/em&gt; is a subset of &lt;em&gt;1&lt;/em&gt; to k, so just divide that part out:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{k!}{c} = \frac{1(2)(3)...k}{(k-n+1)...(k-1)k} = 1(2)(3)...(k-n) = (k-n)! $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now solve for c:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{1}{c} = \frac{(k-n)!}{k!}$$, so $$c = \frac{k!}{(k-n)!} $$&lt;/code&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Puts this back into the original equation to get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^n f(x)}{\partial x^n} = c x^{k-n} = \frac{k!}{(k-n)!} x^{k-n} $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, the &lt;code&gt;\( n^{\text{th}} \)&lt;/code&gt; derivative of &lt;code&gt;\( x^k \)&lt;/code&gt; is equal to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{k!}{(k-n)!} x^{k-n} $$&lt;/code&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Verify this with a couple of derivatives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial}{\partial x} x^2 = \frac{k!}{(k-n)!} x^{k-n} = \frac{2!}{(2-1)!} x^{2-1} = \frac{2}{1} x^1 = 2x $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^2}{\partial x^2} 4 x^4 = 4 (\frac{k!}{(k-n)!}) x^{k-n} = 4 (\frac{4!}{(4-2)!}) x^{4-2} = 4 (\frac{24}{2}) x^2 = 4(12) x^2 = 48 x^2 $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And a couple of integrals:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^{-1}}{\partial x^{-1}} x^2 = \frac{k!}{(k-n)!} x^{k-n} = \frac{2!}{(2+1)!} x^{2+1} = \frac{2}{6} x^3 = \frac{1}{3} x^3 $$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$$ \frac{\partial^{-7}}{\partial x^{-7}} 3 x^4 = 3 (\frac{k!}{(k-n)!}) x^{k-n} = 3 (\frac{4!}{(4+7)!}) x^{4+7} = 3 (\frac{24}{39916800}) x^{11} = \frac{1}{554400} x^{11} $$&lt;/code&gt;&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Extending derivatives and integrals into fractions</title>
        <link href="http://doswa.com/2009/01/03/extending-derivatives-and-integrals-into-fractions.html"/>
        <updated>2009-01-03T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/01/03/extending-derivatives-and-integrals-into-fractions</id>
        <content type="html">&lt;p&gt;&lt;em&gt;Disclaimer: This is sloppy math. Don't take it too seriously.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href=&quot;/2009/01/03/generalizing-derivatives-and-integrals.html&quot;&gt;previous post&lt;/a&gt;, I wrote about a way to generalize derivatives and integrals into one function. What happens if a number other than an integer is passed to that function?&lt;/p&gt;

&lt;p&gt;Here is the generalization from the last post:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\[ \frac{\partial^n}{\partial x^n} x^k = \frac{k!}{(k-n)!} x^{k-n} \]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Set &lt;em&gt;k = 2&lt;/em&gt; and &lt;em&gt;n = 1/2&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\[ \frac{\sqrt{\partial}}{\sqrt{\partial x}} x^2 = \frac{2!}{(2-\frac{1}{2})!} x^{2-\frac{1}{2}} = \frac{2!}{1.5!} x^{1.5} \]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now there's a problem. Since &lt;em&gt;a!&lt;/em&gt; is only defined for integers 0 or greater, a different way of calculating factorials is needed. Luckily, there exists a &lt;a href=&quot;http://en.wikipedia.org/wiki/Gamma_function&quot;&gt;Gamma function&lt;/a&gt; defined for all real and complex numbers such that:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\[ \Gamma(n+1) = n! \]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Substitute the Gamma function into the other equation and simplify:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\[ \frac{\sqrt{\partial}}{\sqrt{\partial x}} x^2 = \frac{2!}{1.5!} x^{1.5} = \frac{2}{\Gamma(1.5+1)} x^{1.5} = \frac{2}{\Gamma(2.5)} x^{1.5} = \frac{2}{(\frac{3 \sqrt{\pi}}{4})} x^{1.5} \]&lt;/code&gt;
&lt;code&gt;\[ = \frac{2}{1} (\frac{4}{3 \sqrt{\pi}}) x^{1.5} = \frac{8}{3 \sqrt{\pi}} x^{1.5} = \frac{8 \sqrt{\pi}}{3 \pi} x^{1.5} \approx 1.505 x^{1.5} \]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, the half derivative of &lt;code&gt;\( x^2 \)&lt;/code&gt; is approximately equal to &lt;code&gt;\( 1.505 x^{1.5} \)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This can be used for 'fractional integration' as well, if a negative number is used for &lt;em&gt;n&lt;/em&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Fourth order Runge-Kutta numerical integration</title>
        <link href="http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html"/>
        <updated>2009-01-02T00:00:00-08:00</updated>
        <id>http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration</id>
        <content type="html">&lt;p&gt;Here's a Python implementation of &lt;a href=&quot;http://en.wikipedia.org/wiki/RK4&quot;&gt;RK4&lt;/a&gt;, hardcoded for double-integrating the second derivative (acceleration up to position). For a more generalized solution, see &lt;a href=&quot;/2009/04/21/improved-rk4-implementation.html&quot;&gt;my other implementation&lt;/a&gt;. I tried to keep this as simple as I could, so people can easily see the relation between the 'math form' and 'code form' of the algorithm.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rk4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Returns final (position, velocity) tuple after&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        time dt has passed.&lt;/span&gt;

&lt;span class=&quot;sd&quot;&gt;        x: initial position (number-like object)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        v: initial velocity (number-like object)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        a: acceleration function a(x,v,dt) (must be callable)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        dt: timestep (number)&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;x3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;v3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;x4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;v4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;xf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;vf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vf&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Here is an example usage of the function and a comparison to Euler integration:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;accel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Determines acceleration from current position,&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        velocity, and timestep. This particular acceleration&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;        function models a spring.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stiffness&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;damping&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stiffness&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;damping&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Timestep of 1/40 second&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Position, velocity&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# For comparison with Euler integration&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Initial    -position: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%6.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;, velocity: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%6.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Run for 100 seconds&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rk4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c&quot;&gt;# Integrate using Euler&amp;#39;s method&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Final RK4  -position: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%6.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;, velocity: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%6.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Final Euler-position: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%6.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;, velocity: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%6.2f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;euler&lt;/span&gt;
    
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The output of this really shows how much more accurate RK4 integration can be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Initial    -position:  50.00, velocity:   5.00
Final RK4  -position:  52.18, velocity:  38.05
Final Euler-position: 178.38, velocity: 137.62
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As the timestep is decreased (meaning more computation), Euler approaches RK4 (shown at timestep of 1/400 seconds):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Initial    -position:  50.00, velocity:   5.00
Final RK4  -position:  52.28, velocity:  37.92
Final Euler-position:  59.20, velocity:  43.02
&lt;/code&gt;&lt;/pre&gt;
</content>
    </entry>
    
</feed>

