机器人 - 分形的递归函数。

时间:2014-11-14 17:44:07

标签: python recursion robotics myro calico

我目前正在使用机器人技术与Myro / Calico合作。我试图运行分形的递归函数。我使用的是Python。

我一直在这里关注伪代码。 Fractal

到目前为止,我已尝试在没有递归的情况下实现第一步。它运行良好

# 1 foot per 2 seconds.  x * 2 = feet desired.  
def fractal(x):
    waitTime = x*2
    turnRight(1, 0.825) #90 degree turn
    forward(1, x/3) #move length/3 steps
    turnLeft(1, 0.55)#60 degree turn
    forward(1, x/3) #move length/3 steps
    turnRight(1, 1.1) #120 degree turn
    forward(1, x/3) #move length/3 steps
    turnLeft(1, 0.55) #60 degree turn
    forward(1, x/3) #move length/3 steps

虽然这是有效的,但我的目标是递归地执行此操作,但在每次迭代时制作一条较小的曲线。我试图这样做,但我的机器人并没有按照需要移动。

这是我的递归尝试

def fractal(x):
    waitTime = x*2
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnLeft(1,0.55) #60 degrees
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnRight(1, 1.1) #120 degree turn
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnLeft(1, 0.55)#60 degree turn
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3

我的机器人只是向左和向右转,但它没有完全成型。没有递归的那个开始了分形。我只需要递归来完成整个分形。

1 个答案:

答案 0 :(得分:1)

我在想这就是你想做的事情

x = number of interations
l = lenth(wait time)
def fractal(x, l):
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnLeft(1,0.55) #60 degrees
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnRight(1, 1.1) #120 degree turn
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnLeft(1, 0.55)#60 degree turn
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)