Peripherals

Peripherals include any external I/O devices. With the Raspberry Pi connected to your robot it makes it very easy for you to connect peripheral devices and sensors.

This page gives you a brief introduction into the following peripherals for controlling your robot:

  • Pi Camera
  • Keyboards
  • Gamepad Controllers

PiCamera

The PiCamera is a part of the of the OAR kit.  The Raspberry Pi has the ability to connect various models of camera via the bus on the board and also via USB.  This information is regarding the base level PiCamera which is pictured.

Connection of the Camera

The following video demonstrates how to connect your Pi Camera into the correct bus.

Programming the Camera

Picamera is a pure Python interface for the Raspberry Pi camera module for Python 2.7 (or above).  You can find documentation and sample code on it at the Raspberry Pi website.  The picamera library should be installed by default on the Raspbian Linux image so you can start importing the library and configuring the output.


import picamera
camera = picamera.PiCamera()
camera.start_preview()
camera.stop_preview()
camera.close()

You can also define the image settings.

Keyboards

Keyboards can be connected to your Raspberry Pi and you can map the keys to different functions. This will allow you to control your robot with your keyboard. Any plug-and-play keyboard which works with Linux will work on your robot.

Whilst key events can be mapped using Pygame it is easier to use Python’s de-facto Graphical User Interface (GUI) package called Tkinter. Tkinter is not the only GUI toolkit but, it is the most commonly used one with the most support.

When importing Tkinter there is a slight difference when using Python 2.7 and Python 3. The library is renamed from using a capital. You can easily import the library and open a window.


import Tkinter as tk
window = tk.Tk()
window.resizeable(width=False, height=False)
window.geometry("600x800")
def keypress(event):
x = event.char
if x == "q":
window.destory()
window.bind_all("", keypress)
window.mainloop()

You will find more information regarding connection of Gamepads in the related peripheral tutorials.

GamePad Controllers

gamepad1There are various different gamepad controllers on the market. This section will cover a few of the more popular models, but essentially if the operating system is able to detect them you should easily be able to use them with your robot.

There are two python libraries which will allow you to map input devices. The first is pygame which only work with Python 2.7 and requires the creation of an app. The second and more simple option is a library called evdev.

evdev is a generic input event interface in the Linux kernel. it generalises raw input events drivers and ames them available through character devices in the /dev/input/directory. This makes it perfect when we wish to use 3rd party gamepads to control our robots.

Start by installing the evdev into your python development environment.


sudo pip install evdev

Once we have found our gamepad we can easily output the raw input events.

gamepad = find_controller()
for event in gamepad.read_loop():
print(event)

Logitech F310 and F710
Depending on if you are using the F310 (wired) or F710 (wireless) and which USB port you plug into the event will appear differently. The following snippet of code will allow you to search through all possible ports and controller names. You can modify the list if you want to use a different controller.


from evdev import InputDevice, categorize, ecodes, KeyEvent
def find_controller():
event0 = InputDevice('/dev/input/event0')
event1 = InputDevice('/dev/input/event1')
event2 = InputDevice('/dev/input/event2')
event3 = InputDevice('/dev/input/event3')
controller_list = ["Logitech Gamepad F710", "Logitech Gamepad F310", "Microsoft X-Box 360 pad"]
for controller in controller_list:
if event0.name == controller:
gamepad = event0
elif event1.name == controller:
gamepad = event1
elif event2.name == controller:
gamepad = event2
elif event3.name == controller:
gamepad = event3
else:
print("controller not found")
return gamepad
gamepad = find_controller()

Xbox 360 Controller (Wired)
The xbox 360 controller will work exactly the same as the Logitech gamepads accept you will need to install an additional driver to help you recognise the button presses.


sudo apt-get install xboxdrv

You will find more information regarding connection of Gamepads in the related peripheral tutorials.