2017-02-02 11 views
0

그래서 저는 파이에 파이썬으로 프로그래밍하고 있습니다. cwiid 라이브러리를 통해 나는 wii 컨트롤러를 연결하고있다. 이것은 잘 작동합니다.GPIO 핀이 내 프로그램의 중간에서 더 이상 작동하지 않습니다.

그러나 내 wii 컨트롤러가 트로프 블루투스를 연결하면 갑자기 LED가 작동하지 않습니다. 동일한 코드 행은 연결하기 전에 작동하지만 연결 후 더 이상 작동하지 않습니다. 이것이 어떻게 가능한지?

내 전체 코드 :

if __name__ == '__main__': 
from sys import path 

path.append("..") 


import RPi.GPIO as GPIO 
from time import sleep 
import os 
import cwiid 
from MotorPwm2 import MotorPwm 
from GPIOFuckUp import GPIOFuckUp 
from Basis import Basis 

GPIOFuckUp() 

GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False) 

motor = MotorPwm([10, 9, 8, 7]) 
basis = Basis() 
startbool = False 

running = True 

left_light = 21 
right_light = 20 
left_siren = 16 
right_siren = 26 

GPIO.setup(left_light, GPIO.OUT) 
GPIO.setup(right_light, GPIO.OUT) 
GPIO.setup(left_siren, GPIO.OUT) 
GPIO.setup(right_siren, GPIO.OUT) 

left_pin = GPIO.PWM(left_light, 1000) 
right_pin = GPIO.PWM(right_light, 1000) 
left_pin_s = GPIO.PWM(left_siren, 1000) 
right_pin_s = GPIO.PWM(right_siren, 1000) 

left_pin.start(100) 
right_pin.start(100) 
left_pin_s.start(0) 
right_pin_s.start(0) 

# Code above works. Leds turn on, with no problems. 

def turn_on_left(): 
    dim_left(100) 

def turn_on_right(): 
    dim_right(100) 

def turn_on_lefts(): 
    dim_lefts(100) 

def turn_on_rights(): 
    dim_rights(100) 

def turn_on(): 
    print 'aan' 
    dim_left(100) 
    dim_right(100) 

def turn_off_lefts(): 
    dim_lefts(0) 

def turn_off_rights(): 
    dim_rights(0) 

def turn_off_left(): 
    dim_left(0) 

def turn_off_right(): 
    dim_right(0) 

def turn_off(): 
    dim_left(0) 
    dim_right(0) 

def dim(percentage): 
    dim_left(percentage) 
    dim_right(percentage) 

def dim_left(percentage): 
    left_pin.ChangeDutyCycle(percentage) 
    print 'left' 

def dim_right(percentage): 
    right_pin.ChangeDutyCycle(percentage) 
    print 'right' 

def dim_lefts(percentage): 
    left_pin_s.ChangeDutyCycle(percentage) 

def dim_rights(percentage): 
    right_pin_s.ChangeDutyCycle(percentage) 

def siren_loop(): 
    while running: 
     dim_lefts(100) 
     dim_rights(100) 
     sleep(0.05) 

     turn_off_lefts() 
     turn_off_rights() 
     sleep(0.05) 

     dim_rights(100) 
     sleep(0.05) 

     turn_on_rights() 
     sleep(0.05) 

     dim_lefts(100) 
     sleep(0.05) 

     dim_lefts(0) 
     sleep(0.05) 

# Def things above also work. Copied the code in another file just to try 
the leds, and that also worked. 


button_delay = 0.1 

print 'Press 1 + 2 on your Wii Remote now ...' 

# Here the controller is connecting. After it is connected the leds turn of 
# Before the following print lines are printed. 

# Connect to the Wii Remote. If it times out 
# then quit. 
try: 
    wii = cwiid.Wiimote() 
    #GPIO.output(PIN_LED, 0) 

except RuntimeError: 
    print "Error opening wiimote connection" 
    #GPIO.output(PIN_LED, 0) 
    # Uncomment this line to shutdown the Pi if pairing fails 
    os.system("sudo reboot") 
    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 
     #GPIO.output(PIN_LED, 1) 
     sleep(1) 
     wii.rumble = 0 
     #GPIO.output(PIN_LED, 0) 
     os.system("sudo reboot") 
     exit(wii) 


    # Button presses below work and print the correct lines. Just no leds :(

    if (buttons & cwiid.BTN_LEFT): 
     print 'Left pressed' 
     motor.left_forward(50) 
     sleep(button_delay) 

    elif (buttons & cwiid.BTN_RIGHT): 
     print 'Right pressed' 
     motor.right_forward(50) 
     sleep(button_delay) 

    elif (buttons & cwiid.BTN_UP): 
     print 'Up pressed' 
     motor.forward(100) 
     sleep(button_delay) 

    elif (buttons & cwiid.BTN_DOWN): 
     print 'Down pressed' 
     motor.backward(100) 
     sleep(button_delay) 

    elif (buttons & cwiid.BTN_A): 
     print 'A' 
     if startbool == False: 
      print 'start aan' 
      startbool = True 
      sleep(0.5) 
     else: 
      print 'start uit' 
      startbool = False 
      sleep(0.5) 

    elif (buttons & cwiid.BTN_1): 
     if running: 
      turn_on() 
      sleep(5) 
     print '1' 
     print 'Licht 5 sec aan' 

    elif (buttons & cwiid.BTN_2): 
     print '2' 
     print 'sirene 5 sec aan' 
     siren_loop() 
     sleep(5) 


    else: 
     GPIO.cleanup() 

따라서, 예를 들어, 시작 값은 내가 프로그램을 실행할 때의 LED를 켜십시오. 이런 일이 발생하면 LED가 켜집니다. 그러나 Wii 리모컨을 연결하면 전원이 꺼지고 다시 켜지는 것이 불가능합니다.

+0

당신이 쉽게 파손되는 부분 이해할 수 있도록 코드의 당신의 라인에 몇 가지 주석을 추가 시겠어요 문제를 알아? 고마워, –

+0

몇 가지 의견과 함께 그것을 업데이 트되었습니다. – Noralie

+0

초기 연결이 실패 할 수 있습니까? 파이를 재부팅하는 런타임 예외 조항이 있음을 알았습니다. 일시적으로 시간을 절약하기 위해 이것을 print 문으로 변경할 수 있습니다. 재부팅하는 대신 "연결 실패"를 인쇄하십시오. 이 텍스트가 표시되면 라즈베리 파이가 자체 재부팅 중임을 의미하므로 프로그램이 중지됩니다. 고마워. 고마워. –

답변

0

몇 시간 동안 노력한 후에 마침내 문제가 발견되었습니다.

마지막 줄은 말했다 : 그래서 그들은 작동하지 않는 한, 모든 핀을 resetted

else: 
    GPIO.cleanup() 

. else를 삭제하면 모든 것이 다시 작동합니다.

는 지금은 그렇게 간단한의 xD 보인다