Im creating a robot controlled by raspberry pi using python and controlling it with a xbox 360 controller using pygame to read the inputs.

I have it so it accelerates faster the more you hold down the trigger, kind of like a gas pedal or racing video games. but I also wanted to include a feature so that if I pull the trigger fully and the robot is currently at rest, it would slowly climb to its top speed. I've managed to achieve that using a while loop but now I figured it's important to also stop the robot while it's climbing to its top speed incase I need it to. but as of now, I can't figure out how to do it. what's happening is that, it keeps the initial variable that its trying to speed up to, and until it reaches that value, then it will notice that I have let go of the trigger and stop.

if I add a break in that loop, it would break, but if I keep the trigger held because I want it to keep speeding up, the loop would stop, but would not continue because the program doesn't see a change in the trigger value so it just doesn't do anything, so it would not increase it's speed.

here is the section of my script. Thank you for your time.

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)
有帮助吗?

解决方案

I haven't dug through your code yet, but from your description I assume your pseudo-code is like this:

if GO_TO_MAX_SPEED_CONDITION:
    while NOT_AT_MAX_SPEED:
        ACCELERATE

I'd suggest changing your strategy to be like this:

if GO_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = True
if STOP_GOING_TO_MAX_SPEED_CONDITION:
    GO_TO_MAX_SPEED = False

then at each iteration of your program, you'd have something like this:

if GO_TO_MAX_SPEED and NOT_AT_MAX_SPEED:
    ACCELERATE
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top