Build cool stuff with littleBits, a Pi, and some Lego Bricks
Raspberry Pi with a Wii Remote
A remote controller is another way to control the rover (Figure 7). We chose to use a Wii remote, which needs the cwiid Python library to communicate via Bluetooth. To install this package, enter:
sudo apt-get install python-cwiid
We used the simple test program shown in Listing 4 to verify that the Pi and the Wii remote were communicating. To get the program to see the Wii remote, you need to push the buttons labeled 1 and 2 at the same time.
Listing 4
wii_test.py
01 # 02 # Simple test code to show that a Wii remote is talking 03 # 04 import time 05 import cwiid 06 07 print 'Press button 1+2 on your Wiimote now...' 08 wii = cwiid.Wiimote() 09 10 time.sleep(1) 11 12 wii.rpt_mode = cwiid.RPT_BTN 13 print 'WII Remote Connected' 14 15 while True: 16 buttons = wii.state['buttons'] 17 18 if (buttons & cwiid.BTN_UP): 19 print 'up button' 20 time.sleep(0.2) 21 22 if (buttons & cwiid.BTN_DOWN): 23 print 'down button' 24 time.sleep(0.2)
For the final application (Listing 5), we translated the Wii commands into the keyboard commands and wrote to the USB port as before. In lines 17-31, you can see that pressing the upper side of the control pad at the top of the Wii remote sends a g
, for go, and pressing the bottom side sends an s
, for stop; pressing the left side sends an l
, for left, and pressing the right side sends an r
, for right.
Listing 5
wii_2_rover.py
01 # 02 # wii_2_rover.py - Wii remote sends commands to a littleBits Arduino via the USB 03 # 04 import serial 05 import time 06 import cwiid 07 08 print 'Press 1+2 on your Wiimote now...' 09 wii = cwiid.Wiimote() 10 11 time.sleep(1) 12 13 wii.rpt_mode = cwiid.RPT_BTN 14 print 'WII Remote Connected' 15 16 littlebits = serial.Serial("/dev/ttyACM0", baudrate=9600) 17 while True: 18 buttons = wii.state['buttons'] 19 20 if (buttons & cwiid.BTN_UP): 21 littlebits.write('g') 22 time.sleep(0.2) 23 24 if (buttons & cwiid.BTN_DOWN): 25 littlebits.write('s') 26 time.sleep(0.2) 27 28 if (buttons & cwiid.BTN_LEFT): 29 littlebits.write('l') 30 time.sleep(0.2) 31 32 if (buttons & cwiid.BTN_RIGHT): 33 littlebits.write('r') 34 time.sleep(0.2)
Summary
The littleBits motors cannot handle a lot of weight, so be careful when you design your rover. Cardboard, elastic bands, and tape might be a better solution than the Lego blocks we used in our project.
The littleBits modules by themselves are a ton of fun, but if you include a Raspberry Pi and some Lego bricks, there is no telling how far you can go.
Infos
- littleBits: http://littlebits.cc/
- PuTTY: http://www.putty.org/
- Arduino IDE: https://www.arduino.cc/en/Main/Software
- Code for this article: ftp://anonymous@ftp.linux-magazine.com/pub/listings/raspberry-pi-geek.com/12
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 6
(incl. VAT)