I have unsuccesfully been trying to use this code:
Whenever I try it, I get an error saying it's already in use. Ignoring it, I proceeded to continue with the instructions. When I pressed 1 and 2 it said my Wiimote is connected, but then whenever I press anything my rover chassis does not move ( everything is connected.) My hardware is setup like so:
Ryanteck v3 board has a power supply.each wire is from the right side of the terminal blocks. which go in to my four motor robot chassis on the left side of each port. one port is on the left side of the chassis and one is on the right side of the chassis. I connected everything and put the board on the RPi and then I turned the RPi on. When I tried the code it didn't work. (i already installed cwiid) What am I doing wrong?
Code: Select all
#!/usr/bin/python
#based on Matt Hawkins' code http://www.raspberrypi-spy.co.uk/?p=1101
#Re written by Ryan Walmsley
import cwiid
import time
import RPi.GPIO as io
io.setmode(io.BCM)
#Motor 1 is designed to be the motors on the left, Motor 2 is designed to be on the right
#If one motor is in the wrong direction you can swap the pins around to save you having to re-wrire the robot.
m1a = 17 #Motor 1 Forwards
m1b = 18 #Motor 1 Backwards
m2a = 22 #Motor 2 Forwards
m2b = 23 #Motor 2 Backwards
pins = (m1a,m1b,m2a,m2b)
for i in pins:
io.setup(i,io.OUT)
for i in pins:
io.output(i,False)
button_delay = 0.1
print 'Press 1 + 2 on your Wii Remote now ...'
time.sleep(1)
# Try to connect to the Wiimote & quit if not found
try:
wii=cwiid.Wiimote()
except RuntimeError:
print "Can't connect to Wiimote"
quit()
print 'Wiimote connected'
wii.rpt_mode = cwiid.RPT_BTN
while True:
buttons = wii.state['buttons']
if (buttons & cwiid.BTN_UP):
#Forwards
time.sleep(button_delay)
io.output(m1a, True)
io.output(m2a, True)
elif (buttons & cwiid.BTN_DOWN):
time.sleep(button_delay)
io.output(m1b, True)
io.output(m2b, True)
elif (buttons & cwiid.BTN_LEFT):
time.sleep(button_delay)
io.output(m1a, True)
io.output(m2b, True)
elif(buttons & cwiid.BTN_RIGHT):
time.sleep(button_delay)
io.output(m1b, True)
io.output(m2a, True)
else:
io.output(m1a, False)
io.output(m1b, False)
io.output(m2a, False)
io.output(m2b, False)
#press button A to stop all motors
if (buttons & cwiid.BTN_A):
time.sleep(button_delay)
for i in pins:
io.output(i, False)