Welcome! Log In Create A New Profile

Advanced

z=z+pitch doesn't work in for-loop? [solved]

Posted by o_lampe 
z=z+pitch doesn't work in for-loop? [solved]
November 18, 2016 10:20AM
I tried to use openScad to generate coordinates for a helix, but the simple z=z+pitch term doesn't work.

What am I doing wrong?

//generating coordinates for a helix
radius=100;
height=150;
num_circles=10;
step=10;
pitch=height/(num_circles*(360/step));
z=0;
echo(pitch=pitch);
echo("G1 F2000 Z1");
for(i=[1:1:num_circles]){
    for(angle=[0:step:360-step]){
    echo("X",radius*sin(angle)," Y",radius*cos(angle),"Z",z);
    z=z+pitch;
  }
}

Edited 1 time(s). Last edit at 11/19/2016 06:13AM by o_lampe.
Re: z=z+pitch doesn't work in for-loop?
November 18, 2016 11:24AM
This might explain it: [en.wikibooks.org]
Re: z=z+pitch doesn't work in for-loop?
November 18, 2016 11:27AM
TLDR:
OpenSCAD "variables" don't change value, except for loop control variables.

Try
echo("X",radius*sin(angle)," Y",radius*cos(angle),"Z",(i-1)*pitch);

Long story: More accurately, a for (i in [1:1:num_circles]) "loop" is actually saying execute this block num_circles-1 times with a different value for i in each execution. So, in fact, loop control variables also don't change value. The executions are conceptually in parallel (so not actually a "loop"), so a variable can't depend on the value of something (itself) in another execution of the block.

Edited 2 time(s). Last edit at 11/18/2016 11:40AM by frankvdh.
Re: z=z+pitch doesn't work in for-loop?
November 18, 2016 12:47PM
Thanks Frank,
I think I get the idea. Instead of calculating pitch before the for loops start, I must calculate "z" in the loop based on the I and step values..
Re: z=z+pitch doesn't work in for-loop?
November 19, 2016 06:08AM
Ok, found a workaround this morning. Quite different from z=z+pitch, but it works.
I also added a cone-like shape and made it loop eternally. ( for showcase reasons )

...
    echo("X",radius*sin(angle)," Y",radius*cos(angle),"Z",
       
       (i-1)*360/step*pitch+(angle/step)*pitch     );
   ...

If anyone is interested to use it for testing, the gcode file requires 200mm printbed diameter and 150mm printheight ( at 200mm, not just in the center )
Or you can modify the .scad file and edit the console output to become a gcode file.
Attachments:
open | download - helix_cone.gcode (15.8 KB)
open | download - gcode_helixgenerator.scad (654 bytes)
open | download - helix_cone.JPG (54 KB)
Sorry, only registered users may post in this forum.

Click here to login