简体   繁体   English

尽管多次调用函数,如何保持变量的值呢?

[英]How to keep the value of a variable despite function being called multiple times?

I have a python function that will turn the servo to the left or right everytime a button on the web interface is clicked. 我有一个python函数,每次单击Web界面上的按钮时,该函数都会将伺服器向左或向右旋转。 Currently it is working like this: 目前它正在这样工作:

@cherrypy.expose
def turnCamera (self, **data):

    import pigpio
    import time

    servos=[4,7] #GPIO number
    pigpio.start()
    key = data['direction']
    if key=="left":
            servostatus = "left"
            print servostatus
            m=1500

    elif key=="right":
            servostatus = "right"
            print servostatus
            m=1000

    #to dispense pill
    if key=="dispense":
        m=900
        pigpio.set_servo_pulsewidth(servos[0], m) 
        servostat= "Position of servo: %s"%m
        print servostat
        time.sleep(1)
        m=1500
        pigpio.set_servo_pulsewidth(servos[0], m) 
        servostat= "Position of servo: %s"%m
        print servostat
        time.sleep(1)


    pigpio.stop()

    return servostat

The data is posted from jQuery: 数据是从jQuery发布的:

$('#left_button').click(function(){
                        $.post('/turnCamera', {direction:"left"}).done(function (reply) {
                            $('#camerapos').empty().append(reply);
                            alert("left button clicked");});

                    });
                    $('#right_button').click(function(){
                        $.post('/turnCamera', {direction:"right"}).done(function (reply) {
                            $('#camerapos').empty().append(reply);
                            alert("right button clicked");});
                    });

However I would like the servo to turn such that each click will make it turns by 100. When the script is first run, it goes to initial position m=1500 . 但是,我希望伺服器转动,使每次单击都可以使它转动100。第一次运行脚本时,它将转到初始位置m=1500 Then user can control the camera position by clicking left or right. 然后,用户可以通过单击左或右来控制摄像机的位置。 Something like this: 像这样:

m=1500
while (m >= 500 and m <= 2500):
        if (key =="left"):
            m=m+100
        elif (key =="right"):
            m=m-100

However i do understand that this will not work because m will get reset to 1500 everytime the function is triggered (button is clicked). 但是我确实知道这是行不通的,因为每次触发该功能(单击按钮)时,m都会重置为1500。 How can i store the value of m? 如何存储m的值?

You can make the method 你可以做方法

return [servostat, m]

and then change the method so that it has a parameter where you pass in the m from the previous call like def turnCamera(self, **data, m): 然后更改方法,使其具有一个参数,您可以在其中传递上一个调用中的m,例如def turnCamera(self, **data, m):

As follows: 如下:

$('#left_button').click(function(){
                $.post('/turnCamera', {direction:"left",m_value:m}).done(function (reply) {
                    $('#camerapos').empty().append(reply);
                    alert("left button clicked");});

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

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