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

问题描述 投票:0回答:1

我正在使用 python 创建一个由树莓派控制的机器人,并使用 xbox 360 控制器使用 pygame 来控制它来读取输入。

我有它,所以你按住扳机越多,它加速得越快,有点像油门踏板或赛车电子游戏。 但我还想添加一个功能,这样如果我完全扣动扳机并且机器人当前处于静止状态,它会慢慢爬升至最高速度。我已经成功地使用 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)
python while-loop break
1个回答
1
投票

我还没有深入研究你的代码,但从你的描述我假设你的伪代码是这样的:

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
© www.soinside.com 2019 - 2024. All rights reserved.