Programming Remote Control - Robotics with Python Raspberry Pi and GoPiGo p.4




In this robotics with Python, the Raspberry Pi, and the GoPiGo, we're going to cover remote control, based on keyboard input from the user. In order to do this, we're going to need access to the GUI desktop, referred to as X. To remote desktop to the Raspberry Pi, there are quite a few options depending on your Operating System. On Windows, run sudo apt-get update, then sudo apt-get upgrade, then finally: sudo apt-get install xrdp

From here, on Windows, you can go to your search bar and type in "remote desktop." This will open and let you put in the IP address of your Raspberry Pi. Then you put in the username and password, and you're going to be loaded into your GUI desktop. Need more help with this? Check out the connecting to your GoPiGo tutorial, and look at Option #1.

Once you've done that, you should now be connected to the GUI desktop. You can either develop the next script in here, or you can do it via SSH, or via something like WinSCP or Filezilla. I will name this new file tutorial4.py, placing it again in that GoPiGoLocal directory that we created earlier.

First, to allow the keyboard to control, we need to create some way of tracking keypresses live. We'll use Tkinter for this, which is a GUI application development framework, which we'll just utilize for this.

When someone refers to "the console," they are referring to where information from your program is ouput. You will see an example of "output to console" below. If you want this message to go away, just click again on the "console" button that you originally clicked on.


import Tkinter as tk
from gopigo import *

def key_input(event):
    key_press = event.keysym.lower()
    print(key_press)

command = tk.Tk()
command.bind_all('', key_input)
command.mainloop()

Import Tkinter and gopigo libraries

key_input function handles key_input events. For now, it simply prints them out so we can see what we press. Later, we will perform actions based on the key presses.

Now, we create a new Tkinter window method calling it command, we bind keypresses here and pass them through the key_input function, then run the mainloop.


Run this within the desktop environment. A small window should pop up, this is the Tkinter window we made. So long as this window is the active window, you can begin pressing letters on your keyboard and the output in the console should display the keys you pressed.

Building on this, what we can do is start using if-statements to conditionally run functions based on what users press.

import Tkinter as tk
from gopigo import *

servo_range = [2,3,4,5,6,7,8]

def key_input(event):
    key_press = event.keysym.lower()
    print(key_press)

    if key_press == 'w':
        fwd()
    elif key_press == 's':
        bwd()
    elif key_press == 'a':
        left()
    elif key_press == 'd':
        right()
    elif key_press == 'q':
        left_rot()
    elif key_press == 'e':
        right_rot()
    elif key_press == 'space':
        stop()
    elif key_press == 'u':
        print(us_dist(15))

    elif key_press.isdigit():
        if int(key_press) in servo_range:
            enable_servo()
            servo(int(key_press)*14)
            time.sleep(1)
            disable_servo()

command = tk.Tk()
command.bind_all('', key_input)
command.mainloop()

Now, depending on what you press, the GoPiGo will do a specific action. Of course if you don't have a servo, you shouldn't have that code in there. If you don't have a distance sensor, same thing.


There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Robotics with Python Raspberry Pi and GoPiGo Introduction
  • Supplies Needed - Robotics with Python Raspberry Pi and GoPiGo p.2
  • Programming Robot Basics - Robotics with Python Raspberry Pi and GoPiGo p.3
  • Programming Remote Control - Robotics with Python Raspberry Pi and GoPiGo p.4
  • Weaponizing our Robot - Robotics with Python Raspberry Pi and GoPiGo p.5
  • Programming Autonomy - Robotics with Python Raspberry Pi and GoPiGo p.6
  • OpenCV with Raspberry Pi Camera Face Detection Tutorial - Robotics with Python Raspberry Pi and GoPiGo p.7