-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (122 loc) · 4.51 KB
/
app.js
File metadata and controls
139 lines (122 loc) · 4.51 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
const delayMap = {};
const axisValues = { 1: 0, 2: 0, 3: 0, 4: 0 };
const ESP8266_IP = "192.168.43.2"; // <----------IP of esp8266
function updateSlider(slider, axisNumber) {
const tooltip = document.getElementById("tooltip" + axisNumber);
tooltip.textContent = slider.value;
const tooltipX = (slider.value - slider.min) / (slider.max - slider.min) * slider.offsetWidth;
tooltip.style.left = tooltipX + "px";
axisValues[axisNumber] = parseInt(slider.value);
clearTimeout(delayMap[axisNumber]);
delayMap[axisNumber] = setTimeout(() => {
console.log(`Sending to ESP: Axis ${axisNumber}: ${slider.value}°`);
sendAxisData();
}, 300);
}
function sendAxisData() {
const url = `http://${ESP8266_IP}/servoPos?x=${axisValues[1]}&y=${axisValues[2]}&z=${axisValues[3]}&g=${axisValues[4]}`;
console.log(url);
fetch(url)
.then(response => {
if (!response.ok) throw new Error("Network response was not ok");
return response.text();
})
.then(data => {
console.log("ESP Response:", data);
})
.catch(error => {
console.error("Error contacting ESP8266:", error);
});
}
// Initialize tooltips on page load
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll('input[type="range"]').forEach((slider, index) => {
updateSlider(slider, index + 1);
// Attach input event listener
slider.addEventListener("input", () => {
updateSlider(slider, index + 1);
});
});
});
function testESPLink() {
const url = `http://${ESP8266_IP}/ping`
fetch(url)
.then(response => {
if (response.ok) {
alert("Pong");
} else {
alert("Not connected");
}
})
.catch(() => {
alert("Time out");
});
}
// function PresetMotions(PresetMotionName){
// const url = `http://${ESP8266_IP}/Preset?func=${PresetMotionName}`;
// fetch(url)
// .then(response =>{
// if(response.ok){
// alert(`Function ${PresetMotionName} done successful`);
// }
// else{
// alert("Please check PING");
// }
// })
// .catch(()=>{
// alert('Some Error Accured!');
// })
// }
function PresetMotions(PresetMotionName) {
// If the demo button is pressed
if (PresetMotionName === "demo") {
// Update the axisValues to the initial position
axisValues[1] = 35;
axisValues[2] = 50;
axisValues[3] = 22;
axisValues[4] = 0;
// Step 1: Move to the initial position first
const initUrl = `http://${ESP8266_IP}/servoPos?x=35&y=50&z=22&g=0`;
fetch(initUrl)
.then(response => {
if (!response.ok) throw new Error("Failed to move to initial position");
return response.text();
})
.then(() => {
console.log("Initial position reached. Now running demo...");
// Step 2: Call the preset motion after short delay
setTimeout(() => {
const url = `http://${ESP8266_IP}/Preset?func=${PresetMotionName}`;
fetch(url)
.then(response => {
if (response.ok) {
alert(`Function ${PresetMotionName} done successfully`);
} else {
alert("Please check PING");
}
})
.catch(() => {
alert('Some Error Occurred while running preset!');
});
}, 1000); // 1 second delay
})
.catch(error => {
alert('Error during initial positioning: ' + error.message);
});
}
else {
// For other preset motions, call directly
const url = `http://${ESP8266_IP}/Preset?func=${PresetMotionName}`;
fetch(url)
.then(response => {
if (response.ok) {
alert(`Function ${PresetMotionName} done successfully`);
} else {
alert("Please check PING");
}
})
.catch(() => {
alert('Some Error Occurred!');
});
}
}