Pi-Py-Pi: Calculating Pi in Python on Raspberry Pi (Cylon / Peper’s Ghost Edition!)
Pi-Py-Pi Calculating

Pi-Py-Pi: Calculating Pi in Python on Raspberry Pi (Cylon / Peper’s Ghost Edition!)

It’s that time of the year when we honor our beloved constant of nature: Pi.

3.141592653589793238462643383279502884197...        

All salute Pi!!

From ancient Babylonians to modern XYZW generations, we all love Pi. Our irrational friend is the ratio of the circumference to its diameter; it has been calculated by its 62 trillion digits already, with no end in sight.

Pizza Pi!

Does Pi encode the secrets of the universe? I don’t know, but that will not prevent us from having fun with it.

If you have been following my profile, you know that every Pi day (03/14) I published a Pi-devoted gadget. This time wouldn’t be different.

This time I built a Pi trifecta: Pi-Py-Pi, or a machine that calculates Pi, with the code written in Python and running on the Raspberry Pi 2040 MCU.

To take the fun to a new level, I also incorporated two elements of popular culture: A Pepper’s Ghost display and a Battlestar Galactica-inspired activity monitor.


Pepper’s Ghost: In the middle of 1800, the British scientist named John Henry Pepper popularized an optical illusion where a semi-reflective piece of glass would be positioned at an angle between two rooms, one that the public could see directly and another one, hidden from their line of sight. That arrangement would produce an illusion where objects carefully placed in the hidden room would appear as a faint image, sometimes “floating”, in the second room.

Pepper's Ghost stage set up - Bertrand

This effect has been used for decades and is in use even today in amusement parks.

I tried to reproduce this effect here by “hiding” the calculating device in an especially 3D printed enclosure, where a small window will allow the hidden LED display light to reach a 25mm optical glass beam-splitting prism cube, which will partially reflect the display image to an observer positioned perpendicular to it as a faint floating sequence of Pi digits as they get computed.

Top of the 3D-printed case


Battlestar Galactica: No self-proclaimed nerd ignores the importance of the Battlestar Galactica series to our culture. It’s a harrowing storyline, of humans fighting against machines for survival. Everything about it is iconic, the ships, uniforms, helmets, etc. But one special item terrorized me as a kid: The chasing red light, coming back and forth, of the Cylons, the robots.

Cylon Cosplay

Since, after some time running, the rate of calculated digits being displayed will reduce, it may appear that the machine froze. Nothing far from the truth, my machine never gives up and will calculate Pi for eternity; however, I needed a way to keep the observer informed that the machine is alive and well, and I did that with a back-and-forth red-led chasing effect, ala the Cylons.

Since RP2040 MCU is a dual-core one, I can use one core for calculating Pi (which is pretty heavy in processing by itself) while the other core keeps our Cylon-inspired activity monitor kicking.


Internals: Initially I was going to use a Raspberry Pi Pico for this project, however, I was fortunate enough to be able to participate at the #MFR2023 (Maker Faire Rome 2023), this past October, where I had an 8 sq. meters booth and presented my GNSS Time server (please see here: GNSSTimeServer - Project Page), and, there, could purchase some gadgets.

One of them was the Melopero Electronics Cookie board, an RP2040 device that appears perfect for wearable electronics, with its shape, Qiic connector, battery support, and Neopixel matrix!

Cookie!

The Neopixel on the Cookie is a 5 x 5 matrix of independently addressable RGB LEDs, which I used to display the Pi digits and device activity.

RP2040 supports Micropython and C/C++. Since the last time I used C/C++ already, decided to give it a try to Micropython and build my code around the excellent article published by Konstantinos Gavalas .

I can see this Cookie board being used as a badge, necklace, desk messenger, etc.

Cookie inside jar.


The code: There are several pages scattered on the internet with papers, expressions, and implementations in several languages of Pi calculating algorithms. The one that caught my attention was https://www.gavalas.dev/blog/spigot-algorithms-for-pi-in-python/ by Konstantino. It’s a compact reading on a complex subject where he goes over several algorithms and refinements, their Haskell code and ports them to Python, compares their efficiency and, by the end of the text, the most efficient (proven) is displayed, based on an expression attributed to Bill Gosper. It′s indeed the shortest and most efficient Pyhton Pi code I have seen so far.


The device: Now, with the Cookie board programmed, 3D case printed, and prism positioned over the aperture, here comes the final product:

Assembled device

And a video of it working:

The video from my smartphone camera doesn't do it justice, it's much more beautiful in person.

I hope you all enjoyed it! The source files for the code and 3D case can be found here:


Thanks, and see you next year!


References:

https://www.guinnessworldrecords.com/news/2022/3/new-value-of-pi-calculated-by-swiss-university-at-over-62-billion-digits-694748

https://en.wikipedia.org/wiki/Pepper%27s_ghost

https://www.raspberrypi.com/documentation/microcontrollers/rp2040.html

https://www.flickr.com/photos/myklroventine/3355106480

https://commons.wikimedia.org/wiki/File:Pi-Star.png

https://commons.wikimedia.org/w/index.php?curid=58613337

https://freesound.org/people/krytosss/sounds/167388/


Konstantinos Gavalas

Software Engineer | Passion for Researching, Developing and Learning | ECE Graduate @ UoP

1 年

Love seeing my code being used in such an interesting project! Happy #PiDay!!

要查看或添加评论,请登录

Cristiano Monteiro的更多文章

社区洞察

其他会员也浏览了