如何在主程序中定义的模块中使用变量?

时间:2016-06-21 15:54:03

标签: python

我正在编写一个程序来控制连接到树莓派的两个步进电机的相机。我在python中写作,我在理解全局变量(或python中的名字,我猜)时遇到了麻烦。我已经查看了有关范围和全局变量的一些内容,但我对使用python进行编程真的很陌生,而且它对我来说并不完全有意义。我有一个程序" MainProgram"为变量D_Alt赋值。然后该程序从模块" stepper"调用一个函数。该函数需要使用D_Alt。我将D_Alt设置为主程序中的全局变量,但我仍然收到错误。如何设置D_Alt以便主程序和模块可以使用它?

以下是主程序的代码:

doSomething

这是我需要使用的模块的功能:

from astropy.io import fits
import os
import RPi.GPIO as GPIO
import time
import math
import sys
import stepper

global In_Alt, In_Az, C_Alt, C_Az, D_Alt, D_Az

#start server and get initial position
os.system("indi.sh")
os.system("indi.takepic.sh %s %s" %(15, "InitialPosition"))
os.system("platesolve.sh %s" %("InitialPosition"))
stepper.initPosition()
stepper.initPins()

list_of_stars = [[0,0],[1,1],[2,2]]     #star in list_of_stars is [RA,D]
for star in list_of_stars:
    D_Az=star[0]                        #
    D_Alt=star[1]
    print D_Az
    print D_Alt
    stepper.moveMotor()
    for x in range(0,10):
        os.system("indi.takepic.sh {0}+str(x) .format(list_of_stars[star])")      #take pic with 15s exposure and rename with name of star and number
    stepper.resetPosition()
    os.system("indi.takepic.sh 15 ResetPosition")
    os.system("platesolve.sh ResetPosition")
GPIO.cleanup()
sys.exit(0)

我得到的错误是:

def moveMotor():
    #move altitude motor
    global D_Alt
    result = D_Alt - C_Alt            #find number of steps to move
    if(result>0):                    #if number of steps is positive, move forward
        steps = result
        forward(int(delay) / 1000.0, int(steps), C_Alt)
        C_Alt = C_Alt + steps
    else:                            #if number of steps is negative, move backward
        steps = abs(result)          #steps must be positive, so turn result into positive number
        backwards(int(delay) / 1000.0, int(steps), C_Alt)
        C_Alt = C_Alt - steps

1 个答案:

答案 0 :(得分:1)

global语句是 声明 ,它包含整个当前代码块。这意味着列出的标识符将被解释为全局变量。

您仍然需要在某处定义标识符。这是您得到的错误:global name 'D_Alt' is not defined,您尚未定义变量。

通过在模块根目录中定义变量而不是在函数或其他代码块中,您可以将整个模块作为只读进行访问。但是,要在不同的代码块中修改它,您需要在代码块中将其声明为全局

试试这个:

from astropy.io import fits
import os
import RPi.GPIO as GPIO
import time
import math
import sys
import stepper

# Defining the variables
In_Alt = None
In_Az = None
C_Alt = None
C_Az = None
D_Alt = None
D_Az = None
delay = None

def forward(someArgs):
    .. your implementation ..

def backwards(someArgs):
    ..your implementation..

def moveMotor():
    #move altitude motor
    global D_Alt, C_Alt, delay
    result = D_Alt - C_Alt            #find number of steps to move
    if(result>0):                    #if number of steps is positive, move forward
        steps = result
        forward(int(delay) / 1000.0, int(steps), C_Alt)
        C_Alt = C_Alt + steps
    else:                            #if number of steps is negative, move backward
        steps = abs(result)          #steps must be positive, so turn result into positive number
        backwards(int(delay) / 1000.0, int(steps), C_Alt)
        C_Alt = C_Alt - steps


# start server and get initial position
os.system("indi.sh")
os.system("indi.takepic.sh %s %s" % (15, "InitialPosition"))
os.system("platesolve.sh %s" % ("InitialPosition"))
stepper.initPosition()
stepper.initPins()

list_of_stars = [[0, 0], [1, 1], [2, 2]]  # star in list_of_stars is [RA,D]
for star in list_of_stars:
    D_Az = star[0]  #
    D_Alt = star[1]
    print D_Az
    print D_Alt
    stepper.moveMotor()
    for x in range(0, 10):
        os.system(
            "indi.takepic.sh {0}+str(x) .format(list_of_stars[star])")  # take pic with 15s exposure and rename with name of star and number
    stepper.resetPosition()
    os.system("indi.takepic.sh 15 ResetPosition")
    os.system("platesolve.sh ResetPosition")
GPIO.cleanup()
sys.exit(0)

您可以在此处找到有关global的更多信息:https://docs.python.org/2/reference/simple_stmts.html#global