openscad variables can't be assigned inside an if?

626 views
Skip to first unread message

James Newton

unread,
Nov 28, 2012, 11:09:04 AM11/28/12
to makerspace...@googlegroups.com
Ok, 3D printing people, it's time for a newbie question about openscad: Why can't openscad manage this:
flangeHeight = 0;
if(flanges==1) {
flangeHeight = 2;
 }
Throws a compile error if you try to set a variable inside an if... 

More to the point. If you wanted to select a nut size based on the diameter of a shaft, how would you do that? lookup is going to interpolate which is not going to give valid sizes of nuts.

Richard Rouse

unread,
Nov 28, 2012, 11:28:00 AM11/28/12
to makerspace...@googlegroups.com
I use shapes.scad (library) and draw a hexagon then make a hole in the middle

include <shapes.scad>

//here's approx m5 settings

difference(){
hexagon(5.5,3);
cylinder(r=5.4, 4);
--
Richard Rouse
HTS Resources, LLC.
6450 Lusk Blvd #E103
San Diego CA 92121
619-846-8291
rjdr...@htsresources.com
www.htsresources.com

James Newton

unread,
Nov 28, 2012, 1:55:02 PM11/28/12
to makerspace...@googlegroups.com
Thanks for that information, but I guess I'm not being clear. I have a parametric file for a timing belt pulley:
http://www.thingiverse.com/thing:23992
It allows a space for a nut to be inserted to hold the pulley onto a threaded rod when the pulley is big enough to support it. The users of this file can specify the diameter of the rod (the shaft) as one of the parameters. The script is expected to then include space for a nut which would fit that shaft. Currently, it is hard coded to an M8 nut. I'd like to update the script so that it will automatically select a standard nut size which is appropriate for the shaft size. E.g. if the shaft diameter is between x and y, then use a z size nut. 

I'm not seeing a way to do this in openscad. 

Moreover, it just blows me away that 1) you apparently just can't make an assignment inside an "if" block. 2) that fact isn't documented or explained anywhere that I can find. 

Actually... it is documented now, as I've just updated the wikibook manual to document it. It is apparently a byproduct of the fact that variables are assigned at compile time not runtime:
http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General
so the conditional reassignment of a variables value is not supported. 

Which still leaves me wondering how I'm going to support multiple nut sizes... I guess I have to change my code to call a routine which then has the if's which then call another routine with hard coded numbers for the nut sizes.

Richard Rouse

unread,
Nov 28, 2012, 5:47:15 PM11/28/12
to makerspace...@googlegroups.com
Not sure but that is a cool pulley which we use actually from time to time.

When I have run into assigning variables in a loop in past and run
into a problem like this I was able to solve by creating a module
where to pass variables and return value. Not sure if this will help
you though.

Richard

On Wed, Nov 28, 2012 at 10:55 AM, James Newton

James Newton

unread,
Nov 29, 2012, 1:46:25 PM11/29/12
to makerspace...@googlegroups.com
Glad you got some use out of it. I have a newer version that adds the ability to include a flat in the pulley center hole to match a flat on a motor shaft. Let me know if that or any other features are useful.

Yes, I think that modular approach is exactly what I will have to go to.

Richard Rouse

unread,
Nov 29, 2012, 6:31:21 PM11/29/12
to makerspace...@googlegroups.com
Well our problem right now is printing to fit a GT2 belt, we are using
3mm filament. Maybe I can try 1.75mm PLA using 0.5mm nozzle orifice, I
have other smaller nozzles but too lazy to put them on for this print
but may have too

Curious have you tried GT2 belt? If yes, what conditions did you use
to get good ones.

thanx

On Thu, Nov 29, 2012 at 10:46 AM, James Newton

James Newton

unread,
Dec 1, 2012, 1:12:07 AM12/1/12
to makerspace...@googlegroups.com
Believe it or not Richard, I've never 3d printed a single thing in my life and don't own a 3d printer. I have an XY table for testing and a sort of a rep-strap I'm working on with a drill press. I'm an embedded programmer, and I make and sell stepper motor drivers, but that's as close as I get. The pully design I'm polishing is to make a better Z axis for my rep-strap if I can get someone to print it for me once I have it finished.
 
What goes wrong with the GT2 belt? Too fine for the thicker filament?

Richard Rouse

unread,
Dec 1, 2012, 9:50:36 AM12/1/12
to makerspace...@googlegroups.com
Yeah its not easy for printing a GT2 belt. Its great for T5. But there
are many parameters I havent' tweaked ... maybe its possible. We have
1.75mm filament, if necessary we can set up a smaller nozzle and there
is this new sailfish firmware I havent' tried yet.

That's cool though that you don't even print but yet make a one of the
best pulley openscads. Anyway if you need help getting started, we
have some printers, random reprap stuff and building more.

We are definitely interested in seeing if can use your boards if you
sell that stuff.

Richard

James Newton

unread,
Dec 2, 2012, 1:38:08 AM12/2/12
to makerspace...@googlegroups.com
You will love the next version. It now supports multiple shaft / nut sizes, shafts with flats, and allows multiple pulleys in one file. 

I'm working on adding the ability to calculate the size of the second pulley based on a desired ratio. E.g., you have a minimum sized pulley for a given motor shaft, and you want a 1:4 ratio, so it just makes the second pulley for you.

Maybe I should release this one as is and then add in the ratio generation in the next version?

What exactly goes wrong with the GT2 belt when you try to print it? Just too rough? Have you tried different tooth types?

Richard Rouse

unread,
Dec 2, 2012, 9:56:00 AM12/2/12
to makerspace...@googlegroups.com
It would be cool to have a lego motor interface too ... on this library

The GT2 pulley doesn't catch the belt the teeth are too fine but we
haven't tried 1.75mm filament and I have some. Ended up ordering some
pulleys since right now we are tearing larger models and I like the
3mm since its fast and accurate just can't do small gt2 pulleys in my
hands. Plus buying ones are more accurate just expensive when you add
them up.

James Newton

unread,
Dec 2, 2012, 2:29:54 PM12/2/12
to makerspace...@googlegroups.com
LOL! A lego motor interface? You mean to make a pulley that will fit on the shaft of a lego motor? I like it!
 
I had a quick search for any existing files:
http://www.thingiverse.com/search?q=lego+motor+
but didn't find anything specific. The "Rotating Drop Wheel" seems like it might be right, but that hub looks really tiny. "Lego Wheel Axle for Motor/Gearbox" is exactly the opposite, no?
 
Do you have any source of the specs for a lego motor shaft? We have legos around here, but I don't have a lego motor anywhere.
 
 
And the ratio definition of the second pulley is in there now.

Richard Rouse

unread,
Dec 2, 2012, 4:22:51 PM12/2/12
to makerspace...@googlegroups.com
T5 pulleys are easy and belts plentiful. lego wheel axle is good
http://www.thingiverse.com/thing:35353 wonder if it fits smoothly

Richard

James Newton

unread,
Dec 3, 2012, 3:49:09 PM12/3/12
to makerspace...@googlegroups.com
Hey Richard, where you do buy Timing Belts? 

Richard Rouse

unread,
Dec 3, 2012, 8:55:28 PM12/3/12
to makerspace...@googlegroups.com
About 2 years ago I bought a big wheel of T5's but I forget where. It
was like $100 and I still have plenty.

Nowadays we are trying work with GT2 and Ultimachine, Misumi ... well
there are many places
Reply all
Reply to author
Forward
0 new messages