我有两个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)
第二个脚本是运行steppermotor。这是剧本。
# 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请求,第二个脚本可以使steppermotor工作。 这样做的最佳方法是什么?这种方法是否可行?
答案 0 :(得分:0)
也许你可以在第一个脚本中调用控制电机的脚本,
或 如果你有问题从http脚本直接调用python函数 你可以用子进程(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)
答案 1 :(得分:0)
我正在考虑将PI用于类似的东西,收集数据然后将其发送到另一台服务器。
Haven尚未到达,但初步想法是使用pythons多线程能力
查看这些链接
How to use threading in Python?
http://www.tutorialspoint.com/python/python_multithreading.htm
我很想知道你如何上场