Now someone just has to create that robot with an NXT brick inside it and have follow preprogrammed paths.
[via: Woodgears]
I spent the last few days of my winter vacation experimenting with generating random music. This was inspired by my simple NXT Dancer robot, which currently just plays random tones. The idea behind this latest experiment is that (1) I build a probability density function of played notes from well known music and (2) I use that PDF to pick the notes to be played next, based on the previous note. I will post more details about this in a few days. But the important part is that I used MIDI files, which contain commands like “play this note” and “use this volume” to recreate the actual music.
Being lazy, I decided not to search a lot (I trie a bit) for a pre-made library for reading MIDI files and instead I decided to create my own. The result is libmidi, a very simple MIDI reading and writing library. To parse a MIDI file, just do this:
MIDI::File file("/path/to/file.mid"); file.open();
Then the contents of that file are available as they were found in the MIDI file. For example, to get a track and print out all of the events (the commands that tell the MIDI synchronizer what to do), do this:
MIDI::Track* track=file.track(0); for(int j=0;j<track->numEvents();j++) { MIDI::Event* event=track->event(j); event->debug(); }
Also libmidi contains a basic write support. You can add new events to the end of a track and call the MIDI::File::write() function to save the file that the track represents. See the randommusic example, included in the libmidi-0.1 package, on how to do this.
The downside to using the libmidi library right now is that it is not well developed yet. For example, there are only a few functions to modify the MIDI file (ei: tracks can only be appended to the end of the track) and not all meta event types have a nice define yet. Of course, I plan to fix this in the future.
But for now, you can download version 0.1 or checkout the latest development code at GitHUB. The API documentation is available here.
For more information about MIDI files, check out these websites:
Over the last few days, I’ve noticed that Wordpress started converting all of the special HTML characters into their proper HTML entities (ei: & will be translated into &), even though I explicitly specified that I do want the character itself. This interfered with the wp-syntax plugin that I use when I post snippets of code, because the plugin assumes that non-HTML encoded code is being used. It applied the conversion to HTML entities after all that is done. But now because of Wordpress’ over-zealous compliance to XHTML specs, when the following is part of a post:
<pre lang=”php”>if(true && false) echo “Hello World”;</pre>
You will see the following:
if(true && false) echo "Hello World";
Luckily, the fix is quite simple. It does not even involve using the seemingly abandoned role-manager plugin as suggested by wp-syntax’s FAQ. Just add the escaped=”true” argument to the pre tag, and wp-syntax will decode the HTML entities for you automatically. Now
<pre lang=”php” escaped=”true”>if(true && false) echo “Hello World”;</pre>
turns into
if(true && false) echo "Hello World";
To make things easier, I suggest that you install the wp-syntax-button plugin. This plugin adds a “Code” button to the visual Wordpress editor, using which you can automatically highlight any code without having to switch to the HTML editor. And of course, the wp-syntax-button plugin automatically adds the escaped=”true” argument.

For Christmas, I’ve got a fun little toy: The LEGO MindStorm NXT 2.0 Kit. It came with a NXT brick and a CD containing software for programming it. However that software only runs on a Windows and Mac, neither of which I use regularly. But the MindStorm franchise has a large hobbyist community, so large that LEGO even saw it fit to to open source the firmware on the NXT brick! This made it easy for people to develop tools that run on Linux.
After some quick searching, I’ve decided to settle with Next Byte Codes (NCB) and Not eXactly C (NXC). These are two text-based (as opposed to the official graphics-based) programming languages. NCB is very similar to assembly, and NXC is very similar to C. Of the two, I use NXT, because I find that C is better at making complex code more readable. Of course, that is just my personal preference.
In this tutorial, I want to provide a quick guide about settting up and using the NCB/NXC compilers to write code for the NXT brick. This is not a programming guide, as several already exist and are listed on the NCB/NXC site (NXC Guide, NBC Guide, NBC Tutorial, and also a book can be purchased!). But those tutorial suppose that you are using the Windows-only bricxcc IDE to transfer code to the NXT brick. They do not document the steps that you have to take by using the tools directly, which is what you have to do on Linux.
There are several parts to this tutorial:
- Downloading and setting up the tools
- Transferring code over USB
- Transferring code over Bluetooth
This tutorial was written and tested using Ubuntu Karmic Koala, but it should work fine on other distributions. In fact I mainly use a Gentoo desktop to program my NXT brick, but it does not have Bluetooth so I would not be able to compile this entire tutorial on it.
Finally, during the tutorial I use a sample program called “dancer”, that can be downloaded here. The package contains a version written in NCB and a version written in NXC. It makes a car-bot, built by following the instructions in the booklet from the Mindstorm box, dance by following random instructions (move, turn, make sounds). The result are very boring, but very simple and thus serve their purpose in this tutorial.
If you have any questions about this tutorial or how to accomplish other things, do not hesitate to contact me.
Merry Christmas, and an even more merry Boxing Day tomorrow! But most importantly, take some time to remember that today (in Julian Calendar) is the birthday of a man without whom we wouldn’t have the world as we know it today.
[Image Source: Wikipedia]
A week after the 0.3 release of my AutoRotate daemon, a big bug was found (Thank you, Jonas). By default on Ubuntu systems (and probably others), system buses are not allowed to be created unless they are specified as a special exception in a file. And for 0.3, I’ve forgot to include this special file in the release so if you tried to run the auto-rotate.py program, you would get this error:
Traceback (most recent call last):
File “/usr/bin/auto-rotate.py”, line 50, in
main()
File “/usr/bin/auto-rotate.py”, line 40, in main
busname = dbus.service.BusName(“net.krizka.autorotate”,system_bus);
File “/usr/lib/pymodules/python2.6/dbus/service.py”, line 129, in __new__
retval = bus.request_name(name, name_flags)
File “/usr/lib/pymodules/python2.6/dbus/bus.py”, line 306, in request_name
’su’, (name, flags))
File “/usr/lib/pymodules/python2.6/dbus/connection.py”, line 620, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: Connection “:1.148″ is not allowed to own the service “net.krizka.autorotate” due to security policies in the configuration file
This is now fixed in the 0.4 release of AutoRotate, the sources for which you can download here and follow these instructions to install it. Also, the Ubuntu package is currently being build in my PPA and will be installable soon, following these instructions.
Finally, there are a few other changes in this release. The most notable being the employment of libnotify, by the manual-rotate.py script, to notify you when the automatic rotation has been enabled/disabled.

The full change log is as follows:
- Added .conf file to allow creation of system bus
- manual-rotate.py notifies the user when automatic rotation is enabled/disabled via libnotify
- Check if autorotate daemon already running before starting another one
- Added dbus .service file describing the autorotate service
- Fixed the dependencies in the debian control file
One missing piece in Linux tablet support is an easy way to automatically rotate the screen, based on either the orientation of the laptop or other parameters. I’ve tried to fix this by writing a simple daemon that uses the hdaps driver to check the orientation of the tablet and calculates the rotation. This new version contains a lot of improvements over the previous hack-job, in that it:
- Can be controlled via dbus
- Directly determines laptop vs tablet mode from hardware, and no longer depends on ACPI events
- Much easier to install (using a deb from my PPA or a setup.py script)
The new version, 0.3, can be downloaded from here with installation instruction here (Ubuntu) and here. More up-to-date details can be found on the AutoRotate project page.
I’ve only tested it on my laptop, which is a ThinkPad X61T running Ubuntu Karmic Koala. But there is no reason why it shouldn’t work on other hardware that uses hdaps and other distributions.
Unlike MATLAB, the open source numerical package GNU Octave does not have support for quickly creating animations. That is quite disappointing, because as a physics student, I like to see what the time-dependent equations I solve look like. For example, I am currently taking a course with a section on boundary value problems. This includes the motion of waves on different surfaces, and following their evolution using Fourier series. As you can imagine, that might look quite cool.
Luckily, with the amount of tools available for GNU/Linux, I’ve managed to hack together a quick script that allows me to create AVI movies with Octave that I can then play back. It might not be as simple as MATLAB’s movie() function, but it works! Here is how:
- Create a for loop, with each pass displaying the next frame in the animation.
- Save each frame as an png (or some other type) image such that when the file names are ordered alphabetically, they are also ordered chronologically.
- Use mencoder (or some other encoder) to stitch together the images into a movie file.
- mencoder (AVI Movie)
mencoder mf://*.png -mf w=640:h=480:fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi - ffmpeg (MPEG Movie)
ffmpeg -i "%d5.png" -y output.mpeg - ImageMagick’s convert (Animated GIF’s)
convert -delay 10 -loop 0 *png output.gif
- mencoder (AVI Movie)
- Play the output.avi file to see your animation.
Here is my example of animating the vibrations of a square membrane, that was initially at rest with the displacement of z=x*(1-x)*y*(1-y), but scaled by some constant.
- In your project directory, create an empty directory called output.
- In your project folder, create an Octave script called simulation.m that contains the code for your simulation. For example, my looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
set(0, 'defaultfigurevisible', 'off'); [x,y]=meshgrid([0:0.01:1],[0:0.01:1]); for t=[0:1000] z=x-x; %Initialize empty z grid for nx=[1:10] for ny=[1:10] coeff=(1-(-1)^nx)*(1-(-1)^ny)/(nx^3*ny^3); z=z+coeff*sin(nx*pi.*x).*sin(ny*pi.*y)*cos(sqrt(nx^2+ny^2)*pi*t/100); end end surf(x,y,z); axis([0 1 0 1 -4 4]); shading interp; xlabel("x"); ylabel("y"); zlabel("u(x,y)"); filename=sprintf('output/%05d.png',t); print(filename); end
Some notes of importance in this code:
- Line 1 hides the plot display, so it is not redrawn on the screen every time. This speeds up things considerably.
- Line 19 sets the filename as “#####.png”, a 5 digits frame number. For example, if the frame number is 2, then the filename is 00002.png. This guarantees that the bash shell will order the filenames chronologically.
- Line 20 actually saves the plot to the file
- Run the simulation using octave.
octave simulation.m - Stitch together the images into a movie.
mencoder mf://output/*.png -mf w=640:h=480:fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi - Watch and enjoy!
Download AVI
Finally, a few other sources that I would recommend to learn more about animation using octave:
Displaying an animation / “movie” – This thread helped me a lot when I started.
Animating Laplace in Octave – Talks about creating animated GIFs and using ffmpeg to create an MPEG movie.

A few days ago Canoical released a new version of Ubuntu, nicknamed Karmic Koala. So I decided to write an update on how well it supports the ThinkPad X61 Tablet. Quite a lot of problems were fixed since Hardy Heron, but quite a lot of them still remain. I should also recommend checking out the ThinkWiki page for the X61 that has more information. (UPDATE: Ekain Milton posted a link to the Ubuntu X61 Table Community Documentation. It lists more useful tips and tricks.)
But first, a quick summary about the hardware configuration of my ThinkPad X61. I have the model with the SWGA screen that does not have the touch screen capabilities, so instead I have to use the special pen (included). I also have 2GB of memory and a Core 2 Duo processor running at a maximum of 1.6GHz (although in reality I run it at 800MHz to conserve batter life).
Wireless
The wireless connection works out of the box. It uses the iwlagn kernel module.
Touchscreen (Pen Only)
Works out of the box. I cannot comment of finger touch support, since my laptop does not have the hardware for that.
As for software to use with the touchscreen, I recommend xournal for note-taking and cellwriter (my tips and tricks) for handwriting recognition. Both are available in the official Ubuntu repository.
Extra Keyboard/Screen Buttons
All of the extra keyboard buttons work out of the box. This includes the volume buttons, ThinkVantage button (this is fixed), rotate button on the screen, the toolbox button on the screen and the arrow buttons on the screen.
By work, I mean the buttons report a valid keystroke to X.Org. For most of them, you still have to map them to something useful.
Middle Button Scrolling
To use the middle button, along with the TrackPoint, as a scroll wheel, you have to create the following file.
sudo gedit /etc/hal/fdi/policy/mouse-wheel.fdi
And put in the following text:
<?xml version="1.0" encoding="UTF-8"?>
<match key="info.product" string="TPPS/2 IBM TrackPoint">
<merge key="input.x11_options.EmulateWheel" type="string">true</merge>
<merge key="input.x11_options.EmulateWheelButton" type="string">2</merge>
<merge key="input.x11_options.XAxisMapping" type="string">6 7</merge>
<merge key="input.x11_options.YAxisMapping" type="string">4 5</merge>
<merge key="input.x11_options.ZAxsisMapping" type="string">4 5</merge>
<merge key="input.x11_options.Emulate3Buttons" type="string">true</merge>
</match>
After you reboot you computer, you should be able to hold down the middle button and use the TrackPoint as a scroll-wheel.
Suspend/Hibernate
Both suspend and hibernate work out of the box.
Hard Drive Accelerometer
The ThinkPad X61 Tablet comes with a cool piece of hardware that tells you the rotation of your laptop, which can be used to either protect the hard disk from damage during movement or rotate the screen automatically when using it in tablet mode. And the Linux kernel supports it via the hdaps module! The bad news is that the hdaps module shipped with Ubuntu has been broken for the last couple of years. It had received some attention from the Ubuntu developers in the past; about a year ago went from "not loading at all" to "loading, but reporting the same position over and over again" (bug 281977). In the meantime, you can get it working by recompiling the hdaps module.
Hard disk protection also works via the hdapsd daemon.
sudo aptitude install hdapsd
Fingerprint Scanner
The fingerprint scanner works after you install the fprint packages from the Ubuntu repositories and make some modifications to PAM configuration. But there is still a lot of room for improvement. For example, user identification is not supported. You still have to select your username when logging in via GDM.
One of my main beefs with Ubuntu is that the hard drive accelerometer that comes with built in the ThinkPad tablets does not work out of the box. After some research, I found out that this is because the mainline hdaps module (default) and the tp_smapi module (that works, after manual compilation) are different, and the tp_smapi one will not be merged into the mainline for political reasons. That means that to get decent support, one will have to compile the tp_smapi version by hand for some time. About a year ago, I wrote a guide on how to do this. But today, I’ve found out that there is a much simpler way to do this:
sudo aptitude install tp-smapi-source
sudo module-assistant build tp-smapi-source
sudo module-assistant install tp-smapi-source
sudo modprobe hdaps
Note that you will have to rebuild the module after every kernel update.
To actually make it do something useful, like protect the hard disk when moving the laptop, try hdapsd:
sudo aptitude install hdapsd



