CamJam Robot & Wii Mote

A couple of weeks ago I got one of the CamJam Edu kits. Myself and the kids had a lot of fun talking about it and learning about what all the parts did.

One thing I wanted to do was to test out the robot using a Wiimote.

Thanks to a bunch of people out there for varying degree's of guidance I managed to meld the code together and get this working. It allowed my kids to really enjoy it as they now had the power to move it around!

I'm not going to cover any of the guides around the CamJam kit as this is all covered over at the CamJam website found here.

Also the setup of bluetooth on the PI is well documented and a good guide is found here.

My contribution today is around the code to control the robot, its a mash up of the code provided by the CamJam kit and also some added Wiimote code with the relevant link listed in the code output below.

This was thrown together in a very short space of time to keep my kids entertained for an evening please keep that in mind when reviewing.

#!/usr/bin/python
#
# wii_remote_1.py
# Connect a Nintendo Wii Remote via Bluetooth
# and  read the button states in Python.
#
# Project URL :
# http://www.raspberrypi-spy.co.uk/?p=1101
#
# Author : Matt Hawkins
# Date   : 30/01/2013
#
# Updated : 30/05/2016
# Author : Ken Darker
#
# -----------------------
# Import required Python libraries
# -----------------------
import cwiid  
import time  
#import RPi.GPIO as io

import RPi.GPIO as GPIO # Import the GPIO Library  
import time # Import the Time library

# Set the GPIO modes
GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)

# Set variables for the GPIO motor pins
pinMotorAForwards = 10  
pinMotorABackwards = 9  
pinMotorBForwards = 8  
pinMotorBBackwards = 7

# Set the GPIO Pin mode
GPIO.setup(pinMotorAForwards, GPIO.OUT)  
GPIO.setup(pinMotorABackwards, GPIO.OUT)  
GPIO.setup(pinMotorBForwards, GPIO.OUT)  
GPIO.setup(pinMotorBBackwards, GPIO.OUT)

# Turn all motors off
def StopMotors():  
  GPIO.output(pinMotorAForwards, 0)
  GPIO.output(pinMotorABackwards, 0)
  GPIO.output(pinMotorBForwards, 0)
  GPIO.output(pinMotorBBackwards, 0)

# Turn both motors forwards
def Forwards():  
  GPIO.output(pinMotorAForwards, 1)
  GPIO.output(pinMotorABackwards, 0)
  GPIO.output(pinMotorBForwards, 1)
  GPIO.output(pinMotorBBackwards, 0)

# Turn both motors backwards
def Backwards():  
  GPIO.output(pinMotorAForwards, 0)
  GPIO.output(pinMotorABackwards, 1)
  GPIO.output(pinMotorBForwards, 0)
  GPIO.output(pinMotorBBackwards, 1)

# Turn left
def Left():  
  GPIO.output(pinMotorAForwards, 0)
  GPIO.output(pinMotorABackwards, 1)
  GPIO.output(pinMotorBForwards, 1)
  GPIO.output(pinMotorBBackwards, 0)

# Turn Right
def Right():  
  GPIO.output(pinMotorAForwards, 1)
  GPIO.output(pinMotorABackwards, 0)
  GPIO.output(pinMotorBForwards, 0)
  GPIO.output(pinMotorBBackwards, 1)

button_delay = 0.1

print 'Press 1 + 2 on your Wii Remote now ...'  
time.sleep(1)

# Connect to the Wii Remote. If it times out
# then quit.
try:  
  wii=cwiid.Wiimote()
except RuntimeError:  
  print "Error opening wiimote connection"
  quit()

print 'Wii Remote connected...\n'  
print 'Press some buttons!\n'  
print 'Press PLUS and MINUS together to disconnect and quit.\n'

wii.rpt_mode = cwiid.RPT_BTN

while True:

  buttons = wii.state['buttons']

  # If Plus and Minus buttons pressed
  # together then rumble and quit.
  if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
    print '\nClosing connection ...'
    wii.rumble = 1
    time.sleep(1)
    wii.rumble = 0
    exit(wii)

  # Check if other buttons are pressed by
  # doing a bitwise AND of the buttons number
  # and the predefined constant for that button.
  if (buttons & cwiid.BTN_LEFT):
    print 'Left pressed'
    time.sleep(button_delay)
    Left()

  if(buttons & cwiid.BTN_RIGHT):
    print 'Right pressed'
    time.sleep(button_delay)
    Right()

  if (buttons & cwiid.BTN_UP):
    print 'Up pressed'
    time.sleep(button_delay)
    Forwards()

  if (buttons & cwiid.BTN_DOWN):
    print 'Down pressed'
    time.sleep(button_delay)
    Backwards()

  if (buttons & cwiid.BTN_1):
    print 'Button 1 pressed'
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_2):
    print 'Button 2 pressed'
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_A):
    print 'Button A pressed'
    time.sleep(button_delay)
    StopMotors()

  if (buttons & cwiid.BTN_B):
    print 'Button B pressed'
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_HOME):
    print 'Home Button pressed'
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_MINUS):
    print 'Minus Button pressed'
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_PLUS):
    print 'Plus Button pressed'
    time.sleep(button_delay)

GPIO.cleanup()

Ken Darker

Read more posts by this author.