简体   繁体   English

运行 python 脚本后将 rpi-gpio 引脚设置为高电平

[英]setting rpi-gpio pin on high after running a python script

I'm building a photogrametry setup with raspberry pi and stepper motor.我正在使用树莓派和步进电机构建摄影测量设置。 The python script runs fine, but i got a problem with setting a pin to high after the script ran through. python脚本运行良好,但在脚本运行后我将引脚设置为高电平时遇到问题。

The stepper driver has an enable input, which diasables the motor with high input, so i set the pin (gpio26) on high on boot with pigpio, this works fine.步进驱动器有一个使能输入,它用高输入禁用电机,所以我用 pigpio 启动时将引脚(gpio26)设置为高,这工作正常。 While running the python script, the pin is set on low, so the stepper can proceed, after proceeding i want to set the pin on high again.在运行 python 脚本时,引脚设置为低电平,因此步进器可以继续,继续后我想再次将引脚设置为高电平。

i tried following commands:我尝试了以下命令:

os.system('pigs w 26 1') and subprocess.call("pigs w 26 1", shell=True) os.system('pigs w 26 1')和 subprocess.call( subprocess.call("pigs w 26 1", shell=True)

for a moment they work, but after exiting the script the pin is set on low again.他们工作了一会儿,但退出脚本后,引脚再次设置为低电平。 It's like the commands are resetted after the script stops.就像脚本停止后命令被重置一样。

Where is my fault?我的错在哪里?

Thank you谢谢

Edit: Here is the gpio related code:编辑:这是gpio相关代码:

import os, sys
import subprocess
from time import sleep
from gpiozero import DigitalOutputDevice as stepper

def InitGPIO():
    try:
        global step_pul                     #pulse
        global step_en                      #enable
        step_pul=stepper(21)
        step_en=stepper(26)
        print ("GPIO initialisiert.")
    except:
        print ("Fehler bei der GPIO-Initialisierung.")
        
def motor_step():
    SPR=40000           #steps per rotation
    step_count = SPR
    delay = .000025
    for x in range(step_count):
        step_pul.on()
        sleep(delay)
        step_pul.off()
        sleep(delay)
        
InitGPIO()
step_en.off()

for i in range(1):
    #camTrigger(1)
    motor_step()   

#os.system('sudo -u root -S pigs w 26 1')
subprocess.call("pigs w 26 1", shell=True)

When i type pigs w 26 1 in the shell, it works...当我在 shell 中输入pigs w 26 1时,它可以工作......

To make my comment an answer:为了让我的评论成为答案:

Gpiozero only resets the pins it touches, so if you don't initialize or touch pin 26 with gpiozero (ie replace step_en.off() with pigs w 26 0 and don't even initialize step_en), it shouldn't also reset the pin: Gpiozero 仅重置它接触的引脚,因此如果您不使用 gpiozero 初始化或接触引脚 26(即用pigs w 26 0替换step_en.off()并且甚至不初始化 step_en),它也不应该重置别针:

import os
import time

import gpiozero

step_pul = gpiozero.DigitalOutputDevice(21)


def motor_step():
    SPR = 40000  # steps per rotation
    step_count = SPR
    delay = .000025
    for x in range(step_count):
        step_pul.on()
        time.sleep(delay)
        step_pul.off()
        time.sleep(delay)


# Enable motor
os.system("pigs w 26 0")

for i in range(1):
    # camTrigger(1)
    motor_step()

# Disable motor
os.system("pigs w 26 1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM