Cookies

This page loads YouTube videos and related cookies. Accept to view the videos as embedded on this page. Reject to decline cookies and view in a new tab.

If you accept, click a video to load; click again to play.

Music, Physics Engines, and the Raspberry Pi

(return)  (P.E.T.)

You're welcome to scroll down to the physics-engine section if you like. There you will find a video and installation instructions for the Raspberry Pi (RPi). (This tale is related to the scripts and videos presented on the main tutorial page.)

Click to manage cookies. If you accept them, click a video to load; click again to play.

RPi First, some music

From need to revitalize my stereo system came incentive to buy a first Raspberry Pi (RPi). My college-age sons had introduced me to Spotify. This amazing music resource needed to somehow get connected to a Bryston amplifier and Magneplanars that had been too quiet in recent years.

A web search yielded several solutions to this "connection" challenge where new meets old. The Pi MusicBox seemed like a nice approach: headless, and with control from my laptop or Android device. The overall cost of the project was about $100:

The photo shows the DAC board stacked on top of the RPi (in the shadow underneath). Both are housed in the Dog Bone case. If you look carefully, you can see the little extensions to the case's corner posts that I ordered to accommodate the added height of the DAC board.

The instructions for imaging the RPi can be found at the Pi MusicBox site. I followed the English howto. Basically, you write the Pi MusicBox image to an SD card and edit the settings.ini file to configure a Wi-Fi connection to your router (SSID and password). Put the card in the RPi and power it up. Then connect any browser to its little webserver and start playing music out of the analog outputs of the DAC board.

It works well and with a significant step up from Bluetooth audio quality. I like to make playlists using the Spotify client on my laptop and then access the playlists from the Pi MusicBox client. But you can also search for music directly on the Pi MusicBox client. An endless supply of new tunes...

Update note, April 26, 2017:  Recently tried Volumio and like it as an alternate for the Pi MusicBox software. Found the Spotify Connect plugin for Volumio to be very good. Playlists are accessible via a Spotify client running on a laptop, tablet, phone, etc. You do this by using the "Connect to a device" feature in the Spotify client. This renders (casts) your choices (e.g. pick a song, start a playlist,...), in the Spotify client, onto to your Raspberry Pi which is the hardware host for Volumio. Get the plugin (volspotconnect2.zip), then follow the installation instructions there.

Another update, September 15, 2023: Volumio has integrated the functionality of the Spotify Connect plugin into the main Spotify plugin. So, it's much easier to setup now, only need one plugin, just install the main Spotify plugin that is available in Volumio.

Fast enough for a physics engine?

It seemed like only seconds after I put down my cash on the RPi-1-B+ that the new and improved RPi-2 was announced. Wow, a promising six-fold improvement in performance and a new incentive to buy. Instead of encouraging me to listen to new tunes, this RPi-2 would be making objects move on its screen and hopefully at frame rates that provide a smooth and stable rendering.

The video here shows that, yes, its 900MHz ARMv7 CPU and VideoCore IV GPU are enough for the job. This was taken using a camera aimed at the RPi's display (note that software-based screen-capture methods burden the RPi and reduce framerates). The script file running here is A16c_2D_B2D_serverN.py (see corresponding topic on Using Pybox2d in Our Framework from the main page). The last section on this page has discussion on the example scripts included with the Linux-installation zip file.

Try running this script on your RPi and interact with the objects in the Pygame window. First, you'll need to run the installation script described in the next section. Then, from a command line on your RPi, type the following two commands:

cd ~/Downloads/Linux-install/example-scripts
python A16c_2D_B2D_serverN.py

Note that a wildcard character (*) is helpful for typing these long filenames. For example, the second line can be typed using the much shorter command:

python A16c*

You'll notice that when this script starts, a second (practice) puck is active and firing. When it comes time to play with real-people clients, you might not want the practice puck. In that case, start the script with a command-line parameter of "off."

python A16c* off

Now it's time to use your keyboard and mouse to interact with objects in the Pygame window. These commands were used in the video and are summarized here:

So again, yes, the little RPi-2 has enough muscle to host a Python physics engine. Its low cost opens all this to teaching/learning environments that are not as well funded as a college J-term course: a high school, a community center, or even a church looking for a way to share its message and give the gift of learning to its parishioners and neighbors in its area.

Installation

The list of Python modules needed for the Linux installation of the working environment is the same as for the Windows installation:

Of course the details for Linux will differ from the Window's process described on the main page. Python and Pygame are already on Raspbian, so only three module sets are needed. The Linux installation will involve building from source code (no .exe executable files like those used for part of the Windows installation). But in the end, the Linux installation is easier because of an installation script that does most of the work.

But first, do these two things: (1) check that your keyboard settings match your keyboard, and (2) expand the file system. Both can be done from the RPi configuration interface (Menu / Preferences / Raspberry Pi Configuration). From there, click on the "Localisation" tab, then the "Set Keyboard" button and set the country and variant (e.g. "United States" and "English (US)"), then click "OK." Next, click on the "System" tab and then the "Expand Filesystem" button. Then click "OK," then "OK" again, and then "Yes" when asked if you want to reboot. If you're not prompted for the reboot, just do a reboot from the main menu (Menu / Shutdown / Reboot).

The Linux installation procedure is base on a script of mine that installs these modules: PodSixNet, pgu, and pybox2d. So basically you download the zip file, unzip it, and run the installation script. The following outline gives step-by-step instructions. Commands typed at the terminal command line are shown in a bold yellow font.

  1. Use the RPi's web browser to go to the Google Drive share and download the Linux-install.zip file.
  2. Open a terminal window on the RPi and then change to the Downloads folder:
    cd ~
    cd Downloads

  3. Unzip the file:
    unzip Linux-install.zip
  4. Change to the Linux-install folder:
    cd Linux-install
  5. Make the installation file executable:
    chmod +x installation.sh
  6. Run the installation file:
    sudo ./installation.sh

The installation.sh file takes about 6 minutes to finish. You'll be prompted a few times and will need to respond and say "yes." There will be many warning messages, especially during the build for the pybox2d module. That's normal. The final act of the installation script is to add a few lines to the end of your config.txt file. This sets the color depth to be compatible with the pybox2d scripts. When it completes, you may notice this message: "That should do it. Done." If the config.txt file was modified, the script will reboot the RPi; this serves to activate these color-depth changes.

An alternative approach to installation is to use the Raspbian-Jessie-withPET.zip file on the Google Drive share. This zip contains an image which is based on Raspbian-Jessie and has made use of the installation.sh script to install the working environment. Uncompress the file and then write the image to your RPi's micro SD card (16 GB or larger), using Win32DiskImager or similar, then boot your RPi from it. You'll find the example scripts in ~/Downloads/Linux-install/example-scripts. Oops, just noticed that the image is set up for left-handed mouse buttons (sorry I'm a lefty). It's easy to change this: go to Menu / Preferences / Mouse and Keyboard Settings and uncheck the "Left handed..." box.

Example scripts and tweaks to the code

Included with the zip file are a handful of example scripts in the example-scripts folder. These are identical to the corresponding scripts from the main page. They represent the end of the three sequences presented there: 1D, 2D, and Box2D. The discussion below describes recent code changes that were made to improve the performance of these scripts on the RPi.

.
.
.

(return)  (P.E.T.)