Robotics #1: Programming a Robot with Raspberry Pi 3, GoPiGo3 and Python

Athichart Tangpong
4 min readJun 16, 2024

--

Back in my college days, over 25 years ago, I built a simple robot to collect tennis balls. However, that knowledge faded over time. The global pandemic lockdown and the shift to working from home in 2020 rekindled my long-lost interest in robotics. Beyond enjoying myself, I saw this as a great chance to improve my skills and inspire my kids. I decided to embark on a journey to deepen my understanding of robotics and eventually build my own robot. This article is the first in a series where I share what I’ve learned about robotics.

I chose Raspberry Pi and Python as my platform and programming language due to their popularity and extensive community support. For the robot hardware, I had two options: build everything from scratch using Arduino, motor drivers, and other components, or purchase a robotic kit with a ready-to-use software development kit (SDK). I opted for the latter because my primary interest was in software development.

Robotic Kits

From my research, I found that Dexter Industries offers two intriguing options. The GoPiGo3 is a complete robotic kit that includes a HAT for controlling motors and sensors, along with a simple robot chassis. The BrickPi3, on the other hand, is a Raspberry Pi HAT designed to control Lego EV3 motors and sensors. Both come with Python SDKs. The GoPiGo3 is clearly the best platform for beginners due to its affordability and completeness; I just needed to add a Raspberry Pi to have a working robot.

Update: I began this project back in 2020 when the Raspberry Pi 3 was the top model. At that time, the available HATs were GoPiGo, BrickPi and PiStorm. The GoPiGo worked with traditional motors, while the BrickPi and PiStorm were designed for Lego EV3 motors. There were also some Chinese-made robotic kits that I did not try. Now, in 2024, the Raspberry Pi 5 has been released, along with the Raspbery Pi Build Hat, which can control four Lego SPIKE Prime motors or sensors.

I will skip the GoPiGo chassis assembly part and assume that we have the GoPiGo robot kit ready because a very good instruction is already available from the vendor’s website. On the software side, I installed the Raspbian OS Buster and the GoPiGo3 SDK back in 2020. Another option that is available now is to use the free Dexter OS. To my understanding, it is a customized Raspbian OS with pre-installed GoPiGo3 software. This could be a simpler and faster way to go. Note that in addition to Python and other programming languages, GoPiGo3 also supports a graphic-based programming language, called Scratch. I plan to explore that front later with my kids.

My GoPiGo3 Robot

This is the hardware summary of my GoPiGo3 robot.

  • Raspberry Pi 3
  • GoPiGo3 HAT
  • Two motors for motion
  • A Pi camera connected to two servo motors for pan and tilt
  • A distance sensor
  • A line following sensor
  • A 12V rechargeable battery
  • A mini Bluetooth speaker housing in a custom Lego case
  • USB receivers for a keyboard and a game pad

Software Installation

After assembling the robotic kit, we need to install an operating system and the GoPiGo3 SDK on the Raspberry Pi.

  1. Download and burn the Raspbian Buster image to a micro SD card using a free software like Etcher. The software is very intuitive.
  2. Boot the Raspberry Pi and follow the instruction on the screen to complete the initial setup.
  3. Install GoPiGo3 SDK using the following commands.
# Install or update the GoPiGo3 SDK to the latest version
curl -kL dexterindustries.com/update_gopigo3 | bash
# Install or update the Dexter sensors e.g. line follower, distance sensor
curl -kL dexterindustries.com/update_sensors | bash

Programming

Let’s jump to the fun part, which is how to program the robot using Python. The following code will move the robot, pan and tilt the camera.

Before we wrap up, let’s build a Python class, called GoPiGoRobot, to control the robot for our next articles. Here is the summary of our code.

  1. Encapsulate the GoPiGo3 SDK calls inside friendlier class methods.
  2. Handle hardware access exceptions using try-except statements
  3. Provide mutually exclusive hardware access to make the code thread-safe because we expect our code to be multithreaded. To achieve this purpose, we decorate the hardware access codes with the _lockAndExceptionHandler decorator for the sake of brevity and convenience.

The source code provided in this article may not be the most current version, as it may have evolved with the addition of new features. For the latest source code, please visit my robotics repository on GitHub.

Conclusion

We began this article with a discussion on the robotic kits for Raspberry Pi. We decided to use GoPiGo for our project. Then we show how to install the Raspbian operating system and the GoPiGo SDK. Finally, we demonstrated a simple program to give our robot a little exercise. In the next articles, we will explore more options for our robot.

--

--