Use a wireless keyboard to control a mobile missile launcher

Wireless Keyboard

Although our missile test program worked fine, we didn't want to keep hitting the Enter key after pressing each control letter key, so we used the Python evdev package [7]. To install this library enter:

sudo pip install evdev

Using the evdev library, we could move or aim the launcher with a single keypress. Listing 3 shows a small test program that we used to capture the up and down keypresses.

Listing 3

evdev_test.py

01 from evdev import InputDevice, categorize, ecodes
02 gamepad = InputDevice('/dev/input/event0')
03 for event in gamepad.read_loop():
04     if event.type == ecodes.EV_KEY and event.value == 1:
05         keyevent = categorize(event)
06         if keyevent.keycode == "KEY_UP":
07             print "DRIVE FORWARD"
08         if keyevent.keycode == "KEY_DOWN":
09             print "DRIVE STOP"
10         #print (keyevent) # uncomment to see key messages

The wireless keyboard required no extra setup, we simply plugged the USB dongle in, and it worked. The Up, Down, Left, and Right arrow keys control the motors for the robot chassis wheels, and the number pad keys aim and shoot the missiles (Figure 5).

Figure 5: Keyboard keys that control the launcher.

The final program that we used is shown in Listing 4.

Listing 4

missilelauncher.py

01 ##########################################################################
02 # Copyright 2011 PaperCut Software Int. Pty. Ltd. http://www.papercut.com/
03 # Modifications: 2015 Leah, Brooke, and Pete Metcalfe
04 #
05 #   Licensed under the Apache License, Version 2.0 (the "License");
06 #   you may not use this file except in compliance with the License.
07 #   You may obtain a copy of the License at
08 #
09 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 #  Author:  Chris Dance <chris.dance@papercut.com>
18 #  Version: 1.0 : 2011-08-15
19 ##########################################################################
20
21 import pifacedigitalio as p
22 import time
23 import usb
24 import sys
25 from evdev import InputDevice, categorize, ecodes
26
27 global device
28
29 down = 1
30 up = 2
31 left = 4
32 right = 8
33 fire = 16
34 stop = 32
35
36 # Define the missile launcher function
37 def launcher(action):
38   print 'in launcher routine'
39   print action
40   device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, action, 0x00,0x00,0x00,0x00,0x00,0x00])
41   if action <> 16:
42     time.sleep(0.10)
43     device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, stop, 0x00,0x00,0x00,0x00,0x00,0x00])
44   else:
45     time.sleep(2)
46
47 # Setup the Missile Launcher
48
49 device = usb.core.find(idVendor=0x2123, idProduct=0x1010)
50 try:
51   device.detach_kernel_driver(0)
52 except Exception, e:
53   pass # already unregistered
54
55 device.set_configuration()
56 endpoint = device[0][(0,0)][0]
57
58 p.init()
59
60 gamepad = InputDevice('/dev/input/event0')
61
62 for event in gamepad.read_loop():
63     if event.type == ecodes.EV_KEY and event.value == 1:
64         keyevent = categorize(event)
65         if keyevent.keycode == "KEY_KP8":
66             print "AIM UP"
67             launcher (up)
68         if keyevent.keycode == "KEY_KP2":
69             print "AIM DOWN"
70             launcher (down)
71         if keyevent.keycode == "KEY_KP4":
72             print "AIM LEFT"
73             launcher (left)
74         if keyevent.keycode == "KEY_KP6":
75             print "AIM RIGHT"
76             launcher (right)
77         if keyevent.keycode == "KEY_KP5":
78             print "FIRE"
79             launcher (fire)
80         if keyevent.keycode == "KEY_UP":
81             print "DRIVE FORWARD"
82             p.digital_write(7, 1)
83             p.digital_write(6, 1)
84         if keyevent.keycode == "KEY_DOWN":
85             print "DRIVE STOP"
86             p.digital_write(7, 0)
87             p.digital_write(6, 0)
88         if keyevent.keycode == "KEY_LEFT":
89             print "TURN LEFT"
90             p.digital_write(7, 0)
91             p.digital_write(6, 1)
92         if keyevent.keycode == "KEY_RIGHT":
93             print "TURN RIGHT"
94             p.digital_write(7, 1)
95             p.digital_write(6, 0)
96         print (keyevent)

Start on Boot

Once the missile launcher was assembled, we no longer had access to a monitor and mouse, so we needed the Raspberry Pi to start the program automatically. Although we could do this a few different ways, we decided to have the pi user automatically log in, and then we added an autologin script.

For this method to work, you need Raspbian "Wheezy" with the SysVinit system installed. If you are using any Raspbian greater than v2015-05-05 (or NOOBS greater than or equal to v1.4.2), this method will not work, because it will not have the /etc/inittab file.

To log in automatically, you need to edit the startup task list in /etc/inittab. First, comment out the line indicated below by adding a hash mark (#) at the beginning, then add a new line just under it:

sudo nano /etc/inittab
...
#1:2345:respawn:/sbin/getty 115200 tty1
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Pressing Ctrl+X saves the changes and exits the Nano editor. For the pi user to start the script automatically on login, edit the .bash_login file and add a line that runs the missile launcher program:

nano /home/pi/.bash_login
sudo python /home/pi/missilelauncher.py

Again, enter Ctrl+X to save and exit.

After the autologin and startup script were added, we were able to reboot the Raspberry Pi without a monitor. It might take about 45 seconds to boot up, but after that, we used the wireless keyboard to drive, aim, and shoot missiles.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content