Astro Pi Up­date

David Brid­dock marks the ISS Prox­ima Mis­sion with Astro Pi coding hints, tips and ex­am­ples

Micro Mart - - Contetnts -

A while ago, we looked at how the Rasp­berry Pi was be­ing used in space for a num­ber of in­ter­est­ing sci­ence projects. Now, we’re re­turn­ing to the void to find out what it’s been up to and what new things are on the hori­zon

In is­sue 1419 we cov­ered the Astro Pi project ( astro-pi.org) which sent two Rasp­berry Pi based sci­en­tific mo­d­ules to the In­ter­na­tional Space Sta­tion (ISS). Af­fec­tion­ately named Ed and Izzy these Pi mo­d­ules were used by Bri­tish astronaut Tim Peake to per­form sci­en­tific ex­per­i­ments dur­ing his six-month ISS mis­sion. Look in­side the cus­tom-made spaceruggedised flight cases and you’ll see each mod­ule is a Rasp­berry Pi 2 plus a cam­era and a multi-sen­sor SenseHAT.

A key part of the mis­sion was an open com­pe­ti­tion to schools across the UK to de­sign and code soft­ware pro­grams. The win­ning en­tries were up­loaded to the ISS then in­stalled and run on Ed and Izzy.

Prox­ima Mis­sion

In Novem­ber a Soyuz rocket launch from Baikonur cos­mod­rome in Kaza­khstan marked the start of an­other six-month-long ISS mis­sion called Prox­ima.

As you prob­a­bly know, Ed and Izzy are still aboard the ISS and there are plans to put them to use run­ning new sci­en­tific pro­grams. This time they’ll be un­der the guid­ance of ESA French astronaut Thomas Pes­quet ( goo.gl/vyT8cq).

Un­der­stand­ably the Rasp­berry Pi Foun­da­tion was de­lighted to hear about the sec­ond mis­sion, es­pe­cially af­ter all the hard work in­volved in putting the two Pi units through the rig­or­ous Flight Safety Cer­tifi­cate (FSC) test­ing process.

As part of the ISS Prox­ima Mis­sion there’s a brand new Astro Pi School Chal­lenge com­pe­ti­tion for schools. Once again they’ve been asked to de­sign and code novel and in­ter­est­ing pro­grams suit­able for the Astro Pi mo­d­ules. How­ever, this time UK schools will be joined by ones from France, Spain, Ger­many and other Euro­pean coun­tries. Once again the win­ning pro­grams will be in­stalled and run on Ed and Izzy.

Be­fore re­turn­ing to Earth astronaut Tim Peake plugged in an Eth­er­net ca­ble to the Astro Pi de­vices. This small ac­tion makes a big dif­fer­ence as there’s now di­rect space-to-ground com­mu­ni­ca­tions with Ed and Izzy. This means that the Prox­ima Mis­sion team is go­ing to find it much eas­ier to up­load Pi soft­ware to the ISS and down­load the ex­per­i­ment data back to the ESA mis­sion con­trol cen­tre.

To find out more about this ISS mis­sion, take at look at the new Mis­sion Plan Tem­plate by nav­i­gat­ing your web browser to magpi. cc/2enYSWP.

SenseHAT

A Rasp­berry Pi and a SenseHAT is all you need to code and run the sort of pro­grams that op­er­ate aboard the ISS. This code could be created from scratch from an idea you’ve al­ready had buzzing around. Al­ter­na­tively you could start with an idea from a magazine or web­site, then add your own be­spoke fea­tures and func­tion­al­ity.

Which­ever op­tion you choose this week we’ll show you a num­ber of Astro Pi cen­tric Python ver­sion 3 coding tips and tricks. To get started boot up your Rasp­berry Pi, then fire up the IDLE de­vel­op­ment tool by go­ing to the ‘Pro­gram­ming’ sec­tion of the main menu and se­lect­ing the ‘Python 3 (IDLE)’ op­tion.

The first task is to en­sure we have at our dis­posal all the Python func­tion­al­ity pro­vided by the SenseHAT li­brary mod­ule. In Python all we need to do is im­port this li­brary mod­ule and cre­ate a ‘sense’ ob­ject with these two lines of code:

from sense_hat im­port SenseHat sense = SenseHat()

Now we’re able to call any of the meth­ods on the ‘sense’ ob­ject. For ex­am­ple, we can dis­play a char­ac­ter and a mes­sage on the 8x8 square LED dis­play like this:

sense.show_let­ter(“X”) sense.show_mes­sage(“Hello”)

Pretty straight­for­ward stuff, but we can go much fur­ther. For ex­am­ple, set­ting text and back­ground colours, the mes­sage scroll speed and ro­ta­tion an­gles. And by con­trol­ling each in­di­vid­ual pixel you can cre­ate multi-coloured 8x8 pixel images.

Now let’s look at ac­cess­ing en­vi­ron­men­tal sen­sor read­ings. Here’s a code ex­am­ple to ob­tain the cur­rent tem­per­a­ture, pres­sure and hu­mid­ity:

t = sense.get_tem­per­a­ture() p = sense.get_­pres­sure() h = sense.get_hu­mid­ity()

By the way, the nor­mal en­vi­ron­men­tal oper­at­ing con­di­tions for the ISS are a tem­per­a­ture range of 18.3C and 26.7C, a pres­sure range of 979 mil­libars to 1027 mil­libars and around 60% hu­mid­ity.

Ascer­tain­ing the Pi’s po­si­tion is three-di­men­sional space in terms of pitch, roll and yaw is also pretty sim­ple. Here’s an ex­am­ple:

ori­ent = sense.get_ori­en­ta­tion() pitch = ori­ent[‘pitch’] roll = ori­ent[‘roll’] yaw = ori­ent[‘yaw’]

In a sim­i­lar way we can ob­tain the ac­celerom­e­ter val­ues, which in­di­cate how fast or slow the Pi is mov­ing. Here’s an ex­am­ple:

ac­cel = sense.get_ac­celerom­e­ter_raw() x = ac­cel­er­a­tion[‘x’] y = ac­cel­er­a­tion[‘y’] z = ac­cel­er­a­tion[‘z’]

SenseHAT Emu­la­tor

But what if you don’t have a Rasp­berry Pi and SenseHAT to hand? Well, it’s still pos­si­ble to play along as all you need is a web browser. With Trin­ket’s SenseHAT sim­u­la­tion web­site (trin­ket.io/sense-hat) you en­ter Python code and see the re­sults via an an­i­mated im­age.

The web page is split into two sec­tions. Code is en­tered on the left and the SenseHAT im­age ap­pears on the right. While not the most er­gonomic web app de­sign I’ve seen, there’s a ‘Run’ and ‘Stop’ menu to try out your code, Python mod­ule doc­u­men­ta­tion plus a num­ber of code shar­ing op­tions.

Ini­tially the code ed­i­tor half con­tains an LED dis­play ex­am­ple code list­ing. But to­wards the bot­tom of the page you’ll find a sen­sor data code ex­am­ple plus a num­ber of games. A sim­ple ‘click’ re­places the con­tents of the ed­i­tor with the code for the se­lected ex­am­ple.

A Rasp­berry Pi and a SenseHAT is all you need to code and run the sort of pro­grams that op­er­ate aboard the ISS

Most im­por­tantly, the code you write is iden­ti­cal to what you’d use on a Rasp­berry Pi equipped with a SenseHAT. This means your tested code can sim­ply be copied to a real Pi with SenseHAT. This is a boon for class­room sit­u­a­tions where there’s not enough Pi hard­ware to go around. And ideal for stu­dents want­ing to try out code, ei­ther as home­work or just for fun.

The web­site is the ideal way to ex­per­i­ment with LED in­ter­ac­tion code. And there are slid­ers to change the tem­per­a­ture, pres­sure and hu­mid­ity val­ues. But when it comes to things like mo­tion sens­ing us­ing the on­line em­u­la­tion just isn’t the same thing as play­ing around with a real Pi with at­tached SenseHAT.

Trin­ket also of­fer a Pi-hosted emu­la­tor app, which is al­ready in­cluded in the lat­est Rasp­bian Jessie re­lease. Al­ter­na­tively use the down­load in­struc­tions ( goo.gl/9TSSjd) which cover Rasp­bian, the Ubuntu oper­at­ing sys­tem and the Python ‘pip’ util­ity.

Once in­stalled on a Rasp­berry Pi the emu­la­tor app is ac­cessed from the Desk­top menu un­der ‘Pro­gram­ming’. You can write your code in the Python IDLE ed­i­tor just as be­fore. All you need to do is sub­tly change the im­port state­ment re­plac­ing ‘sense_hat’ with ‘sense_emu’, like this:

from sense_emu im­port SenseHat

Af­ter this change any SenseHAT code ex­am­ple should work fine. Just change it back to use ‘sense_hat’ when you run the code with a real SenseHAT.

On­line Re­sources

The SenseHAT has been around for quite a while now. Con­se­quently there’s a healthy col­lec­tion of on­line doc­u­men­ta­tion and code ex­am­ples. While there are too many web­sites to list here it is worth men­tion­ing a cou­ple of high­lights.

Par­ents and teach­ers will find plenty of use­ful in­for­ma­tion on the of­fi­cial Rasp­berry Pi ed­u­ca­tion re­source pages. Here you’ll also find a ‘get­ting started’ tu­to­rial based around the Trin­ket web-based emu­la­tor ( goo.gl/kCDV6m).

One par­tic­u­larly use­ful Python coding re­source is the SenseHAT ap­pli­ca­tion pro­gram­ming in­ter­face (API) ref­er­ence pages (python­hosted.org/sense-hat/api). It cov­ers the LED Ma­trix, all three en­vi­ron­men­tal sen­sors (tem­per­a­ture/pres­sure/hu­mid­ity), the in­er­tial mea­sure­ment unit (IMU) sen­sor and the built-in joy­stick.

For those play­ing with the emu­la­tor app the ‘sense-emu’ doc­u­men­ta­tion pages pro­vide a de­tailed pic­ture of the func­tion­al­ity on of­fer, along with nu­mer­ous code ex­am­ples ( sense-emu. readthe­docs.io).

Sci­ence Data

Ed and Izzy have also been put to work ac­quir­ing sci­en­tific data. The data was col­lected via the var­i­ous SenseHAT sen­sors with a special ‘flight recorder’ pro­gram that ran 24 hours a day over a num­ber of weeks.

This ‘flight recorder’ ex­per­i­ment gen­er­ated three large CSV data files (see box­out) filled with Astro Pi sen­sor data. These files have been down­loaded back to Earth and up­loaded to the on­line GitHub repos­i­tory in the form of com­pressed zip files ( goo.gl/cSUY9d).

Two of these CSV zip files con­tain data col­lected by Ed in the ISS Colum­bus mod­ule. The first ‘Colum­bus_Ed_as­tro_pi_­dat­a­log.csv.zip’ file has two weeks worth of data, while the sec­ond ‘Colum­bus2_Ed_ as­tro_pi_­dat­a­log.csv.zip’ con­tains data cap­tured over four weeks.

Izzy col­lected data from the ISS Node 2 lo­ca­tion over a twoweek pe­riod and her data is con­tained in the ‘Node2_Izzy_as­tro_ pi_­dat­a­log.csv.zip’ file. In the same GitHub lo­ca­tion you’ll also found an ex­am­ple CSV file, which con­tains test data recorded be­fore the launch.

Down­load and un­zipped these files and you’ll soon dis­cover they are crammed full of sen­sor data; even the small­est has over 100,000 rows across 20 data col­umns (see box­out). The high vol­umes are due to fre­quent sen­sor read­ings, some­times as of­ten as ev­ery 10 sec­onds.

This poses quite a chal­lenge when it comes to iden­ti­fy­ing reg­u­lar pat­terns or pick­ing out strange anom­alies. How­ever, with a lit­tle Python code we can ac­cess spe­cific col­umns and par­tic­u­lar time pe­ri­ods.

Python 3 Ex­am­ple

Here’s how to get started us­ing Python 3. First we’ll need to im­port a few li­brary mo­d­ules:

im­port os, csv, date­time

With a Rasp­berry Pi, we have ac­cess to the very same Astro Pi tech­nol­ogy that’s in­stalled on board the ISS

With these im­ports in place we can now open one of these CSV files and ex­tract the data with just a few lines of code. If we put this code into a func­tion we can call it again and again:

def load­_csv(file­name): with open(file­name, “r”) as f: r = csv.reader(f, de­lim­iter=”,”) re­turn list(r)

This func­tion re­turns the data as a Python list. If the un­zipped CSV files are re­named to ‘Colum­bus-Ed1.csv’, ‘Colum­bus-Ed2.csv’ and ‘Node2-Izzy.csv’, we can open a file in the cur­rent work­ing direc­tory like this:

f = os.getcwd() + “/Colum­bus-Ed1.csv” rows = load­_csv(f)

The ‘rows’ vari­able now con­tains a Python two-di­men­sional list ob­ject con­tain­ing the CSV data col­umns and rows. In ef­fect, it’s very sim­i­lar to the rep­re­sen­ta­tion you’d see in a spread­sheet, where each cell can be ad­dressed by its col­umn and row num­ber.

The first row is a lit­tle dif­fer­ent as it con­tains the col­umn names rather than data. We can ex­tract these names into a vari­able, then re­move this row like this:

colNames = rows[0] rows.pop(0)

Now the ‘rows’ list con­tains pure sen­sor data. To ex­tract data for a spe­cific sen­sor we’ll need to spec­ify a col­umn num­ber. Note the col­umn num­bers ac­tu­ally start from zero.

Here’s a loop that ex­tract the first 1,000 rows of hu­mid­ity data, con­vert­ing the text value into a float­ing point num­ber and print­ing out this num­ber each time we it­er­ate round the loop:

COL_HUMIDITY = 4 for i in range(0,1000): row = rows[i] hum = float(row[COL_HUMIDITY]) print(‘Hu­mid­ity: %.2f’ % (hum))

For full data anal­y­sis we’ll need the date and time of the read­ings, con­tained in col­umn num­ber 19. To be use­ful we’ll need to con­vert the text value into a na­tive Python ‘date­time’ ob­ject. This code can then be added to the row pro­cess­ing loop, as shown in CSV code list­ing.

Now take these ba­sic coding prin­ci­ples and build an app of your own that uses some or all of the CSV file data. There are re­ally no lim­its to what’s pos­si­ble, so just let your imag­i­na­tion flow.

If you need some in­spi­ra­tion take a look at Han­nah Belshaw’s SpaceCRAFT, one of the win­ning en­tries from the Tim Peake mis­sion com­pe­ti­tion, which dis­plays CSV-sourced data in the Minecraft game ( astro-pi.org/com­pe­ti­tion/win­ners/#spacecraft).

To find de­tails of all the school com­pe­ti­tion win­ning en­tries as­so­ci­ated with Tim Peake’s Astro Pi mis­sion go to the astro-pi.org/ com­pe­ti­tion/win­ners web­page.

Wrap Up

Un­for­tu­nately, we can’t all be as­tro­nauts. But, we can take an ac­tive part in space mis­sions. With a Rasp­berry Pi we have ac­cess to the very same Astro Pi tech­nol­ogy that’s in­stalled on board the ISS, then cre­ate our own ‘space’ pro­grams.

That’s amaz­ing thing to be able to say. And the Rasp­berry Pi Foun­da­tion is jus­ti­fi­ably proud to have achieved such a feat with such an af­ford­able prod­uct.

SenseHAT

Astro Pi

CSV code ex­am­ple

Ed and Izzy

Newspapers in English

Newspapers from UK

© PressReader. All rights reserved.