Python 需要帮助检测while循环外变量的变化吗

Python 需要帮助检测while循环外变量的变化吗,python,while-loop,break,robot,Python,While Loop,Break,Robot,我使用python创建了一个由raspberry pi控制的机器人,并使用pygame的xbox 360控制器来读取输入 我有它,所以你越是按下扳机,它的加速速度就越快,有点像油门踏板或赛车电子游戏。 但我还想加入一个功能,这样如果我完全扣动扳机,机器人目前处于静止状态,它就会慢慢爬到最高速度。我用一个while循环成功地实现了这一点,但现在我认为在机器人达到最高速度时停止它也很重要,以防我需要它。但到目前为止,我还不知道怎么做。发生的事情是,它保持它试图加速到的初始变量,直到它达到那个值,然后

我使用python创建了一个由raspberry pi控制的机器人,并使用pygame的xbox 360控制器来读取输入

我有它,所以你越是按下扳机,它的加速速度就越快,有点像油门踏板或赛车电子游戏。 但我还想加入一个功能,这样如果我完全扣动扳机,机器人目前处于静止状态,它就会慢慢爬到最高速度。我用一个while循环成功地实现了这一点,但现在我认为在机器人达到最高速度时停止它也很重要,以防我需要它。但到目前为止,我还不知道怎么做。发生的事情是,它保持它试图加速到的初始变量,直到它达到那个值,然后它会注意到我已经放开触发器并停止

如果我在循环中添加一个中断,它会中断,但是如果我保持触发器,因为我希望它继续加速,那么循环会停止,但不会继续,因为程序没有看到触发器值的变化,所以它什么也不做,所以它不会提高速度

这是我脚本的一部分。谢谢你抽出时间

if event.type == JOYAXISMOTION:
#drive forward with right trigger.  (negative value is RT, positive value is LT)
    if event.axis == 2:
        direction = ""
        if event.value > .2: #.2 threshold for trigger movement
        #this is for reverse!
            direction = "back"
            #times by  110 to get close to full speed, absolute value, and round to whole number
            magnitude = ("%.0f" % abs(event.value * 110))  

            ##########
            #this is the code that give the robot the ability to climb to its top speed
            #s = the desired speed( the amount your pulling the trigger
            global s
            s = int(magnitude)  #the code gave a error that magnitude was a string. this turns it to a integer                                    
            if xromp <= (s - 25): #this is just so the script doesnt get in the way for little trigger adjustments.
                #it will skip to the else portion and work just as if this part wasnt implemented
                while xromp in range(0,s):  #xromp is the current climbing speed
                    xromp += 10  #increase by 10
                    print "speed increasing backwards:", + xromp   
                    sleep(1) #the wait time to increase the speed
                    self.joystickEvent(xromp, direction, 2) #here we actualy set the speed and direction and transmit it to the main script to send to raspberry


                else:
                    xromp = s  #so if the current speed was already close to the desiered speed ( for small trigger changes); just set equal to eachother
                    self.joystickEvent(xromp, direction, 2)
        elif event.value < -.2:
            #this is forward!
            direction = "forward"
            magnitude = ("%.0f" % abs(event.value * 110))  

            s = int(magnitude)

            if xromp <= (s - 25):
                #while xromp in range(0,s):
                    #curpos = ""
                    #event.value = curpos
                    xromp += 10
                    print "speed increasing:", + xromp   
                    sleep(1)
                    print s

            else:
                xromp = s
                self.joystickEvent(xromp, direction, 2) 
        else:
            #turn off motors when trigger is released                        
            control = "stopWheels"
            xromp = 0  #sets current speed to 0 for the speed climb script
            print "stopping"
            self.btnEvent(control)
如果event.type==JOYAXISMOTION:
#用右扳机向前行驶。(负值为RT,正值为LT)
如果event.axis==2:
方向=“”
如果event.value>.2:#.2触发器移动的阈值
#这是反向的!
方向=“后退”
#乘以110以接近全速、绝对值,并四舍五入到整数
幅值=(“%.0f”%abs(event.value*110))
##########
#这是赋予机器人爬到最高速度能力的代码
#s=所需速度(扳机量
全球s
s=int(震级)#代码给出了一个错误,认为震级是字符串。这将它转换为整数

如果xromp我还没有深入研究您的代码,但根据您的描述,我假设您的伪代码如下:

if GO_TO_MAX_SPEED_CONDITION:
    while NOT_AT_MAX_SPEED:
        ACCELERATE
if GO_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = True
if STOP_GOING_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = False
if GO_TO_MAX_SPEED and NOT_AT_MAX_SPEED:
    ACCELERATE
我建议改变你的策略如下:

if GO_TO_MAX_SPEED_CONDITION:
    while NOT_AT_MAX_SPEED:
        ACCELERATE
if GO_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = True
if STOP_GOING_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = False
if GO_TO_MAX_SPEED and NOT_AT_MAX_SPEED:
    ACCELERATE
然后在程序的每次迭代中,您都会遇到如下情况:

if GO_TO_MAX_SPEED_CONDITION:
    while NOT_AT_MAX_SPEED:
        ACCELERATE
if GO_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = True
if STOP_GOING_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = False
if GO_TO_MAX_SPEED and NOT_AT_MAX_SPEED:
    ACCELERATE

仅供参考,如果您在显示问题的同时尽可能简化代码,您更有可能得到快速响应。这是一个好主意,但我认为即使我合并了此方法,它也会起到相同的作用,因为一旦触发器一直按下,脚本将不会再次运行,因为它需要我执行某些操作我和控制器打了个招呼,但是因为它已经被按住了,所以它没有反应。但是现在当我打这个的时候,我开始有想法了,所以我必须尝试一下!谢谢你的洞察力!