我正在尝试用Python实现机器人的状态机。 机器人的行为如下: 当开关关闭时,机器人什么都不做;
开关打开时: 默认情况下,机器人向前移动
机器人不断地从光传感器读取并使用它来跟踪地板上代表道路的线路#34;。当传感器读数为40或更低时,传感器不在路上
如果右侧的传感器离开道路,机器人将右侧车轮加速至75%直至返回道路,然后将右侧电机返回至50%;
如果左侧的传感器离开道路,机器人将左轮加速到75%直到它返回道路,然后它将左侧电机返回到50%。
如果碰撞传感器被触发,机器人以100%的速度后退1秒,然后顺时针旋转180度,并在两个电机上再次以50%的速度向前移动。
我的代码是:
Robot = input("Turn on or off: ")
if Robot == "off":
print("stayed off")
else:
print("I am on and moving")
while Robot:
Robot= int(input("enter shine number. or turn off: "))
if Robot=="turn off": ## THE problem is turn off is not a number so this is not working.
break
if Robot >= 40:
print("I stay on the road")
elif Robot <40:
Robot =input("right sensor leaves the road or the left? ")
if Robot== "right":
print("I speed up the right wheel to 75% until I return to the road, then I return the right motor to 50%")
if Robot=="left":
print("I speed up the left wheel to 75% until I return to the road, then I return the left motor to 50%.")
Robot= input("trigger bumper sensor? or turn off: ")
if Robot== "trigger":
print("I back up at 100% speed for 1 second, then rotate clockwise 180 degrees and start moving forward again at 50% on both motors.")
if Robot=="dont trigger":
print("I stay on the road")
if Robot=="turn off":
break
如何改进此代码,了解机器人的状态是什么?