user3577397 - 6 months ago 37

Python Question

I'm currently working with Myro/Calico with Robotics. I'm trying to run a recursive function of a fractal. I'm using Python.

I've been following pseudocode here.

Fractal

So far I've tried to implement the first step without recursion. And it runs well

`# 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

While this works, my goal is to go through this recursively, but make a smaller curve at each iteration. I attempted to do it, but my robot isn't moving as desired.

Here's my attempt at recursion

`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

My robot is only turning left and right, but it's not making the full shape. The one without the recursion started the fractal. I just need recursion to go through the whole fractal.

Answer

I'm thinking this is what you want to do

```
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)
```