Robot Buggy Module 5 – Interactive Controls

Interactive Controls

Interactive control of your robot buggy let’s an operator give it instructions in real time, move it forward, back, left or right. 
If that’s all it does, then the robot is basically a (WiFi) radio-controlled machine with a human operator.

Give the interactive controls a means of recording the real-time instructions, and we’ve added a capability for any real robot: a training mode.  Add a capability to playback a training sequence recorded earlier, and we have the most basic elements in an industrial robot.

The simple command-line-interface (CLI) Python program described here (full  Code Listing – Interactive GPIO Robot on this page) provides basic features that allow an operator to control a robot interactively, record the directions given it as a time-stamped training sequence, and play back previously recorded training sequences to perform the same actions once again.

Code Dependencies

As with most programs, our Python code is greatly simplified and streamlined by reliance on predefined libraries for basic functions. This includes the gpiozero, keyboard interface, and time libraries among others. 

				
					import time
import os
import sys
import glob
from pynput import keyboard
from datetime import timedelta, datetime
from gpiozero import Robot
				
			

Global Variables

Our program has a number of global variables declared (too many in all likelihood! Good programming localizes and hides as much information as possible from global exposure.). 

‘robby’ is our robot under control, as an instance of the gpiozero library’s Robot Class. The Training and Playback functions of our program use the remaining variables, which are declared global to simplify data sharing between different functions. 
These are the declared global variables: 

				
					global robby
global trk_file
global last_key_event_was_release
global t_start
global t_now
global delta_t
global track_record
				
			

Keyboard Control using the Pynput Libraries

The pynput libraries allow your Python program to interact with the keyboard. Our interactive controls respond to the keyboard’s arrow keys to control robot motion, responding to key-press events (function on_press(key)) and key-release events (function on_release(key)).

An on_press() event determines which key is pressed and if it is an arrow key moves the robot in the specified direction and calls  record_command().

An on_release() event stops the robot unconditionally, sets the global Boolean flag indicating that the last key event was a key release. 

				
					def on_press(key):
    global track_record
    global last_key_event_was_release
    try:
        if key == keyboard.Key.up:
            robby.forward()
            record_command("f")
        elif key == keyboard.Key.down:
            robby.backward()
            record_command("b")
        elif key == keyboard.Key.left:
            robby.left()
            record_command("l")
        elif key == keyboard.Key.right:
            robby.right()
            record_command("r")
    except AttributeError as error:
        print(error)

def on_release(key):
    global track_record
    global last_key_event_was_release
    #print('{0} released; car stopped'.format(key))
    last_key_event_was_release = True
    robby.stop()
    print("Release time (robot.stopped) = ", t_now)
    track_record.write("s"+("\t") + str(time.time()) +"\n")
    track_record.flush()

				
			

Training

Keyboard inputs are time-stamped and recorded during training mode to the track_record file defined and opened an the start of training. 

The command recorded (i.e., the argument ‘cmd ’  passed to the function record_command is defined by the key pressed and parsed by the function on_press(key). The time recorded and associated with cmd is the time the key is released. The command is active and the robot moves in the specified direction as long as the key is pressed. 

				
					def record_command(cmd)
    if (last_key_event_was_release == True):
        last_key_event_was_release = False
        track_record.write(cmd+("\t") + str(time.time()) +"\n")
        track_record.flush()
				
			

Playback 

Playback offers the operator a choice of previously recorded training sequences to run. 

Remote  Control

To control the car remotely, ensure that you can:

  • log into the car’s Raspberry Pi controller over WiFi using the ssh secure shell command from another computer 
  • start the CLI Interactive Controller and give it commands. 

 

Advanced Controls – Adding the Robot Car to The Internet of Things

Check out the remote control alternative on this page, based on adding a web-server to the car’s Raspberry Pi controller, allowing control of the car through a web browser as a device on the Internet of Things. 

This approach to remote control is a great introduction to programming with JavaScript, the structure of Internet communication between client applications (I.e., your Web Browser) and server applications (I.e., the Robot).