I'm made a Turing Machine, and here's the code to run it! A Turing Machine is an early model of a computer developed by Alan Turing, where a computer has access to an infinite tape (array) of information to operate on. Inside the computer are predefined states that act like code. Based on reading, say, 1 (a filled square) or 0 (an empty square), a state decides whether to change the bit, whether to move left/right, and which state to switch to next.
My project uses a Raspberry Pi to simulate a Turing Machine, with a camera to read square bit values from the tape. The physical tape is used as input. Because of the difficulty of actually writing to a real piece of tape, the computer first stores the values into a large array of bits internally. The Turing Machine code operates on this array, and then a portion of the resulting tape is printed in the console.
- Make sure you have a working Raspberry Pi with the camera plugged in
- Clone this repository or download and unzip it.
- Install g++ if you do not already have it.
- In your terminal application, run the following command within the repository: g++ TuringMachine.cpp -o TuringMachine.o
- Find or make a thin, long piece of paper to act as the tape.
- Draw squares along the tape, empty white squares for 0 and filled black squares for 1. At the end of the tape, draw a green square. This tells the program to stop reading.
- Ensure in advance that the camera can see the colors on the tape as it passes by.
- I built a lego arch that locked the camera into place facing down. I pulled the tape undeneath the arch, so that the camera could take pictures of bits and interpret their values. You might also be able to simply hold the camera in place.
- In your terminal application, run the following command: python main.py
- First, the program will ask you to input states for the Turing Machine. You can name the states and decide how they respond to reading a 1 or a 0. You can also skip this process to use predefined states, as recorded in states.txt
- Next, the program will read bits on the tape. Drag the tape by the camera so that it captures individual squares or bits along it. The camera will continue reading squares until it reads the green square, so make sure to conclude with the color green.
- Wait for an output. The Turing Machine code is initialized with the states you chose and the array of bits equivalent to those found on the physical tape. The Turing Machine will edit bits on the tape according to the states, and it will either halt or continue for long enough that the program stops the process (e.g. 100,000,000 operations).
- The program concludes by outputting a portion of the resulting array of bits (currently set to 60 bits about the center). Congratulations, you successfully ran a Turing Machine.
For setting up code to interact with the camera:
- Les Pounder. 2022. "How To Use Picamera2 to Take Photos With Raspberry Pi." Tom's Hardware, last modified September 17. https://www.tomshardware.com/how-to/use-picamera2-take-photos-with-raspberry-pi.
For analysing pixels in images:
- Cartucho, João, imxitiz, RMS, et al. 2022. "How do I access the pixels of an image using OpenCV-Python?" Stack Overflow, last modified January 7. https://stackoverflow.com/questions/28981417/how-do-i-access-the-pixels-of-an-image-using-opencv-python.