/*
configurable turbine wheel
copyright 2013 sebastien lorquet f4grx <f4grx@f4grx.net>
cc-by-sa-nc
overlap problem to be avoided:
http://rocklinux.net/pipermail/openscad/2012-March/002765.html
todo: parabolic profiled blades
*/

module turbine(
	thickness			= 3, 
	shaft_diameter		= 3, 
	hub_diameter		= 6,
	blade_count			= 13, 
	blade_length		= 10, 
	blade_thickness	= 0.2, 
	attack_angle		= 50, 
	blade_width_hub	= 3, 
	blade_width_final	= 5, 
	housing_thickness	= 0.5,
	printer_compatible_bevel=0
)
{
	mode=0;
	hub_radius = hub_diameter/2;
	shaft_radius = shaft_diameter/2;
	total_radius = blade_length+hub_radius;

	module hub(r, thickness) {
		cylinder(r=r, h=thickness, center=true);
	}

	//progressive width blade
	//blade hub Z-, blade end Z+, blade profile centered on origin
	module blade_progr(len, start, thickness, width_base, width_final) {
		rotate([0,90,0])
		linear_extrude(height=thickness,center=true)
		polygon(
			points=[
				[0,width_base/2],
				[start,width_base/2],
				[start+len,width_final/2],
				[start+len,-width_final/2],
				[start,-width_base/2],
				[0,-width_base/2],
			], 
			paths=[[0,1,2,3,4,5]]
		);
	}

	module blade_basic(len, start, thickness, width_base, width_final) {
		rotate([0,90,0]) translate([0,0,start])
			cube([len,width_final,thickness]);
	}

	module turbine_body() {
		union() { //connect hub, blades, housing
			//hub, a bit too thick (will be cut by intersection)
			hub(hub_radius, thickness+1);
			intersection() { //ensure blades fit in a cylinder
				//blades
				for(b=[0:blade_count-1]) {
					rotate((360*b)/blade_count,[0,0,1]) //radial position
					rotate(attack_angle,[0,1,0]) //initial attack angle
					rotate(90,[1,0,0])
					translate([0,0,-shaft_radius]) //avoid overlap
					blade_progr(
						len=blade_length,
						start=hub_radius-shaft_radius,
						thickness=blade_thickness,
						width_base=blade_width_hub,
						width_final=blade_width_final
					);
				}
			}
			//exterior tube, a bit too thick (will be cut by intersection)
			//also a bit too wide to avoid slow 0-width intersection
			if(housing_thickness>0) {
				difference() {
					cylinder(r=(total_radius+housing_thickness)*1.1,h=thickness+1, center=true);
					cylinder(r=total_radius,h=thickness+2, center=true);
				}
			}
		} //union of all added parts
	}

	module turbine_outline() {
		difference() {
			cylinder(r=total_radius+housing_thickness, h=thickness, center=true);
			//cut a hollow cone to make the blades thinner at center, while keeping the other side
			if(true) { //printer_compatible_bevel>0) {
				union() {
					translate([0,0,thickness/2-printer_compatible_bevel]) 	
						cylinder(h=printer_compatible_bevel, r1=hub_radius, r2=total_radius,center=false);
					translate([0,0,thickness/2-0.01]) 	
						cylinder(h=printer_compatible_bevel, r=total_radius,center=false);
				}
			}
		//cylinder(center=true, r=hub_radius, h=thickness+1);
		} //diff
		hub(hub_radius, thickness);
	}

	//go

	difference() { //cut shaft hole
		if(mode==0) {
			intersection() { //make sure everything fits in a cylinder
				turbine_body();
				turbine_outline();
			}//intersection to ensure thickness and diameter
		} else if(mode==1) {
			turbine_outline(); //just the outline
		} else {
			turbine_body(); //just the body
		}
	cylinder(r=shaft_radius, h=thickness+2, center=true); //cut shaft hole
	} //difference for shaft hole and conicity

} //turbine


$fn=64;

module test_rotor() {
	translate([-16,-16,2])
	turbine(
		thickness			= 4,
		shaft_diameter		= 3, //important
		hub_diameter		= 3+(0.34*9*2), //9 cercles
		blade_count			= 11,
		blade_length		= 10,
		blade_thickness	= 0.34*2/sqrt(2),
		attack_angle		= 45,
		housing_thickness	= 0.34*2,
		blade_width_hub	= 6,
		blade_width_final	= 6,
		printer_compatible_bevel=1.5
	);
}

//stator
module test_stator() {
	translate([16,-16,2])
	turbine(
		thickness			= 4,
		shaft_diameter		= 8, //important
		hub_diameter		= 9+(0.34*2*2), //2 cercles
		blade_count			= 11,
		blade_length		= 10,
		blade_thickness	= 0.34*2/sqrt(2),
		attack_angle		= -45,
		housing_thickness	= 0.34*3,
		blade_width_hub	= 6,
		blade_width_final	= 6,
		printer_compatible_bevel=1.5
	);

}

//bearing
module test_bearing() {
	translate([16,16,2])
	turbine(
		thickness			= 4,
		shaft_diameter		= 8, //important
		hub_diameter		= 9+(0.34*2*3), //2 cercles
		blade_count			= 6,
		blade_length		= 10,
		blade_thickness	= 0.34*3,
		attack_angle		= 0,
		housing_thickness	= 0.34*3,
		blade_width_hub	= 6,
		blade_width_final	= 6,
		printer_compatible_bevel=0
	);

}
scale(1) {
	test_rotor();
	test_stator();
	test_bearing();
}