我目前正在与Myro/Calico和Robotics合作。 我正在尝试运行分形的递归函数。我正在使用Python。
我一直在这里遵循伪代码。分形
到目前为止,我已经尝试在没有递归的情况下实现第一步。 而且运行良好
# 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
我的机器人只是左右转动,但它并没有形成完整的形状。 没有递归的那个开始分形。 我只需要递归来遍历整个分形。
我想这就是你想做的
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)