-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbmod.html
More file actions
449 lines (405 loc) · 13 KB
/
fbmod.html
File metadata and controls
449 lines (405 loc) · 13 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!doctype html>
<html>
<head>
<title>ExaBerries - feedback mod calc</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div class="background"></div>
<div class="content">
<h1>Feedback mod calc</h1>
<div style="float: left; margin: 0; width: 40%">
<h2>determine R1/R2</h2>
<h3>inputs</h3>
<table>
<tbody>
<tr>
<td>
<label for="Vout">Vout</label>
</td>
<td>
<input type="number" id="Vout-val" value="1.5" step="0.001" min="0.001" max="99.999"/>
</td>
<td>
V
</td>
</tr>
<tr>
<td>
<label for="Vref">Vref</label>
</td>
<td>
<input type="number" id="Vref" value="0.796" step="0.001" min="0.001" max="99.999"/>
</td>
<td>
V
</td>
</tr>
<tr>
<td>
<label for="Rfbgnd">Rfb->gnd</label>
</td>
<td>
<input type="number" id="Rfbgnd" value="5.715" step="0.001" min="0.001" max="999999.999"/>
</td>
<td>
<select name="Rfbgnd-unit" id="Rfbgnd-unit" onclick="calc_R1R2()">
<option value="0">Ω</option>
<option value="3" selected="selected">kΩ</option>
<option value="6">MΩ</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="Rrail">Rrail</label>
</td>
<td>
<input type="number" id="Rrail" value="40" step="0.001" min="0.001" max="999999.999"/>
</td>
<td>
<select name="Rrail-unit" id="Rrail-unit" onclick="calc_R1R2()">
<option value="-3">mΩ</option>
<option value="0" selected="selected">Ω</option>
<option value="3">kΩ</option>
<option value="6">MΩ</option>
</select>
</td>
</tr>
</tbody>
</table>
<h3>values</h3>
<table>
<tr>
<td>R1</td>
<td id="R1-calc" style="text-align: right"></td>
<td id="R1-calc-unit"></td>
</tr>
<tr>
<td>R2</td>
<td id="R2-calc" style="text-align: right"></td>
<td id="R2-calc-unit"></td>
</tr>
</table>
<br/>
<button onclick="copy_R1R2()">copy to fb mod side =></button>
</div>
<div style="float: left; margin: 0; width: 40%">
<h2>calc fb mod</h2>
<h3>inputs</h3>
<table>
<tbody>
<tr>
<td>
<label for="R1">R1</label>
</td>
<td>
<input type="number" id="R1" value="7.2" step="0.001" min="0.001" max="999999.999"/>
</td>
<td>
<select name="R1-unit" id="R1-unit" onclick="calc_fb_mod()">
<option value="0">Ω</option>
<option value="3" selected="selected">kΩ</option>
<option value="6">MΩ</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="R2">R2</label>
</td>
<td>
<input type="number" id="R2" value="14.4" step="0.001" min="0.001" max="999999.999"/>
</td>
<td>
<select name="R2-unit" id="R2-unit" onclick="calc_fb_mod()">
<option value="0">Ω</option>
<option value="3" selected="selected">kΩ</option>
<option value="6">MΩ</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="Vmin">Vmin</label>
</td>
<td>
<input type="number" id="Vmin" value="1.55" step="0.001" min="0.001" max="99.999"/>
</td>
<td>
V
</td>
</tr>
<tr>
<td>
<label for="Vmax">Vmax</label>
</td>
<td>
<input type="number" id="Vmax" value="1.65" step="0.001" min="0.001" max="99.999"/>
</td>
<td>
V
</td>
</tr>
</tbody>
</table>
<h3>values</h3>
<table>
<tr>
<td>Rpot</td>
<td id="Rpot-calc" style="text-align: right"></td>
<td id="Rpot-calc-unit"></td>
</tr>
<tr>
<td>Rseries</td>
<td id="Rseries-calc" style="text-align: right"></td>
<td id="Rseries-calc-unit"></td>
</tr>
<tr>
<td>Vmin</td>
<td id="Vmin-calc" style="text-align: right"></td>
<td>V</td>
</tr>
<tr>
<td>Vmax</td>
<td id="Vmax-calc" style="text-align: right"></td>
<td>V</td>
</tr>
</table>
</div>
<div>
<canvas id="chart"></canvas>
</div>
</div>
<script>
// very hacked together - yes I know its a mess
// https://www.joshwcomeau.com/snippets/javascript/debounce/
const debounce = (callback, wait) => {
let timeoutId = null;
return (...args) => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}
const debounced_calc_R1R2 = debounce((event) => {
calc_R1R2();
}, 400);
const debounced_calc_fb_mod = debounce((event) => {
calc_fb_mod();
}, 400);
document.getElementById("Vout-val").addEventListener("input", debounced_calc_R1R2);
document.getElementById("Vref").addEventListener("input", debounced_calc_R1R2);
document.getElementById("Rfbgnd").addEventListener("input", debounced_calc_R1R2);
document.getElementById("Rrail").addEventListener("input", debounced_calc_R1R2);
document.getElementById("R1").addEventListener("input", debounced_calc_fb_mod);
document.getElementById("R2").addEventListener("input", debounced_calc_fb_mod);
document.getElementById("Vmin").addEventListener("input", debounced_calc_fb_mod);
document.getElementById("Vmax").addEventListener("input", debounced_calc_fb_mod);
var canvas = document.getElementById("chart");
// create chart
var vmod_chart = new Chart(canvas, {
type: "line",
data: {
datasets: [
{
label: "V",
data: [],
borderColor: "rgba(255, 0 , 255, 1)",
fill: false,
tension: 0.2
}
],
labels: [0, 100]
},
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: "Potentiometer Resistance (Ω)"
}
},
y: {
title: {
display: true,
text: "Output voltage calculated from Potentiometer resisance and series resistance (V)"
}
}
},
plugins: {
legend: {
display: false
}
}
}
});
// given a string of the unit for a resistor, return a float that converts to ohms when multiplied
// ex: "kΩ" => 1000.0, "MΩ" => 1000000.0
function res_unit_str_to_float(unit_str) {
if (unit_str == "mΩ") {
return 1.0 / 1000.0;
} else if (unit_str == "kΩ") {
return 1000.0;
} else if (num_str == "MΩ") {
return 1000.0 * 1000.0;
}
return 1.0; // just in ohms
}
// calculate output voltage when modding, given vref, r1, r2, and parallel resistance rmod
// all inputs should be a float, and either in volts or ohms for the respective values
function calc_V_modded(v_ref, r1, r2, r_mod) {
return v_ref * (1 + r1 / (1 / ((1 / r2) + (1 / r_mod))));
}
// update data in chart according to new values
function update_chart(r1, r2, vref, rpot, rseries) {
console.log("updating chart with ", r1, r2, vref, rpot, rseries);
var data = vmod_chart.data;
const NUM_XVALS = 20;
data.labels = [];
for (var i = 0; i <= NUM_XVALS; i++) {
data.labels.push(rpot * i / NUM_XVALS);
}
data.datasets[0].data = [];
// only one dataset
for (var i = 0; i < data.labels.length; i++) {
// fill data according to volt mod
data.datasets[0].data.push(calc_V_modded(vref, r1, r2, data.labels[i] + rseries));
}
vmod_chart.update();
}
const E24 = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1, 10.0];
const BOURNS_3296W = [1.0, 2.0, 5.0];
// round resistance to provided E series
// r should be in ohms
function round_R(r, series) {
if (r === 0.0) {
return r;
}
if (r < 0) {
return r;
}
console.log("rounding ", r);
// basically convert to scientific notation
var exponent = 0.0;
var res = r;
if (res > 10.0) {
while (res > 9.999) {
res /= 10.0;
exponent++;
}
} else if (res < 1.0) {
while (res < 1.0) {
res *= 10.0;
exponent--;
}
}
console.log("round step1: ", res, "e", exponent);
var new_res = 0.0;
var i = 0;
while (new_res <= res) {
new_res = series[i];
i++;
}
// if smaller % change to previous E24 use that, otherwise we'd always round up
if (i > 1) {
const percent_round_up = Math.abs(new_res - res) / res;
const percent_round_down = Math.abs(series[i - 2] - res) / res;
if (percent_round_down < percent_round_up) {
new_res = series[i - 2];
}
}
console.log("rounded ", r, " to ", new_res * Math.pow(10, exponent));
return new_res * Math.pow(10, exponent);
}
// format resistance into more human readable strings
// first string is number with 3 decimal places
// second string is the unit, mΩ, Ω, kΩ, MΩ
// r should be resistance as a float, in ohms
function display_R_strs(r) {
var resistance = r;
var unit = "";
if (resistance < 1) {
resistance *= 1000;
unit = "m";
} else {
if (resistance > 1000) {
resistance /= 1000;
unit = "k";
}
if (resistance > 1000) {
resistance /= 1000;
unit = "M";
}
}
return [resistance.toFixed(3), unit + "Ω"];
}
// calculate feedback mod values
function calc_fb_mod() {
// get values from inputs
const r1 = parseFloat(document.getElementById("R1").value) * Math.pow(10, parseInt(document.getElementById("R1-unit").value));
const r2 = parseFloat(document.getElementById("R2").value) * Math.pow(10, parseInt(document.getElementById("R2-unit").value));
const V_out = parseFloat(document.getElementById("Vout-val").value);
const V_ref = parseFloat(document.getElementById("Vref").value);
var V_min = parseFloat(document.getElementById("Vmin").value);
if (V_min < V_out) {
V_min = V_out * 1.01;
}
var V_max = parseFloat(document.getElementById("Vmax").value);
if (V_max < V_min) {
V_max = V_min * 1.01;
}
if (r1 > ((V_min / V_ref) - 1) * r2) {
console.log("r1 too high");
}
const r_pot_calc = round_R(1 / ((((V_min / V_ref) - 1) / r1) - (1 / r2)), BOURNS_3296W);
const [r_pot_calc_rounded, r_pot_calc_unit] = display_R_strs(r_pot_calc);
document.getElementById("Rpot-calc").innerHTML = r_pot_calc_rounded;
document.getElementById("Rpot-calc-unit").innerHTML = r_pot_calc_unit;
const r_series_calc = round_R(1 / ((((V_max / V_ref) - 1) / r1) - (1 / r2)), E24);
const [r_series_calc_rounded, r_series_calc_unit] = display_R_strs(r_series_calc);
document.getElementById("Rseries-calc").innerHTML = r_series_calc_rounded;
document.getElementById("Rseries-calc-unit").innerHTML = r_series_calc_unit;
document.getElementById("Vmin-calc").innerHTML = calc_V_modded(V_ref, r1, r2, r_series_calc + r_pot_calc).toFixed(3);
document.getElementById("Vmax-calc").innerHTML = calc_V_modded(V_ref, r1, r2, r_series_calc).toFixed(3);
console.log("new Rpot/Rseries", r_pot_calc, r_series_calc);
update_chart(r1, r2, V_ref, r_pot_calc, r_series_calc);
}
// copy calculated R1 and R2 values to feedback mod side
function copy_R1R2() {
document.getElementById("R1").value = round_R(document.getElementById("R1-calc").innerHTML, E24);
document.getElementById("R2").value = round_R(document.getElementById("R2-calc").innerHTML, E24);
calc_fb_mod();
}
// calculate R1 and R2
function calc_R1R2() {
// get values from inputs
const R_fb_gnd = parseFloat(document.getElementById("Rfbgnd").value) * Math.pow(10, parseInt(document.getElementById("Rfbgnd-unit").value));
const R_rail = parseFloat(document.getElementById("Rrail").value) * Math.pow(10, parseInt(document.getElementById("Rrail-unit").value));
const V_out = parseFloat(document.getElementById("Vout-val").value);
const V_ref = parseFloat(document.getElementById("Vref").value);
// calculate R1/R2 and show in output table
// using the quadradic formula to take into account R_rail, below is a simplier solution but less accurate
// const r1_calc = R_fb_gnd * V_out / V_ref;
const V_ratio = V_out / V_ref;
const a = 1;
const b = V_ratio * (R_rail - R_fb_gnd) - R_rail;
const c = R_fb_gnd * R_rail * (V_ratio - 1);
const r1_calc = -b + Math.sqrt(Math.pow(b, 2) -4 * a * c) / (2 * a);
const [r1_calc_rounded, r1_calc_unit] = display_R_strs(r1_calc);
document.getElementById("R1-calc").innerHTML = r1_calc_rounded;
document.getElementById("R1-calc-unit").innerHTML = r1_calc_unit;
const r2_calc = r1_calc / (V_ratio - 1);
const [r2_calc_rounded, r2_calc_unit] = display_R_strs(r2_calc);
document.getElementById("R2-calc").innerHTML = r2_calc_rounded;
document.getElementById("R2-calc-unit").innerHTML = r2_calc_unit;
}
// run by default
calc_R1R2()
copy_R1R2()
calc_fb_mod()
</script>
</body>
</html>