-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
286 lines (238 loc) · 10.6 KB
/
index.js
File metadata and controls
286 lines (238 loc) · 10.6 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
function generateSquareWave(frequency, numPoints) {
const period = 1 / frequency;
const halfPeriod = period / 2;
const xValues = [];
const yValues = [];
for (let i = 0; i < numPoints; i++) {
const t = (i / (numPoints - 1)) * period;
const y = t % period < halfPeriod ? 1 : -1;
xValues.push(t);
yValues.push(y);
}
return { x: xValues, y: yValues, type: 'scatter', mode: 'lines', name: 'Square Wave' };
}
function generatePulseAndSine(frequency, delayStart, duration, numPoints) {
var period = 1 / frequency;
var halfPeriod = period / 2;
var maxT = duration + delayStart + 10*period;
var pulseDelay = maxT/10;
var maxT = maxT + pulseDelay;
var xValues = [];
var yValues = [];
for (let t = 0; t < maxT; t=t+period/10) {
// Short pulse for 1ms
if (t < pulseDelay) {
yValues.push(1);
} else {
// Sine wave with delay and duration
const tWithDelay = t - (delayStart + pulseDelay);
//console.log(tWithDelay)
var y = 0
if(tWithDelay >= 0 && tWithDelay <= duration){
console.log(tWithDelay);
y = Math.sin(2 * Math.PI * frequency * tWithDelay)**2
}
yValues.push(y);
}
xValues.push(t);
}
return { x: xValues, y: yValues, type: 'scatter', mode: 'lines', name: 'Pulse and Sine' };
}
// Example usage:
const frequency = 1e3; // Set your desired frequency
const delayStart = 1e-3; // Set delay in seconds
const duration = 1e-3; // Set duration in seconds
const numPoints = 10000; // Set the number of points for the plot
const maxT = 10;
const pulseAndSineData = generatePulseAndSine(frequency, delayStart, duration, numPoints);
// Layout configuration for the plot
const layout = {
title: 'Pulse and Sine',
xaxis: { title: 'Time' },
yaxis: { title: 'Amplitude', range: [-1.5, 1.5] }
};
// Create the plot
Plotly.newPlot('scatterPlot', [pulseAndSineData], layout);
function captureFormData() {
// Get values from the first set of dropdown and text fields
const polarity1 = document.getElementById('polarity1').value;
const delayStart1 = document.getElementById('delayStart1').value;
const currentDuration1 = document.getElementById('currentDuration1').value;
// Get values from the second set of dropdown and text fields
const polarity2 = document.getElementById('polarity2').value;
const delayStart2 = document.getElementById('delayStart2').value;
const currentDuration2 = document.getElementById('currentDuration2').value;
// Get values from the second set of dropdown and text fields
const polarity3 = document.getElementById('polarity3').value;
const delayStart3 = document.getElementById('delayStart3').value;
const currentDuration3 = document.getElementById('currentDuration3').value;
// Get values from the second set of dropdown and text fields
const polarity4 = document.getElementById('polarity4').value;
const delayStart4 = document.getElementById('delayStart4').value;
const currentDuration4 = document.getElementById('currentDuration4').value;
// Get value from the frequency textbox
const frequency = document.getElementById('frequency').value;
const waitUnitl = document.getElementById('waitUntil').value;
const zeroThreshold = document.getElementById('zeroThreshold').value;
// Create a JSON object with the captured data
const formData = {
sets: [
{
polarity: polarity1,
delayStart: delayStart1,
currentDuration: currentDuration1
},
{
polarity: polarity2,
delayStart: delayStart2,
currentDuration: currentDuration2
},
{
polarity: polarity3,
delayStart: delayStart3,
currentDuration: currentDuration3
},
{
polarity: polarity4,
delayStart: delayStart4,
currentDuration: currentDuration4
}
// Add more sets as needed
],
frequency: frequency,
waitUnitl: waitUnitl,
zeroThreshold: zeroThreshold
};
return formData;
}
function generateArray(start, end, numPoints) {
const step = (end - start) / (numPoints - 1);
return Array.from({ length: numPoints }, (_, index) => start + index * step);
}
function generateSine2(frequency, t,tstart,duration){
var y = {};
for(var i = 0; i<t.length;i++){
var ti = t[i]
if(ti>=tstart){
y.push(Math.sin(2*Math.pi*f*(ti-tstart))**2);
}
else{
y.push(0);
}
}
}
function setCurrentDuration(value1,value2,value3,value4) {
// Set the value of the "Current Duration" textbox for the first set
document.getElementById('currentDuration1').value = value1;
// Set the value of the "Current Duration" textbox for the second set
document.getElementById('currentDuration2').value = value2;
// Add similar lines for more sets if needed
document.getElementById('currentDuration3').value = value3;
document.getElementById('currentDuration4').value = value4;
}
function roundToMultiple(number, multiple) {
return Math.round(number / multiple) * multiple;
}
function analyze(){
var formData = captureFormData();
var period = 1/formData.frequency;
var roundedDuration1 = roundToMultiple(formData.sets[0].currentDuration,period*0.5);
var roundedDuration2 = roundToMultiple(formData.sets[1].currentDuration,period*0.5);
var roundedDuration3 = roundToMultiple(formData.sets[2].currentDuration,period*0.5);
var roundedDuration4 = roundToMultiple(formData.sets[3].currentDuration,period*0.5);
setCurrentDuration(roundedDuration1,roundedDuration2,roundedDuration3,roundedDuration4);
}
//serial communications
let serialPort;
async function connectToArduino() {
try {
serialPort = await navigator.serial.requestPort();
await serialPort.open({ baudRate: 9600 });
console.log('Connected to Arduino');
readDataFromArduino();
} catch (error) {
console.error('Error connecting to Arduino:', error);
}
}
async function readDataFromArduino() {
try {
const reader = serialPort.readable.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) {
console.log('Serial port closed');
break;
}
// Process the received data
processDataFromArduino(value);
}
} catch (error) {
console.error('Error reading data from Arduino:', error);
}
}
function processDataFromArduino(data) {
// Convert the received data (Uint8Array) to a string
const receivedText = new TextDecoder().decode(data);
// Update the webpage with the received data
updateWebpageWithArduinoData(receivedText);
}
var terminalText = "";
function updateWebpageWithArduinoData(data) {
const receivedDataDiv = document.getElementById('receivedData');
terminalText = terminalText+data;
receivedDataDiv.textContent = terminalText;
// Create a cursor element and append it to the end of the received data
const cursorElement = document.createElement('span');
cursorElement.className = 'cursor';
receivedDataDiv.appendChild(cursorElement);
// Scroll the received data to the end
receivedDataDiv.scrollTop = receivedDataDiv.scrollHeight;
}
async function sendDataToArduino(data) {
try {
// Convert the string to ArrayBuffer
const encoder = new TextEncoder();
const dataArrayBuffer = encoder.encode(data);
const writer = serialPort.writable.getWriter();
await writer.write(dataArrayBuffer);
await writer.releaseLock();
console.log('Data sent to Arduino:', data);
} catch (error) {
console.error('Error sending data to Arduino:', error);
}
}
function analyze() {
var formData = captureFormData();
var period = 1 / formData.frequency;
var roundedDuration1 = roundToMultiple(formData.sets[0].currentDuration, period * 0.5);
var roundedDuration2 = roundToMultiple(formData.sets[1].currentDuration, period * 0.5);
var roundedDuration3 = roundToMultiple(formData.sets[2].currentDuration, period * 0.5);
var roundedDuration4 = roundToMultiple(formData.sets[3].currentDuration, period * 0.5);
setCurrentDuration(roundedDuration1, roundedDuration2, roundedDuration3, roundedDuration4);
// Continue with your existing analyze logic
}
async function analyzeArduino(formData) {
// Customize the data structure based on your Arduino code
const dataToSend = JSON.stringify(formData);
var formData = captureFormData();
var period = 1 / formData.frequency;
var roundedDuration1 = roundToMultiple(formData.sets[0].currentDuration, period * 0.5);
var roundedDuration2 = roundToMultiple(formData.sets[1].currentDuration, period * 0.5);
var roundedDuration3 = roundToMultiple(formData.sets[2].currentDuration, period * 0.5);
var roundedDuration4 = roundToMultiple(formData.sets[3].currentDuration, period * 0.5);
setCurrentDuration(roundedDuration1, roundedDuration2, roundedDuration3, roundedDuration4);
var dataSend = `${formData.waitUnitl == 'Enabled'? 1:0}-${formData.zeroThreshold}-${formData.frequency}-${formData.sets[0].polarity == 'Positive'? 1:0}-${formData.sets[0].delayStart*1000}-${formData.sets[0].currentDuration*1000}-${formData.sets[1].polarity == 'Positive'? 1:0}-${formData.sets[1].delayStart*1000}-${formData.sets[1].currentDuration*1000}-${formData.sets[2].polarity == 'Positive'? 1:0}-${formData.sets[2].delayStart*1000}-${formData.sets[2].currentDuration*1000}-${formData.sets[3].polarity == 'Positive'? 1:0}-${formData.sets[3].delayStart*1000}-${formData.sets[3].currentDuration*1000}`;
console.log(dataSend);
try {
// Ensure the connection is established before sending data
if (!serialPort || !serialPort.readable) {
console.error('Arduino connection not established.');
return;
}
// Send data to Arduino
await sendDataToArduino(dataSend);
// Add any additional logic specific to analyzing Arduino data, if needed
} catch (error) {
console.error('Error during Arduino analysis:', error);
}
}