简单的python程序分段错误

时间:2018-02-03 06:11:25

标签: python raspberry-pi3

根据unlock.txt文件的内容,使用伺服电机打开和关闭2个门的简单程序。

它只是给Segmentation Fault

我做了:

ulimit -s 20000

之前它是8000左右,因为我在我的树莓派3上。

仍然获得Segmentation Fault

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

GPIO.setup(40,GPIO.OUT)
GPIO.setup(38,GPIO.OUT)

GPIO.output(40,GPIO.LOW)
GPIO.output(38,GPIO.LOW)

pwm1=GPIO.PWM(40,50)
pwm2=GPIO.PWM(38,50)

flag=0
prevlok=0

while(True):

    with open('/home/pi/Documents/HAP/unlock.txt','r') as f1:
        unlock=f1.read()

    if flag == 0:
        if unlock == '1':
            pwm1.start(2.5)
            pwm2.start(2.5)
            sleep(2)
            pwm1.stop()
            pwm2.stop()
            flag=1
        else:
            pwm1.start(7.5)
            pwm2.start(7.5)
            sleep(2)
            pwm1.stop()
            pwm2.stop()
            flag=1

    if prevlok != unlock:
        flag=0
    else:
        flag=1

    prevlok=unlock

1 个答案:

答案 0 :(得分:0)

谢谢你的阅读...... 我用以下代码解决了这个问题...... 想知道是否有办法停止pwm或不运行伺服电机以节省回路中的功率,我试图在问题中做...

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

GPIO.setup(40,GPIO.OUT)
GPIO.setup(38,GPIO.OUT)

GPIO.output(40,GPIO.LOW)
GPIO.output(38,GPIO.LOW)

pwm1=GPIO.PWM(40,50)
pwm2=GPIO.PWM(38,50)
pwm1.start(7.5)
pwm2.start(7.5)

flag=1
prevlok=0

while(True):

    with open('/home/pi/Documents/HAP/unlock.txt','r') as f1:
        unlock=f1.read()

    if flag == 1:
        if unlock == '1':
            pwm1.ChangeDutyCycle(2.5)
            pwm2.ChangeDutyCycle(2.5)
            sleep(2)
            flag=0
        else:
            pwm1.ChangeDutyCycle(7.5)
            pwm2.ChangeDutyCycle(7.5)
            sleep(2)
            flag=0

    if prevlok != unlock:
        flag=1
    else:
        flag=0

    prevlok=unlock