我使用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)
答案 0 :(得分: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