Thursday 30 November 2023

Pimoroni Yukon - Interfacing a Raspberry Pi to an Micro-controller running Micropython

I've been working on testing some new pre-release modular robotics hardware from Pimoroni. The Yukon board.


I am planning to build my Pi Wars robot around this, which means I need a way to talk to a Micro-python board from a Raspberry Pi. It would be really neat to be able to do this over the USB lead. This board is not a HAT, so needs powering via USB. I thought it would be great to use the USB port on the Pi to power the Yukon board, and for communication between the two.

This opened up a rabbit hole of learning. First to work out how to send data over USB serial from Python on the Pi, and how to read the data in Micropython on a connected micro-controller. I discovered that certain byte values get changed (line ending conversions) and that some kill the micro-python program (the same way pressing the 'stop' button in the Thonny IDE does). I learned how to disable this feature in micropython, and then how difficult it is to get control back when main.py auto-runs on power up and turns off the ability of the IDE to intercept and stop the program!

To avoid these problems I decided to hex encode each byte I wanted to send, so it is sent as a pair of bytes represented by ASCII characters which are safe to send. This topic raised some interest on social media, so I cleaned up the example and made it run on a vanilla Raspberry Pi Pico board. You can find the code on github.com/Footleg/rpi-pico/tree/main/micropython/serial_comms_over_usb

Saturday 11 November 2023

Developing a small robot as a code test platform

I have learned from previous experience in the Pi Wars robotics competition how important it is to get testing code early. So rather than working on the physical build of my robot, I have been working on programming robotics code around areas which are new to me. I was already developing a small robot for STEM outreach events, based around the Inventor HAT mini board from Pimoroni.

This board supports encoder motors, something I have not developed code for previously. I wanted more feedback from my robot to better control it autonomously. Encoders enable you to read the speed of rotation of your motors, and count how many rotations they have made. This makes speed control possible, and distance travelled to be tracked. All my previous robots have been manual control only, so this was all new for me. I also added and IMU, which would enable measuring the orientation of the robot. This makes accurate turns possible.

The triangular tracked robot I was already developing is a little too small for Pi Wars, but the code developed on it should be portable to my competition robot once I have that built. I wrote a hardware abstraction interface so that my code talks to an interface which is independent of the specific hardware it is running on. I converted my manual control robot program from my STEM event robots to use the interface, creating this Universal Robot project on Github.