-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe_calc.cpp
More file actions
336 lines (315 loc) · 14 KB
/
e_calc.cpp
File metadata and controls
336 lines (315 loc) · 14 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// for Schmitt Trigger
float VRef = 0.0;
float Ra = 0; //Resistor between T1 & T2
float Rb = 0; //Resistor between Ra & GND
float R1 = 0; //Resistor between T1 Collector and VCC
float Re = 0; //Resistor between Emitter of T1 and Ground
float Voltage = 0.0; //used for impedance & current
float Voltage2 = 0.0;
float c1 = 0.0; //capacitance 1
float c2 = 0.0; //capacitance 2
float r2 = 0.0; //resistance 2
// for parallel resistance
vector<float> resistors;
int nOfRes = 0;
float overallResistance = 0.0;
// for impedance
float resistance = 0.0; //used for current as well
float impedance = 0.0; //used for current as well
//for power
float watts = 0.0;
//for current density
float crosssection = 0.0;
string helpstring = "-a, -A, -I, -i\t\tCalculate Impedance in Amperes\n-C, -c, -V, -v\t\tCalculate Current in Volts\n-J, -j\t\t\tCalculate Current Density\n-P, -p\t\t\tCalculate Power in Watts\n-R, -r\t\t\tCalculate Resistance in Ohms\n-RP, -rp\t\tCalculate Parallel Resistance\n-s, -S\t\t\tSchmitt Trigger\n-tmv, -TMV\t\tAstable Transistor Multivibrator\nUse -h -[command] for further info";
string schmittstring = "SCHMITT TRIGGER RESISTOR CALC\nExpects 5 values:\tVRef (prob = VCC)\n\t\t\tRa (between T1 & T2)\n\t\t\tRb (between Ra & GND)\n\t\t\tRe (between T1 & GND)\n\t\t\tR1 (between VCC & T1)";
string impedancestring = "IMPEDANCE IN AMPERE CALC\nExpects 2 values:\nCurrent in Volts, Resistance in Ohms";
string voltagestring = "CURRENT IN VOLTS CALC\nExpects 2 values:\nImpedance in Amperes, Resistance in Ohms";
string resistancestring = "RESISTANCE IN OHMS\nExpects 2 values:\nCurrent in Volts, Impedance in Amperes";
string parallelstring = "PARALLEL RESISTANCE IN OHMS\nExpects n values:\nResistance 1 in Ohms, Resistance 2 in Ohms, ...";
string wattstring = "POWER IN WATTS\nExpects 2 values:\nCurrent in Voltage, Impedance in Amperes";
string densitystring = "CURRENT DENSITY\nExpects 2 values:\nCross-Sectional Area in mm^2, Impedance in Amperes";
string vibratorstring = "ASTABLE TRANSISTOR MULTIVIBRATOR\nExpects 2 or 4 values:\nResistance 1 [& 2] in Ohms, Capacitance [& 2] in Farad";
float calcVoltage1() {
// high trigger level
return (((VRef*Rb)/(R1+Ra+Rb))-0.61);
}
float calcVoltage2() {
// low trigger level
return (((VRef*Rb)/(R1+Ra+Rb+(R1*Rb)/Re))+0.61);
}
float calcParallelResistance(int length) {
float multiply = resistors.at(0);
float additive = resistors.at(0);
if(length <= 1) {
cout << "Needs 2 resistors minimum!" << endl;
exit(0);
} else if(length == 2) {
multiply = multiply * resistors.at(1);
additive += resistors.at(1);
return (multiply/additive);
} else if(length > 2) {
additive = 0;
for(int i=0; i<length; i++) {
additive += (1/resistors.at(i));
}
return 1/additive;
}
return (multiply/additive);
}
float calcResistance() {
return (Voltage/impedance);
}
float calcImpedance() {
return (Voltage/resistance);
}
float calcCurrent() {
return (impedance * resistance);
}
float calcPower() {
return (Voltage * impedance);
}
float calcVibratorFrequency(int n) {
float f1 = 0.0;
float f2 = 0.0;
if(n == 2) {
f1 = 1/(1.38 * R1 * c1);
return f1;
} else if(n == 4) {
f2 = 1/(1.38 * r2 * c2);
return f2;
}
return 0;
}
float calcCurrentDensity() {
return (impedance/(crosssection));
}
void printResults(int type) {
/*
0 - Schmitt Trigger
1 - Resistance Parallel
2 - Resistance
3 - Current
4 - Impedance
5 - Watts
6 - Current Density
7 - single frequency
8 - dual frequency
*/
if(type == 0) {
cout << "High Trigger Voltage: " << setprecision(4) << Voltage << endl;
cout << "Low Trigger Voltage: " << setprecision(4) << Voltage2 << endl;
} else if(type == 1) {
cout << "Parallel Resistance: " << setprecision(2) << overallResistance << endl;
} else if(type == 2) {
cout << "Resistance in Ohms: " << setprecision(2) << resistance << endl;
} else if(type == 3) {
cout << "Current in Volts: " << setprecision(4) << Voltage << endl;
} else if(type == 4) {
cout << "Impedance in Ampere: " << setprecision(6) << impedance << endl;
} else if(type == 5) {
cout << "Power in Watts: " << setprecision(2) << watts << endl;
} else if(type == 6) {
cout << "Current Density in A/mm^2: " << setprecision(4) << watts << endl;
if(watts > 25) {
cout << "That's way too high." << endl;
} else if(watts <= 25 && watts > 18) {
cout << "That's quite high. Maybe a thicker wire?" << endl;
} else if(watts <= 18 && watts > 5) {
cout << "That's okay." << endl;
} else if(watts <= 5) {
cout << "There's plenty of room. You could try a thinner wire." << endl;
}
} else if(type == 7) {
cout << "Synchronized Vibration\nOscillation in Hertz: " << setprecision(4) << Voltage << endl;
} else if(type == 8) {
cout << "Asynchrononous Vibration\nOscillation 1 in Hertz: " << setprecision(4) << Voltage << endl << "Oscillation 2 in Hertz: " << setprecision(4) << Voltage2 << endl;
}
}
int main(int argc, char *argv[]) {
if(argc > 1) {
if(strncmp(argv[1], "-h", 2) == 0 || strncmp(argv[1], "-help", 5) == 0 || strncmp(argv[1], "--h", 3) == 0 || strncmp(argv[1], "--help", 6) == 0) {
if(argc > 2) {
if(strncmp(argv[2], "-s", 2) == 0 || strncmp(argv[2], "-S", 2) == 0) {
cout << schmittstring << endl;
} else if(strncmp(argv[2], "-RP", 3) == 0 || strncmp(argv[2], "-rp", 3) == 0) {
cout << parallelstring << endl;
} else if(strncmp(argv[2], "-r", 2) == 0 || strncmp(argv[2], "-R", 2) == 0) {
cout << resistancestring << endl;
} else if(strncmp(argv[2], "-v", 2) == 0 || strncmp(argv[2], "-V", 2) == 0 || strncmp(argv[2], "-c", 2) == 0 || strncmp(argv[2], "-C", 2) == 0) {
cout << voltagestring << endl;
} else if(strncmp(argv[2], "-i", 2) == 0 || strncmp(argv[2], "-I", 2) == 0 || strncmp(argv[2], "-a", 2) == 0 || strncmp(argv[2], "-A", 2) == 0) {
cout << impedancestring << endl;
} else if(strncmp(argv[2], "-p", 2) == 0 || strncmp(argv[2], "-P", 2) == 0) {
cout << wattstring << endl;
} else if(strncmp(argv[2], "-j", 2) == 0 || strncmp(argv[2], "-J", 2) == 0) {
cout << densitystring << endl;
} else if(strncmp(argv[2], "-tmv", 4) == 0 || strncmp(argv[2], "-TMV", 4) == 0) {
cout << vibratorstring << endl;
}
} else {
cout << helpstring << endl;
}
exit(1);
} else if(strncmp(argv[1], "-s", 2) == 0 || strncmp(argv[1], "-S", 2) == 0) {
if(argc > 2) {
VRef = stof(argv[2]);
Ra = stof(argv[3]);
Rb = stof(argv[4]);
Re = stof(argv[5]);
R1 = stof(argv[6]);
} else {
cout << "VRef?" << endl << "> ";
cin >> VRef;
cout << "Ra (between T1 & T2)?" << endl << "> ";
cin >> Ra;
cout << "Rb (between Ra & GND)?" << endl << "> ";
cin >> Rb;
cout << "Re (between T1 & GND)?" << endl << "> ";
cin >> Re;
cout << "R1 (between VRef & T1)?" << endl << "> ";
cin >> R1;
}
Voltage = calcVoltage1();
Voltage2 = calcVoltage2();
printResults(0);
} else if(strncmp(argv[1], "-rp", 3) == 0 || strncmp(argv[1], "-RP", 3) == 0) {
if(argc > 2) {
for(int i=2; i<argc; i++) {
resistors.push_back(stof(argv[i]));
}
overallResistance = calcParallelResistance(argc-2);
} else {
cout << "How many Resistors are there?" << endl << "> ";
cin >> nOfRes;
if(nOfRes < 2) {
cout << "Needs 2 resistors minimum!" << endl;
exit(0);
}
for(int i=0; i<nOfRes; i++) {
float in = 0;
cout << "Value of Resistor " << i+1 << "?" << endl << "> ";
cin >> in;
resistors.push_back(in);
}
overallResistance = calcParallelResistance(nOfRes);
}
printResults(1);
} else if(strncmp(argv[1], "-r", 2) == 0 || strncmp(argv[1], "-R", 2) == 0) {
if(argc > 2) {
Voltage = stof(argv[2]);
impedance = stof(argv[3]);
} else {
cout << "Calculating Resistance in Ohms" << endl;
cout << "Current/Volts?" << endl << "> ";
cin >> Voltage;
cout << "Impedance/Ampere?" << endl << "> ";
cin >> impedance;
}
resistance = calcResistance();
printResults(2);
} else if(strncmp(argv[1], "-v", 2) == 0 || strncmp(argv[1], "-V", 2) == 0 || strncmp(argv[1], "-c", 2) == 0 || strncmp(argv[1], "-C", 2) == 0) {
if(argc > 2) {
impedance = stof(argv[2]);
resistance = stof(argv[3]);
} else {
cout << "Calculating Current in Volts" << endl;
cout << "Impedance/Ampere?" << endl << "> ";
cin >> impedance;
cout << "Resistance?" << endl << "> ";
cin >> resistance;
}
Voltage = calcCurrent();
printResults(3);
} else if(strncmp(argv[1], "-i", 2) == 0 || strncmp(argv[1], "-I", 2) == 0 || strncmp(argv[1], "-a", 2) == 0 || strncmp(argv[1], "-A", 2) == 0) {
if(argc > 2) {
Voltage = stof(argv[2]);
resistance = stof(argv[3]);
} else {
cout << "Calculating Impedance in Ampere" << endl;
cout << "Current/Volts?" << endl << "> ";
cin >> Voltage;
cout << "Resistance?" << endl << "> ";
cin >> resistance;
}
impedance = calcImpedance();
printResults(4);
} else if(strncmp(argv[1], "-P", 2) == 0 || strncmp(argv[1], "-p", 2) == 0) {
if(argc > 2) {
Voltage = stof(argv[2]);
impedance = stof(argv[3]);
} else {
cout << "Calculating power in Watts" << endl;
cout << "Current/Volts?" << endl << "> ";
cin >> Voltage;
cout << "Impedance/Amperes?" << endl << "> ";
cin >> impedance;
}
watts = calcPower();
printResults(5);
} else if(strncmp(argv[1], "-J", 2) == 0 || strncmp(argv[1], "-j", 2) == 0) {
if(argc > 2) {
crosssection = stof(argv[2]);
impedance = stof(argv[3]);
} else {
cout << "Calculating current density" << endl;
cout << "Cross-Section Area of the wire in mm^2?" << endl << "> ";
cin >> crosssection;
cout << "Impedance/Amperes?" << endl << "> ";
cin >> impedance;
}
watts = calcCurrentDensity();
printResults(6);
} else if(strncmp(argv[1], "-tmv", 4) == 0 || strncmp(argv[1], "-TMV", 4) == 0) {
if(argc > 2) {
if(argc < 5) {
R1 = stof(argv[2]);
c1 = stof(argv[3]);
Voltage = calcVibratorFrequency(2);
printResults(7);
} else if(argc > 4) {
R1 = stof(argv[2]);
c1 = stof(argv[3]);
r2 = stof(argv[4]);
c2 = stof(argv[5]);
Voltage = calcVibratorFrequency(2);
Voltage2 = calcVibratorFrequency(4);
printResults(8);
}
} else {
int local;
cout << "Calculating Oscillation Frequency" << endl;
cout << "Is the oscillation synchronous (0/1)" << endl;
cin >> local;
if(local == 1) {
cout << "Resistance in Ohms?" << endl << "> ";
cin >> R1;
cout << "Capacitance in Farads?" << endl << "> ";
cin >> c1;
Voltage = calcVibratorFrequency(2);
printResults(7);
} else {
cout << "Asynchrononous Oscillation" << endl << "Resistance 1 in Ohms?" << endl << "> ";
cin >> R1;
cout << "Capacitance 1 in Farads?" << endl << "> ";
cin >> c1;
cout << "Resistance 2 in Ohms?" << endl << "> ";
cin >> r2;
cout << "Capacitance 2 in Farads?" << endl << "> ";
cin >> c2;
Voltage = calcVibratorFrequency(2);
Voltage2 = calcVibratorFrequency(4);
printResults(8);
}
}
}
} else {
cout << helpstring << endl;
}
exit(1);
}