<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Doswa &#187; Programming</title>
	<atom:link href="http://doswa.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://doswa.com/blog</link>
	<description>Programming, physics, mathematics</description>
	<lastBuildDate>Sat, 28 Aug 2010 16:20:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Kaoss Guitar (Update 1)</title>
		<link>http://doswa.com/blog/2010/04/18/kaoss-guitar-day-1/</link>
		<comments>http://doswa.com/blog/2010/04/18/kaoss-guitar-day-1/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 10:32:47 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[kaoss guitar]]></category>

		<guid isPermaLink="false">http://doswa.com/blog/?p=402</guid>
		<description><![CDATA[For a while, a friend and I have been planning to build a specialized MIDI controller into a guitar to control a Korg Kaoss Pad KP2. The idea came from the guitar used by Matt Bellamy of Muse (check out &#8230; <a href="http://doswa.com/blog/2010/04/18/kaoss-guitar-day-1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a while, a friend and I have been planning to build a specialized MIDI controller into a guitar to control a Korg <a href="http://en.wikipedia.org/wiki/Kaoss_Pad">Kaoss Pad</a> KP2. The idea came from the guitar used by Matt Bellamy of Muse (check out <a href="http://www.youtube.com/watch?v=h1cZDQFgHH8">a video of it</a>).<br />
<span id="more-402"></span></p>
<p>We ordered the electronics for the guitar from <a href="http://sparkfun.com">SparkFun Electronics</a> and <a href="http://digikey.com">Digi-Key</a>. 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.</p>
<p>Today we tested out <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8448">the 4.3&#8243; touchpad from SparkFun</a> 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.</p>
<p>One problem I ran into using my <a href="http://doswa.com/blog/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer/">FTDI programmer</a> 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:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">david<span style="color: #000000; font-weight: bold;">@</span>second:~$ avrdude <span style="color: #660033;">-c</span> ftdi <span style="color: #660033;">-p</span> atmega168 <span style="color: #660033;">-P</span> ft0
avrdude: BitBang OK 
avrdude: pin assign miso <span style="color: #000000;">1</span> sck <span style="color: #000000;">3</span> mosi <span style="color: #000000;">0</span> reset <span style="color: #000000;">4</span>
avrdude: drain OK 
&nbsp;
 ft245r:  bitclk <span style="color: #000000;">230400</span> -<span style="color: #000000; font-weight: bold;">&gt;</span> ft baud <span style="color: #000000;">115200</span>
avrdude: ft245r_program_enable: failed
avrdude: initialization failed, <span style="color: #007800;">rc</span>=-<span style="color: #000000;">1</span>
         Double check connections and try again, or use <span style="color: #660033;">-F</span> to override
         this check.
&nbsp;
&nbsp;
avrdude done.  Thank you.</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">david<span style="color: #000000; font-weight: bold;">@</span>second:~$ avrdude <span style="color: #660033;">-c</span> ftdi <span style="color: #660033;">-p</span> atmega168 <span style="color: #660033;">-P</span> ft0 <span style="color: #660033;">-B</span> <span style="color: #000000;">76800</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2010/04/18/kaoss-guitar-day-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using Cairo in Pygame</title>
		<link>http://doswa.com/blog/2010/03/29/using-cairo-in-pygame/</link>
		<comments>http://doswa.com/blog/2010/03/29/using-cairo-in-pygame/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 05:17:55 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/blog/?p=389</guid>
		<description><![CDATA[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 &#8230; <a href="http://doswa.com/blog/2010/03/29/using-cairo-in-pygame/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>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.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> cairo
<span style="color: #ff7700;font-weight:bold;">import</span> pygame
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>
&nbsp;
size = <span style="color: #ff4500;">400</span>, <span style="color: #ff4500;">400</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Initialize pygame with 32-bit colors. This setting stores the pixels</span>
<span style="color: #808080; font-style: italic;"># in the format 0x00rrggbb.</span>
pygame.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
screen = pygame.<span style="color: black;">display</span>.<span style="color: black;">set_mode</span><span style="color: black;">&#40;</span>size, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">32</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Get a reference to the memory block storing the pixel data.</span>
pixels = pygame.<span style="color: black;">surfarray</span>.<span style="color: black;">pixels2d</span><span style="color: black;">&#40;</span>screen<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Set up a Cairo surface using the same memory block and the same pixel</span>
<span style="color: #808080; font-style: italic;"># format (Cairo's RGB24 format means that the pixels are stored as</span>
<span style="color: #808080; font-style: italic;"># 0x00rrggbb; i.e. only 24 bits are used and the upper 16 are 0).</span>
cairo_surface = cairo.<span style="color: black;">ImageSurface</span>.<span style="color: black;">create_for_data</span><span style="color: black;">&#40;</span>
	pixels.<span style="color: black;">data</span>, cairo.<span style="color: black;">FORMAT_RGB24</span>, size<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, size<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Draw a white circle to the screen using pygame.</span>
radius = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">min</span><span style="color: black;">&#40;</span>size<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">0.7</span><span style="color: black;">&#41;</span>
pos = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>a/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> size<span style="color: black;">&#93;</span>
pygame.<span style="color: black;">draw</span>.<span style="color: black;">circle</span><span style="color: black;">&#40;</span>screen, <span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span>, pos, radius<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Draw a smaller black circle to the screen using Cairo.</span>
context = cairo.<span style="color: black;">Context</span><span style="color: black;">&#40;</span>cairo_surface<span style="color: black;">&#41;</span>
context.<span style="color: black;">set_source_rgb</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
context.<span style="color: black;">arc</span><span style="color: black;">&#40;</span>size<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>/<span style="color: #ff4500;">2</span>, size<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>/<span style="color: #ff4500;">2</span>, <span style="color: #008000;">min</span><span style="color: black;">&#40;</span>size<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: #dc143c;">math</span>.<span style="color: black;">pi</span><span style="color: black;">&#41;</span>
context.<span style="color: black;">fill</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Flip the changes into view.</span>
pygame.<span style="color: black;">display</span>.<span style="color: black;">flip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Wait for the user to quit.</span>
<span style="color: #ff7700;font-weight:bold;">while</span> pygame.<span style="color: black;">QUIT</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span>e.<span style="color: #008000;">type</span> <span style="color: #ff7700;font-weight:bold;">for</span> e <span style="color: #ff7700;font-weight:bold;">in</span> pygame.<span style="color: black;">event</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>:
	<span style="color: #ff7700;font-weight:bold;">pass</span></pre></div></div>

<p>Tested on Python 2.6.4, pycairo 1.8.6, Cairo 1.8.8, pygame 1.8.1, numpy 1.3.0.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2010/03/29/using-cairo-in-pygame/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SparkFun&#8217;s FTDI Basic Breakout as an AVR programmer</title>
		<link>http://doswa.com/blog/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer/</link>
		<comments>http://doswa.com/blog/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 18:28:13 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/blog/?p=308</guid>
		<description><![CDATA[This guide has been merged into my AVRDUDE 5.10 bitbang guide. Original post follows (left here for reference). Now open up avrdude.conf in a text editor. Add this entry by the other programmers: # FTDI basic breakout programmer id = &#8230; <a href="http://doswa.com/blog/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>This guide has been merged into my <a href="http://doswa.com/blog/2010/08/24/avrdude-5-10-with-ftdi-bitbang/">AVRDUDE 5.10 bitbang guide</a>. Original post follows (left here for reference).</strong><br />
<span id="more-308"></span></p>
<p><a href="http://doswa.com/blog/wp-content/uploads/2009/12/screenshot1.jpg"><img src="http://doswa.com/blog/wp-content/uploads/2009/12/screenshot1.jpg" alt="screenshot1" title="screenshot1" width="384" height="320" class="aligncenter size-full wp-image-310" /></a></p>
<p>Now open up <code>avrdude.conf</code> in a text editor. Add this entry by the other programmers:</p>
<pre># FTDI basic breakout
programmer
  id = "ftdi";
  desc = "FTDI Basic Breakout";
  type = ft245r;
  miso = 1; # RxD
  sck = 3; # CTS
  mosi = 0; # TxD
  reset = 4; # DTR
;</pre>
<p>Now you can attach your FTDI Breakout to the target AVR like this:<br />
<u>FTDI -> MCU (ATmega168 PDIP pin)</u><br />
DTR -> RESET (1)<br />
RXI -> MISO (18)<br />
TXO -> MOSI (17)<br />
5V -> VCC (7 &#038; 20)<br />
CTS -> SCK (19)<br />
GND -> GND (8 &#038; 22)</p>
<p>With the 3&#215;2 ICSP header on the Arduino, that would look like this:</p>
<pre>        +----------------+
[RXI]--o| 1 MISO   +5V 2 |o--[5V]
[CTS]--o| 3 SCK   MOSI 4 |o--[TXO]
[DTR]--o| 5 RESET  GND 6 |o--[GND]
        +----------------+</pre>
<p>Note that may need to supply the AVR with an external oscillator or clock source. For virgin ATmega168s, you won&#8217;t need to (because it initially uses the internal oscillator), but you&#8217;ll need an external crystal oscillator if you&#8217;re using one that came out of an Arduino.</p>
<p>From there you can use AVRDUDE like normal using the `-c ftdi` switch. For example, to read fuse bytes:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> .<span style="color: #000000; font-weight: bold;">/</span>avrdude <span style="color: #660033;">-C</span> avrdude.conf <span style="color: #660033;">-c</span> ftdi <span style="color: #660033;">-p</span> m168 <span style="color: #660033;">-P</span> ft0 <span style="color: #660033;">-U</span> hfuse:r:-:h <span style="color: #660033;">-U</span> lfuse:r:-:h <span style="color: #660033;">-U</span> efuse:r:-:h</pre></div></div>

<p><strong>Edit 2010-04-18:</strong></p>
<p>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&#8217;s <code>-B</code> option. This option determines what clock is fed into the AVR&#8217;s SCK pin. I recommend starting with a value of <code>-B 1</code> and working your way up to a reasonable clock speed. Here is the adjusted command for new AVRs:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> .<span style="color: #000000; font-weight: bold;">/</span>avrdude <span style="color: #660033;">-C</span> avrdude.conf <span style="color: #660033;">-c</span> ftdi <span style="color: #660033;">-p</span> m168 <span style="color: #660033;">-P</span> ft0 <span style="color: #660033;">-U</span> hfuse:r:-:h <span style="color: #660033;">-U</span> lfuse:r:-:h <span style="color: #660033;">-U</span> efuse:r:-:h <span style="color: #660033;">-B</span> <span style="color: #000000;">1</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AVRDUDE 5.8 with FTDI bitbang patch on Linux</title>
		<link>http://doswa.com/blog/2009/12/20/avrdude-58-with-ftdi-bitbang-patch-on-linux/</link>
		<comments>http://doswa.com/blog/2009/12/20/avrdude-58-with-ftdi-bitbang-patch-on-linux/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 18:16:23 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/blog/?p=292</guid>
		<description><![CDATA[I updated this walkthrough for AVRDUDE 5.10. Original guide for AVRDUDE 5.8 follows. &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; Description: This is an adaptation of the work by Kimio Kosaka, Nate Phillips, and Massimo. It allows you to use AVRDUDE with an FTDI chip (like &#8230; <a href="http://doswa.com/blog/2009/12/20/avrdude-58-with-ftdi-bitbang-patch-on-linux/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://doswa.com/blog/2010/08/24/avrdude-5-10-with-ftdi-bitbang/">I updated this walkthrough for AVRDUDE 5.10</a>. Original guide for AVRDUDE 5.8 follows.</strong><br />
<span id="more-292"></span></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p><strong>Description:</strong> <em>This is an adaptation of the work by <a href="http://www.geocities.jp/arduino_diecimila/bootloader/index_old_en.html">Kimio Kosaka</a>, <a href="http://labs.coffeebot.net/content/ftdi-drivers-avrdude-linux">Nate Phillips</a>, and <a href="http://savannah.nongnu.org/users/runaway">Massimo</a>. It allows you to use AVRDUDE with an FTDI chip (like the one on the Arduino boards or SparkFun&#8217;s FTDI Basic Breakout). This can be useful if you manage to brick your Arduino and you have no programmer.</em></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>If you&#8217;re on Ubuntu or Debian, install the prerequisites with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">patch</span> build-essential libreadline-dev libncurses-dev libusb-dev
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> build-dep avrdude avrdude-doc</pre></div></div>

<p>&#8212;</p>
<p>Start by grabbing a copy of AVRDUDE 5.8, untarring it in the directory, and switching to that directory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>download.savannah.gnu.org<span style="color: #000000; font-weight: bold;">/</span>releases-noredirect<span style="color: #000000; font-weight: bold;">/</span>avrdude<span style="color: #000000; font-weight: bold;">/</span>avrdude-5.8.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> xzf avrdude-5.8.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> avrdude-<span style="color: #000000;">5.8</span></pre></div></div>

<p>&#8212;</p>
<p>Now get a copy of the FTDI bitbang patch files:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">8</span> <span style="color: #000000;">7</span> <span style="color: #000000;">6</span> <span style="color: #000000;">5</span> <span style="color: #000000;">4</span> <span style="color: #000000;">3</span> <span style="color: #000000;">2</span> <span style="color: #000000;">1</span> <span style="color: #000000;">0</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> patch-<span style="color: #007800;">$i</span>.diff http:<span style="color: #000000; font-weight: bold;">//</span>savannah.nongnu.org<span style="color: #000000; font-weight: bold;">/</span>patch<span style="color: #000000; font-weight: bold;">/</span>download.php?<span style="color: #007800;">file_id</span>=<span style="color: #000000;">1851</span><span style="color: #007800;">$i</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>&#8212;</p>
<p>Apply the patches:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">in</span> patch-<span style="color: #000000; font-weight: bold;">*</span>.diff; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #660033;">-p0</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #007800;">$file</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>&#8212;</p>
<p>Also get a copy of the FTDI driver. For 32-bit:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.ftdichip.com<span style="color: #000000; font-weight: bold;">/</span>Drivers<span style="color: #000000; font-weight: bold;">/</span>D2XX<span style="color: #000000; font-weight: bold;">/</span>Linux<span style="color: #000000; font-weight: bold;">/</span>libftd2xx0.4.16.tar.gz</pre></div></div>

<p>For 64-bit:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.ftdichip.com<span style="color: #000000; font-weight: bold;">/</span>Drivers<span style="color: #000000; font-weight: bold;">/</span>D2XX<span style="color: #000000; font-weight: bold;">/</span>Linux<span style="color: #000000; font-weight: bold;">/</span>libftd2xx0.4.16_x86_64.tar.gz</pre></div></div>

<p>&#8212;</p>
<p>Extract the FTDI driver and copy over the needed files:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> xzf libftd2xx<span style="color: #000000; font-weight: bold;">*</span>.tar.gz
<span style="color: #c20cb9; font-weight: bold;">cp</span> libftd2xx<span style="color: #000000; font-weight: bold;">*/</span>static_lib<span style="color: #000000; font-weight: bold;">/*</span> .
<span style="color: #c20cb9; font-weight: bold;">cp</span> libftd2xx<span style="color: #000000; font-weight: bold;">*/*</span>.h .
<span style="color: #c20cb9; font-weight: bold;">cp</span> libftd2xx<span style="color: #000000; font-weight: bold;">*/*</span>.cfg .</pre></div></div>

<p>&#8212;</p>
<p>Generate your makefile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure</pre></div></div>

<p>&#8212;</p>
<p>Open <code>Makefile</code> in a text editor and perform the following operations:</p>
<ol>
<li>Find the line <code>CFLAGS = -g -O2</code> and replace it with <code>CFLAGS = -g -O2 -DHAVE_LIBUSB -DSUPPORT_FT245R</code>.</li>
<li>Find the line <code>LIBS = -lreadline -lncurses -ltermcap</code> and replace it with <code>LIBS = -lreadline -lncurses -ltermcap ./libftd2xx.a.0.4.16 -lrt</code>.</li>
</ol>
<p>&#8212;</p>
<p>Now to actually compile it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>&#8212;</p>
<p>From here, you can follow the instructions at <a href="http://www.geocities.jp/arduino_diecimila/bootloader/index_old_en.html">http://www.geocities.jp/arduino_diecimila/bootloader/index_old_en.html</a> starting at step 5. Note that you&#8217;ll need to modify the commands a little bit. Instead of:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">avrdude <span style="color: #660033;">-c</span> diecimila <span style="color: #660033;">-P</span> ft0 <span style="color: #660033;">-p</span> m168</pre></div></div>

<p>you should write:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> .<span style="color: #000000; font-weight: bold;">/</span>avrdude <span style="color: #660033;">-C</span> avrdude.conf <span style="color: #660033;">-c</span> duemilanove <span style="color: #660033;">-P</span> ft0 <span style="color: #660033;">-p</span> m168</pre></div></div>

<p>(Note: you can still use it on a Diecimila, just specify the programmer as `-c duemilanove` and it should work.)</p>
<p>If you&#8217;d like to use SparkFun&#8217;s FTDI Basic Breakout as a programmer, try <a href="http://doswa.com/blog/2009/12/20/sparkfuns-ftdi-basic-breakout-as-an-avr-programmer/">these instructions</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/12/20/avrdude-58-with-ftdi-bitbang-patch-on-linux/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Accessing AVR GPIOs via bit fields</title>
		<link>http://doswa.com/blog/2009/12/08/accessing-avr-gpios-via-bit-fields/</link>
		<comments>http://doswa.com/blog/2009/12/08/accessing-avr-gpios-via-bit-fields/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 15:16:56 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/blog/?p=261</guid>
		<description><![CDATA[I noticed that the TMS320F2809 examples accessed GPIOs by bits rather than as 32-bit ints. For example, it&#8217;s possible to set a GPIO like this on the TMS320F280x: GpioDataRegs.GPADAT.bit.GPIO12 = 1; // Sets GPIO12 to high. Normally when working with &#8230; <a href="http://doswa.com/blog/2009/12/08/accessing-avr-gpios-via-bit-fields/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://doswa.com/blog/wp-content/uploads/2009/12/atmega328.png" alt="atmega328" title="atmega328" width="307" height="245" class="aligncenter size-full wp-image-267" /></p>
<p>I noticed that the <a href="http://focus.ti.com/docs/prod/folders/print/tms320f2809.html">TMS320F2809</a> examples accessed GPIOs by bits rather than as 32-bit ints. For example, it&#8217;s possible to set a GPIO like this on the TMS320F280x:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">GpioDataRegs.<span style="color: #202020;">GPADAT</span>.<span style="color: #202020;">bit</span>.<span style="color: #202020;">GPIO12</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sets GPIO12 to high.</span></pre></div></div>

<p>Normally when working with AVRs, I would set bits like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">PORTB <span style="color: #339933;">|=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">&lt;&lt;</span>PB3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sets port b, bit 3 to high.</span></pre></div></div>

<p>After a bit of tinkering, I came up with this (for AVRs):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">portb.<span style="color: #202020;">bit</span>.<span style="color: #202020;">b3</span> <span style="color: #339933;">=</span> HIGH<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sets port b, bit 3 to high.</span></pre></div></div>

<p>I personally think it looks a bit cleaner, so I&#8217;ll probably be using it in the future. If you&#8217;re interested in trying it out, you can download a full example here: <a href="http://doswa.com/projects/avr_bitfields/bitfields.zip">bitfields.zip</a>.</p>
<hr />
<p><small><br />
Side note: It is also possible to access GPIOs as 32-bit ints on the TMS320F280x like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">GpioDataRegs.<span style="color: #202020;">GPADAT</span>.<span style="color: #202020;">all</span> <span style="color: #339933;">|=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #0000dd;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sets GPIO12 to high.</span></pre></div></div>

<p></small></p>
<hr />
<p>Edit: <a href="http://www.avrfreaks.net/index.php?name=PNphpBB2&#038;file=viewtopic&#038;t=86521">Looks like I&#8217;m not alone</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/12/08/accessing-avr-gpios-via-bit-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RK4 in Haskell</title>
		<link>http://doswa.com/blog/2009/07/31/rk4-in-haskell/</link>
		<comments>http://doswa.com/blog/2009/07/31/rk4-in-haskell/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 05:43:42 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=200</guid>
		<description><![CDATA[Ran across this today. It&#8217;s a well written implementation of RK4. 4th order Runge-Kutta (RK4) integration in Haskell]]></description>
			<content:encoded><![CDATA[<p>Ran across this today. It&#8217;s a well written implementation of RK4.<br />
<a href="http://srihari.ramanathan.googlepages.com/runge-kuttawithhaskell">4th order Runge-Kutta (RK4) integration in Haskell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/07/31/rk4-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Line Segment to Circle Collision/Intersection Detection</title>
		<link>http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/</link>
		<comments>http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 18:15:08 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Computational Geometry]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=158</guid>
		<description><![CDATA[Style and Notation All variables are represented in italics except in the diagrams and Python code. Vectors are shown in bold except in the diagrams and Python code. The notation &#124;a&#124; is used to represent the length of a vector &#8230; <a href="http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Style and Notation</h2>
<ul>
<li>All variables are represented in italics except in the diagrams and Python code.</li>
<li>Vectors are shown in bold except in the diagrams and Python code.</li>
<li>The notation |<em>a</em>| is used to represent the length of a vector named <strong><em>a</em></strong>.</li>
</ul>
<h2>Introduction to the problem</h2>
<p>Start by defining a few points (vectors from the world origin):<br />
<a href="http://doswa.com/blog/wp-content/uploads/2009/07/2.png"><img class="alignnone size-full wp-image-162" title="2" src="http://doswa.com/blog/wp-content/uploads/2009/07/2.png" alt="2" width="275" height="192" /></a></p>
<p>Now, from these points, you can easily calculate two useful values, the segment vector, <strong><em>seg_v</em></strong> (from <strong><em>seg_a</em></strong> to <strong><em>seg_b</em></strong>) and the position of <strong><em>circ_pos</em></strong> relative to <strong><em>seg_a</em></strong>, <strong><em>pt_v</em></strong>.</p>
<p><a href="http://doswa.com/blog/wp-content/uploads/2009/07/3.png"></a><a href="http://doswa.com/blog/wp-content/uploads/2009/07/31.png"><img class="alignnone size-full wp-image-166" title="31" src="http://doswa.com/blog/wp-content/uploads/2009/07/31.png" alt="31" width="275" height="192" /></a></p>
<p><strong><em>seg_v</em></strong> = <strong><em>seg_b</em></strong> &#8211; <strong><em>seg_a</em></strong></p>
<p><strong><em>pt_v</em></strong> = <strong><em>circ_pos</em></strong> &#8211; <strong><em>seg_a</em></strong></p>
<h2>Closest point to the circle&#8217;s center on the segment<em><br />
</em></h2>
<p>The next step is to find the closest point to the circle&#8217;s center on the segment (labeled &#8220;<strong><em>closest</em></strong>&#8221; on the diagram). To do this, we must project <strong><em>pt_v</em></strong> onto <strong><em>seg_v</em></strong>:</p>
<p><a href="http://doswa.com/blog/wp-content/uploads/2009/07/4.png"><img class="alignnone size-full wp-image-167" title="4" src="http://doswa.com/blog/wp-content/uploads/2009/07/4.png" alt="4" width="275" height="192" /></a></p>
<p>To project on vector onto another, take the dot product of the vector and the unit vector of the projection target. That is:</p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_d317daa71d17b12f8188376628d58ba8.png" align="absmiddle" class="tex" alt="|proj\_v| = \textbf{pt\_v} \cdot \frac{\textbf{seg\_v}}{|seg\_v|}" /></center></p>
<p>Note that this dot product returns a scalar value (the length of <strong><em>proj_v</em></strong>), not a vector.</p>
<p>&#8211;</p>
<p>We now need to take a small break from the main goal and look into a special case: the ends of the segment. If |<em>proj_v</em>| is less than <em>0</em> or greater than |<em>seg_v</em>|, the closest point to <strong><em>circ_pos</em></strong> on the segment will be one of the segment&#8217;s endpoints. That is:<br />
<code>if |<em>proj_v</em>| &lt; 0: <strong><em>closest</em></strong> = <strong><em>seg_a</em></strong><br />
if |<em>proj_v</em>| &gt; |<em>seg_v</em>|: <em><strong>c</strong><strong>losest</strong></em> = <strong><em>seg_b</em></strong></code></p>
<p>&#8211;</p>
<p>Next, calculate the actual <strong><em>proj_v</em></strong> vector, rather than just its length (|<em>proj_v</em>|). To do this, simply multiply by the <strong><em>seg_v</em></strong> unit vector:</p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_2bc551d3368e2e85233c77ffd0ab9665.png" align="absmiddle" class="tex" alt="\textbf{proj\_v} = |proj\_v| \frac{\textbf{seg\_v}}{|seg\_v|}" /></center></p>
<p>The only thing left to do is to convert this into world coordinates (rather than coordinates relative to <strong><em>seg_a</em></strong>) to get the closest point:</p>
<p><strong><em>closest</em></strong> = <strong><em>seg_a</em></strong> + <strong><em>proj_v</em></strong></p>
<h2>Checking for a intersection</h2>
<p>Now that we&#8217;ve calculated the closest point on the segment (the variable &#8220;<strong><em>closest</em></strong>&#8220;), we can check if the circle and segment intersect. To do this, first find the vector from <strong><em>closest</em></strong> to <strong><em>circ_pos</em></strong>:</p>
<p><strong><em>dist_v</em></strong> = <strong><em>circ_pos</em></strong> &#8211; <strong><em>closest</em></strong></p>
<p>If the length of this vector is less than the circle&#8217;s radius, the circle and segment are intersecting:<br />
<code>if |<em>dist_v</em>| &lt; <em>circ_rad: </em>They are intersecting<br />
else: They are not intersecting</code></p>
<h2>Collision response</h2>
<p>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 <strong><em>dist_v</em></strong> whose length is equal to the difference between <em>circ_rad</em> and |<em>dist_v</em>|.</p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_8339fdad8e95d59472dda12890ebb6ab.png" align="absmiddle" class="tex" alt="\textbf{offset} = (circ\_rad - |dist\_v|) \frac{\textbf{dist\_v}}{|dist\_v|}" /></center></p>
<h2>Python implementation</h2>
<p>Here is a Python implementation of everything in this post. For the sake of clarity, I avoided optimizations. <tt>vec</tt> is assumed to be a fully implemented 2d vector class.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> closest_point_on_seg<span style="color: black;">&#40;</span>seg_a, seg_b, circ_pos<span style="color: black;">&#41;</span>:
	seg_v = seg_b - seg_a
	pt_v = circ_pos - seg_a
	<span style="color: #ff7700;font-weight:bold;">if</span> seg_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>, <span style="color: #483d8b;">&quot;Invalid segment length&quot;</span>
	seg_v_unit = seg_v / seg_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	proj = pt_v.<span style="color: black;">dot</span><span style="color: black;">&#40;</span>seg_v_unit<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> proj <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> seg_a.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> proj <span style="color: #66cc66;">&gt;</span>= seg_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> seg_b.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	proj_v = seg_v_unit <span style="color: #66cc66;">*</span> proj
	closest = proj_v + seg_a
	<span style="color: #ff7700;font-weight:bold;">return</span> closest
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> segment_circle<span style="color: black;">&#40;</span>seg_a, seg_b, circ_pos, circ_rad<span style="color: black;">&#41;</span>:
	closest = closest_point_on_seg<span style="color: black;">&#40;</span>seg_a, seg_b, circ_pos<span style="color: black;">&#41;</span>
	dist_v = circ_pos - closest
	<span style="color: #ff7700;font-weight:bold;">if</span> dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> circ_rad:
		<span style="color: #ff7700;font-weight:bold;">return</span> vec<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>, <span style="color: #483d8b;">&quot;Circle's center is exactly on segment&quot;</span>
	offset = dist_v / dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>circ_rad - dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> offset</pre></div></div>

<h2>Similar algorithms</h2>
<p>Collisions involving <a href="http://mathworld.wolfram.com/Stadium.html">stadiums</a> (a type of rounded rectangle) can be calculated in a similar manner. A stadium is essentially a line segment with a radius.</p>
<p>A stadium-point collision is the same as a segment-circle collision with a circle whose radius is equal to the stadium&#8217;s radius.</p>
<p>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&#8217;s radius and the stadium&#8217;s radius.</p>
<h2>Download</h2>
<p><a href="http://doswa.com/projects/segment_circle/segment_circle.zip">segment_circle.zip</a> &#8211; An implementation in Python and <a href="http://cython.org">Cython</a>.</p>
<h2>Conclusion</h2>
<p>If you find this post useful or have any questions, please leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JavaScript Image Animation</title>
		<link>http://doswa.com/blog/2009/06/24/javascript-image-animation/</link>
		<comments>http://doswa.com/blog/2009/06/24/javascript-image-animation/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 08:29:32 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=136</guid>
		<description><![CDATA[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&#8217;t see any reason why it wouldn&#8217;t work in other browsers. Try it: http://doswa.com/projects/animate/ Download &#8230; <a href="http://doswa.com/blog/2009/06/24/javascript-image-animation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>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&#8217;t see any reason why it wouldn&#8217;t work in other browsers.</p>
<p>Try it: <a href="http://doswa.com/projects/animate/">http://doswa.com/projects/animate/</a><br />
Download it: <a href="http://doswa.com/projects/animate/animate.zip">http://doswa.com/projects/animate/animate.zip</a></p>
<p><a href="http://trac.sagemath.org/sage_trac/ticket/1483">http://trac.sagemath.org/sage_trac/ticket/1483</a> inspired me to make this, since the sagenb.org server doesn&#8217;t have ImageMagick installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/06/24/javascript-image-animation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHPNotify: Debug Notifications From PHP</title>
		<link>http://doswa.com/blog/2009/05/13/phpnotify-debug-notifications-from-php/</link>
		<comments>http://doswa.com/blog/2009/05/13/phpnotify-debug-notifications-from-php/#comments</comments>
		<pubDate>Thu, 14 May 2009 03:36:36 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=111</guid>
		<description><![CDATA[This is a little program to help debug AJAX (or anything else) from PHP. Read the README file to instructions on setting it up on Ubuntu. Note that you must first install python-notify through apt-get for this to work correctly. &#8230; <a href="http://doswa.com/blog/2009/05/13/phpnotify-debug-notifications-from-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is a little program to help debug AJAX (or anything else) from PHP.</p>
<p><img class="aligncenter size-full wp-image-112" title="PHP Debug Notification" src="http://doswa.com/blog/wp-content/uploads/2009/05/screenshot.png" alt="PHP Debug Notification" width="352" height="141" /></p>
<p>Read the README file to instructions on setting it up on Ubuntu. Note that you must first install <code>python-notify</code> through apt-get for this to work correctly.</p>
<p><a style="font-size:large" href="http://doswa.com/projects/phpnotify/phpnotify.tgz">Download PHPNotify</a></p>
<p>If the function name <code>notify()</code> conflicts with any of your functions, you can change it on lines 2 and 3 of <code>prepend.php</code></p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/05/13/phpnotify-debug-notifications-from-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improved RK4 Implementation</title>
		<link>http://doswa.com/blog/2009/04/21/improved-rk4-implementation/</link>
		<comments>http://doswa.com/blog/2009/04/21/improved-rk4-implementation/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 07:02:49 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=102</guid>
		<description><![CDATA[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 F(x, y, y&#8217;, y&#8221;, &#8230;, ). If you&#8217;re new to numerical integration or even RK4 &#8230; <a href="http://doswa.com/blog/2009/04/21/improved-rk4-implementation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://doswa.com/blog/wp-content/uploads/2009/04/sage0.png"><img class="aligncenter size-full wp-image-119" title="Damped harmonic motion" src="http://doswa.com/blog/wp-content/uploads/2009/04/sage0.png" alt="Damped harmonic motion" width="600" height="370" /></a></p>
<p>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 F(x, y, y&#8217;, y&#8221;, &#8230;, <img src="http://doswa.com/blog/wp-content/cache/tex_0d171d6a16ab48052105c6921f8e78d4.png" align="absmiddle" class="tex" alt="y^{(n)}" />).</p>
<p>If you&#8217;re new to numerical integration or even RK4 integration, please <a href="http://doswa.com/blog/?p=16">read my other post</a> first. It&#8217;s easier to understand because it&#8217;s a less generalized function.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> rk4<span style="color: black;">&#40;</span>t0, h, s0, f<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;RK4 implementation.
	t = current value of the independent variable
	h = amount to increase the independent variable (step size)
	s0 = initial state as a list. ex.: [initial_position, initial_velocity]
	f = function(state, t) to integrate&quot;&quot;&quot;</span>
	r = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	s1 = s0 + <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0, s0<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	s2 = <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>s1<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>h <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span>
	s2 += <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0+<span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>h, s2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	s3 = <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>s2<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>h <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span>
	s3 += <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0+<span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>h, s3<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	s4 = <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + s3<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>h <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span>
	s4 += <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0+h, s4<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> t+h, <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: black;">&#40;</span>s1<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>s2<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+s3<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + s4<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>h/<span style="color: #ff4500;">6.0</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span></pre></div></div>

<p>An example usage of this function is:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> damped_spring_accel<span style="color: black;">&#40;</span>t, state<span style="color: black;">&#41;</span>:
	x = state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
	v = state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
	stiffness = <span style="color: #ff4500;">1</span>
	damping = <span style="color: #ff4500;">0.005</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> -stiffness<span style="color: #66cc66;">*</span>x - damping<span style="color: #66cc66;">*</span>v
&nbsp;
t = <span style="color: #ff4500;">0</span>
h = <span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">40.0</span>
s = <span style="color: black;">&#91;</span><span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> t<span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">100</span>:
	t, s = rk4<span style="color: black;">&#40;</span>t, h, s, damped_spring_accel<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Final state: t=%.2f x=%.2f v=%.2f&quot;</span><span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>t,s<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,s<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>On a side note, I recently found out about Sage (<a href="http://sagemath.org">sagemath.org</a> and <a href="http://sagenb.org">sagenb.org</a>). Its plotting capabilities and convenient mathematical notation are especially useful. Plus, it uses Python!</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/04/21/improved-rk4-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
