summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/AoI/scripts/Involute Profile Gear.bsh
blob: dc7233cf475ef83e5890a43f026edfdaaeff9a26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

/*
<?xml version='1.0' standalone='yes' ?>

<script>
	<name>Cog</name>
	<author>Forrest Higgs after Vik Olliver after Francois Guillet</author>
	<version>1.0</version>
	<date>16-May-2006</date>
	<description>
This script creates an involute profile n-toothed gear. Input: pitch radius, pressure angle, and profile definition.
    </description>
</script>
*/

//******************************************************************
//******************************************************************
//******************************************************************

// convert cylindrical to rectangular coordinates...

CylindricalToRectangular(double r,double theta)
{
   double x;
   double y;
   x = r * Math.cos(theta);
   y = r * Math.sin(theta);
   return this;
}

//******************************************************************
//******************************************************************
//******************************************************************

// convert rectangular to cylindrical coordinates...

RectangularToCylindrical(double x, double y )
{
   double r;
   double theta;
   r = Math.sqrt(x * x + y * y);
   theta = Math.atan2(y, x);
   return this;
}

//******************************************************************
//******************************************************************
//******************************************************************

scene = window.getScene();

double N;
double AP;
double AT;
double RP;
double P;
double RI;
double RO;
double RB;
double A1;
double A2;
double AD;
double Theta;

double XTemp;
double YTemp;

numberOfTeeth = new ValueField(4, ValueField.NONNEGATIVE);
pitchRadius = new ValueField(1, ValueField.NONNEGATIVE);
pressureAngle = new ValueField(20, ValueField.NONNEGATIVE);
profileDefinition = new ValueField(6, ValueField.NONNEGATIVE);

dlg = new ComponentsDialog(window, "Involute Gear Profile Generator" ,
new Widget [] { numberOfTeeth, pitchRadius, pressureAngle, profileDefinition },
new String [] { "Number of teeth (integer):", "Pitch radius:", "Pressure angle (degrees):", "Profile definition (surfaces):" } );

if (!dlg.clickedOk()) return;

N = (int) Math.round( numberOfTeeth.getValue() );

if (N < 4)
	N = 4;
AP = pressureAngle.getValue();
AT = (360 / N);
RP = pitchRadius.getValue();

NP = (int) profileDefinition.getValue();
if (NP > 20) 
  NP = 20;
if (NP < 3) 
  NP = 6;

P = N / RP / 2;
RI = RP - (Math.PI / 2) / P;

RO = (1 / P) + RP;
RB = RP * Math.cos(AP * (Math.PI / 180));

A1 = 90 - AT / 4 + AP - RP * Math.sin(AP * (Math.PI/ 180)) / RB * 180 / Math.PI;
A2 = (Math.sqrt(RO * RO - RB * RB)) / RB * 180 / Math.PI + A1;

AD = ((A2 - A1) / (NP - 1));

A = A1;

double[] SplinePointsX = new double [100];
double[] SplinePointsY = new double [100];
double[] SaveClockwiseProfileX = new double [100];
double[] SaveClockwiseProfileY = new double [100];
double[] SaveAntiClockwiseProfileX = new double [100];
double[] SaveAntiClockwiseProfileY = new double [100];
double[] ToothProfileX = new double [100];
double[] ToothProfileY = new double [100];

for (int i = 1; i <= NP; i++)
{
  L = RB * (A - A1) * Math.PI / 180;
  X = RB * Math.cos(A * (Math.PI / 180)) + L * Math.sin(A * (Math.PI / 180));
  Y = RB * Math.sin(A * (Math.PI / 180)) - L * Math.cos(A * (Math.PI / 180));

  SplinePointsX[i] = X;
  SplinePointsY[i] = Y;

  A = A + AD;
}

x1 = RI * Math.cos(A1 * (Math.PI / 180));
y1 = RI * Math.sin(A1 * (Math.PI / 180));
x2 = RB * Math.cos(A1 * (Math.PI / 180));
y2 = RB * Math.sin(A1 * (Math.PI / 180));

XY = CylindricalToRectangular(RI, (90 - AT / 2) * (Math.PI / 180));

// Finangling factor to stop the troughs between teeth distorting > 45 teeth. 
double finanglingFactor=1;

if (N>45) {
	finanglingFactor=1-((N-45)*.00068);
	// Works out at 0.97 for 79 teeth.	But is quite horrible.
}

SaveClockwiseProfileX[1] = XY.x*finanglingFactor;
SaveClockwiseProfileY[1] = XY.y*finanglingFactor;
SaveClockwiseProfileX[2] = x1*finanglingFactor;
SaveClockwiseProfileY[2] = y1*finanglingFactor;
SaveClockwiseProfileX[3] = x2;
SaveClockwiseProfileY[3] = y2;


for (int i = 2; i <= NP; i++)
{

  SaveClockwiseProfileX[2 + i] = SplinePointsX[i];
  SaveClockwiseProfileY[2 + i] = SplinePointsY[i];
}
        
SaveClockwiseProfileX[2 + NP + 1] = 0;
SaveClockwiseProfileY[2 + NP + 1] = RO;

//******************************************************************
//******************************************************************
//******************************************************************

//          mirror the tooth profile

XY = CylindricalToRectangular(RI, (90 + AT / 2) * (Math.PI / 180));

RTheta = RectangularToCylindrical( x1, y1 );
        
Theta = RTheta.theta * (180 / Math.PI);
Theta = 90 - Theta;
Theta = 90 + Theta;

X1Y1 = CylindricalToRectangular(RTheta.r, Theta*(Math.PI / 180));

RTheta = RectangularToCylindrical( x2, y2 );

Theta = RTheta.theta * (180 / Math.PI);
Theta = 90 - Theta;
Theta = 90 + Theta;

X2Y2 = CylindricalToRectangular(RTheta.r, Theta*(Math.PI / 180));

SaveAntiClockwiseProfileX[1] = XY.x*finanglingFactor;
SaveAntiClockwiseProfileY[1] = XY.y*finanglingFactor;
SaveAntiClockwiseProfileX[2] = X1Y1.x*finanglingFactor;
SaveAntiClockwiseProfileY[2] = X1Y1.y*finanglingFactor;
SaveAntiClockwiseProfileX[3] = X2Y2.x;
SaveAntiClockwiseProfileY[3] = X2Y2.y;

for (int i = 2; i <= NP; i++)
{
 
  RTheta = RectangularToCylindrical( SplinePointsX[i - 1], SplinePointsY[i - 1]  );
            
  Theta = RTheta.theta * (180 / Math.PI);

  Theta = 90 - RTheta.theta;
  Theta = 90 + RTheta.theta;

  X1Y1 = CylindricalToRectangular(RTheta.r, Theta*(Math.PI / 180));
  RTheta = RectangularToCylindrical( SplinePointsX[i], SplinePointsY[i] );

  Theta = RTheta.theta * (180 / Math.PI);
  Theta = 90 - Theta ;
  Theta = 90 + Theta ;

  X2Y2 = CylindricalToRectangular(RTheta.r, Theta*(Math.PI / 180));

  SaveAntiClockwiseProfileX[2 + i] = X2Y2.x;
  SaveAntiClockwiseProfileY[2 + i] = X2Y2.y;
}

SaveAntiClockwiseProfileX[2 + NP + 1] = 0;
SaveAntiClockwiseProfileY[2 + NP + 1] = RO;

//******************************************************************
//******************************************************************
//******************************************************************

int ToothProfileLimit = 0; 

for (int i = 1; i <= NP + 3; i++)
{
            ToothProfileLimit = ToothProfileLimit + 1;

            ToothProfileX[ToothProfileLimit] = SaveAntiClockwiseProfileX[i];
            ToothProfileY[ToothProfileLimit] = SaveAntiClockwiseProfileY[i];
}

for (int i = NP + 2; i >= 1; i--)
{

            ToothProfileLimit = ToothProfileLimit + 1;
            ToothProfileX[ToothProfileLimit] = SaveClockwiseProfileX[i];
            ToothProfileY[ToothProfileLimit] = SaveClockwiseProfileY[i];

}

//******************************************************************
//******************************************************************
//******************************************************************

Vec3[] v = new Vec3[(ToothProfileLimit-1)*N];
float[] smoothness = new float[(ToothProfileLimit-1)*N];
int index = 0;
double scale = 1.0;
double Xlast;
double Ylast;

for (int ii = 1; ii <= N; ii++)
  {
//print ("ii = " + ii );
  for (int i = 2; i <= ToothProfileLimit; i++)
    {
    RTheta = RectangularToCylindrical( ToothProfileX[i], ToothProfileY[i] );
    Theta = RTheta.theta - (AT) * ii * (Math.PI / 180);
 
    XY = CylindricalToRectangular( RTheta.r, Theta );

    v[index] = new Vec3( XY.x, XY.y, 0  );
    v[index].scale(scale);
    smoothness[index]=0;

//print ("ii/i/x/y = " + ii + "     " + i + "     " + XY.x + "     " + XY.y);

    index += 1;
    }
}

//******************************************************************
//******************************************************************
//******************************************************************


curve = new Curve( v, smoothness, Mesh.APPROXIMATING, true);
//we're done
window.addObject(curve, new CoordinateSystem(), "Involute Profile Gear", null);