如何正确地将变量传递给方法? (python)

How to pass variable to method properly? (python)

我正在尝试清理我的代码,但我是 python 的新手,所以我很确定我的语法是错误的。我编写了一个简单的控制器,使变量 message_lx_setPoint 接近设定点的值 message_lx_setPoint。我正在使用 机器人操作系统 所以脚本正在循环。事实上,它工作正常,message_lx_current 的值接近 setPoint 的值。下面是我为此编写的代码:

#defs
increment = 0.1

#setPoint values
message_lx_setPoint = 5.0 #value set by some controller(?)
message_ly_setPoint = 5.0

#current values
message_lx_current = 0.0 #value set by some sensor(?)
message_ly_current = 0.0

def approach_lx_setPoint():
    global message_lx_current, message_lx_setPoint, increment

    if message_lx_current < message_lx_setPoint:
        print("current is less than set point")
        message_lx_current += increment

    elif message_lx_current == message_lx_setPoint:
        print("current is equal to set point")
        message_lx_current = 0.0

    elif message_lx_current > message_lx_setPoint:
        print("current is greater than set point")
        message_lx_current -= increment

    return message_lx_current

def approach_ly_setPoint():
    global message_ly_current, message_ly_setPoint, increment

    if message_ly_current < message_ly_setPoint:
        print("current is less than set point")
        message_ly_current += increment

    elif message_ly_current == message_ly_setPoint:
        print("current is equal to set point")
        message_ly_current = 0.0

    elif message_ly_current > message_ly_setPoint:
        print("current is greater than set point")
        message_ly_current -= increment

    return message_ly_current

def main():
    approach_lx_setPoint()
    approach_ly_setPoint()
    print(message_lx_current)
    print(message_ly_current)

我必须为 6 个不同的变量执行此操作,因此我的代码开始变得臃肿;我必须总共制作 6 种方法来控制所有 6 个变量。我想这样做,以便我只制作一个 single 方法,我传递要控制的变量并将变量作为设定点并从那里执行。我在想这样的事情:

#same variables as before

def approachSetPoint(current, target):
    global increment

    if current < target:
        print("current is less than set point")
        current += increment

    elif current == target:
        print("current is equal to set point")
        current = 0.0

    elif current > target:
        print("current is greater than set point")
        current -= increment

    return current

def main():
    approachSetPoint(message_lx_current, message_lx_setPoint)
    approachSetPoint(message_ly_current, message_ly_setPoint)
    print(message_lx_current)
    print(message_ly_current)

我希望这样做的结果是,我不会为每个要控制的变量设置一个 approachSetPoint def,而是只有 one approachSetPoint def,我所要做的就是在 main 中使用适当的变量调用它,它就会完成任务。

我试过了,没有错误或任何错误,但是,值没有更新(?)。我假设我只是没有正确地将我作为参数传递的变量链接到方法中修改的变量。知道如何处理我描述的行为吗?提前致谢!

您需要在运行之间将结果存储在函数之外:

message_lx_current = y_current = 0.0
message_lx_setpoint = message_ly_setpoint = 5.0

def approach_setpoint(current, setpoint, increment=0.1):
    if current < setpoint:
        print("current is less than set point")
        current += increment
    elif current == setpoint:
        print("current is equal to set point")
        current = 0.0
    elif current > setpoint:
        print("current is greater than set point")
        current -= increment
    return current

def main():
    message_lx_current = approach_setpoint(message_lx_current, message_lx_setpoint)
    message_ly_current = approach_setpoint(message_ly_current, message_ly_setpoint)
    print(message_lx_current)
    print(message_ly_current)