GoPiGo is a really nice robot that you can build by your own. It’s inexpensive and a good starting point when learning robotics. First you need to assemble it by your own and install Raspbian for robots (a modified version of Raspbian OS for GoPiGo). With a help of step-by-step instructions it would be an easy deal. Once assembled you can use Python to program it. Usually you need to be remotely logged in and launch your script from remote terminal, so in this article we’ll be exploring another approach using remote procedure call. Thankfully Python has its builtin module for that, so implementing RPC client/server is an easy task.
Creating a server
First we need to define our server: it’s a set of methods it can perform and which will be exposed for our client. Which methods should be exposed? Those could be anything you want but we’ll start with implementing basic movements in all the directions and setting a desired speed.
It consists of a master window, scale slider (for setting speed), quit button, two boolean flags (responsible for lights being turned on/off and flashing in random order) and s field which holds a connection to our server. We then bind keyboard buttons to control ours robot movements with bind_controls method.
There is one tricky part though which requires imported above futures module. In order to make our lights blink we need continuously turn them on and off. This will block the rest of the code, so we need a dedicated thread which will do that in the background. Let’s see how it works
First we define all the available methods for turning a particular led on and their counterparts. After that we create a list of flags storing information about which led is active at the moment. Then within given interval we randomly toggle a state of one of them which creates a blinking effect.
Turning all the lights on is much simpler (the client code is responsible for not turning the lights on when flashing is in progress, no additional measurements was done on this side)
But how does our background thread know when to start flashing? We use our self.flashing flag for that. It just periodically checks whether a flag is enabled and it can be enabled via exposed methods start_flash and stop_flash.