Monday, March 28, 2022

Introducing Python with Astro Pi: Mission Zero


Astro Pi is a yearly Python coding challenge offered by the European Space Agency. It challenges students to write a simple Python program that will be run in space on the International Space Station.

It has been run since the first Raspberry Pi computers were sent to the International Space Station in 2015. It's open to European countries and, thankfully, Canada, a total of 25 participating countries. Last year we had 18 teams enter and they all received a nice certificate that had their team name and a map showing where the ISS was flying when their code ran:

As you can imagine there's lots of excitement that their program will not only be run in space, but astronauts may see their message! There are lots of jokes and alien greetings, but there are some minimal requirements:
  • the Python code must run with no errors
  • a humidity reading must be taken and displayed (using SenseHAT)
  • programs are cut off at 30-seconds runtime
Astro Pi uses Trinket.io, an online IDE with a Raspberry Pi and SenseHAT emulator so students can experiment and test their code in one convenient location. Trinket is also iPad and mobile compatible.


The Sense HAT module is quite limited in its scope (you can see the API calls here) but probably most students will be interested in drawing or animating pictures using the 8x8 LEDs. The simplest method would be to colour each individual pixel using a list:
picture = [
   g, b, b, b, b, b, b, g,
   b, g, g, g, g, g, g, b,
   b, g, b, b, g, w, g, g,
   b, g, b, b, g, g, g, g,
   b, g, g, g, s, s, g, g,
   b, g, r, g, g, g, g, g,
   b, g, g, g, g, g, g, b,
   g, b, b, b, b, b, b, g
   ]
This is incredibly tedious, though a spreadsheet with conditional formatting may help students conceptualize their drawings. Even using a pixel art program like pixilart.com with a canvas size of 8x8 would be easier.

Animations would be the next step up for students. Simple code to advance each colour to draw line by line:
r = (255, 0, 0)     # red
o = (255, 127, 0)   # orange
y = (255, 255, 0)   # yellow
g = (0, 255, 0)     # green
b = (0, 0, 255)   # blue
i = (75, 0, 130)     # indigo
v = (148, 0, 211)   # violet
w =(255, 255, 255)  # white
k = (0, 0, 0)       # black

rainbow = [r, o, y, g, b, i, v, w]


while True:
  for y in range(8):
      colour = rainbow[y]
      for x in range(8):
          sense.set_pixel(x, y, colour)
      sleep(0.25)
  for x in range(8):
      colour = rainbow[x]
      for y in range(8):
          sense.set_pixel(x, y, colour)
      sleep(0.25)
  sense.clear()