Python 与机器人并行处理

Parallel Processing in Python with Robot

我编写了一个机器人,它运行的模块包含执行特定过程所需的命令。截至目前,用户输入所需的命令,我的代码使用 if 语句来确定他们输入的命令,然后运行适当的命令。但是,该命令必须先完成,用户才能输入另一个命令。

现在我想执行以下操作:让用户输入命令,启动命令,然后在命令为 运行 时重新运行命令以获取用户输入。 例如,用户输入向前移动,机器人开始移动,然后用户在机器人向前移动的中途更改命令向后移动,机器人重置并开始向后移动作为响应。

下面是运行模块并要求用户输入的 while 循环代码。如果您对我如何实现此目标有任何想法,或者您是否需要任何说明,请告诉我。我是一名仍在学习如何编码的高中生,所以任何帮助将不胜感激。

提前致谢。

最佳,

克里斯托弗

#runs all the modules and gets user input
while True: 
    defultPosition()
    command = raw_input("Enter move forward, move backward, turn or cancel: ")
    defultPosition()
    if command == "cancel":
        break 
    if command == ("move forward") or (command == "move backward"):
        speedInput = input("Enter the desired speed: ")
        distanceInput = input("Enter the number of inches you wish the robot to move (must be a factor of 5): ")
    if command == "turn":
        speedInput = input("Enter the desired speed: ")
        degrees = input("Enter the number of degrees for the robot to move: ")

    print ("\nINPUTED COMMAND: %s \n" % command)

    if command == "move forward":
        #run the moveForward module

        print "Initiating command\n"

        moveForward(speedInput, distanceInput)

        print "Finished command; restarting and waiting for another input \n"

    if command == "move backward":
        #run the moveBackward module

        print "Initiating command\n"

        moveBackward(speedInput, distanceInput)

        print "Finished command; restarting and waiting for another input \n"

    if command == "turn":
        #runs the turn module

        print "Initiating command\n"

        turnCounterClockwise(speedInput, degrees)

        print "Finished command; restarting and waiting for another input \n" 

我确定使用线程是解决我的问题的最佳方法。它能够在程序中的任何一点发送终止信号,并且它将使用新参数重新启动线程。

如果有人想查看我使用的代码,请告诉我。