Astro Pi Update
David Briddock marks the ISS Proxima Mission with Astro Pi coding hints, tips and examples
A while ago, we looked at how the Raspberry Pi was being used in space for a number of interesting science projects. Now, we’re returning to the void to find out what it’s been up to and what new things are on the horizon
In issue 1419 we covered the Astro Pi project ( astro-pi.org) which sent two Raspberry Pi based scientific modules to the International Space Station (ISS). Affectionately named Ed and Izzy these Pi modules were used by British astronaut Tim Peake to perform scientific experiments during his six-month ISS mission. Look inside the custom-made spaceruggedised flight cases and you’ll see each module is a Raspberry Pi 2 plus a camera and a multi-sensor SenseHAT.
A key part of the mission was an open competition to schools across the UK to design and code software programs. The winning entries were uploaded to the ISS then installed and run on Ed and Izzy.
In November a Soyuz rocket launch from Baikonur cosmodrome in Kazakhstan marked the start of another six-month-long ISS mission called Proxima.
As you probably know, Ed and Izzy are still aboard the ISS and there are plans to put them to use running new scientific programs. This time they’ll be under the guidance of ESA French astronaut Thomas Pesquet ( goo.gl/vyT8cq).
Understandably the Raspberry Pi Foundation was delighted to hear about the second mission, especially after all the hard work involved in putting the two Pi units through the rigorous Flight Safety Certificate (FSC) testing process.
As part of the ISS Proxima Mission there’s a brand new Astro Pi School Challenge competition for schools. Once again they’ve been asked to design and code novel and interesting programs suitable for the Astro Pi modules. However, this time UK schools will be joined by ones from France, Spain, Germany and other European countries. Once again the winning programs will be installed and run on Ed and Izzy.
Before returning to Earth astronaut Tim Peake plugged in an Ethernet cable to the Astro Pi devices. This small action makes a big difference as there’s now direct space-to-ground communications with Ed and Izzy. This means that the Proxima Mission team is going to find it much easier to upload Pi software to the ISS and download the experiment data back to the ESA mission control centre.
To find out more about this ISS mission, take at look at the new Mission Plan Template by navigating your web browser to magpi. cc/2enYSWP.
A Raspberry Pi and a SenseHAT is all you need to code and run the sort of programs that operate aboard the ISS. This code could be created from scratch from an idea you’ve already had buzzing around. Alternatively you could start with an idea from a magazine or website, then add your own bespoke features and functionality.
Whichever option you choose this week we’ll show you a number of Astro Pi centric Python version 3 coding tips and tricks. To get started boot up your Raspberry Pi, then fire up the IDLE development tool by going to the ‘Programming’ section of the main menu and selecting the ‘Python 3 (IDLE)’ option.
The first task is to ensure we have at our disposal all the Python functionality provided by the SenseHAT library module. In Python all we need to do is import this library module and create a ‘sense’ object with these two lines of code:
from sense_hat import SenseHat sense = SenseHat()
Now we’re able to call any of the methods on the ‘sense’ object. For example, we can display a character and a message on the 8x8 square LED display like this:
Pretty straightforward stuff, but we can go much further. For example, setting text and background colours, the message scroll speed and rotation angles. And by controlling each individual pixel you can create multi-coloured 8x8 pixel images.
Now let’s look at accessing environmental sensor readings. Here’s a code example to obtain the current temperature, pressure and humidity:
t = sense.get_temperature() p = sense.get_pressure() h = sense.get_humidity()
By the way, the normal environmental operating conditions for the ISS are a temperature range of 18.3C and 26.7C, a pressure range of 979 millibars to 1027 millibars and around 60% humidity.
Ascertaining the Pi’s position is three-dimensional space in terms of pitch, roll and yaw is also pretty simple. Here’s an example:
orient = sense.get_orientation() pitch = orient[‘pitch’] roll = orient[‘roll’] yaw = orient[‘yaw’]
In a similar way we can obtain the accelerometer values, which indicate how fast or slow the Pi is moving. Here’s an example:
accel = sense.get_accelerometer_raw() x = acceleration[‘x’] y = acceleration[‘y’] z = acceleration[‘z’]
But what if you don’t have a Raspberry Pi and SenseHAT to hand? Well, it’s still possible to play along as all you need is a web browser. With Trinket’s SenseHAT simulation website (trinket.io/sense-hat) you enter Python code and see the results via an animated image.
The web page is split into two sections. Code is entered on the left and the SenseHAT image appears on the right. While not the most ergonomic web app design I’ve seen, there’s a ‘Run’ and ‘Stop’ menu to try out your code, Python module documentation plus a number of code sharing options.
Initially the code editor half contains an LED display example code listing. But towards the bottom of the page you’ll find a sensor data code example plus a number of games. A simple ‘click’ replaces the contents of the editor with the code for the selected example.
A Raspberry Pi and a SenseHAT is all you need to code and run the sort of programs that operate aboard the ISS
Most importantly, the code you write is identical to what you’d use on a Raspberry Pi equipped with a SenseHAT. This means your tested code can simply be copied to a real Pi with SenseHAT. This is a boon for classroom situations where there’s not enough Pi hardware to go around. And ideal for students wanting to try out code, either as homework or just for fun.
The website is the ideal way to experiment with LED interaction code. And there are sliders to change the temperature, pressure and humidity values. But when it comes to things like motion sensing using the online emulation just isn’t the same thing as playing around with a real Pi with attached SenseHAT.
Trinket also offer a Pi-hosted emulator app, which is already included in the latest Raspbian Jessie release. Alternatively use the download instructions ( goo.gl/9TSSjd) which cover Raspbian, the Ubuntu operating system and the Python ‘pip’ utility.
Once installed on a Raspberry Pi the emulator app is accessed from the Desktop menu under ‘Programming’. You can write your code in the Python IDLE editor just as before. All you need to do is subtly change the import statement replacing ‘sense_hat’ with ‘sense_emu’, like this:
from sense_emu import SenseHat
After this change any SenseHAT code example should work fine. Just change it back to use ‘sense_hat’ when you run the code with a real SenseHAT.
The SenseHAT has been around for quite a while now. Consequently there’s a healthy collection of online documentation and code examples. While there are too many websites to list here it is worth mentioning a couple of highlights.
Parents and teachers will find plenty of useful information on the official Raspberry Pi education resource pages. Here you’ll also find a ‘getting started’ tutorial based around the Trinket web-based emulator ( goo.gl/kCDV6m).
One particularly useful Python coding resource is the SenseHAT application programming interface (API) reference pages (pythonhosted.org/sense-hat/api). It covers the LED Matrix, all three environmental sensors (temperature/pressure/humidity), the inertial measurement unit (IMU) sensor and the built-in joystick.
For those playing with the emulator app the ‘sense-emu’ documentation pages provide a detailed picture of the functionality on offer, along with numerous code examples ( sense-emu. readthedocs.io).
Ed and Izzy have also been put to work acquiring scientific data. The data was collected via the various SenseHAT sensors with a special ‘flight recorder’ program that ran 24 hours a day over a number of weeks.
This ‘flight recorder’ experiment generated three large CSV data files (see boxout) filled with Astro Pi sensor data. These files have been downloaded back to Earth and uploaded to the online GitHub repository in the form of compressed zip files ( goo.gl/cSUY9d).
Two of these CSV zip files contain data collected by Ed in the ISS Columbus module. The first ‘Columbus_Ed_astro_pi_datalog.csv.zip’ file has two weeks worth of data, while the second ‘Columbus2_Ed_ astro_pi_datalog.csv.zip’ contains data captured over four weeks.
Izzy collected data from the ISS Node 2 location over a twoweek period and her data is contained in the ‘Node2_Izzy_astro_ pi_datalog.csv.zip’ file. In the same GitHub location you’ll also found an example CSV file, which contains test data recorded before the launch.
Download and unzipped these files and you’ll soon discover they are crammed full of sensor data; even the smallest has over 100,000 rows across 20 data columns (see boxout). The high volumes are due to frequent sensor readings, sometimes as often as every 10 seconds.
This poses quite a challenge when it comes to identifying regular patterns or picking out strange anomalies. However, with a little Python code we can access specific columns and particular time periods.
Python 3 Example
Here’s how to get started using Python 3. First we’ll need to import a few library modules:
import os, csv, datetime
With a Raspberry Pi, we have access to the very same Astro Pi technology that’s installed on board the ISS
With these imports in place we can now open one of these CSV files and extract the data with just a few lines of code. If we put this code into a function we can call it again and again:
def load_csv(filename): with open(filename, “r”) as f: r = csv.reader(f, delimiter=”,”) return list(r)
This function returns the data as a Python list. If the unzipped CSV files are renamed to ‘Columbus-Ed1.csv’, ‘Columbus-Ed2.csv’ and ‘Node2-Izzy.csv’, we can open a file in the current working directory like this:
f = os.getcwd() + “/Columbus-Ed1.csv” rows = load_csv(f)
The ‘rows’ variable now contains a Python two-dimensional list object containing the CSV data columns and rows. In effect, it’s very similar to the representation you’d see in a spreadsheet, where each cell can be addressed by its column and row number.
The first row is a little different as it contains the column names rather than data. We can extract these names into a variable, then remove this row like this:
colNames = rows rows.pop(0)
Now the ‘rows’ list contains pure sensor data. To extract data for a specific sensor we’ll need to specify a column number. Note the column numbers actually start from zero.
Here’s a loop that extract the first 1,000 rows of humidity data, converting the text value into a floating point number and printing out this number each time we iterate round the loop:
COL_HUMIDITY = 4 for i in range(0,1000): row = rows[i] hum = float(row[COL_HUMIDITY]) print(‘Humidity: %.2f’ % (hum))
For full data analysis we’ll need the date and time of the readings, contained in column number 19. To be useful we’ll need to convert the text value into a native Python ‘datetime’ object. This code can then be added to the row processing loop, as shown in CSV code listing.
Now take these basic coding principles and build an app of your own that uses some or all of the CSV file data. There are really no limits to what’s possible, so just let your imagination flow.
If you need some inspiration take a look at Hannah Belshaw’s SpaceCRAFT, one of the winning entries from the Tim Peake mission competition, which displays CSV-sourced data in the Minecraft game ( astro-pi.org/competition/winners/#spacecraft).
To find details of all the school competition winning entries associated with Tim Peake’s Astro Pi mission go to the astro-pi.org/ competition/winners webpage.
Unfortunately, we can’t all be astronauts. But, we can take an active part in space missions. With a Raspberry Pi we have access to the very same Astro Pi technology that’s installed on board the ISS, then create our own ‘space’ programs.
That’s amazing thing to be able to say. And the Raspberry Pi Foundation is justifiably proud to have achieved such a feat with such an affordable product.
CSV code example
Ed and Izzy