Page 1 of 1

code help

Posted: Thu Dec 08, 2016 12:26 am
by razatherogue
Okay I'm following a guide on the rpi learning resources website, the one called robobutler.
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)    

Re: code help

Posted: Thu Dec 08, 2016 4:13 am
by razatherogue
bump

Re: code help

Posted: Thu Dec 08, 2016 9:12 am
by B.Goode
These forums are run by volunteers in their spare time. Their sense of urgency and priorities may be different to yours.

Maybe noone with the knowledge or experience to answer your query logged in in the 4 hours since you asked it.

The forums have a search facility that can point out unanswered posts: your 'bump' has excluded your question from that search.

A meaningful title such as "Problem with RoboButler project" is more likely to attract informed attention. And a direct link to the tutorial you are following would avoid helpers having to search it out for themselves.

Re: code help

Posted: Thu Dec 08, 2016 9:20 am
by B.Goode
From: https://www.raspberrypi.org/learning/ro ... worksheet/
It's always good to break down projects into chunks, as it makes it much easier to troubleshoot. If you do this project all at once and it doesn't work, how do you know if it's the motor board, the motors, the Bluetooth connection or your program at fault?
My suggestion is to go back to the beginning and check each step. If a step fails, fix it before continuing.