繁体   English   中英

运行两个python脚本,其中一个需要另一个

[英]Run two python scripts with one needing the other

我有两个都需要运行以执行我想要的Python脚本。 基本上,第一个脚本用于处理POST请求的服务器。 看起来像这样。

from bottle import Bottle, run, template, get, post, request
app = Bottle()

@app.route('/rotation', method='POST')
def set_rotation():
    rotation = request.forms.get('count')
    return rotation

if __name__ == '__main__':
    run(app, host='145.24.226.121', port=8080)

第二个脚本是使步进电机运行。 这是脚本。

# importeer de GPIO bibliotheek.
import RPi.GPIO as GPIO
# Importeer de time biblotheek voor tijdfuncties.
from time import sleep
# Zet de pinmode op Broadcom SOC.
GPIO.setmode(GPIO.BCM)
# Zet waarschuwingen uit.
GPIO.setwarnings(False)
# Stel de GPIO pinnen in voor de stappenmotor:
StepPins = [4,17,27,22]

# Set alle pinnen als uitgang.
for pin in StepPins:
  print "Setup pins"
  GPIO.setup(pin,GPIO.OUT)
  GPIO.output(pin, False)

# Definieer variabelen.
StepCounter = 0

# Definieer simpele volgorde
StepCount1 = 4
Seq1 = []
Seq1 = range(0, StepCount1)
Seq1[0] = [1,0,0,0]
Seq1[1] = [0,1,0,0]
Seq1[2] = [0,0,1,0]
Seq1[3] = [0,0,0,1]

# Definieer geadvanceerde volgorde (volgens de datasheet)
StepCount2 = 8
Seq2 = []
Seq2 = range(0, StepCount2)
Seq2[0] = [1,0,0,0]
Seq2[1] = [1,1,0,0]
Seq2[2] = [0,1,0,0]
Seq2[3] = [0,1,1,0]
Seq2[4] = [0,0,1,0]
Seq2[5] = [0,0,1,1]
Seq2[6] = [0,0,0,1]
Seq2[7] = [1,0,0,1]

# Welke stappenvolgorde gaan we hanteren?
Seq = Seq2
StepCount = StepCount2

try:
  while True:
    for pin in range(0, 4):
      xpin = StepPins[pin]
      if Seq[StepCounter][pin]!=0:
        print "Stap: %i GPIO Actief: %i" %(StepCounter,xpin)
        GPIO.output(xpin, True)
      else:
        GPIO.output(xpin, False)

    StepCounter += 1

    # Als we aan het einde van de stappenvolgorde zijn beland start dan opnieuw
    if (StepCounter==StepCount): StepCounter = 0
    if (StepCounter<0): StepCounter = StepCount

    # Wacht voor de volgende stap (lager = snellere draaisnelheid)
    sleep(.01)

except KeyboardInterrupt:  
  # GPIO netjes afsluiten
  GPIO.cleanup()

我需要第二个脚本中sleep函数中第一个脚本的旋转值。 这两个脚本都需要运行,因此第一个脚本可以接收POST请求,第二个脚本可以使步进电机工作。 最好的方法是什么,这种方法甚至可能吗?

也许您可以在第一个脚本中调用控制电动机的脚本,

或者如果您在从http脚本直接调用python函数时遇到问题,则可以使用subprocess( https://docs.python.org/2/library/subprocess.html )或类似的方法调用trought shell

我希望你能很好地理解你的问题

或者您可以合并两个脚本,例如

从瓶子进口瓶子,运行,模板,获取,发布,请求

# importeer de GPIO bibliotheek.
import RPi.GPIO as GPIO
# Importeer de time biblotheek voor tijdfuncties.
from time import sleep

app = Bottle()


@app.route('/rotation', method='POST')
def set_rotation():
    rotation = request.forms.get('count')
    rotate(rotation_to_do=rotation)
    return rotation


StepPins = [4, 17, 27, 22]


def inizialize_pin():
    # Zet de pinmode op Broadcom SOC.
    GPIO.setmode(GPIO.BCM)
    # Zet waarschuwingen uit.
    GPIO.setwarnings(False)
    # Stel de GPIO pinnen in voor de stappenmotor:

    # Set alle pinnen als uitgang.
    for pin in StepPins:
        print "Setup pins"
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, False)

# Definieer variabelen.
StepCounter = 0

# Definieer simpele volgorde
StepCount1 = 4
Seq1 = []
Seq1 = range(0, StepCount1)
Seq1[0] = [1, 0, 0, 0]
Seq1[1] = [0, 1, 0, 0]
Seq1[2] = [0, 0, 1, 0]
Seq1[3] = [0, 0, 0, 1]

# Definieer geadvanceerde volgorde (volgens de datasheet)
StepCount2 = 8
Seq2 = []
Seq2 = range(0, StepCount2)
Seq2[0] = [1, 0, 0, 0]
Seq2[1] = [1, 1, 0, 0]
Seq2[2] = [0, 1, 0, 0]
Seq2[3] = [0, 1, 1, 0]
Seq2[4] = [0, 0, 1, 0]
Seq2[5] = [0, 0, 1, 1]
Seq2[6] = [0, 0, 0, 1]
Seq2[7] = [1, 0, 0, 1]

# Welke stappenvolgorde gaan we hanteren?
Seq = Seq2
StepCount = StepCount2


# Definieer variabelen.
StepCounter = 0


def rotate(rotation_to_do=0):
    try:
        while rotation_to_do > 1:
            for pin in range(0, 4):
                xpin = StepPins[pin]
                if Seq[StepCounter][pin] != 0:
                    print "Stap: %i GPIO Actief: %i" % (StepCounter, xpin)
                    GPIO.output(xpin, True)
            else:
                GPIO.output(xpin, False)

            global StepCounter += 1

            # Als we aan het einde van de stappenvolgorde zijn beland start dan opnieuw
            if (StepCounter == StepCount):
                StepCounter = 0
                if (StepCounter < 0):
                    StepCounter = StepCount

            # Wacht voor de volgende stap (lager = snellere draaisnelheid)
            sleep(.01)
            rotation_to_do -= 1

    except KeyboardInterrupt:
        # GPIO netjes afsluiten
        GPIO.cleanup()


if __name__ == '__main__':
    inizialize_pin()
    run(app, host='145.24.226.121', port=8080)

我当时正在考虑将PI用于类似的事情,收集数据,然后将其发送到另一台服务器。

还没到那儿,但是初步的想法是使用pythons多线程功能

查看这些链接

如何在Python中使用线程?

http://www.tutorialspoint.com/python/python_multithreading.htm

我很想知道你过得如何

暂无
暂无

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

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